-
Notifications
You must be signed in to change notification settings - Fork 1
/
NinjaLoveFishEA.mq4
1352 lines (1196 loc) · 89.2 KB
/
NinjaLoveFishEA.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
//+-----------------------------+
//| NinjaLoveFishEA.mq4 |
//| Copyright @2018, renzhe.org |
//+-----------------------------+
/*
2019年7月9日09:41:35统计
EURUSD
TakeProfitPoint=1700 StepATRMultiplier=4.5 MiniGridDistance=600
EURJPY
TakeProfitPoint=950 StepATRMultiplier=4.5 MiniGridDistance=400
GBPUSD
TakeProfitPoint=1700 StepATRMultiplier=6.5 MiniGridDistance=550
2019年8月7日
放弃的直接入场的BB和SS按钮,因为在本月初,由于这个按钮没有按照规则入场,导致了严重的被套,货币兑包括:GBPJPY和GBPUSD。
降低风险,保证单独在场的货币兑只有一个存在,防止由于同一个货币兑判断失误造成的严重失误。
2019年8月8日
入场的RSI降低风险,如果是货币兑含GBP\JPY的话,那么RSI的值为12和14.
2019年8月9日
LOT降低风险,如果货币兑包含GBP,和JPY的话,那么perlot的值分别为2.0和3.0
可以设置双MT4,分别对应短线和中线单,中线单注意库存费的正负。长线单可以是手工单。
如果在一个货币兑中触发了10%的止损,那么不要切换到其他相关货币兑上进行操作,必须仍然操作该货币兑。
2019年8月10日
修改了止损20%
增加了RSI的周期参数,默认为8,如果遇到超级行情,可以增加这个数值,对于GBP=8+4和JPY=8+6。
优化了PerLot 在init中加载。
增加SLPrice参数,用于订单的设置止损,主要用于布局中长线的反转单。
2019年8月11日
增加了TF的止盈参数
增加了SIGNAL的2个参数,切换和执行挂单的周期。
2019年8月12日
修复了TF的挂单瞬间关闭的BUG。去掉了实盘入场RSI的限制。
2019年8月13日
SIGNAL的指标,增加了持仓货币兑,调试优化了很多细节。
EA调整为高风险,高收益模式,即1000对应0.01的模式。记住不做GBP和JPY这两个货币兑。
2019年8月14日
仓位:GBP是1.5倍,JPY是2倍。
RSI:GBP是+2,JPY是+4。
2019年8月15日
增加了SIGNAL的买卖持仓过夜费的显示。
2019年8月16日
去掉了SIGNAL中的MagicSell和MagicBuy的识别代码。进行全仓位识别。
2019年8月25日
修改了仓位下单手数的逻辑,去掉了按5000=0.01的比例下单。
2019年8月31日
重新计算预期盈利。(暂时未执行)填充为Null
2019年9月1日
修改signal的rsi为60-40
修改初始化下单手数为:re=10/earn-0.004;
2019年9月9日
启动了测试过滤时间的功能。
2019年9月14日
进行测试修复AUDUSD和EURGBP
2019年10月2日
修复相同货币出现的时候,另一个货币兑虚拟挂单线,不取消的问题。(尝试解决)
2019年10月7日
上架了demo的信号
2019年10月11日
修复了最大手数限制的问题。
2019年10月12日
测试和真实运作使用了同样的手数限制。新增了GBPCAD和GBPAUD货币兑,但要铭记历史,谨防乱跳。
2019年10月24日
重大的更新,不需要固定的TP和网格间距,采用ATR来衡量货币的波动率。
2019年10月30日
去掉挂单时间的限制,因为这个判断一直无法使用。
2019年12月7日
添加了comm中的空格。
RSI:GBP是+8,JPY是+8+8。
*/
//发布前,要修改3个地方,1个是版本号,一个是DEBUG模式关闭.
#define Version "2.81"
#define EAName "NinjaLoveFishEA"
#property strict
#property version Version
#property copyright "Copyright @2018, Qin Zhao"
#property link "https://www.mql5.com/en/users/zq535228"
#property icon "3232.ico"
#property description "This EA contains two modes, automatic and manual mode,welcome to download.\nIt's recommended to test the EA in the Strategy Tester before using the live account."
#include <stderror.mqh>
#include <stdlib.mqh>
#include "comm.mqh"
#include "Shields.mqh"
#include "comm_signals.mqh"
extern static string EA=EAName+" v"+Version;
extern string s2 = "--------General Setting -------------";
extern int MagicNumberBuy = 123;
extern int MagicNumberSell = 321;
input double StopLossPercent = 15;
input double SLPrice = 0;
extern string s4 = "--------First Order AutoMode Enable -------------";
extern bool AutoMode = false;
extern string s10 = "--------First Order Indicators Setting -------------";
extern double BB_Width = 3000;
extern int RSIperiod = 8;
extern double RSIbuy = 20;
extern double RSIsell = 80;
extern string s12 = "--------Basic Orders Settings -------------";
extern int BaseGridNum = 2;
extern double StepATRMultiplier = 3.5;
extern int MaxTrades = 10;
extern int MiniGridDistance = 250;
string s6 = "--------First Order Filters Setting -------------";
bool ShowInfo = true;
bool ShowLine = true;
double VirtualOrderATRMultiplier = 5;
int PendingHours = 120;
int MaxSymbolInPosition = 1;
double MaxSpreadPoint = 40;
bool TimeFilter = true;
string TimeStart = "02:00";
string TimeEnd = "22:00";
bool NewsFilter = true;
int NewsOffset = 2;
bool NewsVhigh = true;
bool NewsVmedium = true;
bool NewsVlow = false;
int BeforeNewsStopMin = 7;
int AfterNewsStopMin = 7;
extern string s8 = "--------Pips and Grids Settings -------------";
extern double LotMutiple = 1.8;
extern double MaxLot_001 = 100;
extern int SameLotsOrder = 6;
extern double PipStepExponent = 1.3;
extern double TPDecreasePercent = 20;
extern bool IsOverlapping = false;
extern int OverlappingNum = 5;
double GridSplitHour = 0.5;
double ProfitPersent = 30;
double SecondProfitPersent = 50;
int StepATRPeriod=14;
int vDigits;
int OrderSended=0;
int TotalBuyOrders=0,TotalSellOrders=0;
int Lpos,Lpos1,Cpos;
double StepPoint,GridSplitSec,MaxLot;
double vPoint;
double PriceTarget,AveragePrice,LastBuyPrice,LastSellPrice;
double BuySummLot,SellSummLot,TotalProfitBuy,TotalProfitSell;
double BLot,SLot;
double Cprofit,Lprofit,Lprofit1,PrcCL,Lprice,Cprice;
double FirstOrderOpenPrice,LastOrderOpenPrice;
string LastOrderComment="";
string BComment,SComment;
datetime LastOrderOpenTime,FirstOrderOpenTime;
//+-----------------------------+
//| |
//+-----------------------------+
int init()
{
if(!IsTradeAllowed())
Alert("Trade Not Allowed!");
EA=EAName+" v"+Version;
vPoint=Point;
vDigits=Digits;
GridSplitSec=GridSplitHour*3600;
MaxLot = AccountBalance()/MaxLot_001;
if(IsTest())
{
AutoMode=true;
NewsFilter=false;
//MaxTrades=20;//测试的时候,最大订单数量为20.运行模式默认为10
ShowLine=false;
}
if(ShowInfo)
DrawInfo();
long x_distance;
long y_distance;
//--- set window size
if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))
{
Print("Failed to get the chart width! Error code = ",GetLastError());
}
if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))
{
Print("Failed to get the chart height! Error code = ",GetLastError());
}
ObjectCreate(0,"BUY",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"BUY",OBJPROP_XDISTANCE,x_distance/3);
ObjectSetInteger(0,"BUY",OBJPROP_YDISTANCE,20);
ObjectSetString(0,"BUY",OBJPROP_TEXT,"BUY");
ObjectSetInteger(0,"BUY",OBJPROP_COLOR,clrBlack);
ObjectSetInteger(0,"BUY",OBJPROP_FONTSIZE,8);
ObjectSetInteger(0,"BUY",OBJPROP_XSIZE,70);
//ObjectSetString(0,"BUY",OBJPROP_FONT,"Calibri");
if(MarketInfo(Symbol(),MODE_SWAPLONG)>0)
{
ObjectSetInteger(0,"BUY",OBJPROP_BGCOLOR,clrChartreuse);
ObjectSetString(0,"BUY",OBJPROP_TEXT,"BUY +"+DoubleToStr(MarketInfo(Symbol(),MODE_SWAPLONG),1));
}
else
{
ObjectSetInteger(0,"BUY",OBJPROP_BGCOLOR,clrLightSalmon);
ObjectSetString(0,"BUY",OBJPROP_TEXT,"BUY "+DoubleToStr(MarketInfo(Symbol(),MODE_SWAPLONG),1));
}
ObjectCreate(0,"SELL",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"SELL",OBJPROP_XDISTANCE,x_distance/3+90);
ObjectSetInteger(0,"SELL",OBJPROP_YDISTANCE,20);
ObjectSetString(0,"SELL",OBJPROP_TEXT,"SELL");
ObjectSetInteger(0,"SELL",OBJPROP_COLOR,clrBlack);
ObjectSetInteger(0,"SELL",OBJPROP_FONTSIZE,8);
ObjectSetInteger(0,"SELL",OBJPROP_XSIZE,70);
if(MarketInfo(Symbol(),MODE_SWAPSHORT)>0)
{
ObjectSetInteger(0,"SELL",OBJPROP_BGCOLOR,clrChartreuse);
ObjectSetString(0,"SELL",OBJPROP_TEXT,"SELL +"+DoubleToStr(MarketInfo(Symbol(),MODE_SWAPSHORT),1));
}
else
{
ObjectSetInteger(0,"SELL",OBJPROP_BGCOLOR,clrLightSalmon);
ObjectSetString(0,"SELL",OBJPROP_TEXT,"SELL "+DoubleToStr(MarketInfo(Symbol(),MODE_SWAPSHORT),1));
}
ObjectCreate(0,"CLOSE",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"CLOSE",OBJPROP_XDISTANCE,x_distance/3+250);
ObjectSetInteger(0,"CLOSE",OBJPROP_YDISTANCE,20);
ObjectSetString(0,"CLOSE",OBJPROP_TEXT,"CLOSE");
ObjectSetInteger(0,"CLOSE",OBJPROP_BGCOLOR,clrTomato);
ObjectSetInteger(0,"CLOSE",OBJPROP_COLOR,clrBlack);
ObjectSetInteger(0,"CLOSE",OBJPROP_FONTSIZE,8);
ObjectCreate(0,"SHOOT",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"SHOOT",OBJPROP_XDISTANCE,x_distance/3+320);
ObjectSetInteger(0,"SHOOT",OBJPROP_YDISTANCE,20);
ObjectSetString(0,"SHOOT",OBJPROP_TEXT,"SHOOT");
ObjectSetInteger(0,"SHOOT",OBJPROP_BGCOLOR,clrLightBlue);
ObjectSetInteger(0,"SHOOT",OBJPROP_COLOR,clrBlack);
ObjectSetInteger(0,"SHOOT",OBJPROP_FONTSIZE,8);
return(INIT_SUCCEEDED);
}
//
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
double h=0;
//--- If you click on the object with the name buttonID
if(id==CHARTEVENT_OBJECT_CLICK && sparam=="BUY" && ObjectGetInteger(0,"BUY",OBJPROP_STATE))
{
//--- State of the button - pressed or not
bool selected1=ObjectGetInteger(0,"BUY",OBJPROP_STATE);
//--- log a debug message
Print("BUY Button pressed = ",selected1);
double np=Bid-GetPointForFirstManualOrder();
setLine("BuyLine",np,clrChartreuse,1,true,STYLE_SOLID);
dump("I will open the buy order grids at the price of "+DoubleToStr(np,5));
ObjectSetInteger(0,"BUY",OBJPROP_STATE,0);
}
//
if(id==CHARTEVENT_OBJECT_CLICK && sparam=="SELL" && ObjectGetInteger(0,"SELL",OBJPROP_STATE))
{
//--- State of the button - pressed or not
bool selected2=ObjectGetInteger(0,"SELL",OBJPROP_STATE);
//--- log a debug message
Print("SELL Button pressed = ",selected2);
double np=Ask+GetPointForFirstManualOrder();
setLine("SellLine",np,clrChartreuse,1,true,STYLE_SOLID);
dump("I will open the sell order grids at the price of "+DoubleToStr(np,5));
ObjectSetInteger(0,"SELL",OBJPROP_STATE,0);
}
//
if(id==CHARTEVENT_OBJECT_CLICK && sparam=="CLOSE" && ObjectGetInteger(0,"CLOSE",OBJPROP_STATE))
{
//--- State of the button - pressed or not
bool selected3=ObjectGetInteger(0,"CLOSE",OBJPROP_STATE);
//--- log a debug message
Print("CLOSE Button pressed = ",selected3);
if(MessageBox("Are you sure to close this pair's all orders?","Please Confirm!",1)==1)
{
while(OrdersTotal()!=0)
{
closeAll(MagicNumberBuy);
closeAll(MagicNumberSell);
}
}
ObjectSetInteger(0,"CLOSE",OBJPROP_STATE,0);
}
//
if(id==CHARTEVENT_OBJECT_CLICK && sparam=="SHOOT" && ObjectGetInteger(0,"SHOOT",OBJPROP_STATE))
{
//--- State of the button - pressed or not
bool selected4=ObjectGetInteger(0,"SHOOT",OBJPROP_STATE);
//--- log a debug message
Print("SHOOT Button pressed = ",selected4);
screenshot();
ObjectSetInteger(0,"SHOOT",OBJPROP_STATE,0);
}
}
//
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
if(!IsNewBar())
{
return 0;//如果上是新bar,那么直接返回.等待新bar
}
if(ShowInfo)
DrawInfo();
TotalBuyOrders = CountOfOrders(MagicNumberBuy);
TotalSellOrders= CountOfOrders(MagicNumberSell);
CheckSL();
//非测试环境下,才进行止损,挂单等判断。
if(!IsTest())
{
//清理现有仓位的挂单。rsip
if(TotalSellOrders>0)
deleteObjects("SellLine");
if(TotalBuyOrders>0)
deleteObjects("BuyLine");
}
CheckTakeProfit();
if(IsOverlapping)
CheckOverlapping();
// Next buy orders
if(TotalBuyOrders>0 && TotalBuyOrders<MaxTrades && CheckTimeForTrade(TimeStart,TimeEnd) && TimeFilterForTrade(GridSplitSec,MagicNumberBuy))
{
LastBuyPrice = FindLastOrderParameter(MagicNumberBuy, "price");
double nextb = LastBuyPrice - GetPointForStep(TotalBuyOrders + 1);
if(ShowLine)
setLine("NextLine",nextb,clrCornflowerBlue);
bool dnlevel=Bid<=nextb;
if(dnlevel)
{
BLot=getLots(BaseGridNum,LotMutiple,MagicNumberBuy);
if(BLot>MaxLot)
BLot=MaxLot;
BComment=StringSubstr(LastOrderComment,0,StringFind(LastOrderComment,"|",0))+"|"+IntegerToString(TotalBuyOrders);
OrderSended=openBuy(BLot,MagicNumberBuy,BComment);
deleteObjects("NextLine");
}
}
// Next sell orders
if(TotalSellOrders>0 && TotalSellOrders<MaxTrades && CheckTimeForTrade(TimeStart,TimeEnd) && TimeFilterForTrade(GridSplitSec,MagicNumberSell))
{
LastSellPrice= FindLastOrderParameter(MagicNumberSell,"price");
double nexts = LastSellPrice+GetPointForStep(TotalSellOrders+1);
if(ShowLine)
setLine("NextLine",nexts,clrCornflowerBlue);
bool uplevel=Ask>=nexts;
if(uplevel)
{
SLot=getLots(BaseGridNum,LotMutiple,MagicNumberSell);
if(SLot>MaxLot)
SLot=MaxLot;
SComment=StringSubstr(LastOrderComment,0,StringFind(LastOrderComment,"|",0))+"|"+IntegerToString(TotalSellOrders);
OrderSended=openSell(SLot,MagicNumberSell,SComment);
deleteObjects("NextLine");
}
}
//时间判断
if(!IsTest() && TimeFilter && !CheckTimeForTrade(TimeStart,TimeEnd))
{
return 0;
}
//新闻判断
if(!IsTest() && NewsFilter && CheckNews()>0)
{
return 0;
}
//滑点过滤器
if(!IsTest() && !CheckSpread(MaxSpreadPoint))
{
return 0;
}
//最大相同单过滤器
if(GetPositionExistNum(Symbol(),MagicNumberBuy)+GetPositionExistNum(Symbol(),MagicNumberSell)>=MaxSymbolInPosition)
{
//dump("MaxSymbolInPosition over the MaxNumber!");
deletePending();
return 0;
}
//手工单判断(Buy)
if(TotalBuyOrders==0 && getLineValue("BuyLine")>0 && Bid<getLineValue("BuyLine") && GetSignal_RSI(PERIOD_M5,RSIperiod)!=0 && GetSignal_RSI(PERIOD_M5,RSIperiod)<RSIbuy)
{
openBuy(GetStartLot(),MagicNumberBuy,"Ninja/"+Symbol()+"/"+DoubleToStr(MagicNumberBuy,0)+"|M");
deletePending();
}
//手工单判断(Sell)
if(TotalSellOrders==0 && getLineValue("SellLine")>0 && Ask>getLineValue("SellLine") && GetSignal_RSI(PERIOD_M5,RSIperiod)!=0 && GetSignal_RSI(PERIOD_M5,RSIperiod)>RSIsell)
{
openSell(GetStartLot(),MagicNumberSell,"Ninja/"+Symbol()+"/"+DoubleToStr(MagicNumberBuy,0)+"|M");
deletePending();
}
//如果自动交易关闭,或者已经超过最大单量了,那么到此结束.
if(AutoMode==false || CountOfOrders()>MaxTrades)
{
return(0);
}
//进入自动化交易模式,买单的判断。
if(TotalBuyOrders==0 && GetSignal_RSI(PERIOD_M5,RSIperiod)!=0 && GetSignal_RSI(PERIOD_M5,RSIperiod)<RSIbuy)
{
openBuy(GetStartLot(),MagicNumberBuy,"Ninja/"+Symbol()+"/"+DoubleToStr(MagicNumberBuy,0)+"|A");
dump("========================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>openBuy:"+datetime());
dump("========================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>rsi:"+GetSignal_RSI(PERIOD_M5,RSIperiod));
}
//进入自动化交易模式,卖单的判断。
if(TotalSellOrders==0 && GetSignal_RSI(PERIOD_M5,RSIperiod)!=0 && GetSignal_RSI(PERIOD_M5,RSIperiod)<RSIbuy)
{
openSell(GetStartLot(),MagicNumberSell,"Ninja/"+Symbol()+"/"+DoubleToStr(MagicNumberSell,0)+"|A");
dump("========================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>openSell:"+datetime());
dump("========================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>rsi:"+GetSignal_RSI(PERIOD_M5,RSIperiod));
}
return(0);
}
//===================================================================================================================================================
//===================================================================================================================================================
void CheckTakeProfit()
{
double tp = GetTakeProfitPoint();
//BUY
PriceTarget=0;
AveragePrice=0;
if(TotalBuyOrders>0)
{
AveragePrice= CalculateAveragePrice(MagicNumberBuy);
PriceTarget = AveragePrice+tp;
if(TotalBuyOrders>1)
{
PriceTarget=AveragePrice+tp*MathPow(1-TPDecreasePercent*0.01,TotalBuyOrders-1);
}
modifyTP(PriceTarget,MagicNumberBuy);
if(ShowInfo)
{
setLine("AvgLine",AveragePrice,clrYellow);
}
}
if(Ask>PriceTarget && !hasTPValue(MagicNumberBuy))
{
//如果已经获利,并且没有设置TP值,那么自动进入关闭。
while(CountOfOrders(MagicNumberBuy)!=0)
{
closeAll(MagicNumberBuy);
}
}
//SELL
PriceTarget=0;
AveragePrice=0;
if(TotalSellOrders>0)
{
AveragePrice=CalculateAveragePrice(MagicNumberSell);
PriceTarget=AveragePrice-tp;
if(TotalSellOrders>1)
{
PriceTarget=AveragePrice-tp*MathPow(1-TPDecreasePercent*0.01,TotalSellOrders-1);
}
modifyTP(PriceTarget,MagicNumberSell);
if(ShowInfo)
setLine("AvgLine",AveragePrice,clrYellow);
}
if(Bid<PriceTarget && !hasTPValue(MagicNumberSell))
{
//如果已经获利,并且没有设置TP值,那么自动进入关闭。
while(CountOfOrders(MagicNumberSell)!=0)
{
closeAll(MagicNumberSell);
}
}
if(ShowLine==true && (TotalBuyOrders+TotalSellOrders)==0)
{
clearLines();
}
}
bool IsCloseAllBuy=false;
bool IsCloseAllSell=false;
//+-----------------------------+
//|进行止损判断 |
//+-----------------------------+
void CheckSL()
{
//如果已存最大允许货币兑持仓数量,那么自动删除目前的挂单
if(TotalBuyOrders+TotalSellOrders>=MaxTrades && !IsTest())
{
dump("Over the Max Orders. "+IntegerToString(TotalBuyOrders+TotalSellOrders)+">="+IntegerToString(MaxTrades));
deletePending();
}
//buy_SL
if(TotalBuyOrders>0 && SLPrice!=0)
{
modifySL(SLPrice,MagicNumberBuy);
}
if(StopLossPercent!=0 && getProfit(MagicNumberBuy)/AccountBalance()<(-1)*StopLossPercent*0.01)
{
IsCloseAllBuy=true;
}
//判断是否关闭买单,如果是,进入买单循环关闭
while(IsCloseAllBuy)
{
closeAll(MagicNumberBuy);
if(CountOfOrders(MagicNumberBuy)==0)
{
IsCloseAllBuy=false;
}
}
//sell_SL
if(TotalSellOrders>0 && SLPrice!=0)
{
modifySL(SLPrice,MagicNumberSell);
}
if(StopLossPercent!=0 && getProfit(MagicNumberSell)/AccountBalance()<(-1)*StopLossPercent*0.01)
{
IsCloseAllSell=true;
}
//判断是否关闭卖单,如果是,进入卖单循环关闭
while(IsCloseAllSell)
{
closeAll(MagicNumberSell);
if(CountOfOrders(MagicNumberSell)==0)
{
IsCloseAllSell=false;
}
}
}
//计算订单平均价格.
double CalculateAveragePrice(int mNumber)
{
double Count=0;
for(int i=0; i<OrdersTotal(); i++)
//+-----------------------------+
//| |
//+-----------------------------+
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==mNumber)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
AveragePrice+=OrderOpenPrice()*OrderLots();
Count+=OrderLots();
}
if(AveragePrice>0 && Count>0)
return(NormalizeDouble(AveragePrice / Count, vDigits));
else
return(0);
}
//计算下一个订单的距离Point
double GetPointForStep(int CurrStep)
{
double re= 0;
StepPoint=GetSignal_ATR(StepATRPeriod)*StepATRMultiplier;
double CurrPipstep=NormalizeDouble(StepPoint*MathPow(PipStepExponent,CurrStep),Digits);
double mini=MiniGridDistance *vPoint;
re=IIFd(CurrPipstep<mini,mini,CurrPipstep);//如果间距不足,那么取最小间距。
return(re);
}
//获取手动单的距离Point
double GetPointForFirstManualOrder()
{
double re=GetSignal_ATR(StepATRPeriod)*VirtualOrderATRMultiplier;
return(re);
}
//
double GetClosedProfit(int mNumber)
{
double ClosedProfit=0;
for(int i=OrdersHistoryTotal(); i>0; i--)
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==mNumber)
if(StringSubstr(LastOrderComment,0,StringFind(LastOrderComment,"|",0))==StringSubstr(OrderComment(),0,StringFind(OrderComment(),"|",0)))
ClosedProfit=ClosedProfit+OrderProfit();
return(ClosedProfit);
}
//+-----------------------------+
//| OverLapping |
//+-----------------------------+
void CheckOverlapping()
{
//BUY--->
TotalBuyOrders=CountOfOrders(MagicNumberBuy);
//
if(TotalBuyOrders>=OverlappingNum)
{
Lpos=0;
Cpos=0;
Lprofit=0;
Cprofit=0;
Lpos=LidingProfitOrder(MagicNumberBuy);//获取最赚钱的单
Cpos=LeastProfitOrder(MagicNumberBuy);//获取最赔钱的单
if(Lprofit>0 && Lprofit1<=0)
{
if(Lprofit+Cprofit>0 && (Lprofit+Cprofit)*100/Lprofit>ProfitPersent) //1和N,
{
Lpos1=0;
CloseSelectOrder(MagicNumberBuy);
}
}
else
if(Lprofit>0 && Lprofit1>0)
{
if(Lprofit+Lprofit1+Cprofit>0 && (Lprofit+Lprofit1+Cprofit)*100/(Lprofit+Lprofit1)>SecondProfitPersent)
CloseSelectOrder(MagicNumberBuy);
}
}
//<---BUY
//SELL--->
TotalSellOrders=CountOfOrders(MagicNumberSell);
//
if(TotalSellOrders>=OverlappingNum)
{
Lpos = 0;
Cpos = 0;
Lprofit = 0;
Cprofit = 0;
Lpos = LidingProfitOrder(MagicNumberSell);
Cpos = LeastProfitOrder(MagicNumberSell);
if(Lprofit>0 && Lprofit1<=0)
{
if(Lprofit+Cprofit>0 && (Lprofit+Cprofit)*100/Lprofit>ProfitPersent)
{
Lpos1=0;
CloseSelectOrder(MagicNumberSell);
}
}
if(Lprofit>0 && Lprofit1>0)
{
if(Lprofit+Lprofit1+Cprofit>0 && (Lprofit+Lprofit1+Cprofit)*100/(Lprofit+Lprofit1)>SecondProfitPersent)
CloseSelectOrder(MagicNumberSell);
}
}
//<---SELL
}
//======================================== Most profitable order =======================================
int LidingProfitOrder(int mNumber)
{
Lprofit1=0;
Lpos1=0;
int TotalOrders= CountOfOrders(mNumber);
double profit = 0;
int Pos = 0;
//
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==mNumber))
{
if(OrderType()==OP_SELL || OrderType()==OP_BUY)
{
profit = OrderProfit();
Pos = OrderTicket();
if(profit>0 && profit>Lprofit)
{
// Previous value
Lprofit1 = Lprofit;
Lpos1 = Lpos;
Lprice=OrderOpenPrice();
// Maximum value
Lprofit = profit;
Lpos = Pos;
}
}
}
}
}
return (Lpos);
}
//======================================== Least Profitable Order =======================================
int LeastProfitOrder(int mNumber)
{
double profit = 0;
int Pos = 0;
//
for(int trade=OrdersTotal()-1; trade>=0; trade--)
{
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
{
if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==mNumber))
{
if(OrderType()==OP_SELL || OrderType()==OP_BUY)
{
profit = OrderProfit();
Pos = OrderTicket();
if(profit<0 && profit<Cprofit)
{
Cprofit = profit;
Cpos = Pos;
Cprice=OrderOpenPrice();
}
}
}
}
}
return (Cpos);
}
//========================================== Closing Orders ===============================================
int CloseSelectOrder(int mNumber)
{
int error=0;
int error1 = 0;
int error2 = 0;
int Result = 0;
//
while(error1==0)
{
RefreshRates();
int i=OrderSelect(Lpos,SELECT_BY_TICKET,MODE_TRADES);
if(i!=1)
{
Print("Error! Not possible to select most profitable order . Operation cancelled.");
return (0);
}
if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==mNumber))
{
if(OrderType()==OP_BUY)
{
error1=(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,clrNONE));
if(error1==1)
{
Print("Leading Order closed successfully");
Sleep(500);
}
else
{
Print("Error closing leading order, Repeat Operation. ");
}
}
//----------------
if(OrderType()==OP_SELL)
{
error1=(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,clrNONE));
if(error1==1)
{
Print("Leading Order closed successfully");
Sleep(500);
}
else
{
Print("Error closing leading order, Repeat Operation. ");
}
}
}
}
//---------------------- Previous Last -----------------------
if(Lpos1!=0)
{
while(error2==0)
{
RefreshRates();
int i=OrderSelect(Lpos1,SELECT_BY_TICKET,MODE_TRADES);
if(i!=1)
{
Print("Error! Not possible to select previous most profitable order . Operation cancelled.");
return (0);
}
if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==mNumber))
{
if(OrderType()==OP_BUY)
{
error2=(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,clrNONE));
if(error2==1)
{
Print("Previous leading order closed successfully");
Sleep(500);
}
else
{
Print("Error closing previous leading order, Repeat Operation. ");
}
}
//----------------
if(OrderType()==OP_SELL)
{
error2=(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,clrNONE));
if(error2==1)
{
Print("Previous leading order closed successfully");
Sleep(500);
}
else
{
Print("Error closing previous leading order, Repeat Operation. ");
}
}
}
}
}
//----------- Selected (Least profitable order ) -----------
while(error==0)
{
RefreshRates();
int i=OrderSelect(Cpos,SELECT_BY_TICKET,MODE_TRADES);
if(i!=1)
{
Print("Error! Not possible to select least profitable order. Operation cancelled");
return (0);
}
if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==mNumber))
{
if(OrderType()==OP_BUY)
{
error=(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,clrNONE));
if(error==1)
{
Print("Order closed successfully.");
Sleep(500);
}
else
{
Print("Error during Order Close. Repeat operation.");
}
}
//-------------
if(OrderType()==OP_SELL)
{
error=(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,clrNONE));
if(error==1)
{
Print("Order closed successfully.");
Sleep(500);
}
else
{
Print("Error during Order Close. Repeat operation.");
}
}
}
}
Result=1;
return (Result);
}
//+-----------------------------+
//| |
//+-----------------------------+
double FindLastOrderParameter(int mNumber,string ParamName)
{
int mOrderTicket=0;
double mOrderPrice=0;
double mOrderLot=0;
double mOrderProfit=0;
int PrevTicket = 0;
int CurrTicket = 0;
for(int k=OrdersTotal()-1; k>=0; k--)
if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==mNumber)
{
CurrTicket=OrderTicket();
if(CurrTicket>PrevTicket)
{
PrevTicket=CurrTicket;
mOrderPrice=OrderOpenPrice();
mOrderTicket=OrderTicket();
mOrderLot=OrderLots();
mOrderProfit=OrderProfit()+OrderSwap()+OrderCommission();
LastOrderComment=OrderComment();
LastOrderOpenTime=OrderOpenTime();
LastOrderOpenPrice=OrderOpenPrice();
}
}
if(ParamName == "price")
return(mOrderPrice);
else
if(ParamName == "ticket")
return(mOrderTicket);
else
if(ParamName == "lot")
return(mOrderLot);
else
if(ParamName == "profit")
return(mOrderProfit);
return (0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetTakeProfitPoint()
{
double re= 0;
StepPoint=GetSignal_ATR(999)*StepATRMultiplier;
double mini=MiniGridDistance *vPoint;
re=IIFd(StepPoint<mini,mini,StepPoint);//如果间距不足,那么取最小间距。
return re;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetStartLot()
{
double earn=(GetTakeProfitPoint()/MarketInfo(Symbol(),MODE_TICKSIZE))*MarketInfo(Symbol(),MODE_TICKVALUE);
double re=AccountBalance()/500/earn-0.002;
re=AccountBalance()/500/earn-0.002;
return(re);
}
//+-----------------------------+
//|获取下一个订单的下单量
//+-----------------------------+
double getLots(double baseGridNum,double lotMutiple,int magicmunber)
{
double re = 0;
double Lot = GetStartLot();