-
Notifications
You must be signed in to change notification settings - Fork 0
/
20220915(黄金)_1025.mq4
1848 lines (1529 loc) · 135 KB
/
20220915(黄金)_1025.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
//+------------------------------------------------------------------+
//| 20220915(黄金).mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright (C) 2015-2022, Huatao"
#property link "[email protected]"
#property strict
#include <Mql/Lang/ExpertAdvisor.mqh>
#include <Mql/History/TimeSeriesData.mqh>
#include <Mql/History/PriceBreak.mqh>
#include <Mql/Trade/FxSymbol.mqh>
#include <Mql/Trade/Order.mqh>
#include <Mql/Trade/OrderGroup.mqh>
#include <Mql/Trade/OrderManager.mqh>
#include <Mql/Trade/OrderTracker.mqh>
#include <Mql/Charts/LabeledLine.mqh>
#include <Mql/UI/FreeFormElement.mqh>
#include <Mql/UI/ReadOnlyLabel.mqh>
#include <Mql/Utils/ParseUtils.mqh>
#include <Mql/Utils/File.mqh>
//注释下一行, 显示完整参数
#define USE_SHORT_PARAMETERS 1
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
class GuoJiaEAParam: public AppParam
{
public:
ObjectAttr(int,LicenseCode, LicenseCode); // 授权码
ObjectAttr(bool,EnableListKLine,EnableListKLine); // 启动连续K线
ObjectAttr(int,ListKLineValue, ListKLineValue); // 连续K线根数连阳连阴
ObjectAttr(int,ListKLineRange, ListKLineRange); // 连续K线波幅(下限)
ObjectAttr(int,MinAlgorithmValue,MinAlgorithmValue); // 最小K线交易值(下限)
ObjectAttr(int,MaxAlgorithmValue,MaxAlgorithmValue); // 最大K线交易值(上限)
ObjectAttr(int, SignalSuperTime, SignalSuperTime); // 开仓信号后,开单持续时间
ObjectAttr(int, SpreadLimitNum , SpreadLimitNum); // 开仓点差限制值
ObjectAttr(bool, EnableOpenTimes, EnableOpenTimes); // 启动开仓时间控制
ObjectAttr(bool, IsServerTime, IsServerTime); // false=本地时间 true=服务器时间
ObjectAttr(string, OpenTimesRanges, OpenTimesRanges); // 允许开仓时间段
ObjectAttr(bool, EnableIndicatorFilter1, EnableIndicatorFilter1); // 启动指标过滤算法控制
ObjectAttr(int, IndicatorFilter1TimeFrame, IndicatorFilter1TimeFrame); // 指标算法时间窗口周期
ObjectAttr(int, IndicatorFilter1Period, IndicatorFilter1Period); // 指标算法时间跨度周期
ObjectAttr(int, IndicatorFilter1Value1, IndicatorFilter1Value1); // 指标算法限制值1
ObjectAttr(int, IndicatorFilter1Value2, IndicatorFilter1Value2); // 指标算法限制值2
ObjectAttr(bool, EnableMultipleTicket, EnableMultipleTicket); // 开启多单模式
ObjectAttr(int, MultipleOrderGroupNum, MultipleOrderGroupNum); // 允许并发订单组最大上限
ObjectAttr(double,BaseLotValue, BaseLotValue); // 初始下单手数
ObjectAttr(bool, EnableBaseMinKLineNumSinceClosed, EnableBaseMinKLineNumSinceClosed); // 开启初始下单最小K线数
ObjectAttr(int, BaseMinKLineNumSinceClosed, BaseMinKLineNumSinceClosed); // 初始下单最小K线数(以最后平仓时间)
ObjectAttr(string, BackLotValue, BackLotValue); // 逆势下单手数
ObjectAttr(string, ForwardAddBuyRange, ForwardAddBuyRange); // 顺势加仓间距
ObjectAttr(string, BackAddBuyRange, BackAddBuyRange); // 逆势加仓间距
ObjectAttr(string, BackAddBuyRevertRange, BackAddBuyRevertRange); // 逆势加仓回调点数
ObjectAttr(string, ForwardCloseOrderRevertRange, ForwardCloseOrderRevertRange);// 顺势平仓回调点数
ObjectAttr(string, BackStopWinRange, BackStopWinRange); // 逆势止赢点数
ObjectAttr(bool, EnableBackAddLotsLimitEx, EnableBackAddLotsLimitEx); // 允许逆势额外加仓限制
ObjectAttr(int, BackAddLotsLimitExNum, BackAddLotsLimitExNum); // K_15线波动范围
ObjectAttr(double, MoneyStopLostValue, MoneyStopLostValue); // 金额止损
ObjectAttr(double, MoneyProtectProfitValue, MoneyProtectProfitValue); // 最大盈利保护
ObjectAttr(double, FirstOrderMoneyProtectProfitValue, FirstOrderMoneyProtectProfitValue); // 首单盈利保护
ObjectAttr(bool, AddBaseOrderWhenMoneyProtect, AddBaseOrderWhenMoneyProtect); // 盈利保护后补首单
ObjectAttr(int, MagicNumValue, MagicNumValue); // 魔法值
ObjectAttr(bool, ShowOrdersAvgLine, ShowOrdersAvgLine); // 显示订单组平均线
ObjectAttr(bool, ShowListKLine, ShowListKLine); // 显示连续K线指标线
ObjectAttr(int, TargetPeriod, TargetPeriod); // 周期
public:
long m_licensefile_period;
public:
bool check()
{
if(checkBaseParameter() == false)
{
return false;
}
if(m_EnableOpenTimes)
{
if(checkOpenTimesParameter() == false)
{
return false;
}
}
if (Mql::isTesting() == false && Mql::isVisual() == false) {
if (checkLicenseInfo() == false )
{
return false;
}
}
else {
m_licensefile_period = 0;
}
return true;
}
bool checkBaseParameter()
{
int param_size = 0;
double BackLotValue[];
if(ParseDoubles(m_BackLotValue,BackLotValue,',') == false)
{
MessageBox("逆势下单手数配置错误.");
return false;
}
param_size = ArraySize(BackLotValue);
int ForwardAddBuyRange[];
if(ParseIntegers(m_ForwardAddBuyRange,ForwardAddBuyRange,',') == false)
{
MessageBox("顺势加仓间距配置错误.");
return false;
}
if(param_size != ArraySize(ForwardAddBuyRange))
{
MessageBox("顺势加仓间距配置参数个数错误.");
return false;
}
int BackAddBuyRange[];
if(ParseIntegers(m_BackAddBuyRange,BackAddBuyRange,',') == false)
{
MessageBox("逆势加仓间距配置错误.");
return false;
}
if(param_size != ArraySize(BackAddBuyRange))
{
MessageBox("逆势加仓间距配置参数个数错误.");
return false;
}
int BackAddBuyRevertRange[];
if(ParseIntegers(m_BackAddBuyRevertRange,BackAddBuyRevertRange,',') == false)
{
MessageBox("逆势加仓回调点数配置错误.");
return false;
}
if(param_size != ArraySize(BackAddBuyRevertRange))
{
MessageBox("逆势加仓间距配置参数个数错误.");
return false;
}
int ForwardCloseOrderRevertRange[];
if(ParseIntegers(m_ForwardCloseOrderRevertRange,ForwardCloseOrderRevertRange,',') == false)
{
MessageBox("顺势平仓回调点数配置错误.");
return false;
}
if(param_size != ArraySize(ForwardCloseOrderRevertRange))
{
MessageBox("顺势平仓回调点数配置参数个数错误.");
return false;
}
int BackStopWinRange[];
if(ParseIntegers(m_BackStopWinRange,BackStopWinRange,',') == false)
{
MessageBox("逆势止赢点数配置错误.");
return false;
}
if(param_size != ArraySize(BackStopWinRange))
{
MessageBox("逆势止赢点数配置参数个数错误.");
return false;
}
return true;
}
bool checkOpenTimesParameter()
{
bool res = true;
string ranges[];
StringSplit(m_OpenTimesRanges,',',ranges);
int size=ArraySize(ranges);
if(size<=0)
{
res = false;
}
for(int i=0; i<size; i++)
{
string t[];
StringSplit(ranges[i],'-',t);
if(ArraySize(t) != 2)
{
res = false;
break;
}
int starttime[];
int endtime[];
if(ParseIntegers(t[0],starttime,':') == false || ArraySize(starttime) != 2
|| ParseIntegers(t[1],endtime,':') == false || ArraySize(endtime) != 2)
{
res = false;
break;
}
if(starttime[0] >= 0 && starttime[0] < 24 && starttime[1] >= 0 && starttime[1] <= 59
&& endtime[0] >= 0 && endtime[0] < 24 && endtime[1] >= 0 && endtime[1] <= 59
&& (starttime[0] < endtime[0] || (starttime[0] == endtime[0] && starttime[1] < endtime[1])))
{
}
else
{
res = false;
break;
}
}
if(res == false)
MessageBox("允许开仓时间配置错误.");
return res;
}
bool checkLicenseInfo() {
bool res = true;
string file_name = IntegerToString(getLicenseCode()) + ".license";
if (File::exist(file_name, false) == false) {
MessageBox("授权码 licensecode, 授权文件不存在");
return false;
}
BinaryFile license_file(file_name, FILE_READ);
//string license_start = "LICENSESTART";
//license_file.readString(StringBufferLen(license_start));
int file_licensecode = license_file.readInteger(4);
int file_licensemagic = license_file.readInteger(4);
int file_licenseaccount = license_file.readInteger(4);
long file_licenseperiod = license_file.readLong();
int file_authserver_len = license_file.readInteger(4);
string file_authserver;
if (file_authserver_len > 0)
{
file_authserver = license_file.readString(file_authserver_len);
}
int file_currencypairs_str_len = license_file.readInteger(4);
string file_currencypairs;
if (file_currencypairs_str_len > 0)
{
file_currencypairs = license_file.readString(file_currencypairs_str_len);
}
//string license_end = "LICENSEEND";
//license_file.readString(StringBufferLen(license_end));
if (getLicenseCode() != file_licensecode) {
PrintFormat("check licensecode %d , now ( %d)", getLicenseCode(), file_licensecode );
MessageBox("授权码 licensecode, 授权文件格式错误, 错误码 401");
return false;
}
if (getMagicNumValue() != file_licensemagic) {
PrintFormat("check licensemagic %d , now ( %d)", getMagicNumValue(), file_licensemagic );
MessageBox("授权码 licensecode, 授权文件格式错误, 错误码 402");
return false;
}
if (file_licenseperiod < TimeCurrent() ) {
MessageBox("授权码 licensecode, 授权文件格式错误, 错误码 403");
return false;
}
if (file_currencypairs_str_len == 0 ) {
MessageBox("授权码 licensecode, 授权文件格式错误, 错误码 404");
return false;
}
if (file_licenseaccount > 0 ) {
if (AccountInfoInteger(ACCOUNT_LOGIN) != file_licenseaccount) {
MessageBox("授权码 licensecode, 授权文件格式错误, 错误码 405");
return false;
}
}
if (file_currencypairs_str_len > 0) {
string currencypairs[];
StringSplit(file_currencypairs,',',currencypairs);
int size=ArraySize(currencypairs);
if(size<=0)
{
MessageBox("授权码 licensecode, 授权文件格式错误, 错误码 406");
return false;
}
bool check_res = false;
string now_symbol = Symbol();
for(int i=0; i<size; i++) {
string check_symbol = currencypairs[i];
//PrintFormat("check currencypairs %s", check_symbol);
if (StringCompare(now_symbol, check_symbol, true) == 0) {
check_res = true;
break;
}
}
if (check_res == false) {
MessageBox("授权码 licensecode, 授权文件格式错误, 错误码 407");
//MessageBox(StringFormat("当前货币对 %s 授权文件中未授权", now_symbol));
return false;
}
}
m_licensefile_period = file_licenseperiod;
return true;
}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class GuoJiaEAOrderMatcher: public OrderMatcher
{
private:
string m_symbol;
int m_magicnum;
public:
GuoJiaEAOrderMatcher(string symbol, int magicnum)
:m_symbol(symbol),m_magicnum(magicnum)
{
}
bool matches() const
{
if(OrderMagicNumber() == m_magicnum && OrderSymbol() == m_symbol)
{
return true;
}
else
{
//Order oo;
//PrintFormat("check order %s %d , now (%s %d)", m_symbol, m_magicnum, OrderSymbol(),OrderMagicNumber());
}
return false;
}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class OrderGroupEx
{
public:
OrderGroup m_orders;
int m_order_type;
datetime m_orders_basetime;
bool m_signal;
datetime m_signalbartime;
public:
OrderGroupEx(FxSymbol *symbol)
: m_orders(symbol)
{
m_signal = false;
m_order_type = -1;
}
OrderGroupEx::OrderGroupEx(const OrderGroupEx &that)
{
m_order_type = that.m_order_type;
m_orders = that.m_orders;
m_signal = that.m_signal;
m_orders_basetime = that.m_orders_basetime;
m_signalbartime = that.m_signalbartime;
}
~OrderGroupEx()
{
}
};
//+------------------------------------------------------------------+
//| Main EA |
//+------------------------------------------------------------------+
class GuoJiaEA: public ExpertAdvisor
{
private:
GuoJiaEAParam *m_param;
FxSymbol m_fxsymbol;
TimeSeriesData m_data;
TimeSeriesData m_data_15minute;
MqlRates m_updateRates[];
//MqlRates m_lastestRates[];
OrderManager m_ordermanager;
GuoJiaEAOrderMatcher m_ordermatcher;
TradingPool m_orderpool;
OrderTracker m_ordertracker;
Vector<OrderGroupEx *> m_buyOrders_vector;
Vector<OrderGroupEx *> m_sellOrders_vector;
UIRoot m_root;
LabeledLine *m_LineHigh;
LabeledLine *m_LineLow;
LabeledLine *m_LineByeOrdersAvgPrice;
LabeledLine *m_LineSellOrdersAvgPrice;
ReadOnlyLabel *m_timeLable;
ReadOnlyLabel *m_totalLable;
ReadOnlyLabel *m_byeLable;
ReadOnlyLabel *m_sellLable;
int m_marange[];
protected:
public:
GuoJiaEA(GuoJiaEAParam *param);
~GuoJiaEA()
{
SafeDelete(m_timeLable);
SafeDelete(m_LineHigh);
SafeDelete(m_LineLow);
SafeDelete(m_LineByeOrdersAvgPrice);
SafeDelete(m_LineSellOrdersAvgPrice);
SafeDelete(m_totalLable);
SafeDelete(m_byeLable);
SafeDelete(m_sellLable);
}
void LoadLastGrouporders();
void update(bool init);
void main();
void onTimer();
private:
bool InOpenTimesRange();
OrderGroupEx * GetUnused(Vector<OrderGroupEx *> *orders_vector)
{
OrderGroupEx *tmp = NULL;
for(ConstVectorIterator<OrderGroupEx *> it(orders_vector); !it.end(); it.next())
{
OrderGroupEx *o = it.current();
if(o.m_orders.size() == 0)
return o;
}
return NULL;
}
OrderGroupEx * GetOrderGroupByBaseTicket(Vector<OrderGroupEx *> *orders_vector, int ticket)
{
OrderGroupEx *tmp = NULL;
for(ConstVectorIterator<OrderGroupEx *> it(orders_vector); !it.end(); it.next())
{
OrderGroupEx *o = it.current();
if(o.m_orders.size() == 0)
continue;
if (o.m_orders.get(0) == ticket) {
return o;
}
}
return NULL;
}
void InitOrdersSlots(Vector<OrderGroupEx *> *orders_vector,int try_ordertype)
{
if(orders_vector.size() == 0)
{
int i = 0;
for(i = 0 ; i < m_param.getMultipleOrderGroupNum(); i++)
{
OrderGroupEx * try_one = new OrderGroupEx(&m_fxsymbol);
try_one.m_order_type = try_ordertype;
orders_vector.add(try_one);
}
}
}
int FilterByIndicator(int try_ordertype) {
if (m_param.getEnableIndicatorFilter1() == false)
return 1;
if (try_ordertype != OP_SELL && try_ordertype != OP_BUY)
{
return 0;
}
double val=iForce(NULL,m_param.getIndicatorFilter1TimeFrame(),m_param.getIndicatorFilter1Period(),MODE_SMA,PRICE_CLOSE,1);
if (Math::sign(val) == 1 && Math::abs(val) > m_param.getIndicatorFilter1Value1() && try_ordertype == OP_SELL) {
return 0;
}
else if (Math::sign(val) == -1 && Math::abs(val) > m_param.getIndicatorFilter1Value1() && try_ordertype == OP_BUY) {
return 0;
}
else {
return 1;
}
return 0;
}
int CheckForOpen(Vector<OrderGroupEx *> *orders_vector,int try_ordertype)
{
if(orders_vector.size() == 0)
{
int i = 0;
for(i = 0 ; i < m_param.getMultipleOrderGroupNum(); i++)
{
OrderGroupEx * try_one = new OrderGroupEx(&m_fxsymbol);
orders_vector.add(try_one);
}
}
OrderGroupEx * try_one = GetUnused(orders_vector);
if(try_one == NULL)
return 0;
int ticket = CheckForOpen(try_one, try_ordertype);
if(ticket > 0)
{
return 1;
}
else
{
}
return 0;
}
int CheckForClose(Vector<OrderGroupEx *> *orders_vector, bool check_profit, bool check_close)
{
for(ConstVectorIterator<OrderGroupEx *> it(orders_vector); !it.end(); it.next())
{
OrderGroupEx *o = it.current();
if(o != NULL)
CheckForClose(o, check_profit, check_close);
}
return 1;
}
int CheckForAddLot(Vector<OrderGroupEx *> *orders_vector)
{
for(ConstVectorIterator<OrderGroupEx *> it(orders_vector); !it.end(); it.next())
{
OrderGroupEx *o = it.current();
if(o != NULL)
CheckForAddLot(o);
}
return 0;
}
int CheckForOpen(OrderGroupEx *orders_ex,int try_ordertype);
int CheckForClose(OrderGroupEx *orders_ex, bool check_profit, bool check_close);
int CheckForAddLot(OrderGroupEx *orders_ex);
double getHigh()
{
double KBarCloseHigh = m_data.getClose(1);
for(int i = 1; i <= m_param.getListKLineValue(); i++)
{
if(m_data.getOpen(i) > KBarCloseHigh)
KBarCloseHigh = m_data.getOpen(i);
if(m_data.getClose(i) > KBarCloseHigh)
KBarCloseHigh = m_data.getClose(i);
}
return KBarCloseHigh;
}
double getLow()
{
double KBarCloseLow = m_data.getClose(1);
for(int i = 0; i < m_param.getListKLineValue(); i++)
{
if(m_data.getOpen(i) < KBarCloseLow)
KBarCloseLow = m_data.getOpen(i);
if(m_data.getClose(i) < KBarCloseLow)
KBarCloseLow = m_data.getClose(i);
}
return KBarCloseLow;
}
int GetOrderGroupType(OrderGroup *orders)
{
if(orders.size() > 0)
{
Order::Select(orders.get(0));
return Order::Type();
}
return -1;
}
Order* GetFirstOrder(OrderGroup *orders)
{
Order *order = NULL;
if(orders.size() > 0)
{
Order::Select(orders.get(0));
order = new Order();
return order;
}
return order;
}
Order* GetLastOrder(OrderGroup *orders)
{
Order *order = NULL;
if(orders.size() > 0)
{
Order::Select(orders.get(orders.size() - 1));
order = new Order();
return order;
}
return order;
}
datetime getNearestBarDate(datetime time) const {int ps=PeriodSeconds(m_data.getPeriod()); return time/ps*ps;}
double GetBackLotValue(int index);
int GetForwardAddBuyRange(int index);
int GetBackAddBuyRange(int index);
int GetBackAddBuyRevertRange(int index);
int GetForwardCloseOrderRevertRange(int index);
int GetBackStopWinRange(int index);
};
//+------------------------------------------------------------------+
//| Run the main method once to force update on initialization |
//+------------------------------------------------------------------+
GuoJiaEA::GuoJiaEA(GuoJiaEAParam *param)
:
m_param(param),
m_fxsymbol(),
m_data(m_fxsymbol.getName(), m_param.getTargetPeriod()),
m_data_15minute(m_fxsymbol.getName(), 15),
m_ordermanager(m_fxsymbol.getName()),
m_ordermatcher(m_fxsymbol.getName(), param.getMagicNumValue()),
m_orderpool(&m_ordermatcher),
m_ordertracker(&m_orderpool),
m_root()
{
m_ordermanager.setMagic(param.getMagicNumValue());
m_ordermanager.setSlippage(5);
m_ordermanager.setRetry(1);
m_LineHigh = new LabeledLine("PriceHighLine","PriceHighLabel","",STYLE_DASHDOTDOT,clrLightPink,0);
m_LineLow = new LabeledLine("PriceLowLine","PriceLowLabel","",STYLE_DASHDOTDOT,clrLightPink,0);
m_LineByeOrdersAvgPrice = new LabeledLine("ByeOrdersAvgPrice","ByeOrdersAvgPriceLabel","",STYLE_DASHDOTDOT,clrBlue,0);
m_LineSellOrdersAvgPrice = new LabeledLine("SellOrdersAvgPrice","SellOrdersAvgPriceLabel","",STYLE_DASHDOTDOT,clrDeepPink,0);
m_timeLable = new ReadOnlyLabel("timelable", 5, 20, clrHotPink);
m_totalLable = new ReadOnlyLabel("orderstotallable", 5, 40, clrHotPink);
m_byeLable = new ReadOnlyLabel("ordersbyelable", 5, 60, clrHotPink);
m_sellLable = new ReadOnlyLabel("orderselllable", 5, 80, clrHotPink);
ArrayResize(m_updateRates,param.getListKLineValue());
setupTimer(1);
LoadLastGrouporders();
update(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GuoJiaEA::GetBackLotValue(int index)
{
int i = index;
double BackLotValue[];
ParseDoubles(m_param.getBackLotValue(),BackLotValue,',');
if(i >= ArraySize(BackLotValue))
i = ArraySize(BackLotValue) - 1;
return BackLotValue[i];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GuoJiaEA::GetForwardAddBuyRange(int index)
{
int i = index;
int ForwardAddBuyRange[];
ParseIntegers(m_param.getForwardAddBuyRange(),ForwardAddBuyRange,',');
if(i >= ArraySize(ForwardAddBuyRange))
i = ArraySize(ForwardAddBuyRange) - 1;
return ForwardAddBuyRange[i];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GuoJiaEA::GetBackAddBuyRange(int index)
{
int i = index;
int BackAddBuyRange[];
ParseIntegers(m_param.getBackAddBuyRange(),BackAddBuyRange,',');
if(i >= ArraySize(BackAddBuyRange))
i = ArraySize(BackAddBuyRange) - 1;
return BackAddBuyRange[i];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GuoJiaEA::GetBackAddBuyRevertRange(int index)
{
int i = index;
int BackAddBuyRevertRange[];
ParseIntegers(m_param.getBackAddBuyRevertRange(),BackAddBuyRevertRange,',');
if(i >= ArraySize(BackAddBuyRevertRange))
i = ArraySize(BackAddBuyRevertRange) - 1;
return BackAddBuyRevertRange[i];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GuoJiaEA::GetForwardCloseOrderRevertRange(int index)
{
int i = index;
int ForwardCloseOrderRevertRange[];
ParseIntegers(m_param.getForwardCloseOrderRevertRange(),ForwardCloseOrderRevertRange,',');
if(i >= ArraySize(ForwardCloseOrderRevertRange))
i = ArraySize(ForwardCloseOrderRevertRange) - 1;
return ForwardCloseOrderRevertRange[i];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GuoJiaEA::GetBackStopWinRange(int index)
{
int i = index;
int BackStopWinRange[];
ParseIntegers(m_param.getBackStopWinRange(),BackStopWinRange,',');
if(i >= ArraySize(BackStopWinRange))
i = ArraySize(BackStopWinRange) - 1;
return BackStopWinRange[i];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool GuoJiaEA::InOpenTimesRange()
{
if(m_param.getEnableOpenTimes())
{
datetime nowtime = m_param.getIsServerTime() ? TimeCurrent() : TimeLocal();
MqlDateTime time_st;
TimeToStruct(nowtime, time_st);
bool res = false;
string ranges[];
StringSplit(m_param.getOpenTimesRanges(),',',ranges);
int size=ArraySize(ranges);
for(int i=0; !res && i<size; i++)
{
string t[];
StringSplit(ranges[i],'-',t);
int starttime[];
ParseIntegers(t[0],starttime,':');
time_st.hour = starttime[0];
time_st.min = starttime[1];
time_st.sec = 0;
datetime range_starttime = StructToTime(time_st);
int endtime[];
ParseIntegers(t[1],endtime,':');
time_st.hour = endtime[0];
time_st.min = endtime[1];
time_st.sec = 0;
datetime range_endtime = StructToTime(time_st);
if(nowtime >= range_starttime && nowtime <= range_endtime)
{
res = true;
}
}
return res;
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void GuoJiaEA::LoadLastGrouporders()
{
InitOrdersSlots(&m_buyOrders_vector, OP_BUY);
InitOrdersSlots(&m_sellOrders_vector, OP_SELL);
foreachorder(m_orderpool) {
Order o;
string comment = o.getComment();
if (StringBufferLen(comment) == 0) {
continue;
}
int ticket = (int)StringToInteger(comment);
if (o.getType() == OP_BUY ) {
OrderGroupEx * tmp;
if (ticket == 0) {
tmp = GetUnused(&m_buyOrders_vector);
if (tmp == NULL) {
continue;
}
tmp.m_orders.add(o.getTicket());
tmp.m_orders_basetime = o.getOpenTime();
tmp.m_signal = true;
tmp.m_signalbartime = getNearestBarDate(o.getOpenTime());
}
else {
tmp = GetOrderGroupByBaseTicket(&m_buyOrders_vector, ticket);
if (tmp == NULL) {
continue;
}
tmp.m_orders.add(o.getTicket());
}
}
else if (o.getType() == OP_SELL) {
OrderGroupEx * tmp;
if (ticket == 0) {
tmp = GetUnused(&m_sellOrders_vector);
if (tmp == NULL) {
continue;
}
tmp.m_orders.add(o.getTicket());
tmp.m_orders_basetime = o.getOpenTime();
tmp.m_signal = true;
tmp.m_signalbartime = getNearestBarDate(o.getOpenTime());
}
else {
tmp = GetOrderGroupByBaseTicket(&m_sellOrders_vector, ticket);
if (tmp == NULL) {
continue;
}
tmp.m_orders.add(o.getTicket());
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void GuoJiaEA::update(bool init)
{
m_data.updateCurrent();
if(m_data.isNewBar())
{
int bars=(int)m_data.getNewBars();
//PrintFormat("init update bar %d", bars);
ArrayResize(m_updateRates,bars,5);
m_data.copyRates(1,bars,m_updateRates);
}
MqlDateTime server_time;
datetime now_server_time = TimeCurrent(server_time);
MqlDateTime local_time;
datetime now_local_time = TimeLocal(local_time);
if (m_param.m_licensefile_period != 0 && m_param.m_licensefile_period < now_server_time) {
MessageBox("授权到期");
ExpertRemove();
return;
}
else if (m_param.m_licensefile_period != 0 && m_param.m_licensefile_period < now_server_time + 3600 * 24 ) {
m_timeLable.render(StringFormat("服务时间: %s 授权时间: %s 即将过期",
TimeToString(now_server_time, TIME_DATE|TIME_MINUTES),
TimeToString(m_param.m_licensefile_period, TIME_DATE)
));
}
else if (m_param.m_licensefile_period != 0){
m_timeLable.render(StringFormat("服务时间: %s 授权时间: %s",
TimeToString(now_server_time, TIME_DATE|TIME_MINUTES),
TimeToString(m_param.m_licensefile_period, TIME_DATE)
));
}
/*
m_timeLable.render(StringFormat("服务器时间: %4d/%2d/%2d %02d:%02d:%02d 本地时间: %4d/%2d/%2d %02d:%02d:%02d ",
server_time.year, server_time.mon, server_time.day, server_time.hour, server_time.min, server_time.sec,
local_time.year, local_time.mon, local_time.day, local_time.hour, local_time.min, local_time.sec
));
m_totalLable.render(StringFormat("订单总数: %d, 总体收益: %.2f 总手数 %.2f 平均开单价 %.2f",
m_totalOrders.size(), m_totalOrders.groupProfit(), m_totalOrders.groupLots(), m_totalOrders.groupAvg()));
m_byeLable.render(StringFormat("多单总数: %d, 总体收益: %.2f 总手数 %.2f 平均开单价 %.2f",
m_buyOrders.size(), m_buyOrders.groupProfit(), m_buyOrders.groupLots(), m_buyOrders.groupAvg()));
m_sellLable.render(StringFormat("空单总数: %d, 总体收益: %.2f 总手数 %.2f 平均开单价 %.2f",
m_sellOrders.size(), m_sellOrders.groupProfit(), m_sellOrders.groupLots(), m_sellOrders.groupAvg()));
*/
if(m_param.getEnableListKLine() && m_param.getShowListKLine())
{
/*
MqlDateTime lastbartime;
TimeToStruct(m_data.getTime(0),lastbartime);
m_LineHigh.setlablename(StringFormat("HighLine %02d:%02d:%02d-%02d:%02d:%02d diff %d",lastbartime.hour, lastbartime.min, lastbartime.sec, Hour(),Minute(),Seconds(),
int((getHigh() - getLow())/ Point)));
//m_LineHigh.draw(getHigh(),TimeCurrent());
m_LineLow.setlablename(StringFormat("LowLine KBar %d RSI%d %.2f MA%d %.2f MA%d %.2f", m_param.getListKLineValue(),
m_param.getRSITimeFrame(),getRSI(),
m_param.getMAValueShort(),getShortMA(),
m_param.getMAValueLong(),getLongMA()
));
//m_LineLow.draw(getLow(),TimeCurrent());
*/
}
//开单组平均价格
if(m_param.getShowOrdersAvgLine())
{
/*
m_LineByeOrdersAvgPrice.setlablename(StringFormat("Bye Orders %2.2f %d", m_buyOrders.groupLots(),
(int)((m_data.getClose(0) - m_buyOrders.groupAvg()) / m_fxsymbol.getPoint()) * OrderBase::D(OP_BUY)
));
m_LineSellOrdersAvgPrice.setlablename(StringFormat("Sell Orders %2.2f %d", m_sellOrders.groupLots(),
(int)((m_data.getClose(0) - m_sellOrders.groupAvg()) / m_fxsymbol.getPoint()) * OrderBase::D(OP_SELL)
));
m_LineByeOrdersAvgPrice.draw(m_buyOrders.groupAvg(),TimeCurrent());
m_LineSellOrdersAvgPrice.draw(m_sellOrders.groupAvg(),TimeCurrent());
*/
}
m_ordertracker.track();
m_root.redraw();
}
//+------------------------------------------------------------------+