-
Notifications
You must be signed in to change notification settings - Fork 2
/
AreaFiftyOne_Free.mq4
1686 lines (1624 loc) · 79.3 KB
/
AreaFiftyOne_Free.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
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//+------------------------------------------------------------------+
//| AreaFiftyOne.mq4 |
//| VBApps |
//| http://vbapps.co |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017 VBApps::Valeri Balachnin"
#property version "3.48"
#property description "Trades on trend change with different indicators."
#property strict
#resource "\\Indicators\\AreaFiftyOneIndicator.ex4"
#resource "\\Indicators\\AreaFiftyOne_Trend.ex4"
#define SLIPPAGE 5
#define NO_ERROR 1
#define AT_LEAST_ONE_FAILED 2
#define ORDER_TYPE_BUY OP_BUY
#define ORDER_TYPE_SELL OP_SELL
#define ORDER_TYPE_BUY_LIMIT OP_BUYLIMIT
#define ORDER_TYPE_SELL_LIMIT OP_SELLLIMIT
#define ORDER_TYPE_BUY_STOP OP_BUYSTOP
#define ORDER_TYPE_SELL_STOP OP_SELLSTOP
//--- input parameters
extern static string Trading="Base trading params";
extern double LotSize=0.01; // Lot size can be <0.10 in the free version
extern static string LotAutoSize_Comment="Available in the full version!";
extern static string LotRiskPercent_Comment="Available in the full version!";
extern static string MoneyRiskInPercent_Comment="Available in the full version!";
extern static string MaxDynamicLotSize_Comment="Available in the full version!";
extern static string MaxMoneyValueToLose_Comment="Available in the full version!";
bool LotAutoSize=false;
int LotRiskPercent=25;
int MoneyRiskInPercent=0;
double MaxDynamicLotSize=0.0;
int MaxMoneyValueToLose=0;
extern static string TrailingStep_Comment="Available in the full version!";
extern static string DistanceStep_Comment="Available in the full version!";
extern static string Positions="Handle positions params";
int TrailingStep=15;
int DistanceStep=15;
extern int TakeProfit=750;
extern int StopLoss=0;
extern static string Indicators="Choose strategies";
extern static string TrendIndicatorStrategy="-------------------";
extern bool UseTrendIndicator=false;
extern bool UseSMAOnTrendIndicator=true;
extern int UseOneOrTwoSMAOnTrendIndicator=1;
extern bool UseSMAsCrossingOnTrendIndicatorData=false;
extern static string RSIBasedStrategy="-------------------";
extern bool UseRSIBasedIndicator=false;
extern static string MACD_ADX_MA_Strategy="-------------------";
extern bool UseSimpleTrendStrategy=false;
extern static string SimpleStochasticCrossingStrategy="-------------------";
extern bool UseStochasticBasedStrategy=false;
extern static string ADX_RSI_MA_Strategy="-------------------";
extern bool Use5050Strategy=false;
extern static string StochastiCroosingRSIStrategy="-------------------";
extern bool UseStochRSICroosingStrategy=true;
bool AllowPendings=false;
extern static string TimeSettings="Trading time";
extern static string StartHour_Comment="Available in the full versioin!";
extern static string EndHour_Comment="Available in the full versioin!";
int StartHour=0;
int EndHour=23;
extern static string OnlyBuyOrSellMarket="-------------------";
extern bool OnlyBuy=true;
extern bool OnlySell=true;
extern static string UserPositions="Handle user opened positions as a EA own";
extern static string HandleUserPositions_Comment="Available in the full version!";
bool HandleUserPositions=false;
int CountCharsInCommentToEscape=0;
extern static string SignalHandling="Create signals only on new candle or on every tick";
extern bool HandleOnCandleOpenOnly=true;
//extern static string MaxOpenedPositionsOnCandle_Comment="Available in the full versioin!";
//int MaxOpenedPositionsOnCandle=2;
extern static string MaxOrdersSettings="Creates position independently of opened positions";
extern bool AddPositionsIndependently=false;
extern static string MaxConcurrentOpenedOrders_Comment="Available in the full versioin!";
int MaxConcurrentOpenedOrders=4;
extern static string UsingEAOnDifferentTimeframes="-------------------";
extern int MagicNumber=3537;
bool Debug=false;
bool DebugTrace=false;
/*licence*/
bool trial_lic=false;
datetime expiryDate=D'2017.06.17 00:00';
bool rent_lic=false;
datetime rentExpiryDate=D'2018.05.12 00:00';
int rentAccountNumber=0;
string rentCustomerName="";
/*licence_end*/
int RSI_Period=13; //8-25
int RSI_Price=0; //0-6
int Volatility_Band=34; //20-40
int RSI_Price_Line=6;
int RSI_Price_Type=MODE_SMA; //0-3
int Trade_Signal_Line=7;
int Trade_Signal_Line2=18;
int Trade_Signal_Type=MODE_SMA; //0-3
int MAFastPeriod = 7;
int MASlowPeriod = 21;
int KPeriod1=21;
int DPeriod1=7;
int Slowing1=7;
int MAMethod1=0;
int PriceField1=0;
int ma_method=MODE_SMA;
int price_field=0;
int Slippage=3,BreakEven=0;
int TicketNrPendingSell=0,TicketNrPendingSell2=0,TicketNrSell=0;
int TicketNrPendingBuy=0,TicketNrPendingBuy2=0,TicketNrBuy=0;
double LotSizeP1,LotSizeP2;
int StopLevel=0;
double StopLevelDouble=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point();
double CurrentLoss=0;
double TP=TakeProfit,SL=StopLoss;
double SLI=0,TPI=0;
string EAName="AreaFiftyOne_Free";
string IndicatorName="AreaFiftyOneIndicator";
string IndicatorName2="AreaFiftyOne_Trend";
bool WrongDirectionBuy=false,WrongDirectionSell=false;
int WrongDirectionBuyTicketNr=0,WrongDirectionSellTicketNr=0;
int TicketNrBuyWD=0,TicketNrSellWD=0;
int TicketNrBuyStoch=0,TicketNrSellStoch=0;
int handle_ind;
bool OrderDueStoch=false;
int countStochOrders=0;
int countedDecimals=2;
double CurrentTotalLotSize=0.0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
if(LotSize<MarketInfo(Symbol(),MODE_MINLOT))
{
LotSize=MarketInfo(Symbol(),MODE_MINLOT);
}
if(LotSize>=MarketInfo(Symbol(),MODE_MAXLOT))
{
LotSize=MarketInfo(Symbol(),MODE_MAXLOT);
}
//if(HandleUserPositions) bool h=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0);
double lotstep=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
countedDecimals=(int)-MathLog10(lotstep);
if(Debug)
{
Print("AccountNumber="+IntegerToString(AccountNumber()));
Print("AccountCompany="+AccountCompany());
Print("AccountName=",AccountName());
Print("AccountServer=",AccountServer());
Print("MODE_LOTSIZE=",MarketInfo(Symbol(),MODE_LOTSIZE),", Symbol=",Symbol());
Print("MODE_MINLOT=",MarketInfo(Symbol(),MODE_MINLOT),", Symbol=",Symbol());
Print("MODE_LOTSTEP=",MarketInfo(Symbol(),MODE_LOTSTEP),", Symbol=",Symbol());
Print("MODE_MAXLOT=",MarketInfo(Symbol(),MODE_MAXLOT),", Symbol=",Symbol());
Print("countedDecimals="+IntegerToString(countedDecimals));
}
if(trial_lic)
{
if(!IsTesting() && TimeCurrent()>expiryDate)
{
Alert("Expired copy. Please contact vendor.");
return(INIT_FAILED);
} else {
ObjectCreate("TrialVersion",OBJ_LABEL,0,0,0);
ObjectSetText("TrialVersion","End of a trial period: "+TimeToStr(expiryDate),11,"Calibri",clrAqua);
ObjectSet("TrialVersion",OBJPROP_CORNER,1);
ObjectSet("TrialVersion",OBJPROP_XDISTANCE,5);
ObjectSet("TrialVersion",OBJPROP_YDISTANCE,15);
}
}
if(rent_lic)
{
if(!IsTesting() && AccountName()==rentCustomerName && AccountNumber()==rentAccountNumber)
{
if(TimeCurrent()>rentExpiryDate)
{
Alert("Your license is expired. Please contact us.");
} else {
ObjectCreate("RentVersion",OBJ_LABEL,0,0,0);
ObjectSetText("RentVersion","Your version is valid till: "+TimeToStr(rentExpiryDate),11,"Calibri",clrAqua);
ObjectSet("RentVersion",OBJPROP_CORNER,1);
ObjectSet("RentVersion",OBJPROP_XDISTANCE,5);
ObjectSet("RentVersion",OBJPROP_YDISTANCE,15);
}
} else {
if(!IsTesting())
{
Alert("You can use the expert advisor only on accountNumber="+IntegerToString(rentAccountNumber)+" and accountName="+rentCustomerName);
Alert("Current accountNumber="+IntegerToString(AccountNumber())+" && accountName="+AccountName());
return(INIT_FAILED);
}
}
}
if(UseRSIBasedIndicator)
{
handle_ind=(int)iCustom(_Symbol,_Period,"::Indicators\\"+IndicatorName+".ex4",0,0);
if(handle_ind==INVALID_HANDLE)
{
Print("Expert: iCustom call: Error code=",GetLastError());
return(INIT_FAILED);
}
}
if(UseTrendIndicator)
{
handle_ind=0;
handle_ind=(int)iCustom(_Symbol,_Period,"::Indicators\\"+IndicatorName2+".ex4",0,0);
if(handle_ind==INVALID_HANDLE)
{
Print("Expert: iCustom call2: Error code=",GetLastError());
return(INIT_FAILED);
}
}
bool compareContractSizes=false;
if(CompareDoubles(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE),100000.0)) {compareContractSizes=true;}
else {compareContractSizes=false;}
StopLevel=(int)(MarketInfo(Symbol(),MODE_STOPLEVEL)*1.3);
int MarginMode=(int)MarketInfo(Symbol(),MODE_MARGINCALCMODE);
if(StopLevel>0)
{
TrailingStep=TrailingStep+StopLevel;
DistanceStep=DistanceStep+StopLevel;
if(Debug){Print("TrailingStep="+IntegerToString(TrailingStep)+";DistanceStep="+IntegerToString(DistanceStep)+";StopLevel="+IntegerToString(StopLevel));}
}
if((getContractProfitCalcMode()==1 || getContractProfitCalcMode()==2 || MarginMode==4)
&& (AllowPendings) && (compareContractSizes==false))
{AllowPendings=false;Print("Pendings are disabled due CFD or Futures.");}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(Debug)
{
if(UseStochasticBasedStrategy){Print("countStochOrders="+IntegerToString(countStochOrders));}
Print("StopLevelDouble="+DoubleToStr(StopLevelDouble));
Print("StopLevel="+IntegerToString(StopLevel));
}
ObjectsDeleteAll();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
int limit=1,err=0,BuyFlag=0,SellFlag=0;
bool BUY=false,SELL=false;
bool TradingAllowed=false;
/*if(Debug){Print("StartHour>-1="+(StartHour>-1)+"&&EndHour<24="+(EndHour<24)+"&&Hour>StartHour="+(Hour()>StartHour)
+"||Hour==StartHour="+(Hour()==StartHour)+"&&Hour<EndHour="+(Hour()<EndHour)+"||Hour==EndHour"+(Hour()==EndHour));}*/
if((StartHour>-1 && EndHour<24) && (((Hour()>StartHour) || (Hour()==StartHour)) && (Hour()<EndHour || Hour()==EndHour)))
{
TradingAllowed=true;
}
bool CheckForSignal;
if(HandleOnCandleOpenOnly && Volume[0]==1) {CheckForSignal=true;} else {CheckForSignal=false;}
if(HandleOnCandleOpenOnly==false && CurrentCandleHasNoOpenedTrades()) {CheckForSignal=true;}
//double TempTDIGreen=0,TempTDIRed=0;
if(TradingAllowed && CheckForSignal)
{
if(UseRSIBasedIndicator)
{
int i=0;
double TDIGreen=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,4,i);
double TDIGreenPrevious=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,4,i+1);
double TDIYellow=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,2,i);
double TDIYellowPrevous=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,2,i+1);
//double TDIRedPlusOne=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,5,i+1);
double TDIRed=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,5,i);
// double TDIUp=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,1,i);
// double TDIDown=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,3,i);
double TSL2=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,6,i);
double TSL2Previous=iCustom(Symbol(),0,"::Indicators\\"+IndicatorName+".ex4",RSI_Period,RSI_Price,Volatility_Band,RSI_Price_Line,RSI_Price_Type,Trade_Signal_Line,Trade_Signal_Line2,Trade_Signal_Type,6,i+1);
if((TSL2<TDIYellow) && (TDIGreen>TSL2 && (TDIGreenPrevious<TSL2Previous || TDIGreenPrevious==TSL2Previous))) {BuyFlag=true;}
if((TSL2>TDIYellow) && (TDIGreen<TSL2 && (TDIGreenPrevious>TSL2Previous || TDIGreenPrevious==TSL2Previous))) {SellFlag=true;}
if((TDIYellow<50) &&(TSL2<TDIYellow) && (TDIGreen>TDIYellow && (TDIGreenPrevious<TDIYellowPrevous || TDIGreenPrevious==TDIYellowPrevous))) {BuyFlag=true;}
if((TDIYellow>50) && (TSL2>TDIYellow) && (TDIGreen<TDIYellow && (TDIGreenPrevious>TDIYellowPrevous || TDIGreenPrevious==TDIYellowPrevous))) {SellFlag=true;}
if(SellFlag && Debug) { Print("Got sell signal from RSI-based indicator!");}
if(BuyFlag && Debug) { Print("Got buy signal from RSI-based indicator!");}
}
if(UseTrendIndicator)
{
if(true)//(Volume[0]==1)
{
double Trend=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",0,0),1);
double TrendBack=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",0,1),1);
double TrendBack2=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",0,2),1);
double MA=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",1,0),1);
double MABack=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",1,1),1);
double MABack2=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",1,2),1);
double MA_Second=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",2,0),1);
double MABack_Second=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",2,1),1);
double MABack2_Second=NormalizeDouble(iCustom(Symbol(),0,"::Indicators\\"+IndicatorName2+".ex4",2,2),1);
if(Debug)
{
Print("Trend="+DoubleToStr(Trend));
Print("TrendBack="+DoubleToStr(TrendBack));
Print("TrendBack2="+DoubleToStr(TrendBack2));
Print("MA="+DoubleToStr(MA));
Print("MABack="+DoubleToStr(MABack));
Print("MABack2="+DoubleToStr(MABack2));
Print("MA_Second="+DoubleToStr(MA_Second));
Print("MABack_Second="+DoubleToStr(MABack_Second));
Print("MABack2_Second="+DoubleToStr(MABack2_Second));
}
if(!UseSMAOnTrendIndicator)
{
if(((Trend<TrendBack || CompareDoubles(Trend,TrendBack)) && ((Trend<26) && (TrendBack>=23)) && (TrendBack2>=26)))
{
if(Debug)
{
Print("SellSignal!");
Print("Trend="+DoubleToStr(Trend));
Print("TrendBack="+DoubleToStr(TrendBack));
}
SellFlag=1;
}
if(((Trend>TrendBack || CompareDoubles(Trend,TrendBack)) && (Trend>4) && (TrendBack<=8) && (TrendBack2<=5)))
{
if(Debug)
{
Print("BuySignal!");
Print("Trend="+DoubleToStr(Trend));
Print("TrendBack="+DoubleToStr(TrendBack));
}
BuyFlag=1;
}
}
if(UseSMAOnTrendIndicator && (UseOneOrTwoSMAOnTrendIndicator==1 || UseOneOrTwoSMAOnTrendIndicator==2))
{
if(((MathRound(MA)>MathRound(Trend)) || ((MA-0.5)==Trend))
&& (((MA-Trend)>1) || ((MA-Trend)==1)) && (Trend<14.5 || Trend>16.5)
&& ((MathRound(MABack)<MathRound(TrendBack)) || (MathRound(MABack)==MathRound(TrendBack)))
&& (MathRound(MABack2)<MathRound(TrendBack2)))
{
if(Debug)
{
Print("SELL=>MA="+DoubleToStr(MathRound(MA))+">Trend="+DoubleToStr(MathRound(Trend))
+"&&MABack="+DoubleToStr(MathRound(MABack))+"<=TrendBack="+DoubleToStr(MathRound(TrendBack))
+"&&MaBack2="+DoubleToStr(MathRound(MABack2))+"<=TrendBack2="+DoubleToStr(MathRound(TrendBack2)));
}
SellFlag=1;
}
if(((MathRound(MA)<MathRound(Trend)) || ((MA+0.5)==Trend))
&& (((Trend-MA)>1) || ((Trend-MA)==1)) && (Trend<14.5 || Trend>16.5) && (Trend>4)
&& ((MathRound(MABack)>MathRound(TrendBack)) || (MathRound(MABack)==MathRound(TrendBack)))
&& (MathRound(MABack2)>MathRound(TrendBack2)))
{
if(Debug)
{
Print("BUY=>MA="+DoubleToStr(MathRound(MA))+"<Trend="+DoubleToStr(MathRound(Trend))
+"&&MABack="+DoubleToStr(MathRound(MABack))+"=>TrendBack="+DoubleToStr(MathRound(TrendBack))
+"&&MaBack2="+DoubleToStr(MathRound(MABack2))+"=>TrendBack2="+DoubleToStr(MathRound(TrendBack2)));
}
BuyFlag=1;
}
}
if(UseSMAOnTrendIndicator && (UseOneOrTwoSMAOnTrendIndicator==2 || UseOneOrTwoSMAOnTrendIndicator==3))
{
//using ma50
if(((MathRound(MA_Second)>MathRound(Trend)) || (MathRound(MA_Second-0.5)==Trend))
&& (((MA_Second-Trend)>0.5) || ((MA_Second-Trend)==1)) && (Trend<14.5 || Trend>16.5)
&& ((MathRound(MABack_Second)<MathRound(TrendBack)) || (MathRound(MABack_Second)==MathRound(TrendBack)))
&& (MathRound(MABack2_Second)<MathRound(TrendBack2)))
{
if(Debug)
{
Print("SELL=>MA_Second="+DoubleToStr(MathRound(MA_Second))+">Trend="+DoubleToStr(MathRound(Trend))
+"&&MABack_Second="+DoubleToStr(MathRound(MABack_Second))+"<=TrendBack="+DoubleToStr(MathRound(TrendBack))
+"&&MaBack2_Second="+DoubleToStr(MathRound(MABack2_Second))+"<=TrendBack2="+DoubleToStr(MathRound(TrendBack2)));
}
SellFlag=1;
}
if(((MathRound(MA_Second)<MathRound(Trend)) || (MathRound(MA_Second+0.5)==Trend))
&& (((Trend-MA_Second)>0.5) || ((Trend-MA_Second)==1)) && (Trend<14.5 || Trend>16.5)
&& ((MathRound(MABack_Second)>MathRound(TrendBack)) || (MathRound(MABack_Second)==MathRound(TrendBack)))
&& (MathRound(MABack2_Second)>MathRound(TrendBack2)))
{
if(Debug)
{
Print("BUY=>MA_Second="+DoubleToStr(MathRound(MA_Second))+"<Trend="+DoubleToStr(MathRound(Trend))
+"&&MABack_Second="+DoubleToStr(MathRound(MABack_Second))+"=>TrendBack="+DoubleToStr(MathRound(TrendBack))
+"&&MaBack2_Second="+DoubleToStr(MathRound(MABack2_Second))+"=>TrendBack2="+DoubleToStr(MathRound(TrendBack2)));
}
BuyFlag=1;
}
}
if(UseSMAOnTrendIndicator && UseSMAsCrossingOnTrendIndicatorData)
{
//using emas
if(((MathRound(MA_Second)>MathRound(MA)) || (MathRound(MA_Second-0.5)==MA))
&& (((MA_Second-MA)>0.5) || ((MA_Second-MA)==1))
&& ((MathRound(MABack_Second)<MathRound(MABack)) || (MathRound(MABack_Second)==MathRound(MABack2)))
&& (MathRound(MABack2_Second)<MathRound(MABack2)))
{
if(Debug)
{
Print("SELL=>MA_Second="+DoubleToStr(MathRound(MA_Second))+">MATrend="+DoubleToStr(MathRound(MA))
+"&&MABack_Second="+DoubleToStr(MathRound(MABack_Second))+"<=MABack="+DoubleToStr(MathRound(MABack))
+"&&MaBack2_Second="+DoubleToStr(MathRound(MABack2_Second))+"<=MABack2="+DoubleToStr(MathRound(MABack2)));
}
SellFlag=1;
}
if(((MathRound(MA_Second)<MathRound(MA)) || (MathRound(MA_Second+0.5)==MA))
&& (((MA-MA_Second)>0.5) || ((MA-MA_Second)==1))
&& ((MathRound(MABack_Second)>MathRound(MABack)) || (MathRound(MABack_Second)==MathRound(MABack)))
&& (MathRound(MABack2_Second)>MathRound(MABack2)))
{
if(Debug)
{
Print("BUY=>MA_Second="+DoubleToStr(MathRound(MA_Second))+"<MA="+DoubleToStr(MathRound(MA))
+"&&MABack_Second="+DoubleToStr(MathRound(MABack_Second))+"=>MABack="+DoubleToStr(MathRound(MABack))
+"&&MaBack2_Second="+DoubleToStr(MathRound(MABack2_Second))+"=>MABack2="+DoubleToStr(MathRound(MABack2)));
}
BuyFlag=1;
}
}
if((SellFlag || BuyFlag) && Debug) {Print("Got signal from trend-based indicator!");}
}
}
if(UseSimpleTrendStrategy)
{
if(true)//(Volume[0]==1)
{
double MAFastPrevious1,MAFastPrevious2;
double MASlowPrevious1,MASlowPrevious2;
MAFastPrevious1=iMA(NULL,0,MAFastPeriod,0,MODE_EMA,PRICE_CLOSE,1);
MAFastPrevious2=iMA(NULL,0,MAFastPeriod,0,MODE_EMA,PRICE_CLOSE,2);
MASlowPrevious1=iMA(NULL,0,MASlowPeriod,0,MODE_EMA,PRICE_CLOSE,1);
MASlowPrevious2=iMA(NULL,0,MASlowPeriod,0,MODE_EMA,PRICE_CLOSE,2);
//fast MA > slow MA.
if(MAFastPrevious1>MASlowPrevious1)
{
//BuyFlag = true;
//fast MA crosses over slow MA.
if(MAFastPrevious2<MASlowPrevious2)
{
if(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)>0 &&
iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1)>20 && iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1)<33)
{
BuyFlag=true;
}
}
}
//fast MA < slow MA.
else if(MAFastPrevious1<MASlowPrevious1)
{
//SellFlag = true;
//fast MA crosses below slow MA.
if(MAFastPrevious2>MASlowPrevious2)
{
if(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)<0 &&
iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1)>20 && iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1)<33)
{
SellFlag=true;
}
}
}
}
}
if(UseStochasticBasedStrategy)
{
for(int i=0; i<=2; i++)
{
double stochastic1now,stochastic2now,stochastic1previous,stochastic2previous,stochastic1after,stochastic2after;
stochastic1now=iStochastic(NULL,0,KPeriod1,DPeriod1,Slowing1,MAMethod1,PriceField1,0,i);
stochastic1previous=iStochastic(NULL,0,KPeriod1,DPeriod1,Slowing1,MAMethod1,PriceField1,0,i+1);
stochastic1after=iStochastic(NULL,0,KPeriod1,DPeriod1,Slowing1,MAMethod1,PriceField1,0,i-1);
stochastic2now=iStochastic(NULL,0,KPeriod1,DPeriod1,Slowing1,MAMethod1,PriceField1,1,i);
stochastic2previous=iStochastic(NULL,0,KPeriod1,DPeriod1,Slowing1,MAMethod1,PriceField1,1,i+1);
stochastic2after=iStochastic(NULL,0,KPeriod1,DPeriod1,Slowing1,MAMethod1,PriceField1,1,i-1);
if((stochastic1now>stochastic2now) && (stochastic1previous<stochastic2previous) && (stochastic1after>stochastic2after)
&& ((stochastic1now-stochastic2now)>0.5) && (stochastic1now<70.0))
{
if(NewBar())
{
BuyFlag=true;
}
}
if((stochastic1now<stochastic2now) && (stochastic1previous>stochastic2previous) && (stochastic1after<stochastic2after)
&& ((stochastic2now-stochastic1now)>0.5) && (stochastic1now>30.0))
{
if(NewBar())
{
SellFlag=true;
}
}
}
}
if(Use5050Strategy)
{
if(true)//(Volume[0]==1)
{
int i=0;
if((MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))>50) && (MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))<52)
&& ((MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i+1))==50) || (MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i+1))==49))
&& (iMA(NULL,0,34,8,MODE_SMA,PRICE_CLOSE,0)<Ask))
{
BuyFlag=true;
}
if((MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))<50) && (MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))>48)
&& ((MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i+1))==50) || (MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i+1))==49))
&& (iMA(NULL,0,34,8,MODE_SMA,PRICE_CLOSE,0)>Bid))
{
SellFlag=true;
}
}
}
if(UseStochRSICroosingStrategy)
{
if(true)//(CheckForSignal)
{
int i=0,KPeriod2=21,DPeriod2=7,Slowing2=7,MAMethod2=MODE_SMA,PriceField2=0;
double stochastic1now,stochastic1previous;
stochastic1now=iStochastic(NULL,0,KPeriod2,DPeriod2,Slowing1,MAMethod2,PriceField2,0,i);
stochastic1previous=iStochastic(NULL,0,KPeriod2,DPeriod2,Slowing1,MAMethod2,PriceField2,0,i+1);
if((MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))<MathRound(stochastic1now))
&& (MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))>MathRound(stochastic1previous)))
{
BuyFlag=true;
}
if((MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))>MathRound(stochastic1now))
&& (MathRound(iRSI(NULL,0,45,PRICE_CLOSE,i))<MathRound(stochastic1previous)))
{
SellFlag=true;
}
}
}
}
//risk management
double SymbolStep=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
int MarginMode=(int)MarketInfo(Symbol(),MODE_MARGINCALCMODE);
bool compareContractSizes=false;
if(CompareDoubles(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE),100000.0)) {compareContractSizes=true;}
else {compareContractSizes=false;}
double RemainingLotSize=0.0;
int countRemainingMaxLots=0;
bool LotSizeIsBiggerThenMaxLot=false;
double MaxLot=MarketInfo(Symbol(),MODE_MAXLOT);
if(SymbolStep>0.0)
{
MaxLot=NormalizeDouble(MaxLot-MathMod(MaxLot,SymbolStep),countedDecimals);
}
if(LotAutoSize)
{
int Faktor=100;
if(LotRiskPercent<0.1 || LotRiskPercent>1000){Comment("Invalid Risk Value.");}
else
{
if(getContractProfitCalcMode()==0 || (MarginMode==0 && compareContractSizes))
{
//Print("Fall1:"+(MarginMode==0 && compareContractSizes));
LotSize=NormalizeDouble(MathFloor((AccountFreeMargin()*AccountLeverage()*LotRiskPercent*Point*Faktor)/
(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT),countedDecimals);
//Print("LotSize="+LotSize);
LotSizeP1 = NormalizeDouble(LotSize*0.625,countedDecimals);
LotSizeP2 = NormalizeDouble(LotSize*0.5,countedDecimals);
}
else if((getContractProfitCalcMode()==1 || getContractProfitCalcMode()==2 || MarginMode==4) && (compareContractSizes==false))
{
//Print("Fall2:"+((getContractProfitCalcMode()==1 || getContractProfitCalcMode()==2 || MarginMode==4) && (compareContractSizes==false)));
if(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE)==1.0){countedDecimals=0;}
int Splitter=1000;
if(getContractProfitCalcMode()==1){Splitter=100000;}
if(MarginMode==4 && MarketInfo(Symbol(),MODE_TICKSIZE)==0.001){Splitter=1000000;}
if(Digits==3){Faktor=1;}
if(Digits==2){Faktor=10;}
LotSize=NormalizeDouble(MathFloor((AccountFreeMargin()*AccountLeverage()*LotRiskPercent*Faktor*Point)/
(Ask*MarketInfo(Symbol(),MODE_TICKSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT)/Splitter,countedDecimals);
LotSizeP1 = MathFloor(NormalizeDouble(LotSize*0.625,countedDecimals));
LotSizeP2 = MathFloor(NormalizeDouble(LotSize*0.5,countedDecimals));
//Print("LotSize2="+LotSize);
if(SymbolStep>0.0)
{
LotSize=LotSize-MathMod(LotSize,SymbolStep);
LotSizeP1=LotSizeP1-MathMod(LotSizeP1,SymbolStep);
LotSizeP2=LotSizeP2-MathMod(LotSizeP2,SymbolStep);
}
} else {
Print("Cannot calculate the right auto lot size!");
LotSize=MarketInfo(Symbol(),MODE_MINLOT);
LotSizeP1=MarketInfo(Symbol(),MODE_MINLOT);
LotSizeP2=MarketInfo(Symbol(),MODE_MINLOT);
}
}
if(MaxDynamicLotSize>0 && LotSize>MaxDynamicLotSize)
{
LotSize=MaxDynamicLotSize;
}
}
if(LotAutoSize==false){LotSize=LotSize;}
if(LotSize<MarketInfo(Symbol(),MODE_MINLOT))
{
LotSize=MarketInfo(Symbol(),MODE_MINLOT);
LotSizeP1 = NormalizeDouble(LotSize*0.625,countedDecimals);
LotSizeP2 = NormalizeDouble(LotSize*0.5,countedDecimals);
if(SymbolStep>0.0)
{
LotSize=NormalizeDouble(LotSize-MathMod(LotSize,SymbolStep),countedDecimals);
LotSizeP1=NormalizeDouble(LotSizeP1-MathMod(LotSizeP1,SymbolStep), countedDecimals);
LotSizeP2=NormalizeDouble(LotSizeP2-MathMod(LotSizeP2,SymbolStep), countedDecimals);
}
}
if(LotSize>MaxLot)
{
countRemainingMaxLots=(int)(LotSize/MaxLot);
RemainingLotSize=MathMod(LotSize,MaxLot);
LotSizeIsBiggerThenMaxLot=true;
CurrentTotalLotSize=LotSize;
LotSize=MarketInfo(Symbol(),MODE_MAXLOT);
LotSizeP1=NormalizeDouble(LotSizeP1*0.625,countedDecimals);
LotSizeP2=NormalizeDouble(LotSizeP2*0.5,countedDecimals);
if(SymbolStep>0.0)
{
LotSize=NormalizeDouble(LotSize-MathMod(LotSize,SymbolStep),countedDecimals);
LotSizeP1=NormalizeDouble(LotSizeP1-MathMod(LotSizeP1,SymbolStep), countedDecimals);
LotSizeP2=NormalizeDouble(LotSizeP2-MathMod(LotSizeP2,SymbolStep), countedDecimals);
}
}
//in the free version LotSize can be less then 0.10!
if(LotSize>0.10) {LotSize=0.10;}
if(Debug)
{
Print("LotSize="+DoubleToStr(LotSize,countedDecimals));
Print("LotSize*0,625="+DoubleToStr(LotSizeP1,countedDecimals));
Print("LotSize*0,5="+DoubleToStr(LotSizeP2,countedDecimals));
}
//Money Management
double TempLoss=0;
for(int j=0;j<OrdersTotal();j++)
{
if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber))
{
TempLoss=TempLoss+OrderProfit();
}
}
}
if(AccountBalance()>0)
{
CurrentLoss=NormalizeDouble((TempLoss/AccountBalance())*100,2);
}
if((MoneyRiskInPercent>0 && StrToInteger(DoubleToStr(MathAbs(CurrentLoss),0))>MoneyRiskInPercent)
|| (MaxMoneyValueToLose>0 && StrToInteger(DoubleToStr(MathAbs(TempLoss),0))>MaxMoneyValueToLose))
{
while(CloseAll()==AT_LEAST_ONE_FAILED)
{
Sleep(1000);
Print("Order close failed - retrying error: #"+IntegerToString(GetLastError()));
}
}
//positions initialization
int cnt=0,OP=0,OS=0,OB=0,CloseSell=0,OSC=0,OBC=0,CloseBuy=0;OP=0;
for(cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if((OrderType()==OP_SELL || OrderType()==OP_BUY) && OrderSymbol()==Symbol() && ((OrderMagicNumber()==MagicNumber)))
{
OP=OP+1;
if(OrderType()==OP_SELL)OSC=OSC+1;
if(OrderType()==OP_BUY)OBC=OBC+1;
}
}
}
if(OP>=1){OS=0;OB=0;}OB=0;OS=0;CloseBuy=0;CloseSell=0;
//entry conditions verification
if(SellFlag>0){OS=1;OB=0;}if(BuyFlag>0){OB=1;OS=0;}
if(HandleUserPositions){HandleUserPositionsFun();}
//conditions to close positions
/* if(SellFlag>0){CloseBuy=1;}
if(BuyFlag>0){CloseSell=1;}
*/
/*for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if((OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT) && OrderSymbol()==Symbol() && ((OrderMagicNumber()==MagicNumber) || MagicNumber==0))
{
if(CloseBuy==1)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Red);
for(int t=0;t<OrdersTotal();t++)
{
if(OrderType()==OP_BUYLIMIT && OrderSymbol()==Symbol() && ((OrderMagicNumber()==MagicNumber) || MagicNumber==0))
{
if(StringCompare(OrderComment(),EAName+"P1B")==0 || StringCompare(OrderComment(),EAName+"P2B")==0) OrderDelete(OrderTicket(),clrNONE);
}
}
TicketNr=0;
TicketNrPending=0;
TicketNrPending2=0;
CurrentProfit(0);
}
}
if((OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT) && OrderSymbol()==Symbol() && ((OrderMagicNumber()==MagicNumber) || MagicNumber==0))
{
if(CloseSell==1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
for(int k=0;k<OrdersTotal();k++)
{
if(OrderType()==OP_SELLLIMIT && OrderSymbol()==Symbol() && ((OrderMagicNumber()==MagicNumber) || MagicNumber==0))
{
if(StringCompare(OrderComment(),EAName+"P1S") || StringCompare(OrderComment(),EAName+"P2S")) OrderDelete(OrderTicket(),clrNONE);
}
}
TicketNr=0;
TicketNrPending=0;
TicketNrPending2=0;
CurrentProfit(0);
}
}
}*/
for(cnt=0;cnt<OrdersHistoryTotal();cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==Symbol() &&
(TicketNrPendingSell>0 || TicketNrPendingSell2>0 || TicketNrPendingBuy>0 || TicketNrPendingBuy2>0 || TicketNrSellStoch>0 || TicketNrBuyStoch>0) &&
(OrderMagicNumber()==MagicNumber) &&
(OrderTicket()==TicketNrBuy || OrderTicket()==TicketNrSell || OrderTicket()==TicketNrBuyWD || OrderTicket()==TicketNrSellWD
|| OrderTicket()==TicketNrSellStoch || OrderTicket()==TicketNrBuyStoch))
{
bool foundS1=false,foundS2=false,foundB1=false,foundB2=false,foundSWD=false,foundBWD=false,foundSST=false,foundBST=false;
for(int cnt0=0;cnt0<OrdersHistoryTotal();cnt0++)
{
if(WrongDirectionSellTicketNr>0 && WrongDirectionSellTicketNr==OrderTicket()){WrongDirectionSell=false;WrongDirectionSellTicketNr=0;}
if(WrongDirectionBuyTicketNr>0 && WrongDirectionBuyTicketNr==OrderTicket()){WrongDirectionBuy=false;WrongDirectionBuyTicketNr=0;}
if(OrderTicket()==TicketNrPendingSell) {foundS1=true;}
if(OrderTicket()==TicketNrPendingSell2) {foundS2=true;}
if(OrderTicket()==TicketNrPendingBuy) {foundB1=true;}
if(OrderTicket()==TicketNrPendingBuy2) {foundB2=true;}
if(OrderTicket()==TicketNrSellWD) {foundSWD=true;}
if(OrderTicket()==TicketNrBuyWD) {foundBWD=true;}
if(OrderTicket()==TicketNrSellStoch) {foundSST=true;}
if(OrderTicket()==TicketNrBuyStoch) {foundBST=true;}
if(foundSST){TicketNrSellStoch=0;}
if(foundBST){TicketNrBuyStoch=0;}
if(OrderTicket()==TicketNrSell)
{
if(foundS1==false && TicketNrPendingSell>0
&& getTicketCurrentType(TicketNrPendingSell)>-1 && getTicketCurrentType(TicketNrPendingSell)==3)
{
bool delS1; delS1=OrderDelete(TicketNrPendingSell);
if(delS1==false){bool delS11;delS11=OrderDelete(TicketNrPendingSell);TicketNrPendingSell=0;}else{TicketNrPendingSell=0;}
}
if(foundS2==false && TicketNrPendingSell2>0
&& getTicketCurrentType(TicketNrPendingSell2)>-1 && getTicketCurrentType(TicketNrPendingSell2)==3)
{
bool delS2; delS2=OrderDelete(TicketNrPendingSell2);
if(delS2==false){bool delS21;delS21=OrderDelete(TicketNrPendingSell2);TicketNrPendingSell2=0;}else{TicketNrPendingSell2=0;}
}
if(foundBWD==false && getTicketCurrentType(TicketNrBuyWD)>-1 && getTicketCurrentType(TicketNrBuyWD)==3)
{
bool delB; delB=OrderDelete(TicketNrPendingSell2);
if(delB==false){bool delB1;delB1=OrderDelete(TicketNrBuyWD);TicketNrBuyWD=0;}else{TicketNrBuyWD=0;}
}
}
if(OrderTicket()==TicketNrBuy)
{
if(foundB1==false && TicketNrPendingBuy>0
&& getTicketCurrentType(TicketNrPendingBuy)>-1 && getTicketCurrentType(TicketNrPendingBuy)==2)
{
bool delB1; delB1=OrderDelete(TicketNrPendingBuy);
if(delB1==false){bool delB11;delB11=OrderDelete(TicketNrPendingBuy);TicketNrPendingBuy=0;}else{TicketNrPendingBuy=0;}
}
if(foundB2==false && TicketNrPendingBuy2>0
&& getTicketCurrentType(TicketNrPendingBuy2)>-1 && getTicketCurrentType(TicketNrPendingBuy2)==2)
{
bool delB2; delB2=OrderDelete(TicketNrPendingBuy2);
if(delB2==false){bool delB21;delB21=OrderDelete(TicketNrPendingBuy2);TicketNrPendingBuy2=0;}else{TicketNrPendingBuy2=0;}
}
if(foundSWD==false && getTicketCurrentType(TicketNrSellWD)>-1 && getTicketCurrentType(TicketNrSellWD)==3)
{
bool delS; delS=OrderDelete(TicketNrPendingSell2);
if(delS==false){bool delS1;delS1=OrderDelete(TicketNrSellWD);TicketNrSellWD=0;}else{TicketNrSellWD=0;}
}
}
}
}
}
for(cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true && (OrderType()==OP_BUY || OrderType()==OP_SELL) && (OrderSymbol()==Symbol()) && (OrderMagicNumber()==MagicNumber) &&
((TicketNrPendingSell>0 || (TicketNrPendingSell>0 && TicketNrPendingSell2>0)) ||
(TicketNrPendingBuy>0 || (TicketNrPendingBuy>0 && TicketNrPendingBuy2>0))) && (TicketNrBuy>0 || TicketNrSell>0))
{
for(int c=0;c<OrdersTotal();c++)
{
if(OrderSelect(c,SELECT_BY_POS,MODE_TRADES)==true)
{
double TempTP=NormalizeDouble(OrderTakeProfit(),Digits);
if((OrderTicket()==TicketNrPendingSell && OrderType()==OP_SELL) || (OrderTicket()==TicketNrPendingSell2 && OrderType()==OP_SELL))
{
if((TicketNrSell>0) && (OrderSelect(TicketNrSell,SELECT_BY_TICKET,MODE_TRADES)==true) && TempTP!=OrderTakeProfit())
{
if(OrderModifyCheck(OrderTicket(),OrderOpenPrice(),0,TempTP))
{
bool fm;fm=OrderModify(TicketNrSell,OrderOpenPrice(),0,TempTP,0,CLR_NONE);
}
}
/*if((TicketNrPendingSell>0) && (OrderSelect(TicketNrPendingSell,SELECT_BY_TICKET,MODE_TRADES)==true) && TempTP!=OrderTakeProfit())
{bool fm1;fm1=OrderModify(TicketNrPendingSell,OrderOpenPrice(),0,TempTP,0,CLR_NONE);}
if((TicketNrPendingSell2>0) && (OrderSelect(TicketNrPendingSell2,SELECT_BY_TICKET,MODE_TRADES)==true) && TempTP!=OrderTakeProfit())
{bool fm2;fm2=OrderModify(TicketNrPendingSell2,OrderOpenPrice(),0,TempTP,0,CLR_NONE);}*/
WrongDirectionSell=true;
WrongDirectionSellTicketNr=TicketNrSell;
break;
}
}
}
for(int f=0;f<OrdersTotal();f++)
{
if(OrderSelect(f,SELECT_BY_POS,MODE_TRADES)==true)
{
double TempTP=NormalizeDouble(OrderTakeProfit(),Digits);
if((OrderTicket()==TicketNrPendingBuy && OrderType()==OP_BUY) || (OrderTicket()==TicketNrPendingBuy2 && OrderType()==OP_BUY))
{
if((TicketNrBuy>0) && (OrderSelect(TicketNrBuy,SELECT_BY_TICKET,MODE_TRADES)==true) && TempTP!=OrderTakeProfit())
{
if(OrderModifyCheck(OrderTicket(),OrderOpenPrice(),0,TempTP))
{
bool fm;fm=OrderModify(TicketNrBuy,OrderOpenPrice(),0,TempTP,0,CLR_NONE);
}
}
/* if((TicketNrPendingBuy>0) && (OrderSelect(TicketNrPendingBuy,SELECT_BY_TICKET,MODE_TRADES)==true) && TempTP!=OrderTakeProfit())
{bool fm1;fm1=OrderModify(TicketNrPendingBuy,OrderOpenPrice(),0,TempTP,0,CLR_NONE);}
if((TicketNrPendingBuy2>0) && (OrderSelect(TicketNrPendingBuy2,SELECT_BY_TICKET,MODE_TRADES)==true) && TempTP!=OrderTakeProfit())
{bool fm2;fm2=OrderModify(TicketNrPendingBuy2,OrderOpenPrice(),0,TempTP,0,CLR_NONE);}*/
WrongDirectionBuy=true;
WrongDirectionBuyTicketNr=TicketNrBuy;
break;
}
}
}
}
}
//open position
//
if((AddP() && AddPositionsIndependently && OP<=MaxConcurrentOpenedOrders) || (OP==0 && !AddPositionsIndependently))
{
// && TempTDIGreen>RSI_Top_Value && (TempTDIGreen-TempTDIRed)>=3.5
//&& MarketInfo(Symbol(),MODE_TRADEALLOWED)
if(OnlySell==true && !(AccountFreeMarginCheck(Symbol(),OP_SELL,LotSize*3)<=0 || GetLastError()==134))
{
if(OrderDueStoch && UseStochasticBasedStrategy && TicketNrSellStoch==0)
{
if(OrderDueStoch){Print("Sell due Stoch!");countStochOrders=countStochOrders+1;}
if(TP==0)TPI=0;else TPI=Bid-TP*Point;if(SL==0)SLI=Bid+10000*Point;else SLI=Bid+SL*Point;
if(CheckMoneyForTrade(Symbol(),LotSize,OP_SELL))
{
if(CheckStopLoss_Takeprofit(ORDER_TYPE_SELL,SLI,TPI) && CheckVolumeValue(LotSize))
{
TicketNrSellStoch=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,SLI,TPI,EAName,MagicNumber,0,Red);OS=0;
if(TicketNrSellStoch<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
//else{Print("Order Sent Successfully, Ticket # is: "+string(TicketNrSell));}
if(LotSizeIsBiggerThenMaxLot)
{
for(int c=0;c<countRemainingMaxLots-1;c++)
{
if(OrderSend(Symbol(),OP_SELL,MaxLot,Bid,Slippage,SLI,TPI,EAName,MagicNumber,0,Red)<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
}
if(OrderSend(Symbol(),OP_SELL,RemainingLotSize,Bid,Slippage,SLI,TPI,EAName,MagicNumber,0,Red)<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
}
}
}
}
if(OS==1 /*&& OSC==0*/ && !OrderDueStoch)
{
if(TP==0)TPI=0;else TPI=Bid-TP*Point;if(SL==0)SLI=Bid+10000*Point;else SLI=Bid+SL*Point;
if(CheckMoneyForTrade(Symbol(),LotSize,OP_SELL))
{
if(CheckStopLoss_Takeprofit(ORDER_TYPE_SELL,SLI,TPI) && CheckVolumeValue(LotSize))
{
TicketNrSell=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,SLI,TPI,EAName,MagicNumber,0,Red);OS=0;
if(TicketNrSell<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
//else{Print("Order Sent Successfully, Ticket # is: "+string(TicketNrSell));}
if(LotSizeIsBiggerThenMaxLot)
{
for(int c=0;c<countRemainingMaxLots-1;c++)
{
if(OrderSend(Symbol(),OP_SELL,MaxLot,Bid,Slippage,SLI,TPI,EAName,MagicNumber,0,Red)<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
}
if(OrderSend(Symbol(),OP_SELL,RemainingLotSize,Bid,Slippage,SLI,TPI,EAName,MagicNumber,0,Red)<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
}
}
}
if(AllowPendings && !OrderDueStoch)
{
double TempPendingLotSize=LotSizeP1;
if(TempPendingLotSize<MarketInfo(Symbol(),MODE_MINLOT))TempPendingLotSize=MarketInfo(Symbol(),MODE_MINLOT);
if(TicketNrPendingSell>0 && OrderSelect(TicketNrPendingSell,SELECT_BY_POS))
{if(OrderType()==3){bool delS=OrderDelete(TicketNrPendingSell);}TicketNrPendingSell=0;}
else if(!OrderSelect(TicketNrPendingSell,SELECT_BY_POS) && TicketNrPendingSell>0)
{TicketNrPendingSell=0;}
if(TicketNrPendingSell==0 && IsNewOrderAllowed() && (CheckMoneyForTrade(Symbol(),TempPendingLotSize,OP_SELL)))
{
if(CheckStopLoss_Takeprofit(ORDER_TYPE_SELL,SLI,TPI) && CheckVolumeValue(TempPendingLotSize))
{
TicketNrPendingSell=OrderSend(Symbol(),OP_SELLLIMIT,TempPendingLotSize,Bid+TP/2*Point,Slippage,0,Bid,EAName+"P1S",MagicNumber,0,Red);
if(TicketNrPendingSell<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
// else{Print("Order Sent Successfully, Ticket # is: "+strin#g(TicketNrPendingSell));}
}
}
double TempPendingLotSize2=LotSizeP1;
if(TempPendingLotSize2<MarketInfo(Symbol(),MODE_MINLOT))TempPendingLotSize2=MarketInfo(Symbol(),MODE_MINLOT);
if(TicketNrPendingSell2>0 && OrderSelect(TicketNrPendingSell2,SELECT_BY_POS))
{if(OrderType()==3){bool delS2=OrderDelete(TicketNrPendingSell2);}TicketNrPendingSell2=0;}
else if(!OrderSelect(TicketNrPendingSell2,SELECT_BY_POS) && TicketNrPendingSell2>0)
{TicketNrPendingSell2=0;}
if(TicketNrPendingSell2==0 && IsNewOrderAllowed() && (CheckMoneyForTrade(Symbol(),TempPendingLotSize2,OP_SELL)))
{
if(CheckStopLoss_Takeprofit(ORDER_TYPE_SELL,SLI,TPI) && CheckVolumeValue(TempPendingLotSize2))
{
TicketNrPendingSell2=OrderSend(Symbol(),OP_SELLLIMIT,TempPendingLotSize2,Bid+TP/1*Point,Slippage,0,Bid,EAName+"P2S",MagicNumber,0,Red);
if(TicketNrPendingSell2<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
else{Print("Order Sent Successfully, Ticket # is: "+string(TicketNrPendingSell2));}
}
}
if(TP==0)TPI=0;else TPI=Ask+(TP*2)*Point;if(SL==0)SLI=10000*Point;else SLI=Ask-(SL*2)*Point;
if(CheckMoneyForTrade(Symbol(),LotSize,OP_BUY) && IsNewOrderAllowed())
{
if(CheckStopLoss_Takeprofit(ORDER_TYPE_BUY,SLI,TPI) && CheckVolumeValue(LotSize))
{
int expiryTime=(int)TimeCurrent()+(1209600);
TicketNrBuyWD=OrderSend(Symbol(),OP_BUYSTOP,LotSize,Ask+TP*Point,Slippage,SLI,TPI,EAName+"WD_BUY",MagicNumber,expiryTime,Lime);
if(TicketNrBuyWD<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
//else{Print("Order Sent Successfully, Ticket # is: "+string(TicketNrBuy));}
}
}
}
OrderDueStoch=false;
}
}
// && TempTDIGreen<RSI_Down_Value && (TempTDIGreen-TempTDIRed)>=3.5
// && MarketInfo(Symbol(),MODE_TRADEALLOWED)
if(OnlyBuy==true && !(AccountFreeMarginCheck(Symbol(),OP_BUY,LotSize*3)<=0 || GetLastError()==134))
{
if(OrderDueStoch && UseStochasticBasedStrategy && TicketNrBuyStoch==0)
{
if(OrderDueStoch){Print("Buy due Stoch!");countStochOrders=countStochOrders+1;}
if(TP==0)TPI=0;else TPI=Ask+TP*Point;if(SL==0)SLI=Ask-10000*Point;else SLI=Ask-SL*Point;
if(CheckMoneyForTrade(Symbol(),LotSize,OP_BUY))
{
if(CheckStopLoss_Takeprofit(ORDER_TYPE_BUY,SLI,TPI) && CheckVolumeValue(LotSize))
{
TicketNrBuyStoch=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,SLI,TPI,EAName,MagicNumber,0,Lime);OB=0;
if(TicketNrBuyStoch<0)
{Print(EAName+" => OrderSend Error: "+IntegerToString(GetLastError()));}
//else{Print("Order Sent Successfully, Ticket # is: "+string(TicketNrBuy));}
if(LotSizeIsBiggerThenMaxLot)
{