-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFractalTrailingStop.mq5
425 lines (404 loc) · 16.4 KB
/
FractalTrailingStop.mq5
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
#include <Trade\Trade.mqh>
CTrade trade;
#include <Trade\AccountInfo.mqh>
CAccountInfo accountInfo;
#include <Steve\Direction_Fractal_H1H4.mqh>
int upFracIndex=0, downFracIndex=0, H4UpFracIndex=0, H4DownFracIndex=0, handle;
double upFractal, currentUpFractal, H4UpFractal, currentH4UpFractal;
double downFractal, currentDownFractal, H4DownFractal, currentH4DownFractal;
double lotSize, orderPrice;
bool positions = false, pendingTrendOrders = false, pendingCounterTrendOrders = false;
string position = "FLAT", resultDesc = trade.ResultRetcodeDescription(), currentDirection;
double resultCode = trade.ResultRetcode();
input double risk = .001; //Amount of Account Equity to risk on trades
input double highPointsPad = 30;
input double lowPointsPad = 20;
void OnInit()
{
handle = iFractals(_Symbol, _Period);
currentUpFractal = GetUpFractal(); // get the most recent up fractal
upFracIndex = GetUpFractalIndex();
currentDownFractal = GetDownFractal(); // get the most recent down fractal
//currentDownFractal = 0;
downFracIndex = GetDownFractalIndex();
currentDirection = GetDirection();
//Print(currentUpFractal, " ", currentDownFractal, " ",positions, " ",pendingTrendOrders);
//Print("UFI: ", upFracIndex, " DFI: ", downFracIndex);
PrintComment();
//Print(positions," ",pendingTrendOrders," ",pendingCounterTrendOrders);
}
//void OnTimer()
void OnTick()
{
// ***Do we have any open positions?***
if (PositionsTotal() == 0) positions = false; position = "FLAT";
for (int i = PositionsTotal()-1; i>=0; i--)
{
//positions = false; position = "FLAT"; // reset positions flag and position direction
if (PositionGetSymbol(i) == _Symbol) //yes, we have open position(s)
{
positions = true;
// Are we long or short?
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) // If we're long, get the ticket number and current stoploss
{
//Print(_Point);
ResetLastError();
position = "LONG";
ulong ticket = PositionGetTicket(i);
double currentStopLoss = PositionGetDouble(POSITION_SL);
//upFractal = GetUpFractal(); // get the latest up fractal
downFractal = GetDownFractal(); // get the latest down fractal
if (MathRound((downFractal-Point()*lowPointsPad)/Point()) != MathRound(currentStopLoss/Point()))// || PositionGetDouble(POSITION_SL) == 0.0) // Modify stop loss if the Down Fractal has changed
{
//Print(downFractal- _Point * lowPointsPad," ",currentStopLoss);
if(!trade.PositionModify(ticket, downFractal-Point()*lowPointsPad, 0.00))
{
Print("PositionModify failed. Result Code: ",resultCode," ",resultDesc);
//currentDownFractal = downFractal;
}
else
{
Print("PositionModify successful. Result Code: ", resultCode," ",resultDesc);
currentDownFractal = downFractal;
}
}
PrintComment();
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) // If we're short, get the ticket number and current stoploss
{
double currentStopLoss = PositionGetDouble(POSITION_SL);
//Print(MathRound((upFractal+_Point*highPointsPad)/Point())," ",MathRound(currentStopLoss/Point()));
ResetLastError();
position = "SHORT";
ulong ticket = PositionGetTicket(i);
//double currentStopLoss = PositionGetDouble(POSITION_SL);
//downFractal = GetDownFractal(); // get the latest down fractal
upFractal = GetUpFractal(); // Get the latest up fractal
if (MathRound((upFractal+Point()*highPointsPad)/Point()) != MathRound(currentStopLoss/Point()) || PositionGetDouble(POSITION_SL) == 0.0) // Modify stop loss if it has changed
{
if(!trade.PositionModify(ticket, upFractal+Point()*highPointsPad, 0.00))
{
Print("PositionModify failed. Result Code: ",resultCode," ",resultDesc);
}
else
{
Print("PositionModify successful. Result Code: ", resultCode," ",resultDesc);
currentUpFractal = upFractal;
}
}
PrintComment();
}
}
}
// ***Do we have any pending orders?***
MqlRates rates[];
ArraySetAsSeries(rates, true);
CopyRates(Symbol(),PERIOD_CURRENT,0,1,rates);
if (OrdersTotal() == 0) pendingTrendOrders = false; pendingCounterTrendOrders = false;
for(int i=OrdersTotal()-1; i>=0; i--)
{
//pendingTrendOrders = false; pendingCounterTrendOrders = false;
ulong ticket = OrderGetTicket(i);
if(OrderGetString(ORDER_SYMBOL) == _Symbol)
{
if(currentDirection == "UP" && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP)
{
ResetLastError();
pendingTrendOrders = true;
downFractal = GetDownFractal();
upFractal = GetUpFractal();
if(upFractal != currentUpFractal || downFractal != currentDownFractal || rates[0].low < downFractal)
{
if(!trade.OrderDelete(ticket))
{
Print("OrderDelete failed. Result Code: ",resultCode," ",resultDesc);
}
else
{
Print("OrderDelete successful. Result Code: ", resultCode," ",resultDesc);
currentUpFractal = upFractal;
currentDownFractal = downFractal;
}
CreateTrendOrder();
PrintComment();
}
}
else if(currentDirection == "DOWN" && OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP)
{
ResetLastError();
pendingTrendOrders = true;
downFractal = GetDownFractal();
upFractal = GetUpFractal();
if(upFractal != currentUpFractal || downFractal != currentDownFractal || rates[0].high > upFractal)
{
if(!trade.OrderDelete(ticket))
{
Print("OrderDelete failed. Result Code: ",resultCode," ",resultDesc);
}
else
{
Print("OrderDelete successful. Result Code: ", resultCode," ",resultDesc);
currentUpFractal = upFractal;
currentDownFractal = downFractal;
}
CreateTrendOrder();
PrintComment();
}
}
}
}
if(positions == false && pendingTrendOrders == false)
{
ResetLastError();
CreateTrendOrder();
PrintComment();
}
//if(pendingCounterTrendOrders == false)
// {
// ResetLastError();
// //CreateCounterTrendOrder();
// PrintComment();
// }
} // End of OnTick function
//***FUNCTIONS***
//GetUpFractal
//GetUpFractalIndex
//GetDownFractal
//GetDownFractalIndex
//GetH4UpFractal
//GetH4DownFractal
//PrintComment
//CreateTrendOrder
//GetLotSize
double GetUpFractal() // This is the Up Fractal for the current timeframe
{ // Create data table to store fractal values
double upFractalData[];
int fractalHandle;
ArraySetAsSeries(upFractalData,true); // Create dynamic array to hold up fractal values
fractalHandle = iFractals(_Symbol, _Period);
int numberOfUpFractalData = CopyBuffer(fractalHandle, 0, 0, 100, upFractalData);
for(int i=3; i<=99; i++) // Get the first Up Fractal
{
if(upFractalData[i] != EMPTY_VALUE)
{
upFractal = upFractalData[i];
break;
}
}
return(upFractal);
}
int GetUpFractalIndex() // This is the Up Fractal for the current timeframe
{ // Create data table to store fractal values
int index = 0;
double upFractalData[];
int fractalHandle;
ArraySetAsSeries(upFractalData,true); // Create dynamic array to hold up fractal values
fractalHandle = iFractals(_Symbol, _Period);
int numberOfUpFractalData = CopyBuffer(fractalHandle, 0, 0, 100, upFractalData);
for(int i=3; i<=99; i++) // Get the first Up Fractal
{
if(upFractalData[i] != EMPTY_VALUE)
{
index = i;
break;
}
}
return(index);
}
double GetDownFractal() // This is the Down Fractal for the current timeframe
{ // Create data table to store fractal values
double downFractalData[];
int fractalHandle;
ArraySetAsSeries(downFractalData, true); // Create dynamic array to hold down fractal values
fractalHandle = iFractals(_Symbol, _Period);
int numberOfDownFractalData = CopyBuffer(fractalHandle, 1, 0, 100,downFractalData);
for(int i=3; i<=99; i++) // Get the first Down Fractal
{
if(downFractalData[i] != EMPTY_VALUE)
{
downFractal = downFractalData[i];
break;
}
}
return(downFractal);
}
int GetDownFractalIndex() // This is the Down Fractal for the current timeframe
{ // Create data table to store fractal values
int index = 3;
double downFractalData[];
int fractalHandle;
ArraySetAsSeries(downFractalData, true); // Create dynamic array to hold down fractal values
fractalHandle = iFractals(_Symbol, _Period);
int numberOfDownFractalData = CopyBuffer(fractalHandle, 1, 0, 100,downFractalData);
for(int i=3; i<=99; i++) // Get the first Down Fractal
{
if(downFractalData[i] != EMPTY_VALUE)
{
index = i;
break;
}
}
return(index);
}
double GetH4UpFractal() // This is the Up Fractal for the H4 timeframe
{ // Create data table to store fractal values
double upFractalData[];
int fractalHandle;
ArraySetAsSeries(upFractalData,true); // Create dynamic array to hold up fractal values
fractalHandle = iFractals(_Symbol, PERIOD_H4);
int numberOfUpFractalData = CopyBuffer(fractalHandle, 0, 0, 100, upFractalData);
for(int i=3; i<=99; i++) // Get the first Up Fractal
{
if(upFractalData[i] != EMPTY_VALUE)
{
upFractal = upFractalData[i];
break;
}
}
return(upFractal);
}
int GetH4UpFractalIndex() // This is the Up Fractal for the current timeframe
{ // Create data table to store fractal values
int index = 0;
double upFractalData[];
int fractalHandle;
ArraySetAsSeries(upFractalData,true); // Create dynamic array to hold up fractal values
fractalHandle = iFractals(_Symbol, _Period);
int numberOfUpFractalData = CopyBuffer(fractalHandle, 0, 0, 100, upFractalData);
for(int i=3; i<=99; i++) // Get the first Up Fractal
{
if(upFractalData[i] != EMPTY_VALUE)
{
index = i;
break;
}
}
return(index);
}
double GetH4DownFractal() // This is the Down Fractal for the H4 timeframe
{ // Create data table to store fractal values
double downFractalData[];
int fractalHandle;
ArraySetAsSeries(downFractalData, true); // Create dynamic array to hold down fractal values
fractalHandle = iFractals(_Symbol, PERIOD_H4);
int numberOfDownFractalData = CopyBuffer(fractalHandle, 1, 0, 100,downFractalData);
for(int i=3; i<=99; i++) // Get the first Down Fractal
{
if(downFractalData[i] != EMPTY_VALUE)
{
downFractal = downFractalData[i];
break;
}
}
return(downFractal);
}
void PrintComment()
{
Comment ("H4 Trend: ", GetDirection(),
"\nPosition: ", position,
"\nLot Size: ", GetLotSize(upFractal, downFractal),
"\nBuy Price: ",upFractal+_Point*highPointsPad,
"\nSell Price: ", downFractal-_Point*lowPointsPad,
"\nTick Value: ", NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE),4));
}
void CreateTrendOrder()
{
double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK), Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID), price;
string direction = GetDirection();
double high[], low[];
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
upFractal = GetUpFractal();
upFracIndex = GetUpFractalIndex();
downFractal = GetDownFractal();
downFracIndex = GetDownFractalIndex();
CopyHigh(_Symbol,PERIOD_CURRENT,0,upFracIndex,high);
CopyLow(_Symbol,PERIOD_CURRENT,0,downFracIndex,low);
lotSize = GetLotSize(upFractal, downFractal);
if(direction == "UP" && Ask < upFractal && Ask > downFractal
&& low[ArrayMinimum(low,0,downFracIndex)] > downFractal && high[ArrayMaximum(high,0,upFracIndex)] < upFractal)
{
price = upFractal+_Point*highPointsPad;
if(!trade.BuyStop(lotSize,price,_Symbol,0,0,0,0,NULL))
{
//--- failure message
Print("BuyStop method failed. Return code: ",GetLastError()," ",resultDesc);
}
else
{
Print("BuyStop method executed successfully. Return code: ",resultCode," ",resultDesc);
pendingTrendOrders = true;
}
}
else if(direction == "DOWN" && Bid < upFractal && Bid > downFractal
&& low[ArrayMinimum(low,0,downFracIndex)] > downFractal && high[ArrayMaximum(high,0,upFracIndex)] < upFractal)
{
price = downFractal-_Point*lowPointsPad;
if(!trade.SellStop(lotSize,price,_Symbol,0,0,0,0,NULL))
{
//--- failure message
Print("SellStop method failed. Return code: ",resultCode,
" Result Description: ",resultDesc);
}
else
{
Print("SellStop method executed successfully. Return code: ",resultCode," ",resultDesc);
pendingTrendOrders = true;
}
}
}
void CreateCounterTrendOrder()
{
double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK), Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID), price;
string direction = GetDirection();
double high[], low[];
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
upFractal = GetUpFractal();
upFracIndex = GetUpFractalIndex();
H4UpFractal = GetH4UpFractal();
//H4UpFracIndex = Get
downFractal = GetDownFractal();
H4DownFractal = GetH4DownFractal();
CopyHigh(_Symbol,PERIOD_CURRENT,0,upFracIndex,high);
CopyLow(_Symbol,PERIOD_CURRENT,0,downFracIndex,low);
if(direction == "UP" && Bid < upFractal && Bid > H4DownFractal)
{
lotSize = GetLotSize(upFractal, H4DownFractal);
price = H4DownFractal-_Point*lowPointsPad;
if(!trade.SellStop(lotSize,price,_Symbol,0,0,0,0,NULL))
{
//--- failure message
Print("SellStop method failed. Return code: ",GetLastError()," ",resultDesc);
//Print(direction, " ", _Symbol," ",lotSize," ",price);
}
else
{
Print("SellStop method executed successfully. Return code: ",resultCode," ",resultDesc);
pendingCounterTrendOrders = true;
}
}
else if(direction == "DOWN" && Ask < H4UpFractal && Ask > downFractal)
{
lotSize = GetLotSize(H4UpFractal, downFractal);
price = H4UpFractal+_Point*highPointsPad;
if(!trade.BuyStop(lotSize,price,Symbol(),0,0,0,0,NULL))
{
//--- failure message
Print("BuyStop method failed. Return code: ",resultCode,
" Result Description: ",resultDesc);
}
else
{
Print("BuyStop method executed successfully. Return code: ",resultCode," ",resultDesc);
pendingCounterTrendOrders = true;
}
}
}
double GetLotSize(double buyPrice, double sellPrice)
{
Sleep(1000);
//Print(buyPrice," ",sellPrice);
lotSize = NormalizeDouble((accountInfo.Equity()*risk)/((buyPrice+_Point*highPointsPad)-(sellPrice-_Point*lowPointsPad))/MathPow(10,_Digits),2);
return lotSize;
}