-
Notifications
You must be signed in to change notification settings - Fork 1
/
MA Cross Advanced.mq4
executable file
·381 lines (295 loc) · 12.6 KB
/
MA Cross Advanced.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//+------------------------------------------------------------------+
//| MA Cross Advanced.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Ryan Klefas"
#property link "rklefas@inbox.com"
extern string basicheader="== Basic Settings ==";
extern double TakeProfit=0;
extern double Stoploss=0;
double Lots=0.1;
extern string eaheader="== EA Options ==";
extern bool allowClosing=true;
extern int profitTarget1=21;
extern int profitTarget2=34;
extern int profitTarget3=55;
extern int profitTarget4=89;
extern double lotSize1=0.1;
extern double lotSize2=0.1;
extern double lotSize3=0.1;
extern double lotSize4=0.1;
extern double lotSize5=0.1;
extern string stopheader="== Advanced Stops ==";
extern double TrailingStop=100;
extern bool TrailingStopOnlyProfit=false;
extern bool TrailingStopRegular=true;
extern int breakEvenAtProfit=0;
extern int breakEvenShift=0;
extern string indheader = "== Indicators ==";
extern int maOPENperiods=5;
extern int maCLOSEperiods=5;
extern int MAmode=1; /*MODE_SMA 0 Simple moving average,
MODE_EMA 1 Exponential moving average,
MODE_SMMA 2 Smoothed moving average,
MODE_LWMA 3 Linear weighted moving average. */
extern int current=0;
extern int prev=1;
extern int anchorA=2;
extern int anchorB=4;
extern int anchorC=6;
string commentString="MA Cross";
int magicNum=6873516;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if (MAmode<0 || MAmode>3)
MAmode=1;
Lots=lotSize1+lotSize2+lotSize3+lotSize4+lotSize5;
int OrdersOpen=0, buyOrders=0, sellOrders=0, pendingOrders=0;
checkOrderStatus(OrdersOpen,buyOrders,sellOrders,pendingOrders);
advancedStopManager(OrdersOpen);
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
double maOPEN =iMA(NULL,0,maOPENperiods,0,MAmode,PRICE_OPEN,current);
double maCLOSE =iMA(NULL,0,maCLOSEperiods,0,MAmode,PRICE_CLOSE,current);
double maOPENpast =iMA(NULL,0,maOPENperiods,0,MAmode,PRICE_OPEN,prev);
double maCLOSEpast =iMA(NULL,0,maCLOSEperiods,0,MAmode,PRICE_CLOSE,prev);
double maOPENAnchorA =iMA(NULL,0,maOPENperiods,0,MAmode,PRICE_OPEN,anchorA);
double maCLOSEAnchorA =iMA(NULL,0,maCLOSEperiods,0,MAmode,PRICE_CLOSE,anchorA);
double maOPENAnchorB =iMA(NULL,0,maOPENperiods,0,MAmode,PRICE_OPEN,anchorB);
double maCLOSEAnchorB =iMA(NULL,0,maCLOSEperiods,0,MAmode,PRICE_CLOSE,anchorB);
double maOPENAnchorC =iMA(NULL,0,maOPENperiods,0,MAmode,PRICE_OPEN,anchorC);
double maCLOSEAnchorC =iMA(NULL,0,maCLOSEperiods,0,MAmode,PRICE_CLOSE,anchorC);
bool anchorsConfirmBuy;
bool anchorsConfirmSell;
bool confirmBuy;
bool confirmSell;
if (maOPENAnchorA<maCLOSEAnchorA && maOPENAnchorB<maCLOSEAnchorB
&& maOPENAnchorC<maCLOSEAnchorC)
anchorsConfirmBuy=true;
else
anchorsConfirmBuy=false;
if (maOPENAnchorA>maCLOSEAnchorA && maOPENAnchorB>maCLOSEAnchorB
&& maOPENAnchorC>maCLOSEAnchorC)
anchorsConfirmSell=true;
else
anchorsConfirmSell=false;
if (maOPEN>maCLOSE && maOPENpast>maCLOSEpast)
confirmBuy=true;
else
confirmBuy=false;
if (maOPEN<maCLOSE && maOPENpast<maCLOSEpast)
confirmSell=true;
else
confirmSell=false;
if (OrdersOpen == 0)
{
if (anchorsConfirmBuy && confirmBuy)
sendBuyOrder();
if (anchorsConfirmSell && confirmSell)
sendSellOrder();
}
if (allowClosing)
{
if (buyOrders == 1)
{
if (maOPENpast<=maCLOSEpast)
closeBuyOrder();
}
if (sellOrders == 1)
{
if (maOPENpast>=maCLOSEpast)
closeSellOrder();
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void sendSellOrder()
{
double Stopvalue=0, TPvalue=0;
if (Stoploss>0)
{Stopvalue = Ask+Stoploss*Point;}
if (TakeProfit>0)
{TPvalue = Bid-TakeProfit*Point;}
int ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Stopvalue,TPvalue,commentString + " " + Period(),magicNum,0,Red);
if(ticket>0)
{if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(commentString + " SELL order at ",OrderOpenPrice()); }
else
{Print("Error opening SELL order : ",GetLastError());}
}
//+------------------------------------------------------------------+
void sendBuyOrder()
{
double Stopvalue=0, TPvalue=0;
if (Stoploss>0)
{Stopvalue = Bid-Stoploss*Point;}
if (TakeProfit>0)
{TPvalue = Ask+TakeProfit*Point;}
int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Stopvalue,TPvalue,commentString + " " + Period(),magicNum,0,Green);
if(ticket>0)
{if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(commentString + " BUY order at ",OrderOpenPrice()); }
else
{Print("Error opening BUY order : ",GetLastError());}
}
//+------------------------------------------------------------------+
void regularTrailingStop()
{
int cnt, total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicNum )
{if(TrailingStop>0)
{if(OrderType()==OP_BUY)
{if(OrderStopLoss()<Bid-Point*TrailingStop)
{ OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
}}}
else
{if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{ OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
}}}}
}
//+------------------------------------------------------------------+
void onlyProfitTrailingStop()
{
int cnt, total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicNum )
{if(TrailingStop>0)
{if(OrderType()==OP_BUY)
{if(Bid-OrderOpenPrice()>Point*TrailingStop)
{if(OrderStopLoss()<Bid-Point*TrailingStop)
{ OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
}}}}
else
{if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{ OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
}}}}}}
//+------------------------------------------------------------------+
void closeSellOrder()
{
int cnt, total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicNum )
{if(OrderType()==OP_SELL)
{OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
}}}}
//+------------------------------------------------------------------+
void closeBuyOrder()
{
int cnt, total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicNum )
{if(OrderType()==OP_BUY)
{OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
}}}}
//+------------------------------------------------------------------+
void breakEvenManager()
{
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicNum )
{if(breakEvenAtProfit>0)
{if(OrderType()==OP_BUY)
{if(Bid-OrderOpenPrice()>=Point*breakEvenAtProfit)
{if(OrderStopLoss()!=OrderOpenPrice() + breakEvenShift*Point)
{ OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+ breakEvenShift*Point,OrderTakeProfit(),0,Green);
}}}}
else
{if((OrderOpenPrice()-Ask)>(Point*breakEvenAtProfit))
{if(OrderStopLoss()!=OrderOpenPrice() - breakEvenShift*Point)
{ OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()- breakEvenShift*Point,OrderTakeProfit(),0,Red);
}}}}}}
//+------------------------------------------------------------------+
void checkOrderStatus(int& OrdersOpen,int& buyOrders,int& sellOrders,int& pendingOrders)
{
for(int cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==magicNum /**/ )
{
if (OrderType()==OP_BUY)
{buyOrders++; OrdersOpen++;}
else if (OrderType()==OP_SELL)
{sellOrders++; OrdersOpen++;}
else
{pendingOrders++; OrdersOpen++;}
}
}
}
//+------------------------------------------------------------------+
void advancedStopManager(int OrdersOpen)
{
if (TrailingStopOnlyProfit && TrailingStopRegular)
{Print("Both Trailing Stops are enabled. Defaulted to Regular TS.");}
if (OrdersOpen >= 1)
{
profitTargets();
if (breakEvenAtProfit>0)
breakEvenManager();
if (TrailingStopRegular)
regularTrailingStop();
else if (TrailingStopOnlyProfit)
onlyProfitTrailingStop();
}
}
//+------------------------------------------------------------------+
void profitTargets()
// Closes lots on open orders when profit targets are hit
{
int cnt, total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicNum )
{if(OrderType()==OP_BUY)
{if( (Bid >= OrderOpenPrice()+profitTarget1*Point && OrderLots() == Lots) ||
(Bid >= OrderOpenPrice()+profitTarget2*Point && OrderLots() == (lotSize2+lotSize3+lotSize4+lotSize5) )||
(Bid >= OrderOpenPrice()+profitTarget3*Point && OrderLots() == (lotSize3+lotSize4+lotSize5) )||
(Bid >= OrderOpenPrice()+profitTarget4*Point && OrderLots() == (lotSize4+lotSize5) ) )
{ OrderClose(OrderTicket(),(Lots/5),Bid,3,CLR_NONE); } } }
{if(OrderType()==OP_SELL)
{if( (Ask <= OrderOpenPrice()-profitTarget1*Point && OrderLots() == Lots) ||
(Ask <= OrderOpenPrice()-profitTarget2*Point && OrderLots() == (lotSize2+lotSize3+lotSize4+lotSize5) )||
(Ask <= OrderOpenPrice()-profitTarget3*Point && OrderLots() == (lotSize3+lotSize4+lotSize5) )||
(Ask <= OrderOpenPrice()-profitTarget4*Point && OrderLots() == (lotSize4+lotSize5) ) )
{ OrderClose(OrderTicket(),(Lots/5),Ask,3,CLR_NONE); } } }
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+