forked from matthewkastor/Metatrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraderTrainer.mq4
284 lines (272 loc) · 11.8 KB
/
TraderTrainer.mq4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//+------------------------------------------------------------------+
//| TraderTrainer.mq4 |
//| Matthew Colter |
//| https://github.com/matthewkastor |
//+------------------------------------------------------------------+
#property copyright "Matthew Colter"
#property link "https://github.com/matthewkastor"
#property version "1.00"
#property strict
extern bool HedgingAllowed=false;
extern double StopLossDistance=0.00500;
extern double TakeProfitDistance=0.00500;
string CloseAllButton="CloseAllButton";
string BuyButton="Buy";
string SellButton="Sell";
string PositionSizeLabel="PositionSizeLabel";
string PositionSizeInput="PositionSize";
string StopLossLabel="StopLossLabel";
string StopLossInput="StopLoss";
string TakeProfitLabel="TakeProfitLabel";
string TakeProfitInput="TakeProfit";
string OrderCommentLabel="OrderCommentLabel";
string OrderCommentInput="OrderComment";
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void MakeUserInput(ENUM_OBJECT objectType,string inputName,string inputText,int x=0,int y=0,int width=100,int height=50,int inpColor=Gray,int inpBacgroundColor=Black,int inpBorderColor=Black,int inpBorderType=BORDER_FLAT,int fontSize=12)
{
ObjectCreate(0,inputName,objectType,0,0,0);
ObjectSetInteger(0,inputName,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,inputName,OBJPROP_YDISTANCE,y);
ObjectSetInteger(0,inputName,OBJPROP_XSIZE,width);
ObjectSetInteger(0,inputName,OBJPROP_YSIZE,height);
ObjectSetString(0,inputName,OBJPROP_TEXT,inputText);
ObjectSetInteger(0,inputName,OBJPROP_COLOR,inpColor);
ObjectSetInteger(0,inputName,OBJPROP_BGCOLOR,inpBacgroundColor);
ObjectSetInteger(0,inputName,OBJPROP_BORDER_COLOR,inpBorderColor);
ObjectSetInteger(0,inputName,OBJPROP_BORDER_TYPE,inpBorderType);
ObjectSetInteger(0,inputName,OBJPROP_HIDDEN,true);
ObjectSetInteger(0,inputName,OBJPROP_STATE,false);
ObjectSetInteger(0,inputName,OBJPROP_FONTSIZE,fontSize);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void MakeButton(string buttonName,string buttonText,int x=0,int y=0,int width=100,int height=50,int btnColor=Gray,int btnBacgroundColor=Black,int btnBorderColor=Black,int btnBorderType=BORDER_FLAT,int fontSize=12)
{
MakeUserInput(OBJ_BUTTON,buttonName,buttonText,x,y,width,height,btnColor,btnBacgroundColor,btnBorderColor,btnBorderType,fontSize);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void MakeInputBox(string inputName,string inputText,int x=0,int y=0,int width=100,int height=50,int inpColor=Gray,int inpBacgroundColor=Black,int inpBorderColor=Black,int inpBorderType=BORDER_FLAT,int fontSize=12)
{
MakeUserInput(OBJ_EDIT,inputName,inputText,x,y,width,height,inpColor,inpBacgroundColor,inpBorderColor,inpBorderType,fontSize);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void MakeLabel(string labelName,string labelText,int x=0,int y=0,int width=100,int height=50,int lblColor=Gray,int lblBacgroundColor=Black,int lblBorderColor=Black,int lblBorderType=BORDER_FLAT,int fontSize=12)
{
MakeUserInput(OBJ_EDIT,labelName,labelText,x,y,width,height,lblColor,lblBacgroundColor,lblBorderColor,lblBorderType,fontSize);
ObjectSetInteger(0,labelName,OBJPROP_READONLY,1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void MakeManualEntryPanel()
{
int fontSize=12;
MakeButton(BuyButton,"Buy",10,25,87,50,White,Blue,Gray,BORDER_RAISED,fontSize);
MakeButton(SellButton,"Sell",97,25,88,50,White,Red,Gray,BORDER_RAISED,fontSize);
MakeButton(CloseAllButton,"Close All",10,75,175,50,White,Green,Gray,BORDER_RAISED,fontSize);
MakeLabel(PositionSizeLabel,"Lots : ",10,125,100,25,White,Gray,Gray,BORDER_FLAT,fontSize);
MakeInputBox(PositionSizeInput,"0.01",110,125,75,25,Black,White,Gray,BORDER_FLAT,fontSize);
MakeLabel(StopLossLabel,"Stop Loss : ",10,150,100,25,White,Gray,Gray,BORDER_FLAT,fontSize);
MakeInputBox(StopLossInput,"0.00100",110,150,75,25,Black,White,Gray,BORDER_FLAT,fontSize);
SetInputText(StopLossInput,DoubleToString(StopLossDistance,5));
MakeLabel(TakeProfitLabel,"Take Profit : ",10,175,100,25,White,Gray,Gray,BORDER_FLAT,fontSize);
MakeInputBox(TakeProfitInput,"0.00100",110,175,75,25,Black,White,Gray,BORDER_FLAT,fontSize);
SetInputText(TakeProfitInput,DoubleToString(TakeProfitDistance,5));
MakeLabel(OrderCommentLabel,"Comment : ",10,200,100,25,White,Gray,Gray,BORDER_FLAT,fontSize);
MakeInputBox(OrderCommentInput,"Training",110,200,75,25,Black,White,Gray,BORDER_FLAT,fontSize);
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
MakeManualEntryPanel();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(ObjectFind(0,BuyButton)<0)
{
MakeManualEntryPanel();
}
// Only needed in Visual Testing Mode
if(IsVisualMode())
{
if(bool(ObjectGetInteger(0,BuyButton,OBJPROP_STATE)))
{
BuyButton_Click();
}
if(bool(ObjectGetInteger(0,SellButton,OBJPROP_STATE)))
{
SellButton_Click();
}
if(bool(ObjectGetInteger(0,CloseAllButton,OBJPROP_STATE)))
{
CloseAllButton_Click();
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void SetInputText(string inputName,string text)
{
ObjectSetString(0,inputName,OBJPROP_TEXT,text);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string GetInputText(string inputName)
{
return ObjectGetString(0,inputName,OBJPROP_TEXT);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetInputTextAsDouble(string inputName)
{
return StrToDouble(GetInputText(inputName));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetPositionSize()
{
return GetInputTextAsDouble(PositionSizeInput);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetStopLoss(int orderType)
{
if(orderType==OP_BUY)
{
return Bid - GetInputTextAsDouble(StopLossInput);
}
if(orderType==OP_SELL)
{
return Ask + GetInputTextAsDouble(StopLossInput);
}
return 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetTakeProfit(int orderType)
{
if(orderType==OP_BUY)
{
return Bid + GetInputTextAsDouble(TakeProfitInput);
}
if(orderType==OP_SELL)
{
return Ask - GetInputTextAsDouble(TakeProfitInput);
}
return 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string GetOrderComment()
{
return GetInputText(OrderCommentInput);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void BuyButton_Click()
{
ObjectSetInteger(0,BuyButton,OBJPROP_STATE,false);
bool hedgeViolation=false;
for(int i=OrdersTotal()-1; i>=0; i--)
{
bool ret = OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
Comment("Hedging Prohibited");
hedgeViolation=true;
}
}
if(!hedgeViolation)
{
int ret =OrderSend(NULL,OP_BUY,GetPositionSize(),Ask,0,GetStopLoss(OP_BUY),GetTakeProfit(OP_BUY),GetOrderComment());
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void SellButton_Click()
{
ObjectSetInteger(0,SellButton,OBJPROP_STATE,false);
bool hedgeViolation=false;
for(int i=OrdersTotal()-1; i>=0; i--)
{
bool ret = OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
Comment("Hedging Prohibited");
hedgeViolation=true;
}
}
if(!hedgeViolation)
{
int ret = OrderSend(NULL,OP_SELL,GetPositionSize(),Bid,0,GetStopLoss(OP_SELL),GetTakeProfit(OP_SELL),GetOrderComment());
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseAllButton_Click()
{
ObjectSetInteger(0,CloseAllButton,OBJPROP_STATE,false);
for(int i=OrdersTotal()-1; i>=0; i--)
{
bool ret = OrderSelect(i,SELECT_BY_POS);
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
ret = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5);
else
ret = OrderDelete(OrderTicket());
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, // Event ID
const long& lparam, // Parameter of type long event
const double& dparam, // Parameter of type double event
const string& sparam // Parameter of type string events
)
{
if(sparam==BuyButton) // Buy button has been pressed
{
BuyButton_Click();
}
if(sparam==SellButton) // Sell button has been pressed
{
SellButton_Click();
}
if(sparam==CloseAllButton) // Close button has been pressed
{
CloseAllButton_Click();
}
}
//+------------------------------------------------------------------+