-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmq4_mq5_bridge.mqh
1576 lines (1278 loc) · 111 KB
/
mq4_mq5_bridge.mqh
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
//+------------------------------------------------------------------+
//| mq4_mq5_bridge.mqh |
//| |
//+------------------------------------------------------------------+
/*
This file goes into your MT5 "Open Data Folder", then into -> MQL5\Include\
Your main code (near the very top) would then call it using:
#include <mq4_mq5_bridge.mqh>
VERSION HISTORY:
2017-JUN-05 v1_00 by bodi0
mq4.mqh file posted here: https://www.forexfactory.com/showthread.php?p=9943614#post9943614
2019-OCT-13 v1_01 by pips4life
Renamed to mq4_mq5_bridge.mqh, posted this version in the above thread.
(Increment the version and always document dates! Recognize that mutiple people might edit an old version
and choose a conflicting number, but not likely the same date also. There will be some effort required
to "merge" diverging versions).
FUTURE ENHANCEMENTS/FIXES:
NEXT(2019-OCT): Plenty of opportunities to define equivalent functions/#define's/etc. based on:
https://www.mql5.com/en/articles/81 (2010 Article, but still useful).
NEXT(2019-OCT): From https://www.mql5.com/en/articles/81, incorporate Sergey Pavlov's "initmql4.mqh" file, as appropriate.
*/
#property strict
#ifdef __MQL5__
#define show_inputs script_show_inputs
#define extern input
#define init OnInit
#define Point _Point
#define Digits _Digits
#define Bid (::SymbolInfoDouble(_Symbol, ::SYMBOL_BID))
#define Ask (::SymbolInfoDouble(_Symbol, ::SYMBOL_ASK))
#define True true
#define False false
#define TRUE true
#define FALSE false
#define TimeToStr TimeToString
#define DoubleToStr DoubleToString
#define StrToTime StringToTime
#define CurTime TimeCurrent
#define HistoryTotal OrdersHistoryTotal
#define LocalTime TimeLocal
#define MODE_OPEN 0
#define MODE_CLOSE 3
#define MODE_VOLUME 4
#define MODE_REAL_VOLUME 5
#define MODE_LOW 1
#define MODE_HIGH 2
#define MODE_TIME 5
#define MODE_SWAPLONG 18
#define MODE_SWAPSHORT 19
#define MODE_STARTING 20
#define MODE_EXPIRATION 21
#define MODE_TRADEALLOWED 22
#define MODE_SWAPTYPE 26
#define MODE_BID 9
#define MODE_ASK 10
#define MODE_DIGITS 12
#define MODE_SPREAD 13
#define MODE_STOPLEVEL 14
#define MODE_LOTSIZE 15
#define MODE_MINLOT 16
#define MODE_MAXLOT 17
#define MODE_LOTSTEP 18
#define MODE_MARGINREQUIRED 19
#define MODE_POINT 20
#define MODE_TICK_SIZE 21
#define MODE_TICK_VALUE 22
#define MODE_FREEZELEVEL 33
#define MODE_PROFITCALCMODE 27
#define MODE_MARGINCALCMODE 28
#define MODE_MARGININIT 29
#define MODE_MARGINMAINTENANCE 30
#define MODE_MARGINHEDGED 31
#define EMPTY -1
//Graph Object constants
#define OBJPROP_TIME1 0
#define OBJPROP_PRICE1 1
#define OBJPROP_TIME2 2
#define OBJPROP_PRICE2 3
#define OBJPROP_TIME3 4
#define OBJPROP_PRICE3 5
#define OBJPROP_COLOR 6
#define OBJPROP_STYLE 7
#define OBJPROP_WIDTH 8
#define OBJPROP_BACK 9
#define OBJPROP_RAY 10
#define OBJPROP_ELLIPSE 11
#define OBJPROP_SCALE 12
#define OBJPROP_ANGLE 13
#define OBJPROP_ARROWCODE 14
#define OBJPROP_TIMEFRAMES 15
#define OBJPROP_DEVIATION 16
#define OBJPROP_FONTSIZE 100
#define OBJPROP_CORNER 101
#define OBJPROP_XDISTANCE 102
#define OBJPROP_YDISTANCE 103
#define OBJPROP_FIBOLEVELS 200
#define OBJPROP_LEVELCOLOR 201
#define OBJPROP_LEVELSTYLE 202
#define OBJPROP_LEVELWIDTH 203
// fix
#ifndef POSITION_TICKET
#define POSITION_TICKET 1
#endif
#ifndef ORDER_TICKET
#define ORDER_TICKET 1
#endif
#ifndef DEAL_TICKET
#define DEAL_TICKET 1
#endif
#ifndef TRADE_ACTION_CLOSE_BY
#define TRADE_ACTION_CLOSE_BY 1
#endif
#ifndef PositionSelectByTicket
#define PositionSelectByTicket PositionSelectByTickets
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool PositionSelectByTickets(ulong ticket)
{
return (true);
}
#endif
#ifndef PositionGetTicket
#define PositionGetTicket PositionGetTickets
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool PositionGetTickets(ulong ticket)
{
return (true);
}
#endif
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
/*
#ifndef MqlTradeRequest
#define MqlTradeRequest MqlTradeRequests
struct MqlTradeRequests
{
};
#endif
*/
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double MarketInfo(const string Symb,const int Type)
{
switch(Type)
{
case MODE_BID:
return(::SymbolInfoDouble(Symb, ::SYMBOL_BID));
case MODE_ASK:
return(::SymbolInfoDouble(Symb, ::SYMBOL_ASK));
case MODE_DIGITS:
return((double)::SymbolInfoInteger(Symb, ::SYMBOL_DIGITS));
case MODE_SPREAD:
return((double)::SymbolInfoInteger(Symb, ::SYMBOL_SPREAD));
case MODE_STOPLEVEL:
return((double)::SymbolInfoInteger(Symb, ::SYMBOL_TRADE_STOPS_LEVEL));
case MODE_LOTSIZE:
return(::SymbolInfoDouble(Symb, ::SYMBOL_TRADE_CONTRACT_SIZE));
case MODE_LOTSTEP:
return(::SymbolInfoDouble(Symb, ::SYMBOL_VOLUME_STEP));
case MODE_MINLOT:
return(::SymbolInfoDouble(Symb, ::SYMBOL_VOLUME_MIN));
case MODE_MAXLOT:
return(::SymbolInfoDouble(Symb, ::SYMBOL_VOLUME_MAX));
case MODE_POINT:
return(::SymbolInfoDouble(Symb, ::SYMBOL_POINT));
case MODE_TICK_SIZE:
return(::SymbolInfoDouble(Symb, ::SYMBOL_TRADE_TICK_SIZE));
case MODE_TICK_VALUE:
return(::SymbolInfoDouble(Symb, ::SYMBOL_TRADE_TICK_VALUE));
}
return(-1);
}
#define StringGetChar StringGetCharacter
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string StringSetChar(const string &String_Var,const int iPos,const ushort Value)
{
string Str=String_Var;
::StringSetCharacter(Str,iPos,Value);
return(Str);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int TimeDayOfWeek(const datetime Date)
{
MqlDateTime mTime;
::TimeToStruct(Date,mTime);
return(mTime.day_of_week);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Hour()
{
MqlDateTime mHour;
TimeCurrent(mHour);
return (mHour.hour);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Minute()
{
MqlDateTime mMinute;
TimeCurrent(mMinute);
return (mMinute.min);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Day()
{
MqlDateTime mDay;
TimeCurrent(mDay);
return (mDay.day);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsTesting(void)
{
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsTradeContextBusy(void)
{
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsTradeAllowed(void)
{
return((bool) ::MQLInfoInteger(::MQL_TRADE_ALLOWED));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsOptimization()
{
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool RefreshRates(void)
{
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string WindowExpertName(void)
{
return(::MQLInfoString(::MQL_PROGRAM_NAME));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string AccountName(void)
{
return(::AccountInfoString(::ACCOUNT_NAME));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int AccountNumber(void)
{
return((int)::AccountInfoInteger(::ACCOUNT_LOGIN));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AccountFreeMargin(void)
{
return(::AccountInfoDouble(::ACCOUNT_MARGIN_FREE));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AccountFreeMarginCheck(const string Symb,const int Cmd,const double dVolume)
{
double Margin;
return(::OrderCalcMargin((ENUM_ORDER_TYPE)Cmd, Symb, dVolume,
::SymbolInfoDouble(Symb,(Cmd==::ORDER_TYPE_BUY) ? ::SYMBOL_ASK : ::SYMBOL_BID),Margin) ?
::AccountInfoDouble(::ACCOUNT_MARGIN_FREE) - Margin : -1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AccountBalance()
{
return(::AccountInfoDouble(ACCOUNT_BALANCE));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AccountEquity(void)
{
return(::AccountInfoDouble(::ACCOUNT_EQUITY));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int MT4Bars(void)
{
return(::Bars(_Symbol, _Period));
}
#define Bars (::MT4Bars())
#define DEFINE_TIMESERIE(NAME,FUNC,T) \
class CLASS##NAME \
{ \
public: \
static T Get(const string Symb,const int TimeFrame,const int iShift) \
{ \
T tValue[]; \
\
return((Copy##FUNC((Symb == NULL) ? _Symbol : Symb, _Period, iShift, 1, tValue) > 0) ? tValue[0] : -1); \
} \
\
T operator[](const int iPos) const \
{ \
return(CLASS##NAME::Get(_Symbol, _Period, iPos)); \
} \
}; \
\
CLASS##NAME NAME; \
\
T i##NAME(const string Symb,const int TimeFrame,const int iShift) \
{ \
return(CLASS##NAME::Get(Symb, TimeFrame, iShift)); \
}
DEFINE_TIMESERIE(Volume,TickVolume,long)
DEFINE_TIMESERIE(Time,Time,datetime)
DEFINE_TIMESERIE(Open,Open,double)
//DEFINE_TIMESERIE(ThisHigh,High,double)
//DEFINE_TIMESERIE(ThisLow,Low,double)
DEFINE_TIMESERIE(Close,Close,double)
#endif // __MQL5__
#ifdef __MQL5__
#ifndef __MT4ORDERS__
#define __MT4ORDERS__
#define RESERVE_SIZE 1000
#define DAY (24 * 3600)
#define HISTORY_PAUSE (MT4HISTORY::IsTester ? 0 : 5)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class MT4HISTORY
{
private:
static const bool IsTester;
long Tickets[];
uint Amount;
datetime LastTime;
int LastTotalDeals;
int LastTotalOrders;
datetime LastInitTime;
#define GETNEXTPOS_FUNCTION(NAME) \
static int GetNextPosMT4##NAME(int iPos) \
{ \
const int Total=::History##NAME##sTotal(); \
\
while(iPos<Total) \
{ \
if(MT4HISTORY::IsMT4##NAME(::History##NAME##GetTicket(iPos))) \
break; \
\
iPos++; \
} \
\
return(iPos); \
}
GETNEXTPOS_FUNCTION(Order)
GETNEXTPOS_FUNCTION(Deal)
#undef GETNEXTPOS_FUNCTION
bool RefreshHistory(void)
{
bool Res=false;
const datetime LastTimeCurrent=::TimeCurrent();
if((!MT4HISTORY::IsTester) && (LastTimeCurrent>=this.LastInitTime+DAY))
{
this.LastTime=0;
this.LastTotalOrders= 0;
this.LastTotalDeals = 0;
this.Amount=0;
::ArrayResize(this.Tickets,this.Amount,RESERVE_SIZE);
this.LastInitTime=LastTimeCurrent;
}
if(::HistorySelect(this.LastTime,::MathMax(LastTimeCurrent,this.LastTime)+DAY)) // суточный запас
{
const int TotalOrders= ::HistoryOrdersTotal();
const int TotalDeals = ::HistoryDealsTotal();
Res=((TotalOrders!=this.LastTotalOrders) || (TotalDeals!=this.LastTotalDeals));
if(Res)
{
int iOrder= MT4HISTORY::GetNextPosMT4Order(this.LastTotalOrders);
int iDeal = MT4HISTORY::GetNextPosMT4Deal(this.LastTotalDeals);
long TimeOrder=(iOrder<TotalOrders) ? ::HistoryOrderGetInteger(::HistoryOrderGetTicket(iOrder),ORDER_TIME_DONE/*_MSC*/) : LONG_MAX; // ORDER_TIME_DONE_MSC returns NULL (build 1470)
long TimeDeal =(iDeal<TotalDeals) ? ::HistoryDealGetInteger(::HistoryDealGetTicket(iDeal),DEAL_TIME/*_MSC*/) : LONG_MAX;
while((iDeal<TotalDeals) || (iOrder<TotalOrders))
if(TimeOrder<TimeDeal)
{
this.Amount=::ArrayResize(this.Tickets,this.Amount+1,RESERVE_SIZE);
this.Tickets[this.Amount-1]=-(long)::HistoryOrderGetTicket(iOrder);
iOrder=MT4HISTORY::GetNextPosMT4Order(iOrder+1);
TimeOrder=(iOrder<TotalOrders) ? ::HistoryOrderGetInteger(::HistoryOrderGetTicket(iOrder),ORDER_TIME_DONE/*_MSC*/) : LONG_MAX; // ORDER_TIME_DONE_MSC returns NULL (build 1470)
}
else
{
this.Amount=::ArrayResize(this.Tickets,this.Amount+1,RESERVE_SIZE);
this.Tickets[this.Amount-1]=(long)::HistoryDealGetTicket(iDeal);
iDeal=MT4HISTORY::GetNextPosMT4Deal(iDeal+1);
TimeDeal=(iDeal<TotalDeals) ? ::HistoryDealGetInteger(::HistoryDealGetTicket(iDeal),DEAL_TIME/*_MSC*/) : LONG_MAX;
}
TimeOrder=(TotalOrders>0) ? ::HistoryOrderGetInteger(::HistoryOrderGetTicket(TotalOrders-1),ORDER_TIME_DONE/*_MSC*/) : 0;
TimeDeal =(TotalDeals>0) ? ::HistoryDealGetInteger(::HistoryDealGetTicket(TotalDeals-1),DEAL_TIME/*_MSC*/) : 0;
const long MaxTime=::MathMax(TimeOrder,TimeDeal);
this.LastTotalOrders= 0;
this.LastTotalDeals = 0;
if(LastTimeCurrent-HISTORY_PAUSE>MaxTime)
this.LastTime=LastTimeCurrent-HISTORY_PAUSE;
else
{
this.LastTime=(datetime)MaxTime;
if(TimeOrder== MaxTime)
for(int i = TotalOrders-1; i>= 0; i--)
{
if(TimeOrder>::HistoryOrderGetInteger(::HistoryOrderGetTicket(i),ORDER_TIME_DONE/*_MSC*/))
break;
this.LastTotalOrders++;
}
if(TimeDeal == MaxTime)
for(int i = TotalDeals - 1; i >= 0; i--)
{
if(TimeDeal!=::HistoryDealGetInteger(::HistoryDealGetTicket(TotalDeals-1),DEAL_TIME/*_MSC*/))
break;
this.LastTotalDeals++;
}
}
}
else if(LastTimeCurrent-HISTORY_PAUSE>this.LastTime)
{
this.LastTime=LastTimeCurrent-HISTORY_PAUSE;
this.LastTotalOrders= 0;
this.LastTotalDeals = 0;
}
}
return(Res);
}
public:
static bool IsMT4Deal(const ulong Ticket)
{
const ENUM_DEAL_TYPE Type=(ENUM_DEAL_TYPE)::HistoryDealGetInteger(Ticket,DEAL_TYPE);
return(((Type != DEAL_TYPE_BUY) && (Type != DEAL_TYPE_SELL)) || ((ENUM_DEAL_ENTRY)::HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT));
}
static bool IsMT4Order(const ulong Ticket)
{
return((::HistoryOrderGetDouble(Ticket, ORDER_VOLUME_CURRENT) > 0) || (::HistoryOrderGetInteger(Ticket, ORDER_POSITION_ID) == 0));
}
MT4HISTORY(void) : Amount(0),LastTime(0),LastTotalDeals(0),LastTotalOrders(0),LastInitTime(0)
{
::ArrayResize(this.Tickets,this.Amount,RESERVE_SIZE);
this.RefreshHistory();
}
int GetAmount(void)
{
this.RefreshHistory();
return((int)this.Amount);
}
long operator[](const uint Pos)
{
long Res=0;
if(Pos>=this.Amount)
{
this.RefreshHistory();
if(Pos< this.Amount)
Res=this.Tickets[Pos];
}
else
Res=this.Tickets[Pos];
return(Res);
}
};
static const bool MT4HISTORY::IsTester=(::MQLInfoInteger(MQL_TESTER) || ::MQLInfoInteger(MQL_OPTIMIZATION) ||
::MQLInfoInteger(MQL_VISUAL_MODE) || ::MQLInfoInteger(MQL_FRAME_MODE));
#undef HISTORY_PAUSE
#undef DAY
#undef RESERVE_SIZE
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
struct MT4_ORDER
{
int Ticket;
int Type;
double Lots;
string Symbol;
string Comment;
double OpenPrice;
datetime OpenTime;
double StopLoss;
double TakeProfit;
double ClosePrice;
datetime CloseTime;
datetime Expiration;
int MagicNumber;
double Profit;
double Commission;
double Swap;
string ToString(void) const
{
static const string Types[]={"buy","sell","buy limit","sell limit","buy stop","sell stop","balance"};
const int digits=(int)::SymbolInfoInteger(this.Symbol,SYMBOL_DIGITS);
return("#" + (string)this.Ticket + " " +
(string)this.OpenTime+" "+
((this.Type<::ArraySize(Types)) ? Types[this.Type]: "unknown")+" "+
::DoubleToString(this.Lots,2)+" "+
this.Symbol+" "+
::DoubleToString(this.OpenPrice, digits) + " " +
::DoubleToString(this.StopLoss, digits) + " " +
::DoubleToString(this.TakeProfit, digits) + " " +
((this.CloseTime>0) ?((string)this.CloseTime+" ") : "")+
::DoubleToString(this.ClosePrice, digits) + " " +
::DoubleToString(this.Commission, 2) + " " +
::DoubleToString(this.Swap, 2) + " " +
::DoubleToString(this.Profit, 2) + " " +
((this.Comment=="") ? "" :(this.Comment+" "))+
(string)this.MagicNumber+
(((this.Expiration>0) ?(" expiration "+(string)this.Expiration): "")));
}
};
#define OP_BUY ORDER_TYPE_BUY
#define OP_SELL ORDER_TYPE_SELL
#define OP_BUYLIMIT ORDER_TYPE_BUY_LIMIT
#define OP_SELLLIMIT ORDER_TYPE_SELL_LIMIT
#define OP_BUYSTOP ORDER_TYPE_BUY_STOP
#define OP_SELLSTOP ORDER_TYPE_SELL_STOP
#define OP_BALANCE 6
#define SELECT_BY_POS 0
#define SELECT_BY_TICKET 1
#define MODE_TRADES 0
#define MODE_HISTORY 1
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class MT4ORDERS
{
private:
static MT4_ORDER Order;
static MT4HISTORY History;
static const bool IsTester;
static ulong GetPositionDealIn(const ulong PositionIdentifier=0)
{
ulong Ticket=0;
if((PositionIdentifier==0) ? ::HistorySelectByPosition(::PositionGetInteger(POSITION_IDENTIFIER)) : ::HistorySelectByPosition(PositionIdentifier))
{
const int Total=::HistoryDealsTotal();
for(int i=0; i<Total; i++)
{
const ulong TicketDeal=::HistoryDealGetTicket(i);
if(TicketDeal>0)
if((ENUM_DEAL_ENTRY)::HistoryDealGetInteger(TicketDeal,DEAL_ENTRY)==DEAL_ENTRY_IN)
{
Ticket=TicketDeal;
break;
}
}
}
return(Ticket);
}
static double GetPositionCommission(void)
{
double Commission=::PositionGetDouble(POSITION_COMMISSION);
if(Commission==0)
{
const ulong Ticket=MT4ORDERS::GetPositionDealIn();
if(Ticket>0)
{
const double LotsIn=::HistoryDealGetDouble(Ticket,DEAL_VOLUME);
if(LotsIn>0)
Commission=::HistoryDealGetDouble(Ticket,DEAL_COMMISSION)*::PositionGetDouble(POSITION_VOLUME)/LotsIn;
}
}
return(Commission);
}
static string GetPositionComment(void)
{
string comment=::PositionGetString(POSITION_COMMENT);
if(comment=="")
{
const ulong Ticket=MT4ORDERS::GetPositionDealIn();
if(Ticket>0)
comment=::HistoryDealGetString(Ticket,DEAL_COMMENT);
}
return(comment);
}
static void GetPositionData(void)
{
MT4ORDERS::Order.Ticket=(int)::PositionGetInteger(POSITION_TICKET);
MT4ORDERS::Order.Type=(int)::PositionGetInteger(POSITION_TYPE);
MT4ORDERS::Order.Lots=::PositionGetDouble(POSITION_VOLUME);
MT4ORDERS::Order.Symbol=::PositionGetString(POSITION_SYMBOL);
MT4ORDERS::Order.Comment=MT4ORDERS::GetPositionComment();
MT4ORDERS::Order.OpenPrice= ::PositionGetDouble(POSITION_PRICE_OPEN);
MT4ORDERS::Order.OpenTime =(datetime)::PositionGetInteger(POSITION_TIME);
MT4ORDERS::Order.StopLoss=::PositionGetDouble(POSITION_SL);
MT4ORDERS::Order.TakeProfit=::PositionGetDouble(POSITION_TP);
MT4ORDERS::Order.ClosePrice= ::PositionGetDouble(POSITION_PRICE_CURRENT);
MT4ORDERS::Order.CloseTime = 0;
MT4ORDERS::Order.Expiration=0;
MT4ORDERS::Order.MagicNumber=(int)::PositionGetInteger(POSITION_MAGIC);
MT4ORDERS::Order.Profit=::PositionGetDouble(POSITION_PROFIT);
MT4ORDERS::Order.Commission=MT4ORDERS::GetPositionCommission();
MT4ORDERS::Order.Swap=::PositionGetDouble(POSITION_SWAP);
return;
}
static void GetOrderData(void)
{
MT4ORDERS::Order.Ticket=(int)::OrderGetInteger(ORDER_TICKET);
MT4ORDERS::Order.Type=(int)::OrderGetInteger(ORDER_TYPE);
MT4ORDERS::Order.Lots=::OrderGetDouble(ORDER_VOLUME_CURRENT);
MT4ORDERS::Order.Symbol=::OrderGetString(ORDER_SYMBOL);
MT4ORDERS::Order.Comment=::OrderGetString(ORDER_COMMENT);
MT4ORDERS::Order.OpenPrice= ::OrderGetDouble(ORDER_PRICE_OPEN);
MT4ORDERS::Order.OpenTime =(datetime)::OrderGetInteger(ORDER_TIME_SETUP);
MT4ORDERS::Order.StopLoss=::OrderGetDouble(ORDER_SL);
MT4ORDERS::Order.TakeProfit=::OrderGetDouble(ORDER_TP);
MT4ORDERS::Order.ClosePrice= ::OrderGetDouble(ORDER_PRICE_CURRENT);
MT4ORDERS::Order.CloseTime =(datetime)::OrderGetInteger(ORDER_TIME_DONE);
MT4ORDERS::Order.Expiration=(datetime)::OrderGetInteger(ORDER_TIME_EXPIRATION);
MT4ORDERS::Order.MagicNumber=(int)::OrderGetInteger(ORDER_MAGIC);
MT4ORDERS::Order.Profit=0;
MT4ORDERS::Order.Commission=0;
MT4ORDERS::Order.Swap=0;
return;
}
static void GetHistoryOrderData(const ulong Ticket)
{
MT4ORDERS::Order.Ticket=(int)::HistoryOrderGetInteger(Ticket,ORDER_TICKET);
MT4ORDERS::Order.Type=(int)::HistoryOrderGetInteger(Ticket,ORDER_TYPE);
MT4ORDERS::Order.Lots=::HistoryOrderGetDouble(Ticket,ORDER_VOLUME_CURRENT);
if(MT4ORDERS::Order.Lots== 0)
MT4ORDERS::Order.Lots = ::HistoryOrderGetDouble(Ticket,ORDER_VOLUME_INITIAL);
MT4ORDERS::Order.Symbol=::HistoryOrderGetString(Ticket,ORDER_SYMBOL);
MT4ORDERS::Order.Comment=::HistoryOrderGetString(Ticket,ORDER_COMMENT);
MT4ORDERS::Order.OpenPrice= ::HistoryOrderGetDouble(Ticket,ORDER_PRICE_OPEN);
MT4ORDERS::Order.OpenTime =(datetime)::HistoryOrderGetInteger(Ticket,ORDER_TIME_SETUP);
MT4ORDERS::Order.StopLoss=::HistoryOrderGetDouble(Ticket,ORDER_SL);
MT4ORDERS::Order.TakeProfit=::HistoryOrderGetDouble(Ticket,ORDER_TP);
MT4ORDERS::Order.ClosePrice= 0;
MT4ORDERS::Order.CloseTime =(datetime)::HistoryOrderGetInteger(Ticket,ORDER_TIME_DONE);
MT4ORDERS::Order.Expiration=(datetime)::HistoryOrderGetInteger(Ticket,ORDER_TIME_EXPIRATION);
MT4ORDERS::Order.MagicNumber=(int)::HistoryOrderGetInteger(Ticket,ORDER_MAGIC);
MT4ORDERS::Order.Profit=0;
MT4ORDERS::Order.Commission=0;
MT4ORDERS::Order.Swap=0;
return;
}
static void GetHistoryPositionData(const ulong Ticket)
{
MT4ORDERS::Order.Ticket=(int)::HistoryDealGetInteger(Ticket,DEAL_TICKET);
MT4ORDERS::Order.Type=(int)::HistoryDealGetInteger(Ticket,DEAL_TYPE);
if((MT4ORDERS::Order.Type>OP_SELL))
MT4ORDERS::Order.Type+=(OP_BALANCE-OP_SELL-1);
else
MT4ORDERS::Order.Type=1-MT4ORDERS::Order.Type;
MT4ORDERS::Order.Lots=::HistoryDealGetDouble(Ticket,DEAL_VOLUME);
MT4ORDERS::Order.Symbol=::HistoryDealGetString(Ticket,DEAL_SYMBOL);
MT4ORDERS::Order.Comment=::HistoryDealGetString(Ticket,DEAL_COMMENT);
MT4ORDERS::Order.OpenPrice= ::HistoryDealGetDouble(Ticket,DEAL_PRICE);
MT4ORDERS::Order.OpenTime =(datetime)::HistoryDealGetInteger(Ticket,DEAL_TIME);
MT4ORDERS::Order.StopLoss=0;
MT4ORDERS::Order.TakeProfit=0;
MT4ORDERS::Order.ClosePrice= ::HistoryDealGetDouble(Ticket,DEAL_PRICE);
MT4ORDERS::Order.CloseTime =(datetime)::HistoryDealGetInteger(Ticket,DEAL_TIME);;
MT4ORDERS::Order.Expiration=0;
MT4ORDERS::Order.MagicNumber=(int)::HistoryDealGetInteger(Ticket,DEAL_MAGIC);
MT4ORDERS::Order.Profit=::HistoryDealGetDouble(Ticket,DEAL_PROFIT);
MT4ORDERS::Order.Commission=::HistoryDealGetDouble(Ticket,DEAL_COMMISSION);
MT4ORDERS::Order.Swap=::HistoryDealGetDouble(Ticket,DEAL_SWAP);
const ulong OpenTicket=MT4ORDERS::GetPositionDealIn(::HistoryDealGetInteger(Ticket,DEAL_POSITION_ID));
if(OpenTicket>0)
{
MT4ORDERS::Order.OpenPrice= ::HistoryDealGetDouble(OpenTicket,DEAL_PRICE);
MT4ORDERS::Order.OpenTime =(datetime)::HistoryDealGetInteger(OpenTicket,DEAL_TIME);
const double OpenLots=::HistoryDealGetDouble(OpenTicket,DEAL_VOLUME);
if(OpenLots>0)
MT4ORDERS::Order.Commission+=::HistoryDealGetDouble(OpenTicket,DEAL_COMMISSION)*MT4ORDERS::Order.Lots/OpenLots;
if(MT4ORDERS::Order.MagicNumber== 0)
MT4ORDERS::Order.MagicNumber =(int)::HistoryDealGetInteger(OpenTicket,DEAL_MAGIC);
if(MT4ORDERS::Order.Comment== "")
MT4ORDERS::Order.Comment = ::HistoryDealGetString(OpenTicket,DEAL_COMMENT);
}
return;
}
static bool Waiting(const bool FlagInit=false)
{
static ulong StartTime=0;
if(FlagInit)
StartTime=::GetMicrosecondCount();
const bool Res=(::GetMicrosecondCount()-StartTime<MT4ORDERS::OrderSend_MaxPause);
if(Res)
::Sleep(0);
return(Res);
}
static bool EqualPrices(const double Price1,const double Price2,const int digits)
{
return(::NormalizeDouble(Price1 - Price2, digits) == 0);
}
#define WHILE(A) while (!(Res = (A)) && MT4ORDERS::Waiting())
static bool OrderSend(const MqlTradeRequest &Request,MqlTradeResult &Result)
{
bool Res=::OrderSend(Request,Result);
if(Res && !MT4ORDERS::IsTester && (Result.retcode<TRADE_RETCODE_ERROR) && (MT4ORDERS::OrderSend_MaxPause>0))
{
Res=(Result.retcode==TRADE_RETCODE_DONE);
MT4ORDERS::Waiting(true);
if(Request.action==TRADE_ACTION_DEAL)
{
WHILE(::HistoryOrderSelect(Result.order))
;
Res=Res && (((ENUM_ORDER_STATE)::HistoryOrderGetInteger(Result.order,ORDER_STATE)==ORDER_STATE_FILLED) ||
((ENUM_ORDER_STATE)::HistoryOrderGetInteger(Result.order,ORDER_STATE)==ORDER_STATE_PARTIAL));
if(Res)
WHILE(::HistoryDealSelect(Result.deal))
;
}
else if(Request.action==TRADE_ACTION_PENDING)
{
if(Res)
WHILE(::OrderSelect(Result.order))
;
else
{
WHILE(::HistoryOrderSelect(Result.order))
;
Res=false;
}
}
else if(Request.action==TRADE_ACTION_SLTP)
{
if(Res)
{
bool EqualSL = false;
bool EqualTP = false;
const int digits=(int)::SymbolInfoInteger(Request.symbol,SYMBOL_DIGITS);
if((Request.position==0) ? ::PositionSelect(Request.symbol) : ::PositionSelectByTicket(Request.position))
{
EqualSL = MT4ORDERS::EqualPrices(::PositionGetDouble(POSITION_SL), Request.sl, digits);
EqualTP = MT4ORDERS::EqualPrices(::PositionGetDouble(POSITION_TP), Request.tp, digits);
}
WHILE((EqualSL && EqualTP))
if((Request.position==0) ? ::PositionSelect(Request.symbol) : ::PositionSelectByTicket(Request.position))
{
EqualSL = MT4ORDERS::EqualPrices(::PositionGetDouble(POSITION_SL), Request.sl, digits);
EqualTP = MT4ORDERS::EqualPrices(::PositionGetDouble(POSITION_TP), Request.tp, digits);
}
}
}
else if(Request.action==TRADE_ACTION_MODIFY)
{
if(Res)
{
bool EqualSL = false;
bool EqualTP = false;
const int digits=(int)::SymbolInfoInteger(Request.symbol,SYMBOL_DIGITS);
if(::OrderSelect(Result.order))
{
EqualSL = MT4ORDERS::EqualPrices(::OrderGetDouble(ORDER_SL), Request.sl, digits);
EqualTP = MT4ORDERS::EqualPrices(::OrderGetDouble(ORDER_TP), Request.tp, digits);
}
WHILE((EqualSL && EqualTP))
if(::OrderSelect(Result.order))
{
EqualSL = MT4ORDERS::EqualPrices(::OrderGetDouble(ORDER_SL), Request.sl, digits);
EqualTP = MT4ORDERS::EqualPrices(::OrderGetDouble(ORDER_TP), Request.tp, digits);
}
}
}
else if(Request.action==TRADE_ACTION_REMOVE)
if(Res)
WHILE(::HistoryOrderSelect(Result.order))
;
}