-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBASIC_Martingale_V2.mq5
869 lines (813 loc) · 31.8 KB
/
BASIC_Martingale_V2.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//+------------------------------------------------------------------+
//| ProjectName |
//| Copyright 2020, CompanyName |
//| http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021"
#property link "[email protected]"
#property version "2.0"
#include <Trade\PositionInfo.mqh> CPositionInfo m_position;
#include <Trade\Trade.mqh> CTrade trade;
enum MartinMode
{
Multiply, //Multiply
Increment //Addition
};
enum AveragingMode
{
AverageDown, //Average Down
AverageUp, //Average Up
None //No Average
};
sinput const string Mm="";//-=[ Money Management Settings ]=-
input double iStartLots = 0.01; //Constant lot
sinput const string SLTP="";//-=[ SL-TP Settings ]=-
input double StopLoss = 20; //StopLoss
input double TakeProfit = 20; //TakeProfit
sinput const string Time="";//-=[ Time Settings ]=-
input int StartTime = 3; // Opening Time (Server Time)
input int EndTime = 18; // Last Open Position Time (Server Time)
sinput const string signal="";//-=[ Signal Settings ]=-
input ENUM_TIMEFRAMES Period1 = PERIOD_H1; //Entry Signal Timeframe
sinput const string Indicator="";//-=[ Indicator Settings ]=-
input int bar = 3; //Bars Calculated
input ENUM_TIMEFRAMES ATRTF = PERIOD_CURRENT; //ATR Timeframe
input int ATRPeriod = 14; //ATR Period
sinput const string Martin="";//-=[ Martingale Settings ]=-
input AveragingMode AverageMode = AverageDown; //Choose Averaging Mode
input MartinMode MartinsMode = Multiply; //Choose Averaging/Martingale Mode
input double LotMultiplier = 1.5; // Lot Multiplier
input double LotIncrement = 0.1; // Additional Lot for Averaging
input double GridMultiplier = 0.75; // Grid Multiplier (in ATR)
input int MaxAverageOrders = 3; // Max Average Orders
sinput const string Other="";//-=[ Other Settings ]=-
input bool TradeAtNewBar = false; //Trade at New Bar
input int iMagicNumber = 227; // Magic Number
input int iSlippage = 10; // Slippage/Deviation
input int TrailingStart = 100; // Distance from Start Price (for Average Up Only)
input int TrailingStop = 50; // Trailing Stop (in Points)
input int TrailingStep = 30; // Trailing Step (in Points)
input string Commentary = "Basic Martingale EA"; // Order Comment
input bool ChartInfo = true; //Display Chart Info
//---
double PipValue=1; // this variable is here to support 5-digit brokers
int TEMA, ATR;
//************************************************************************************************/
//* */
//************************************************************************************************/
int OnInit(void)
{
Comment("");
trade.LogLevel(LOG_LEVEL_ERRORS);
trade.SetExpertMagicNumber(iMagicNumber);
trade.SetDeviationInPoints(iSlippage);
trade.SetMarginMode();
trade.SetTypeFilling(ORDER_FILLING_FOK);
trade.SetTypeFillingBySymbol(Symbol());
//Indicators
TEMA = iTEMA(Symbol(),Period(),50,0,PRICE_CLOSE);
if(TEMA == INVALID_HANDLE)
{
Print("Errors for Create TEMA Indicator ",GetLastError());
return(INIT_FAILED);
}
ATR = iATR(Symbol(),ATRTF,ATRPeriod);
if(ATR == INVALID_HANDLE)
{
Print("Errors for Create ATR Indicator ",GetLastError());
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectDelete(ChartID(),"Background");
ObjectDelete(ChartID(),"AccNumber");
ObjectDelete(ChartID(),"AccLeverage");
ObjectDelete(ChartID(),"AccBalance");
ObjectDelete(ChartID(),"AccEquity");
ObjectDelete(ChartID(),"AccMargin");
ObjectDelete(ChartID(),"AccFMargin");
ObjectDelete(ChartID(),"AccProfit");
ObjectDelete(ChartID(),"Signal1");
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool createBackground(string name)
{
ObjectCreate(ChartID(), name, OBJ_RECTANGLE_LABEL, 0, 0, 0);
ObjectSetInteger(ChartID(), name, OBJPROP_XDISTANCE, 10);
ObjectSetInteger(ChartID(), name, OBJPROP_YDISTANCE, 10);
ObjectSetInteger(ChartID(), name, OBJPROP_XSIZE, 300);
ObjectSetInteger(ChartID(), name, OBJPROP_YSIZE, 300);
ObjectSetInteger(ChartID(), name, OBJPROP_BGCOLOR, clrBlack);
ObjectSetInteger(ChartID(), name, OBJPROP_BORDER_COLOR, clrWhite);
ObjectSetInteger(ChartID(), name, OBJPROP_BORDER_TYPE, BORDER_FLAT);
ObjectSetInteger(ChartID(), name, OBJPROP_WIDTH, 0);
ObjectSetInteger(ChartID(), name, OBJPROP_BACK, false);
ObjectSetInteger(ChartID(), name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(ChartID(), name, OBJPROP_HIDDEN, true);
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool createObject(string name,string text,int x,int y,int size, int clr)
{
ObjectCreate(ChartID(),name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(ChartID(),name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(ChartID(),name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(ChartID(),name, OBJPROP_YDISTANCE, y);
ObjectSetString(ChartID(),name,OBJPROP_TEXT, text);
ObjectSetInteger(ChartID(),name,OBJPROP_FONTSIZE,size);
ObjectSetInteger(ChartID(),name,OBJPROP_COLOR,clr);
return (true);
}
//+------------------------------------------------------------------+
void OnTick(void)
{
if(ChartInfo)
{
createBackground("Background");
createObject("AccNumber","Account Number: "+IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN),0),15,20,16,clrRed);
createObject("AccLeverage","Account Leverage: "+IntegerToString(AccountInfoInteger(ACCOUNT_LEVERAGE),2),15,45,10,clrWhite);
createObject("AccBalance","Account Balance: "+DoubleToString(AccountInfoDouble(ACCOUNT_BALANCE),2),15,65,10,clrWhite);
createObject("AccEquity","Account Equity: "+DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY),2),15,85,10,clrWhite);
createObject("AccMargin","Account Margin: "+DoubleToString(AccountInfoDouble(ACCOUNT_MARGIN),2),15,105,10,clrWhite);
createObject("AccFMargin","Account Free Margin: "+DoubleToString(AccountInfoDouble(ACCOUNT_MARGIN_FREE),2),15,125,10,clrWhite);
createObject("AccProfit","Floating P/L: "+DoubleToString(AccountInfoDouble(ACCOUNT_PROFIT),2),15,155,16,clrRed);
createObject("Signal1",EnumToString(Period1)+" Entry Signal: "+Sig(Symbol(),Period1),15,185,10,clrRed);
}
if(!ChartInfo)
{
ObjectDelete(ChartID(),"Background");
ObjectDelete(ChartID(),"AccNumber");
ObjectDelete(ChartID(),"AccLeverage");
ObjectDelete(ChartID(),"AccBalance");
ObjectDelete(ChartID(),"AccEquity");
ObjectDelete(ChartID(),"AccMargin");
ObjectDelete(ChartID(),"AccFMargin");
ObjectDelete(ChartID(),"AccProfit");
ObjectDelete(ChartID(),"Signal1");
}
Trade(Symbol());
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Trade(string sym)
{
if(!RefreshRates(sym))
{
Print("Can't Load "+sym+" Data");
return;
}
double ask = SymbolInfoDouble(sym,SYMBOL_ASK);
double bid = SymbolInfoDouble(sym,SYMBOL_BID);
double points = SymbolInfoDouble(sym,SYMBOL_POINT);
int digits = (int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
if(digits == 3 || digits == 5)
PipValue = 10;
double
BuyPriceMax=0,BuyPriceMin=0,BuyPriceMaxLot=0,BuyPriceMinLot=0,
SelPriceMin=0,SelPriceMax=0,SelPriceMinLot=0,SelPriceMaxLot=0;
ulong
BuyPriceMaxTic=0,BuyPriceMinTic=0,SelPriceMaxTic=0,SelPriceMinTic=0;
ulong tkb= 0,tks=0;
double
op=0,lt=0,tpb=0,tps=0, slb=0,sls=0;
int b=0,s=0, Opens = 0;
for(int k=PositionsTotal()-1; k>=0; k--)
{
if(m_position.SelectByIndex(k))
{
if(m_position.Symbol()==sym)
{
if(m_position.Magic()==iMagicNumber)
{
op=NormalizeDouble(m_position.PriceOpen(),digits);
lt=NormalizeDouble(m_position.Volume(),2);
Opens++;
if(m_position.Select(sym) && m_position.PositionType()==POSITION_TYPE_BUY)
{
b++;
tpb = PositionGetDouble(POSITION_TP);
slb = PositionGetDouble(POSITION_SL);
tkb = m_position.Ticket();
if(op>BuyPriceMax || BuyPriceMax==0)
{
BuyPriceMax = op;
BuyPriceMaxLot = lt;
BuyPriceMaxTic = tkb;
}
if(op<BuyPriceMin || BuyPriceMin==0)
{
BuyPriceMin = op;
BuyPriceMinLot = lt;
BuyPriceMinTic = tkb;
}
}
// ===
else
if(m_position.Select(sym) && m_position.PositionType()==POSITION_TYPE_SELL)
{
s++;
tps = PositionGetDouble(POSITION_TP);
sls = PositionGetDouble(POSITION_SL);
tks = m_position.Ticket();
if(op>SelPriceMax || SelPriceMax==0)
{
SelPriceMax = op;
SelPriceMaxLot = lt;
SelPriceMaxTic = tks;
}
if(op<SelPriceMin || SelPriceMin==0)
{
SelPriceMin = op;
SelPriceMinLot = lt;
SelPriceMinTic = tks;
}
}
}
}
}
}
double BuyLot=0,SelLot=0;
if(MartinsMode == Multiply)
{
BuyLot = NormalizeDouble(BuyPriceMinLot*LotMultiplier,2);
SelLot = NormalizeDouble(SelPriceMaxLot*LotMultiplier,2);
}
else
if(MartinsMode == Increment)
{
BuyLot = NormalizeDouble(BuyPriceMinLot+LotIncrement,2);
SelLot = NormalizeDouble(SelPriceMaxLot+LotIncrement,2);
}
if(!CheckVolumeValue(sym,iStartLots))
return;
double SLsell = 0, SLbuy = 0, TPbuy = 0,TPsell = 0;
if(StopLoss > 0)
{
SLbuy = NormalizeDouble(ask - StopLoss*PipValue*points,digits);
SLsell = NormalizeDouble(bid + StopLoss*PipValue*points,digits);
}
else
{
SLbuy = 0;
SLsell = 0;
}
if(TakeProfit > 0)
{
TPbuy = NormalizeDouble(ask + TakeProfit*PipValue*points,digits);
TPsell = NormalizeDouble(bid - TakeProfit*PipValue*points,digits);
}
else
{
TPbuy = 0;
TPsell = 0;
}
if(((TradeAtNewBar && isNewBar(sym))||(!TradeAtNewBar)) && Times())
{
if(!Opens)
{
if(b == 0 && (Sig(sym,Period1)=="Buy") && CheckMoneyForTrade(sym,iStartLots,ORDER_TYPE_BUY))
{
if(!trade.Buy(NormalizeDouble(iStartLots,2),sym,NormalizeDouble(ask,digits),SLbuy,TPbuy,Commentary))
Print("Open Trade error #",GetLastError());
else
Print("Open Trade Success");
}
else
if(s == 0 && (Sig(sym,Period1)=="Sell") && CheckMoneyForTrade(sym,iStartLots,ORDER_TYPE_SELL))
{
if(!trade.Sell(NormalizeDouble(iStartLots,2),sym,NormalizeDouble(bid,digits),SLsell,TPsell,Commentary))
Print("Open Trade error #",GetLastError());
else
Print("Open Trade Success");
}
}
}
if(Opens)
{
double Grids = NormalizeDouble(atr(sym,ATRTF,0,1),digits);
if(AverageMode == AverageDown)
{
if(b>0 && CheckMoneyForTrade(sym,BuyLot,ORDER_TYPE_BUY))
{
if(b < MaxAverageOrders && BuyPriceMin-ask >= GridMultiplier*Grids)
{
if(!trade.Buy(NormalizeDouble(BuyLot,2),sym,NormalizeDouble(ask,digits),slb,tpb,Commentary))
Print("Averaging Down error #",GetLastError());
else
Print("Averaging Down Success");
return;
}
}
else
if(s>0 && CheckMoneyForTrade(sym,SelLot,ORDER_TYPE_SELL))
{
if(s < MaxAverageOrders && bid-SelPriceMax >= GridMultiplier*Grids)
{
if(!trade.Sell(NormalizeDouble(SelLot,2),sym,NormalizeDouble(bid,digits),sls,tps,Commentary))
Print("Averaging Down error #",GetLastError());
else
Print("Averaging Down Success");
return;
}
}
if(b <= 1)
{
Trailing(sym);
}
if(b > 1)
{
if(ProfitLossOrders(sym) > 1)
CloseAll(sym);
}
if(s <= 1)
{
Trailing(sym);
}
if(s > 1)
{
if(ProfitLossOrders(sym) > 1)
CloseAll(sym);
}
}
else
if(AverageMode == AverageUp)
{
if(b>0 && CheckMoneyForTrade(sym,BuyLot,ORDER_TYPE_BUY))
{
if(b < MaxAverageOrders && ask - GridMultiplier*Grids >= BuyPriceMax)
{
if(!trade.Buy(NormalizeDouble(BuyLot,2),sym,NormalizeDouble(ask,digits),slb,tpb,Commentary))
Print("Averaging Up error #",GetLastError());
else
Print("Averaging Up Success");
return;
}
}
else
if(s>0 && CheckMoneyForTrade(sym,SelLot,ORDER_TYPE_SELL))
{
if(s < MaxAverageOrders && SelPriceMin - GridMultiplier*Grids >= bid)
{
if(!trade.Sell(NormalizeDouble(SelLot,2),sym,NormalizeDouble(bid,digits),sls,tps,Commentary))
Print("Averaging Up error #",GetLastError());
else
Print("Averaging Up Success");
return;
}
}
double TrailStart = 0;
if(b >= MaxAverageOrders)
{
TrailStart = BuyPriceMin + TrailingStart*points;
AverageUpTrail(sym,TrailStart,TrailingStop,TrailingStep);
}
if(s >= MaxAverageOrders)
{
TrailStart = SelPriceMax - TrailingStart*points;
AverageUpTrail(sym,TrailStart,TrailingStop,TrailingStep);
}
}
else
if(AverageMode == None)
{
Trailing(sym);
}
}
ResetLastError();
return;
}
//************************************************************************************************/
//* */
//************************************************************************************************/
bool CheckVolumeValue(string sym, double volume)
{
//--- минимально допустимый объем для торговых операций
double min_volume=SymbolInfoDouble(sym,SYMBOL_VOLUME_MIN);
if(volume<min_volume)
return(false);
//--- максимально допустимый объем для торговых операций
double max_volume=SymbolInfoDouble(sym,SYMBOL_VOLUME_MAX);
if(volume>max_volume)
return(false);
//--- получим минимальную градацию объема
double volume_step=SymbolInfoDouble(sym,SYMBOL_VOLUME_STEP);
int ratio=(int)MathRound(volume/volume_step);
if(MathAbs(ratio*volume_step-volume)>0.0000001)
return(false);
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Times()
{
MqlDateTime currTime;
TimeCurrent(currTime);
int hour0 = currTime.hour;
if(StartTime < EndTime)
if(hour0 < StartTime || hour0 >= EndTime)
return (false);
if(StartTime > EndTime)
if(hour0 >= EndTime || hour0 < StartTime)
return(false);
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseAll(string sym)
{
int total=PositionsTotal();
for(int k=total-1; k>=0; k--)
if(m_position.SelectByIndex(k))
if(m_position.Symbol()==sym)
if(m_position.Magic()==iMagicNumber)
{
// position with appropriate ORDER_MAGIC, symbol and order type
trade.PositionClose(PositionGetInteger(POSITION_TICKET), iSlippage);
Print("All Positions Closed Successfully");
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetProfit(string sym)
{
double profit = 0;
double floating = 0;
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
if(symbol == sym)
{
if(PositionGetInteger(POSITION_MAGIC) == iMagicNumber)
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY || PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
profit += PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP)+AccountInfoDouble(ACCOUNT_COMMISSION_BLOCKED);
floating = profit;
}
}//3
}//2
}//1
return (floating);
}//0
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CheckMoneyForTrade(string symb,double lots,ENUM_ORDER_TYPE type)
{
//--- Getting the opening price
MqlTick mqltick;
SymbolInfoTick(symb,mqltick);
double price=mqltick.ask;
if(type==ORDER_TYPE_SELL)
price=mqltick.bid;
//--- values of the required and free margin
double margin,free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
//--- call of the checking function
if(!OrderCalcMargin(type,symb,lots,price,margin))
{
//--- something went wrong, report and return false
return(false);
}
//--- if there are insufficient funds to perform the operation
if(margin>free_margin)
{
//--- report the error and return false
return(false);
}
//--- checking successful
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool isNewBar(string sym)
{
static datetime dtBarCurrent=WRONG_VALUE;
datetime dtBarPrevious=dtBarCurrent;
dtBarCurrent=(datetime) SeriesInfoInteger(sym,Period(),SERIES_LASTBAR_DATE);
bool NewBarFlag=(dtBarCurrent!=dtBarPrevious);
if(NewBarFlag)
return(true);
else
return (false);
return (false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool RefreshRates(string sym)
{
double ask = SymbolInfoDouble(sym,SYMBOL_ASK);
double bid = SymbolInfoDouble(sym,SYMBOL_BID);
//--- protection against the return value of "zero"
if(ask==0 || bid==0)
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double tema(string sym, ENUM_TIMEFRAMES tf, int buffer_num, int index)
{
//--- array for the indicator values
double arr[];
double indicator = 0;
datetime end = iTime(sym,tf,0);
datetime start = iTime(sym,tf,1);
ArraySetAsSeries(arr, true);
int handle = iTEMA(sym,tf,50,0,PRICE_CLOSE);
int copied = CopyBuffer(handle, buffer_num, 0, bar, arr);
if(copied>0 && index<copied)
indicator = arr[index];
return (indicator);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double atr(string sym, ENUM_TIMEFRAMES tf, int buffer_num, int index)
{
//--- array for the indicator values
double arr[];
double indicator = 0;
datetime end = iTime(sym,tf,0);
datetime start = iTime(sym,tf,1);
ArraySetAsSeries(arr, true);
int handle = iATR(sym,ATRTF,ATRPeriod);
int copied = CopyBuffer(handle, buffer_num, 0, bar, arr);
if(copied>0 && index<copied)
indicator = arr[index];
return (indicator);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string Sig(string sym, ENUM_TIMEFRAMES tf)
{
string sigs;
bool sell = false, buy = false, open = false, spikes = false;
double close[],supp[],res[],ema[],val[];
datetime end = iTime(sym,tf,0);
datetime start = iTime(sym,tf,1);
ArrayResize(val,bar);
ArrayResize(supp,bar);
ArrayResize(res,bar);
ArrayResize(close,bar);
ArrayResize(ema,bar);
ArraySetAsSeries(val,true);
ArraySetAsSeries(supp,true);
ArraySetAsSeries(res,true);
ArraySetAsSeries(close,true);
ArraySetAsSeries(ema,true);
for(int x=bar-1; x>=0; x--)
{
close[x]=iClose(sym,tf,x);
ema[x]=tema(sym,tf,0,x);
}
if(close[0] > ema[0])
sigs = "Buy";
else
if(close[0] < ema[0])
sigs = "Sell";
else
sigs = "No Signal";
return(sigs);
}
//+------------------------------------------------------------------+
void Trailing(string sym)
{
int b=0,s=0;
ulong TicketB=0,TicketS=0;
double TP = 0,SL = 0, OP = 0;
double ask = SymbolInfoDouble(sym,SYMBOL_ASK);
double bid = SymbolInfoDouble(sym,SYMBOL_BID);
double points = SymbolInfoDouble(sym,SYMBOL_POINT);
int digits =(int) SymbolInfoInteger(sym,SYMBOL_DIGITS);
int StopLevel =(int)SymbolInfoInteger(sym,SYMBOL_TRADE_STOPS_LEVEL);
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==sym)
if(m_position.Magic()==iMagicNumber)
{
if(m_position.Select(sym) && m_position.PositionType()==POSITION_TYPE_BUY)
{
b++;
TicketB=m_position.Ticket();
TP = m_position.TakeProfit();
SL = m_position.StopLoss();
OP = m_position.PriceOpen();
}
if(m_position.Select(sym) && m_position.PositionType()==POSITION_TYPE_SELL)
{
s++;
TicketS=m_position.Ticket();
TP = m_position.TakeProfit();
SL = m_position.StopLoss();
OP = m_position.PriceOpen();
}
}
//---
if(b > 0)
{
if(bid - OP > (TrailingStop+10)*points && (SL < OP || SL == 0))
{
if(!trade.PositionModify(TicketB,NormalizeDouble(OP + TrailingStop*points,digits),TP))
Print("Breakeven error #",GetLastError());
else
Print("Breakeven success");
return;
}
if(SL!=0 && SL>OP && bid - SL > (TrailingStep+5)*points)
{
if(!trade.PositionModify(TicketB,NormalizeDouble(SL + TrailingStep*points,digits),TP))
Print("Trailing error #",GetLastError());
else
Print("Trailing success");
return;
}
}
if(s > 0)
{
if(OP - ask > (TrailingStop+10)*points && (SL > OP || SL == 0))
{
if(!trade.PositionModify(TicketS,NormalizeDouble(OP - TrailingStop*points,digits),TP))
Print("Breakeven error #",GetLastError());
else
Print("Breakeven success");
return;
}
if(SL!=0 && SL<OP && SL - ask > (TrailingStep+5)*points)
{
if(!trade.PositionModify(TicketS,NormalizeDouble(SL - TrailingStep*points,digits),TP))
Print("Trailing error #",GetLastError());
else
Print("Trailing success");
return;
}
}
ResetLastError();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void AverageUpTrail(string sym, double openprice, int JumlahPoin,int Step=1)
{
int b=0,s=0;
ulong TicketB=0,TicketS=0;
double TP = 0,SL = 0, OP = 0;
double ask = SymbolInfoDouble(sym,SYMBOL_ASK);
double bid = SymbolInfoDouble(sym,SYMBOL_BID);
double points = SymbolInfoDouble(sym,SYMBOL_POINT);
int digits =(int) SymbolInfoInteger(sym,SYMBOL_DIGITS);
int stopLevel =(int)SymbolInfoInteger(sym,SYMBOL_TRADE_STOPS_LEVEL);
double TS = JumlahPoin*points;
double TST = Step*points;
if(TS < stopLevel*points)
TS = stopLevel*points;
else
TS = TS;
if(TST < stopLevel*points)
TST = stopLevel*points;
else
TST = TST;
if(TS < TST)
{
Print("Set the Trailing Stop higher than Traling Step");
return;
}
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==sym)
if(m_position.Magic()==iMagicNumber)
{
if(m_position.Select(sym) && m_position.PositionType()==POSITION_TYPE_BUY)
{
b++;
TicketB=m_position.Ticket();
TP = m_position.TakeProfit();
SL = m_position.StopLoss();
OP = m_position.PriceOpen();
}
if(m_position.Select(sym) && m_position.PositionType()==POSITION_TYPE_SELL)
{
s++;
TicketS=m_position.Ticket();
TP = m_position.TakeProfit();
SL = m_position.StopLoss();
OP = m_position.PriceOpen();
}
}
if(b > 0)
{
if((SL == 0 || SL <= openprice) && bid - openprice > TS+TST)
{
ModifyAllInstant(sym, openprice + TS);
}
else
if(SL > 0 && SL > openprice && bid - SL > TS+TST+10*points)
{
ModifyAllInstant(sym, SL + TS);
}
}
else
if(s > 0)
{
if((SL == 0 || SL >= openprice) && openprice - ask > TS+TST)
{
ModifyAllInstant(sym, openprice - TS);
}
else
if(SL > 0 && SL < openprice && SL - ask > TS+TST+10*points)
{
ModifyAllInstant(sym, SL - TS);
}
}
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int ProfitLossOrders(string sym)
{
int count = CountPositions(sym);
double profit = GetProfit(sym);
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==sym)
if(m_position.Magic()==iMagicNumber)
if(m_position.Type() == POSITION_TYPE_BUY || m_position.Type() == POSITION_TYPE_SELL)
{
if(count > 1)
{
if(profit > 0)
count++;
if(profit < 0)
count--;
}
}
return (count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountPositions(string sym)
{
int count = 0;
double profit = 0;
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==sym)
if(m_position.Magic()==iMagicNumber)
if(m_position.Type() == POSITION_TYPE_BUY || m_position.Type() == POSITION_TYPE_SELL)
{
count++;
}
return (count);
}
//+------------------------------------------------------------------+
void ModifyAllInstant(string sym, double sl)
{
//--- declare and initialize the trade request and result of trade request
MqlTradeRequest request;
MqlTradeResult result;
int total=PositionsTotal(); // number of open positions
//--- iterate over all open positions
for(int i=total-1; i>=0; i--)
{
if(PositionGetString(POSITION_SYMBOL) == sym)
{
ZeroMemory(request);
ZeroMemory(result);
//--- setting the operation parameters
request.action =TRADE_ACTION_SLTP; // type of trade operation
request.position=PositionGetTicket(i); // ticket of the position
request.deviation = iSlippage;
request.symbol = sym; // symbol
request.sl = sl; // Stop Loss of the position
request.tp = PositionGetDouble(POSITION_TP); // Take Profit of the position
//--- output information about the modification
//--- send the request
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code
//--- information about the operation
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
}
}
//+------------------------------------------------------------------+