-
Notifications
You must be signed in to change notification settings - Fork 1
/
anti-martingale by MA.mq4
executable file
·429 lines (385 loc) · 34.5 KB
/
anti-martingale by MA.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//+------------------------------------------------------------------+
//| anti-martingale by MA.mq4 |
//| Angus Chiu 2015 |
//| https://www.google.com |
//+------------------------------------------------------------------+
//Preprocessor
#include <IncludeExample.mqh>
#include <stdlib.mqh>
#property copyright "Angus Chiu 2015"
#property link "https://www.google.com"
#property version "1.00"
#property strict
//External, variables
extern double InitalLotSize=0.5;
extern double StopLoss=50;
extern double TrailingStopLoss=50;
extern double TrailingMinProfit=-100;
extern double AntiMartingaleStopLoss=50;
extern double AntiMartingaleMinLotInc=0.1;
extern double TakeProfit=100;
extern double EquityRiskPercentage=4;
//extern int PendingPips = 10;
extern int Slippage=5;
extern int MagicNumber=123;
extern int FastMAPeriod = 10;
extern int SlowMAPeriod = 20;
extern bool useAntiMartingale=true;
extern bool CheckOncePerBar=true;
extern bool useTimer=false;
extern bool useLocalTimeInTimer=false;
extern string AllowedTradeStartTime="1970.1.1.0.0";
extern string AllowedTradeEndTime="2099.12.31.23.59";
//Global variables
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;
datetime currentTimeStamp;
//int ErrorCode;
//int Ticket;
//--- input parameters
input bool DynamicLotSize=true;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
currentTimeStamp=Time[0];
UsePoint=PipPoint(Symbol());
Alert("UsePoint: ",UsePoint);
UseSlippage=GetSlippage(Symbol(),Slippage);
Alert("UseSlippage: ",UseSlippage);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//Alert("OnTick() triggered");
double BuyStopLoss=0;
double BuyTakeProfit=0;
//double CloseLots=0;
//double ClosePrice=0;
//bool Closed;
//bool Deleted;
double OpenPrice=0;
double SellStopLoss=0;
double SellTakeProfit=0;
bool newBar=false;
int barShift=0;
bool isTradeAllowed=false;
datetime currentTime=TimeLocal();
// Condition to trigger trade algorithm
datetime tradeStartTime=StrToTime(AllowedTradeStartTime);
datetime tradeEndTime=StrToTime(AllowedTradeEndTime);
//Check whether entered allow trade time is defined by server time instead of local time.
if(useLocalTimeInTimer)
currentTime=TimeLocal();
else
currentTime=TimeCurrent();
// Check current time is allowed to trade
//Alert("useTime=",useTimer," isTradeAllowed=",isTradeAllowed," currentTime=",currentTime," tradeStartTime=",tradeStartTime);
if(useTimer)
{
if(currentTime<=tradeEndTime && currentTime>=tradeStartTime)
{
isTradeAllowed=true;
}
else
{
isTradeAllowed=false;
}
}
else
isTradeAllowed=true;
if(CheckOncePerBar)
{
barShift=1;
if(currentTimeStamp==Time[0])
{
//Bar has not updated yet.
//Alert("OnTick(): New time bar is not drawn yet, no need to trigger trading algorithm.");
newBar=false;
}
else
{
//Bar has been updated, new time bar is drawn.
Alert("OnTick(): New time bar is drawn, should trigger trading algorithm.");
currentTimeStamp=Time[0]; //Adjust currentTimeStamp to open time of latest K bar.
newBar=true;
}
}
if(newBar && isTradeAllowed) // Check trade condition is allowed to run
{
//--- Begin trade block
Alert("OnTick(): Trade condition true, trade decision started at ",currentTimeStamp);
//Moving averages
if(FastMAPeriod>=SlowMAPeriod)
{
//string ErrDesc;
//string ErrAlert;
string ErrLog;
Alert("OnTick(): FastMAPeriod is longer than SlowMAPeriod, EA should not proceed.");
ErrLog=StringConcatenate("OnTick(): FastMAPeriod is longer than SlowMAPeriod, EA should not proceed.");
Print(ErrLog);
return;
}
double FastMA = iMA(NULL,0, FastMAPeriod,0,MODE_SMA,PRICE_CLOSE,barShift);
double SlowMA = iMA(NULL,0, SlowMAPeriod,0,MODE_SMA,PRICE_CLOSE,barShift);
//Buy order
Alert("OnTick(): FastMA=",FastMA,",SlowMA=",SlowMA,", BuyTicket=",BuyTicket,",SellTicket=",SellTicket);
if(FastMA>=SlowMA)
{
Alert("OnTick(): Buy action: FastMA=",FastMA,",SlowMA=",SlowMA,", BuyTicket=",BuyTicket,",SellTicket=",SellTicket);
//Close existing sell position, if any
//if(SellMarketCount(Symbol(),MagicNumber)>=0)
//{
CloseAllSellOrders(Symbol(),MagicNumber,UseSlippage);
Alert("OnTick(): SellMarketCount() now returns ",SellMarketCount(Symbol(),MagicNumber));
if(SellMarketCount(Symbol(),MagicNumber)==0)
{
SellTicket=0;
Alert("OnTick(): reset SellTicket to ",SellTicket);
}
//}
//Delete outstanding sell order, if any
CloseAllSellStopOrders(Symbol(),MagicNumber);
Alert("OnTick(): SellStopCount() now returns ",SellStopCount(Symbol(),MagicNumber));
CloseAllSellLimitOrders(Symbol(),MagicNumber);
Alert("OnTick(): SellLimitCount() now returns ",SellLimitCount(Symbol(),MagicNumber));
//Open buy order
OpenPrice=Ask;
int marketOrderCnt=BuyMarketCount(Symbol(),MagicNumber);
if(marketOrderCnt==0)
{
// First order for this MA cross, enter initial ordering logic
//Calculate lot size
InitalLotSize=CalcLotSize(DynamicLotSize,EquityRiskPercentage,StopLoss,InitalLotSize);
InitalLotSize=VerifyLotSize(InitalLotSize);
Alert("OnTick(): DynamicLotSize=",DynamicLotSize,", InitalLotSize=",InitalLotSize);
//Calculate stop loss and take profit
if(StopLoss>0)
{
BuyStopLoss=CalcBuyStopLoss(Symbol(),StopLoss,OpenPrice);
BuyStopLoss=AdjustBelowStopLevel(Symbol(),BuyStopLoss);
}
if(TakeProfit>0)
{
BuyTakeProfit=CalcBuyTakeProfit(Symbol(),TakeProfit,OpenPrice);
BuyTakeProfit=AdjustAboveStopLevel(Symbol(),BuyTakeProfit);
}
BuyTicket=OpenBuyOrder(Symbol(),InitalLotSize,UseSlippage,MagicNumber);
AddStopProfit(BuyTicket,BuyStopLoss,BuyTakeProfit);
Alert("OnTick(): BuyTicket=",BuyTicket,", TP=",BuyTakeProfit,", SL=",BuyStopLoss);
if(BuyTicket==-1)
{
//Problem in open market buy order, BuyTicket set back to 0 to denote no buy order opened.
Alert("OnTick(): Problem to open buy order, BuyTicket=",BuyTicket);
BuyTicket=0;
Alert("OnTick(): Problem to open buy order, now BuyTicket reset to ",BuyTicket);
}
}
else if(marketOrderCnt>0 && useAntiMartingale)
{
// Buy order is already in place, calculate whether follow up order is needed.
double newLotShouldAdd=CalcMartingaleLotSize(MagicNumber,AntiMartingaleStopLoss,AntiMartingaleMinLotInc);
bool selected=OrderSelect(BuyTicket,SELECT_BY_TICKET);
if(selected==false)
{
int ErrorCode=GetLastError();
string ErrDesc=ErrorDescription(ErrorCode);
string ErrAlert=StringConcatenate(Symbol(),"OnTick(): Select First Buy Order - Error: ",ErrorCode,": ",ErrDesc);
Alert(ErrAlert);
string ErrLog=StringConcatenate("Ticket: ",BuyTicket);
Print(ErrLog);
}
else
{
if(newLotShouldAdd>0)
{
// Create add-on order
int addonBuyTicket=OpenBuyOrder(Symbol(),newLotShouldAdd,UseSlippage,MagicNumber);
AddStopProfit(addonBuyTicket,OrderStopLoss(),OrderTakeProfit());
Alert("OnTick(): addonBuyTicket=",addonBuyTicket,", TP=",BuyTakeProfit,", SL=",BuyStopLoss,", lot size=",newLotShouldAdd);
if(addonBuyTicket==-1)
{
//Problem in open add-on market buy order
Alert("OnTick(): Problem to open add-on buy order, BuyTicket=",addonBuyTicket);
}
}
}
}
else
{
int ErrorCode=GetLastError();
string ErrDesc=ErrorDescription(ErrorCode);
string ErrAlert=StringConcatenate(Symbol(),"OnTick(): Market order count smaller than 0 - Error: ",ErrorCode,": ",ErrDesc);
Alert(ErrAlert);
string ErrLog=StringConcatenate("Ticket: ",BuyTicket);
Print(ErrLog);
}
}
//Sell order
if(FastMA<SlowMA)
{
Alert("OnTick(): Sell action: FastMA=",FastMA,",SlowMA=",SlowMA,", BuyTicket=",BuyTicket,",SellTicket=",SellTicket);
CloseAllBuyOrders(Symbol(),MagicNumber,UseSlippage);
Alert("OnTick(): CloseAllBuyOrders()");
Alert("OnTick(): BuyMarketCount() now returns ",BuyMarketCount(Symbol(),MagicNumber));
if(BuyMarketCount(Symbol(),MagicNumber)==0)
{
BuyTicket=0;
Alert("OnTick(): reset BuyTicket to ",BuyTicket);
}
//}
//Delete outstanding buy order, if any
CloseAllBuyStopOrders(Symbol(),MagicNumber);
Alert("OnTick(): BuyStopCount() now returns ",BuyStopCount(Symbol(),MagicNumber));
CloseAllBuyLimitOrders(Symbol(),MagicNumber);
Alert("OnTick(): BuyLimitCount() now returns ",BuyLimitCount(Symbol(),MagicNumber));
//Open sell order
OpenPrice=Bid;
int marketOrderCnt=SellMarketCount(Symbol(),MagicNumber);
if(marketOrderCnt==0)
{
// First order for this MA cross, enter initial ordering logic
//Calculate lot size
InitalLotSize=CalcLotSize(DynamicLotSize,EquityRiskPercentage,StopLoss,InitalLotSize);
InitalLotSize=VerifyLotSize(InitalLotSize);
Alert("OnTick(): DynamicLotSize=",DynamicLotSize,", InitalLotSize=",InitalLotSize);
//Calculate stop loss and take profit
if(StopLoss>0)
{
SellStopLoss=CalcSellStopLoss(Symbol(),StopLoss,OpenPrice);
SellStopLoss=AdjustAboveStopLevel(Symbol(),SellStopLoss);
}
if(TakeProfit>0)
{
SellTakeProfit=CalcSellTakeProfit(Symbol(),TakeProfit,OpenPrice);
SellTakeProfit=AdjustBelowStopLevel(Symbol(),SellTakeProfit);
}
SellTicket=OpenSellOrder(Symbol(),InitalLotSize,UseSlippage,MagicNumber);
AddStopProfit(SellTicket,SellStopLoss,SellTakeProfit);
Alert("OnTick(): SellTicket=",SellTicket,", TP=",SellTakeProfit,", SL=",SellStopLoss);
if(SellTicket==-1)
{
//Problem in open market sell order, SellTicket set back to 0 to denote no buy order opened.
Alert("OnTick(): Problem to open sell order, SellTicket=",SellTicket);
SellTicket=0;
Alert("OnTick(): Problem to open sell order, now SellTicket reset to ",SellTicket);
}
}
else if(marketOrderCnt>0 && useAntiMartingale)
{
// Sell order is already in place, calculate whether follow up order is needed.
double newLotShouldAdd=CalcMartingaleLotSize(MagicNumber,AntiMartingaleStopLoss,AntiMartingaleMinLotInc);
bool selected=OrderSelect(SellTicket,SELECT_BY_TICKET);
if(selected==false)
{
int ErrorCode=GetLastError();
string ErrDesc=ErrorDescription(ErrorCode);
string ErrAlert=StringConcatenate(Symbol(),"OnTick(): Select First Sell Order - Error: ",ErrorCode,": ",ErrDesc);
Alert(ErrAlert);
string ErrLog=StringConcatenate("Ticket: ",SellTicket);
Print(ErrLog);
}
else
{
if(newLotShouldAdd>0)
{
// Create add-on order
int addonSellTicket=OpenSellOrder(Symbol(),newLotShouldAdd,UseSlippage,MagicNumber);
AddStopProfit(addonSellTicket,OrderStopLoss(),OrderTakeProfit());
Alert("OnTick(): addonSellTicket=",addonSellTicket,", TP=",SellTakeProfit,", SL=",SellStopLoss,", lot size=",newLotShouldAdd);
if(addonSellTicket==-1)
{
//Problem in open add-on market sell order
Alert("OnTick(): Problem to open add-on sell order, SellTicket=",addonSellTicket);
}
}
}
}
else
{
int ErrorCode=GetLastError();
string ErrDesc=ErrorDescription(ErrorCode);
string ErrAlert=StringConcatenate(Symbol(),"OnTick(): Market order count smaller than 0 - Error: ",ErrorCode,": ",ErrDesc);
Alert(ErrAlert);
string ErrLog=StringConcatenate("Ticket: ",SellTicket);
Print(ErrLog);
}
//OpenPrice=Bid;
//Calculate stop loss and take profit
// if(StopLoss>0)
// {
// SellStopLoss=CalcSellStopLoss(Symbol(),StopLoss,OpenPrice);
// SellStopLoss=AdjustAboveStopLevel(Symbol(),SellStopLoss);
// }
// if(TakeProfit>0)
// {
// SellTakeProfit=CalcSellTakeProfit(Symbol(),TakeProfit,OpenPrice);
// SellTakeProfit=AdjustBelowStopLevel(Symbol(),SellTakeProfit);
// }
//
// //Calculate lot size
// InitalLotSize=CalcLotSize(DynamicLotSize,EquityRiskPercentage,StopLoss,InitalLotSize);
// InitalLotSize=VerifyLotSize(InitalLotSize);
// Alert("OnTick(): DynamicLotSize=",DynamicLotSize,", InitalLotSize=",InitalLotSize);
//
// //Open sell order
// SellTicket=OpenSellOrder(Symbol(),InitalLotSize,UseSlippage,MagicNumber);
// AddStopProfit(SellTicket,SellStopLoss,SellTakeProfit);
// Alert("OnTick(): SellTicket=",SellTicket,", TP=",SellTakeProfit,", SL=",SellStopLoss);
//
// if(SellTicket==-1)
// {
// //Problem in open market sell order, SellTicket set back to 0 to denote no sell order opened.
// Alert("OnTick(): Problem to open sell order, SellTicket=",SellTicket);
// SellTicket=0;
// Alert("OnTick(): Problem to open sell order, now SellTicket reset to ",SellTicket);
// }
}
//-- End of trade block
}
//-- Begin trailing stop adjustment
//TrailingStop and CalMartingaleLotSize is analyzed and adjusted every tick.
BuyTrailingStop(Symbol(),TrailingStopLoss,TrailingMinProfit,MagicNumber); // Once opened, immediately trigger trailing SL no matter what TP
SellTrailingStop(Symbol(),TrailingStopLoss,TrailingMinProfit,MagicNumber); // Once opened, immediately trigger trailing SL no matter what TP
//-- End of trailing stop adjustment
return;
}
//+------------------------------------------------------------------+
//| Tester function |
//+------------------------------------------------------------------+
double OnTester()
{
//---
double ret=0.0;
Alert("OnTester(): triggered");
//---
//---
return(ret);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+