|
|
@@ -57,19 +57,24 @@ void PrintStructure();
|
|
57
|
57
|
|
|
58
|
58
|
struct new_trade_store
|
|
59
|
59
|
{
|
|
60
|
|
- ulong buy_ticket; // Buy Ticket
|
|
61
|
|
- ulong sell_ticket; // Sell Ticket
|
|
62
|
|
- string symbol; // Symbol
|
|
63
|
|
- double price; // Price
|
|
64
|
|
- double stop_loss; // StopLoss
|
|
65
|
|
- double take_profit; // TakeProfit
|
|
66
|
|
- datetime start_time; // Start time
|
|
67
|
|
- datetime end_time; // End Time
|
|
|
60
|
+ ulong buy_ticket; // Buy Ticket
|
|
|
61
|
+ ulong sell_ticket; // Sell Ticket
|
|
|
62
|
+ string symbol; // Symbol
|
|
|
63
|
+ double price; // Price
|
|
|
64
|
+ double stop_loss; // StopLoss
|
|
|
65
|
+ double take_profit; // TakeProfit
|
|
|
66
|
+ datetime start_time; // Start time
|
|
|
67
|
+ datetime end_time; // End Time
|
|
|
68
|
+ bool buy_hit_virtual_sl; // Buy Hit Virtual StopLoss
|
|
|
69
|
+ bool sell_hit_virtual_sl; // Sell Hit Virtual StopLoss
|
|
68
|
70
|
|
|
69
|
71
|
new_trade_store()
|
|
70
|
72
|
{
|
|
71
|
73
|
buy_ticket = -1;
|
|
72
|
74
|
sell_ticket = -1;
|
|
|
75
|
+ price = 0;
|
|
|
76
|
+ buy_hit_virtual_sl = false;
|
|
|
77
|
+ sell_hit_virtual_sl = false;
|
|
73
|
78
|
}
|
|
74
|
79
|
|
|
75
|
80
|
};
|
|
|
@@ -89,6 +94,7 @@ input int maxTrades = 2;
|
|
89
|
94
|
input int maxSlippage = 5; // Max Slippage
|
|
90
|
95
|
input bool enableSpreadFilter = false; // Enable Spread Filter
|
|
91
|
96
|
input double maximumSpread = 10; // Maximum Spread
|
|
|
97
|
+input string dataFileName = "vol_hedge_data.csv"; // Data File Name
|
|
92
|
98
|
|
|
93
|
99
|
input string string_1 = "<><><><><><> Lot Management<><><><><><>"; //__
|
|
94
|
100
|
input lotcalculator lot_calculator = fix; // Lot Size Option
|
|
|
@@ -106,6 +112,11 @@ input bool indivial_trailing = false;
|
|
106
|
112
|
input double ts_sl = 15; // Trailing Start in Pips
|
|
107
|
113
|
input double ts = 5; // Trailing Stop in Pips
|
|
108
|
114
|
|
|
|
115
|
+input string strMA14 ="<><><><><><> BreakEven Settings <><><><><><>";//_
|
|
|
116
|
+input bool UseBreakEven = false; // Use Break Even
|
|
|
117
|
+input int breakEvenPoints = 15; // BreakEven Trigger Pips
|
|
|
118
|
+input int breakStopPoint = 5; // BreakEven Above OpenPrice Pips
|
|
|
119
|
+
|
|
109
|
120
|
input string news = "<><><><><><> News Settings <><><><><><>"; // News
|
|
110
|
121
|
input NewsCloseOrder newsClose = CloseAllRunningOrder; // On News Action on Running Orders
|
|
111
|
122
|
input bool High_Impact_News = true; //High Impact News
|
|
|
@@ -132,6 +143,7 @@ datetime startSessionTime, endSessionTime;
|
|
132
|
143
|
int GMT_Broker_Time = +2; // GMT_Broker_Time Time of your Broker
|
|
133
|
144
|
int gmt = 0; // GMT_Broker_Time Time of your Broker
|
|
134
|
145
|
string Lname="newsLabel3";
|
|
|
146
|
+double levelsAre[];
|
|
135
|
147
|
//+------------------------------------------------------------------+
|
|
136
|
148
|
//| |
|
|
137
|
149
|
//+------------------------------------------------------------------+
|
|
|
@@ -145,7 +157,12 @@ int OnInit()
|
|
145
|
157
|
trade.LogLevel(LOG_LEVEL_ALL);
|
|
146
|
158
|
trade.SetAsyncMode(false);
|
|
147
|
159
|
|
|
148
|
|
- int filehandle = FileOpen("vol_hedge_data.csv", FILE_READ | FILE_CSV | FILE_COMMON | FILE_ANSI);
|
|
|
160
|
+ if(!MQLInfoInteger(MQL_TESTER))
|
|
|
161
|
+ {
|
|
|
162
|
+ loadNewTradeStoreFile();
|
|
|
163
|
+ }
|
|
|
164
|
+
|
|
|
165
|
+ int filehandle = FileOpen(dataFileName, FILE_READ | FILE_CSV | FILE_COMMON | FILE_ANSI);
|
|
149
|
166
|
if(filehandle != INVALID_HANDLE)
|
|
150
|
167
|
{
|
|
151
|
168
|
Print(" Valid Handler. ");
|
|
|
@@ -177,14 +194,32 @@ int OnInit()
|
|
177
|
194
|
datetime start_local = StringToTime(orderData[4]);
|
|
178
|
195
|
datetime end_local = StringToTime(orderData[5]);
|
|
179
|
196
|
|
|
|
197
|
+ ArrayResize(levelsAre,ArraySize(levelsAre)+1);
|
|
|
198
|
+ levelsAre[ArraySize(levelsAre) - 1] = price_local;
|
|
|
199
|
+
|
|
180
|
200
|
// OPTIONAL: only add when price == 0:
|
|
181
|
201
|
// if(MathAbs(price_local) > 1e-9) { Print("Skipped: price != 0"); continue; }
|
|
|
202
|
+ bool buy_virtual_tp_hit = true;
|
|
|
203
|
+ bool sell_virtual_tp_hit = true;
|
|
|
204
|
+ if(bothHitsSl)
|
|
|
205
|
+ {
|
|
|
206
|
+ buy_virtual_tp_hit = false;
|
|
|
207
|
+ sell_virtual_tp_hit = false;
|
|
|
208
|
+ }
|
|
182
|
209
|
|
|
183
|
210
|
// call the single-responsibility function that writes into struct array
|
|
184
|
|
- addToNewTradeStore(buy_ticket_local, sell_ticket_local,
|
|
185
|
|
- symbol_local, price_local,
|
|
186
|
|
- sl_local, tp_local,
|
|
187
|
|
- start_local, end_local);
|
|
|
211
|
+ if(!level_present(price_local))
|
|
|
212
|
+ {
|
|
|
213
|
+ addToNewTradeStore(buy_ticket_local, sell_ticket_local,
|
|
|
214
|
+ symbol_local, price_local,
|
|
|
215
|
+ sl_local, tp_local,
|
|
|
216
|
+ start_local, end_local,
|
|
|
217
|
+ buy_virtual_tp_hit, sell_virtual_tp_hit);
|
|
|
218
|
+ }
|
|
|
219
|
+ else
|
|
|
220
|
+ {
|
|
|
221
|
+ Print("Level is already Present. Level: ", price_local);
|
|
|
222
|
+ }
|
|
188
|
223
|
}
|
|
189
|
224
|
}
|
|
190
|
225
|
}
|
|
|
@@ -195,6 +230,8 @@ int OnInit()
|
|
195
|
230
|
Print(" InValid Handler. Error: ", GetLastError());
|
|
196
|
231
|
}
|
|
197
|
232
|
|
|
|
233
|
+ struct_level_check();
|
|
|
234
|
+
|
|
198
|
235
|
timeFilter(true,start_time_session, end_time_session, startSessionTime, endSessionTime);
|
|
199
|
236
|
Print(" Session Start = ", startSessionTime, " Asian Session End = ", endSessionTime);
|
|
200
|
237
|
|
|
|
@@ -232,7 +269,10 @@ int OnInit()
|
|
232
|
269
|
void OnDeinit(const int reason)
|
|
233
|
270
|
{
|
|
234
|
271
|
//---
|
|
235
|
|
-
|
|
|
272
|
+ if(!MQLInfoInteger(MQL_TESTER))
|
|
|
273
|
+ {
|
|
|
274
|
+ saveNewTradeStoreFile();
|
|
|
275
|
+ }
|
|
236
|
276
|
}
|
|
237
|
277
|
//+------------------------------------------------------------------+
|
|
238
|
278
|
//| Expert tick function |
|
|
|
@@ -358,6 +398,10 @@ void mainActivity()
|
|
358
|
398
|
{
|
|
359
|
399
|
Individual_Trailing();
|
|
360
|
400
|
}
|
|
|
401
|
+ if(UseBreakEven)
|
|
|
402
|
+ {
|
|
|
403
|
+ breakEven();
|
|
|
404
|
+ }
|
|
361
|
405
|
double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
|
|
362
|
406
|
double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
|
|
363
|
407
|
|
|
|
@@ -388,11 +432,12 @@ void mainActivity()
|
|
388
|
432
|
// Comment(" Session Start = ", startSessionTime, " Asian Session End = ", endSessionTime);
|
|
389
|
433
|
if((!enableTimeSession) || (enableTimeSession && TimeCurrent() >= startSessionTime && TimeCurrent() <= endSessionTime))
|
|
390
|
434
|
{
|
|
391
|
|
- tradePlacingCheck();
|
|
|
435
|
+ removeFromStructure();
|
|
392
|
436
|
if(bothHitsSl)
|
|
393
|
437
|
{
|
|
394
|
|
- removeBuySellOnSlHit();
|
|
|
438
|
+ virtualSLHitCheck();
|
|
395
|
439
|
}
|
|
|
440
|
+ tradePlacingCheck();
|
|
396
|
441
|
}
|
|
397
|
442
|
}
|
|
398
|
443
|
//+------------------------------------------------------------------+
|
|
|
@@ -475,33 +520,106 @@ int orderCount(int type)
|
|
475
|
520
|
//+------------------------------------------------------------------+
|
|
476
|
521
|
//| |
|
|
477
|
522
|
//+------------------------------------------------------------------+
|
|
|
523
|
+bool level_present(double priceIs)
|
|
|
524
|
+ {
|
|
|
525
|
+ for(int i = 0 ; i < MaxOrders ; i++)
|
|
|
526
|
+ {
|
|
|
527
|
+ if(newTradeStore[i].price > 0)
|
|
|
528
|
+ {
|
|
|
529
|
+ if(priceIs == newTradeStore[i].price)
|
|
|
530
|
+ {
|
|
|
531
|
+ return true;
|
|
|
532
|
+ }
|
|
|
533
|
+ }
|
|
|
534
|
+ }
|
|
|
535
|
+ return false;
|
|
|
536
|
+ }
|
|
|
537
|
+//+------------------------------------------------------------------+
|
|
|
538
|
+//| |
|
|
|
539
|
+//+------------------------------------------------------------------+
|
|
|
540
|
+void struct_level_check()
|
|
|
541
|
+ {
|
|
|
542
|
+ for(int i = 0; i < MaxOrders; i++)
|
|
|
543
|
+ {
|
|
|
544
|
+ if(newTradeStore[i].price > 0)
|
|
|
545
|
+ {
|
|
|
546
|
+ bool found = false;
|
|
|
547
|
+
|
|
|
548
|
+ for(int j = 0; j < ArraySize(levelsAre); j++)
|
|
|
549
|
+ {
|
|
|
550
|
+ if(newTradeStore[i].price == levelsAre[j])
|
|
|
551
|
+ {
|
|
|
552
|
+ found = true;
|
|
|
553
|
+ break;
|
|
|
554
|
+ }
|
|
|
555
|
+ }
|
|
|
556
|
+
|
|
|
557
|
+ if(!found)
|
|
|
558
|
+ {
|
|
|
559
|
+ Print("Price not found in levelsAre[] -> index:", i, " | price:", newTradeStore[i].price);
|
|
|
560
|
+ newTradeStore[i].buy_ticket = (ulong)-1;
|
|
|
561
|
+ newTradeStore[i].sell_ticket = (ulong)-1;
|
|
|
562
|
+ bool buy_virtual_tp_hit = true;
|
|
|
563
|
+ bool sell_virtual_tp_hit = true;
|
|
|
564
|
+ if(bothHitsSl)
|
|
|
565
|
+ {
|
|
|
566
|
+ buy_virtual_tp_hit = false;
|
|
|
567
|
+ sell_virtual_tp_hit = false;
|
|
|
568
|
+ }
|
|
|
569
|
+ newTradeStore[i].buy_hit_virtual_sl = buy_virtual_tp_hit;
|
|
|
570
|
+ newTradeStore[i].sell_hit_virtual_sl = sell_virtual_tp_hit;
|
|
|
571
|
+ newTradeStore[i].symbol = "";
|
|
|
572
|
+ newTradeStore[i].price = 0.0;
|
|
|
573
|
+ newTradeStore[i].stop_loss = 0.0;
|
|
|
574
|
+ newTradeStore[i].take_profit = 0.0;
|
|
|
575
|
+ newTradeStore[i].start_time = 0;
|
|
|
576
|
+ newTradeStore[i].end_time = 0;
|
|
|
577
|
+ return;
|
|
|
578
|
+ }
|
|
|
579
|
+ }
|
|
|
580
|
+ }
|
|
|
581
|
+
|
|
|
582
|
+ return;
|
|
|
583
|
+ }
|
|
|
584
|
+
|
|
|
585
|
+//+------------------------------------------------------------------+
|
|
|
586
|
+//| |
|
|
|
587
|
+//+------------------------------------------------------------------+
|
|
478
|
588
|
void addToNewTradeStore(ulong r_buy_ticket, ulong r_sell_ticket,
|
|
479
|
589
|
string r_symbol, double r_price,
|
|
480
|
590
|
double r_stop_loss, double r_take_profit,
|
|
481
|
|
- datetime r_start_time, datetime r_end_time)
|
|
|
591
|
+ datetime r_start_time, datetime r_end_time, bool r_buy_hit_sl, bool r_sell_hit_sl)
|
|
482
|
592
|
{
|
|
|
593
|
+ Print(" Tier 1. ");
|
|
483
|
594
|
for(int i = 0; i < MaxOrders; i++)
|
|
484
|
595
|
{
|
|
485
|
596
|
// treat slot as empty when both tickets are -1 (same convention as constructor)
|
|
486
|
597
|
if(newTradeStore[i].buy_ticket == -1 && newTradeStore[i].sell_ticket == -1)
|
|
487
|
598
|
{
|
|
488
|
|
- newTradeStore[i].buy_ticket = r_buy_ticket;
|
|
489
|
|
- newTradeStore[i].sell_ticket = r_sell_ticket;
|
|
490
|
|
- newTradeStore[i].symbol = r_symbol;
|
|
491
|
|
- newTradeStore[i].price = r_price;
|
|
492
|
|
- newTradeStore[i].stop_loss = r_stop_loss;
|
|
493
|
|
- newTradeStore[i].take_profit = r_take_profit;
|
|
494
|
|
- newTradeStore[i].start_time = r_start_time;
|
|
495
|
|
- newTradeStore[i].end_time = r_end_time;
|
|
496
|
|
-
|
|
497
|
|
- Print("Stored -> idx: ", i,
|
|
498
|
|
- " | sym: ", newTradeStore[i].symbol,
|
|
499
|
|
- " | price: ", DoubleToString(newTradeStore[i].price, Digits()),
|
|
500
|
|
- " | sl: ", DoubleToString(newTradeStore[i].stop_loss, Digits()),
|
|
501
|
|
- " | tp: ", DoubleToString(newTradeStore[i].take_profit, Digits()),
|
|
502
|
|
- " | start: ", TimeToString(newTradeStore[i].start_time, TIME_DATE|TIME_SECONDS),
|
|
503
|
|
- " | end: ", TimeToString(newTradeStore[i].end_time, TIME_DATE|TIME_SECONDS));
|
|
504
|
|
- break;
|
|
|
599
|
+ if(newTradeStore[i].price == 0)
|
|
|
600
|
+ {
|
|
|
601
|
+ newTradeStore[i].buy_ticket = r_buy_ticket;
|
|
|
602
|
+ newTradeStore[i].sell_ticket = r_sell_ticket;
|
|
|
603
|
+ newTradeStore[i].symbol = r_symbol;
|
|
|
604
|
+ newTradeStore[i].price = r_price;
|
|
|
605
|
+ newTradeStore[i].stop_loss = r_stop_loss;
|
|
|
606
|
+ newTradeStore[i].take_profit = r_take_profit;
|
|
|
607
|
+ newTradeStore[i].start_time = r_start_time;
|
|
|
608
|
+ newTradeStore[i].end_time = r_end_time;
|
|
|
609
|
+ newTradeStore[i].buy_hit_virtual_sl = r_buy_hit_sl;
|
|
|
610
|
+ newTradeStore[i].sell_hit_virtual_sl = r_sell_hit_sl;
|
|
|
611
|
+
|
|
|
612
|
+ Print("Stored -> idx: ", i,
|
|
|
613
|
+ " | Symbol: ", newTradeStore[i].symbol,
|
|
|
614
|
+ " | price: ", DoubleToString(newTradeStore[i].price, Digits()),
|
|
|
615
|
+ " | sl: ", DoubleToString(newTradeStore[i].stop_loss, Digits()),
|
|
|
616
|
+ " | tp: ", DoubleToString(newTradeStore[i].take_profit, Digits()),
|
|
|
617
|
+ " | start: ", TimeToString(newTradeStore[i].start_time, TIME_DATE|TIME_SECONDS),
|
|
|
618
|
+ " | end: ", TimeToString(newTradeStore[i].end_time, TIME_DATE|TIME_SECONDS),
|
|
|
619
|
+ " | Buy Virtal Sl Hit: ", newTradeStore[i].buy_hit_virtual_sl,
|
|
|
620
|
+ " | Sell Virtual Sl Hit: ", newTradeStore[i].sell_hit_virtual_sl);
|
|
|
621
|
+ break;
|
|
|
622
|
+ }
|
|
505
|
623
|
}
|
|
506
|
624
|
}
|
|
507
|
625
|
}
|
|
|
@@ -518,19 +636,22 @@ void tradePlacingCheck()
|
|
518
|
636
|
{
|
|
519
|
637
|
if(TimeCurrent() > newTradeStore[i].start_time && TimeCurrent() < newTradeStore[i].end_time)
|
|
520
|
638
|
{
|
|
521
|
|
- double levelPriceIs = newTradeStore[i].price;
|
|
522
|
|
- if((tickPreviousBid > levelPriceIs && tickCurrentBid < levelPriceIs) ||
|
|
523
|
|
- (tickPreviousBid < levelPriceIs && tickCurrentBid > levelPriceIs))
|
|
|
639
|
+ if(newTradeStore[i].buy_hit_virtual_sl == true && newTradeStore[i].sell_hit_virtual_sl == true)
|
|
524
|
640
|
{
|
|
525
|
|
- if(countLiveTrades() < maxTrades)
|
|
|
641
|
+ double levelPriceIs = newTradeStore[i].price;
|
|
|
642
|
+ if((tickPreviousBid > levelPriceIs && tickCurrentBid < levelPriceIs) ||
|
|
|
643
|
+ (tickPreviousBid < levelPriceIs && tickCurrentBid > levelPriceIs))
|
|
526
|
644
|
{
|
|
527
|
|
- if((enableSpreadFilter && spreadFilter()) || !enableSpreadFilter)
|
|
|
645
|
+ if(countLiveTrades() < maxTrades)
|
|
528
|
646
|
{
|
|
529
|
|
- ulong buyTicket = placeBuyTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
|
|
530
|
|
- ulong sellTicket = placeSellTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
|
|
|
647
|
+ if((enableSpreadFilter && spreadFilter()) || !enableSpreadFilter)
|
|
|
648
|
+ {
|
|
|
649
|
+ ulong buyTicket = placeBuyTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
|
|
|
650
|
+ ulong sellTicket = placeSellTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
|
|
531
|
651
|
|
|
532
|
|
- newTradeStore[i].buy_ticket = buyTicket;
|
|
533
|
|
- newTradeStore[i].sell_ticket = sellTicket;
|
|
|
652
|
+ newTradeStore[i].buy_ticket = buyTicket;
|
|
|
653
|
+ newTradeStore[i].sell_ticket = sellTicket;
|
|
|
654
|
+ }
|
|
534
|
655
|
}
|
|
535
|
656
|
}
|
|
536
|
657
|
}
|
|
|
@@ -754,7 +875,7 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
|
|
754
|
875
|
//+------------------------------------------------------------------+
|
|
755
|
876
|
//| |
|
|
756
|
877
|
//+------------------------------------------------------------------+
|
|
757
|
|
-void removeBuySellOnSlHit()
|
|
|
878
|
+void removeFromStructure()
|
|
758
|
879
|
{
|
|
759
|
880
|
for(int i = 0 ; i < MaxOrders ; i++)
|
|
760
|
881
|
{
|
|
|
@@ -787,18 +908,24 @@ void removeBuySellOnSlHit()
|
|
787
|
908
|
|
|
788
|
909
|
if(!isBuyPresent && !isSellPresent)
|
|
789
|
910
|
{
|
|
790
|
|
- if(tradeHitSL(newTradeStore[i].buy_ticket) && tradeHitSL(newTradeStore[i].sell_ticket))
|
|
|
911
|
+ Print("Buy/Sell Ticket is closed so removed from struct. Buy Ticket: ", newTradeStore[i].buy_ticket, " Sell Ticket: ", newTradeStore[i].sell_ticket);
|
|
|
912
|
+ newTradeStore[i].buy_ticket = (ulong)-1;
|
|
|
913
|
+ newTradeStore[i].sell_ticket = (ulong)-1;
|
|
|
914
|
+ bool buy_virtual_tp_hit = true;
|
|
|
915
|
+ bool sell_virtual_tp_hit = true;
|
|
|
916
|
+ if(bothHitsSl)
|
|
791
|
917
|
{
|
|
792
|
|
- Print("Buy/Sell Ticket is closed on StopLoss so removed from struct. Buy Ticket: ", newTradeStore[i].buy_ticket, " Sell Ticket: ", newTradeStore[i].sell_ticket);
|
|
793
|
|
- newTradeStore[i].buy_ticket = (ulong)-1;
|
|
794
|
|
- newTradeStore[i].sell_ticket = (ulong)-1;
|
|
795
|
|
- //newTradeStore[i].symbol = "";
|
|
796
|
|
- //newTradeStore[i].price = 0.0;
|
|
797
|
|
- //newTradeStore[i].stop_loss = 0.0;
|
|
798
|
|
- //newTradeStore[i].take_profit = 0.0;
|
|
799
|
|
- //newTradeStore[i].start_time = 0;
|
|
800
|
|
- //newTradeStore[i].end_time = 0;
|
|
|
918
|
+ buy_virtual_tp_hit = false;
|
|
|
919
|
+ sell_virtual_tp_hit = false;
|
|
801
|
920
|
}
|
|
|
921
|
+ newTradeStore[i].buy_hit_virtual_sl = buy_virtual_tp_hit;
|
|
|
922
|
+ newTradeStore[i].sell_hit_virtual_sl = sell_virtual_tp_hit;
|
|
|
923
|
+ //newTradeStore[i].symbol = "";
|
|
|
924
|
+ //newTradeStore[i].price = 0.0;
|
|
|
925
|
+ //newTradeStore[i].stop_loss = 0.0;
|
|
|
926
|
+ //newTradeStore[i].take_profit = 0.0;
|
|
|
927
|
+ //newTradeStore[i].start_time = 0;
|
|
|
928
|
+ //newTradeStore[i].end_time = 0;
|
|
802
|
929
|
}
|
|
803
|
930
|
}
|
|
804
|
931
|
}
|
|
|
@@ -806,33 +933,35 @@ void removeBuySellOnSlHit()
|
|
806
|
933
|
//+------------------------------------------------------------------+
|
|
807
|
934
|
//| |
|
|
808
|
935
|
//+------------------------------------------------------------------+
|
|
809
|
|
-bool tradeHitSL(ulong positionID)
|
|
|
936
|
+void virtualSLHitCheck()
|
|
810
|
937
|
{
|
|
811
|
|
-
|
|
812
|
|
- if(HistorySelectByPosition(positionID))
|
|
|
938
|
+ for(int i = 0 ; i < MaxOrders ; i++)
|
|
813
|
939
|
{
|
|
814
|
|
- for(int i = 0; i < HistoryDealsTotal(); i++)
|
|
|
940
|
+ if(newTradeStore[i].buy_ticket == -1 && newTradeStore[i].sell_ticket == -1)
|
|
815
|
941
|
{
|
|
816
|
|
- ulong dealTicket = HistoryDealGetTicket(i);
|
|
817
|
|
- ulong dealPositionID = HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID);
|
|
818
|
|
-
|
|
819
|
|
- if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT && dealPositionID == positionID)
|
|
|
942
|
+ if(TimeCurrent() > newTradeStore[i].start_time && TimeCurrent() < newTradeStore[i].end_time)
|
|
820
|
943
|
{
|
|
821
|
|
- int dealType = (int)HistoryDealGetInteger(dealTicket, DEAL_TYPE);
|
|
822
|
|
- string comment = HistoryDealGetString(dealTicket, DEAL_COMMENT);
|
|
823
|
|
-
|
|
824
|
|
- if(StringFind(comment, "sl", 0) >= 0)
|
|
|
944
|
+ double buy_sl = newTradeStore[i].price - (newTradeStore[i].stop_loss * Point());
|
|
|
945
|
+ double sell_sl = newTradeStore[i].price + (newTradeStore[i].stop_loss * Point());
|
|
|
946
|
+ if(newTradeStore[i].buy_hit_virtual_sl == false)
|
|
825
|
947
|
{
|
|
826
|
|
- // if(dealType == DEAL_TYPE_BUY)
|
|
827
|
|
- // {
|
|
828
|
|
- //
|
|
829
|
|
- // }
|
|
830
|
|
- return true;
|
|
|
948
|
+ if((tickPreviousBid < buy_sl && tickCurrentBid > buy_sl))
|
|
|
949
|
+ {
|
|
|
950
|
+ newTradeStore[i].buy_hit_virtual_sl = true;
|
|
|
951
|
+ Print(" Buy Virtual Sl Hit. newTradeStore[i].buy_hit_virtual_sl: ", newTradeStore[i].buy_hit_virtual_sl, " Order Price is: ", newTradeStore[i].price, " Stop Loss Price is: ", buy_sl, " Time Virtual Sl Hit is: ", TimeCurrent());
|
|
|
952
|
+ }
|
|
|
953
|
+ }
|
|
|
954
|
+ if(newTradeStore[i].sell_hit_virtual_sl == false)
|
|
|
955
|
+ {
|
|
|
956
|
+ if((tickPreviousAsk > sell_sl && tickCurrentAsk < sell_sl))
|
|
|
957
|
+ {
|
|
|
958
|
+ newTradeStore[i].sell_hit_virtual_sl = true;
|
|
|
959
|
+ Print(" Sell Virtual Sl Hit. newTradeStore[i].sell_hit_virtual_sl: ", newTradeStore[i].sell_hit_virtual_sl, " Order Price is: ", newTradeStore[i].price, " Stop Loss Price is: ", sell_sl, " Time Virtual Sl Hit is: ", TimeCurrent());
|
|
|
960
|
+ }
|
|
831
|
961
|
}
|
|
832
|
962
|
}
|
|
833
|
963
|
}
|
|
834
|
964
|
}
|
|
835
|
|
- return false;
|
|
836
|
965
|
}
|
|
837
|
966
|
//+------------------------------------------------------------------+
|
|
838
|
967
|
//| |
|
|
|
@@ -875,6 +1004,63 @@ bool spreadFilter()
|
|
875
|
1004
|
//+------------------------------------------------------------------+
|
|
876
|
1005
|
//| |
|
|
877
|
1006
|
//+------------------------------------------------------------------+
|
|
|
1007
|
+void breakEven()
|
|
|
1008
|
+ {
|
|
|
1009
|
+ for(int i = PositionsTotal()-1; i>=0; i--)
|
|
|
1010
|
+ {
|
|
|
1011
|
+ ulong ticket = PositionGetTicket(i);
|
|
|
1012
|
+ string symbol=PositionGetSymbol(i);
|
|
|
1013
|
+ double mySL = 0,newSL = 0;
|
|
|
1014
|
+ double SymbolTickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
|
|
|
1015
|
+ ////Print("ticket ",ticket," Symbol : ",symbol);
|
|
|
1016
|
+ if(PositionSelectByTicket(ticket))
|
|
|
1017
|
+ {
|
|
|
1018
|
+ if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC) == magic_no)
|
|
|
1019
|
+ {
|
|
|
1020
|
+ //========================================================Buy Condition=========================================================================
|
|
|
1021
|
+ if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
|
|
|
1022
|
+ {
|
|
|
1023
|
+ mySL = PositionGetDouble(POSITION_PRICE_OPEN) + (breakEvenPoints* 10 * SymbolTickSize);
|
|
|
1024
|
+ if(SymbolInfoDouble(Symbol(),SYMBOL_BID) >= mySL && PositionGetDouble(POSITION_PRICE_OPEN) > PositionGetDouble(POSITION_SL))
|
|
|
1025
|
+ {
|
|
|
1026
|
+ newSL= PositionGetDouble(POSITION_PRICE_OPEN) + (breakStopPoint* 10 * SymbolTickSize);
|
|
|
1027
|
+ if(trade.PositionModify(ticket,newSL,PositionGetDouble(POSITION_TP)))
|
|
|
1028
|
+ {
|
|
|
1029
|
+
|
|
|
1030
|
+ Print("Buy Order BreakEven Successfully ");
|
|
|
1031
|
+ }
|
|
|
1032
|
+ else
|
|
|
1033
|
+ {
|
|
|
1034
|
+ Print("Error in BreakEven Buy Position ",GetLastError());
|
|
|
1035
|
+ }
|
|
|
1036
|
+ }
|
|
|
1037
|
+ }
|
|
|
1038
|
+ //=======================================================Sell condition ===============================================================
|
|
|
1039
|
+ if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
|
|
|
1040
|
+ {
|
|
|
1041
|
+ mySL = PositionGetDouble(POSITION_PRICE_OPEN) - (breakEvenPoints* 10 * SymbolTickSize);
|
|
|
1042
|
+ if(SymbolInfoDouble(Symbol(),SYMBOL_ASK) <= mySL && PositionGetDouble(POSITION_SL) > PositionGetDouble(POSITION_PRICE_OPEN))
|
|
|
1043
|
+ {
|
|
|
1044
|
+ newSL = PositionGetDouble(POSITION_PRICE_OPEN) - (breakStopPoint* 10 * SymbolTickSize);
|
|
|
1045
|
+
|
|
|
1046
|
+ if(trade.PositionModify(ticket,newSL,PositionGetDouble(POSITION_TP)))
|
|
|
1047
|
+ {
|
|
|
1048
|
+ Print("Order Sell BreakEven Successfully ");
|
|
|
1049
|
+ }
|
|
|
1050
|
+ else
|
|
|
1051
|
+ {
|
|
|
1052
|
+ Print("Error in BreakEven Sell Position ",GetLastError());
|
|
|
1053
|
+ }
|
|
|
1054
|
+ }
|
|
|
1055
|
+ }
|
|
|
1056
|
+ }
|
|
|
1057
|
+ }
|
|
|
1058
|
+
|
|
|
1059
|
+ }
|
|
|
1060
|
+ }
|
|
|
1061
|
+//+------------------------------------------------------------------+
|
|
|
1062
|
+//| |
|
|
|
1063
|
+//+------------------------------------------------------------------+
|
|
878
|
1064
|
void Individual_Trailing()
|
|
879
|
1065
|
{
|
|
880
|
1066
|
int openedpositions;
|
|
|
@@ -934,5 +1120,190 @@ void Individual_Trailing()
|
|
934
|
1120
|
//+------------------------------------------------------------------+
|
|
935
|
1121
|
//| |
|
|
936
|
1122
|
//+------------------------------------------------------------------+
|
|
|
1123
|
+void saveNewTradeStoreFile()
|
|
|
1124
|
+ {
|
|
|
1125
|
+// don't run in strategy tester
|
|
|
1126
|
+ if(MQLInfoInteger(MQL_TESTER))
|
|
|
1127
|
+ return;
|
|
|
1128
|
+
|
|
|
1129
|
+ string file_name = "new_trade_store.csv";
|
|
|
1130
|
+ int fileHandle = FileOpen(file_name,
|
|
|
1131
|
+ FILE_WRITE | FILE_CSV | FILE_COMMON |
|
|
|
1132
|
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_READ);
|
|
|
1133
|
+ if(fileHandle == INVALID_HANDLE)
|
|
|
1134
|
+ {
|
|
|
1135
|
+ PrintFormat("saveNewTradeStoreFile() -> Cannot open file '%s'. Error: %d", file_name, GetLastError());
|
|
|
1136
|
+ return;
|
|
|
1137
|
+ }
|
|
|
1138
|
+
|
|
|
1139
|
+// header (optional) - comment out if you don't want header
|
|
|
1140
|
+//string header = "buy_ticket,sell_ticket,symbol,price,stop_loss,take_profit,start_time,end_time,buy_hit_virtual_sl,sell_hit_virtual_sl\r\n";
|
|
|
1141
|
+//FileWriteString(fileHandle, header);
|
|
|
1142
|
+
|
|
|
1143
|
+ for(int i = 0; i < MaxOrders; i++)
|
|
|
1144
|
+ {
|
|
|
1145
|
+ // decide whether this slot has useful data:
|
|
|
1146
|
+ // you can tweak this condition as you prefer (symbol != "" is simple)
|
|
|
1147
|
+ bool slotPopulated = (StringLen(newTradeStore[i].symbol) > 0)
|
|
|
1148
|
+ || (newTradeStore[i].price != 0.0)
|
|
|
1149
|
+ || (newTradeStore[i].start_time != 0);
|
|
|
1150
|
+
|
|
|
1151
|
+ if(!slotPopulated)
|
|
|
1152
|
+ continue;
|
|
|
1153
|
+
|
|
|
1154
|
+ // convert values to strings
|
|
|
1155
|
+ string buyTicketStr = IntegerToString((long)newTradeStore[i].buy_ticket);
|
|
|
1156
|
+ string sellTicketStr = IntegerToString((long)newTradeStore[i].sell_ticket);
|
|
|
1157
|
+ string priceStr = DoubleToString(newTradeStore[i].price, _Digits);
|
|
|
1158
|
+ string slStr = DoubleToString(newTradeStore[i].stop_loss, _Digits);
|
|
|
1159
|
+ string tpStr = DoubleToString(newTradeStore[i].take_profit, _Digits);
|
|
|
1160
|
+
|
|
|
1161
|
+ string startTimeStr = (newTradeStore[i].start_time != 0) ? TimeToString(newTradeStore[i].start_time, TIME_DATE|TIME_SECONDS) : "";
|
|
|
1162
|
+ string endTimeStr = (newTradeStore[i].end_time != 0) ? TimeToString(newTradeStore[i].end_time, TIME_DATE|TIME_SECONDS) : "";
|
|
|
1163
|
+
|
|
|
1164
|
+ string buyHitStr = ""; // IntegerToString(newTradeStore[i].buy_hit_virtual_sl ? 1 : 0);
|
|
|
1165
|
+ string sellHitStr = ""; // IntegerToString(newTradeStore[i].sell_hit_virtual_sl ? 1 : 0);
|
|
|
1166
|
+
|
|
|
1167
|
+ if(newTradeStore[i].buy_hit_virtual_sl == true)
|
|
|
1168
|
+ {
|
|
|
1169
|
+ buyHitStr = "true";
|
|
|
1170
|
+ }
|
|
|
1171
|
+ else
|
|
|
1172
|
+ {
|
|
|
1173
|
+ buyHitStr = "false";
|
|
|
1174
|
+ }
|
|
|
1175
|
+
|
|
|
1176
|
+ if(newTradeStore[i].sell_hit_virtual_sl == true)
|
|
|
1177
|
+ {
|
|
|
1178
|
+ sellHitStr = "true";
|
|
|
1179
|
+ }
|
|
|
1180
|
+ else
|
|
|
1181
|
+ {
|
|
|
1182
|
+ sellHitStr = "false";
|
|
|
1183
|
+ }
|
|
|
1184
|
+
|
|
|
1185
|
+ // build CSV line and write
|
|
|
1186
|
+ string line = buyTicketStr + "," + sellTicketStr + "," +
|
|
|
1187
|
+ newTradeStore[i].symbol + "," +
|
|
|
1188
|
+ priceStr + "," + slStr + "," + tpStr + "," +
|
|
|
1189
|
+ startTimeStr + "," + endTimeStr + "," +
|
|
|
1190
|
+ buyHitStr + "," + sellHitStr + "\r\n";
|
|
|
1191
|
+
|
|
|
1192
|
+ FileWriteString(fileHandle, line);
|
|
|
1193
|
+ }
|
|
|
1194
|
+
|
|
|
1195
|
+ FileClose(fileHandle);
|
|
|
1196
|
+ PrintFormat("saveNewTradeStoreFile() -> saved to '%s'.", file_name);
|
|
|
1197
|
+ }
|
|
|
1198
|
+//+------------------------------------------------------------------+
|
|
|
1199
|
+//| |
|
|
|
1200
|
+//+------------------------------------------------------------------+
|
|
|
1201
|
+void loadNewTradeStoreFile()
|
|
|
1202
|
+ {
|
|
|
1203
|
+ if(MQLInfoInteger(MQL_TESTER))
|
|
|
1204
|
+ return;
|
|
|
1205
|
+
|
|
|
1206
|
+ string file_name = "new_trade_store.csv";
|
|
|
1207
|
+ int fileHandle = FileOpen(file_name,
|
|
|
1208
|
+ FILE_READ | FILE_CSV | FILE_COMMON |
|
|
|
1209
|
+ FILE_SHARE_READ | FILE_SHARE_WRITE);
|
|
|
1210
|
+ if(fileHandle == INVALID_HANDLE)
|
|
|
1211
|
+ {
|
|
|
1212
|
+ Print("loadNewTradeStoreFile() -> Cannot open file '", file_name, "'. Error: ", GetLastError());
|
|
|
1213
|
+ return;
|
|
|
1214
|
+ }
|
|
|
1215
|
+
|
|
|
1216
|
+// reset defaults
|
|
|
1217
|
+ for(int j = 0; j < MaxOrders; j++)
|
|
|
1218
|
+ {
|
|
|
1219
|
+ newTradeStore[j].buy_ticket = (ulong)-1;
|
|
|
1220
|
+ newTradeStore[j].sell_ticket = (ulong)-1;
|
|
|
1221
|
+ newTradeStore[j].symbol = "";
|
|
|
1222
|
+ newTradeStore[j].price = 0.0;
|
|
|
1223
|
+ newTradeStore[j].stop_loss = 0.0;
|
|
|
1224
|
+ newTradeStore[j].take_profit = 0.0;
|
|
|
1225
|
+ newTradeStore[j].start_time = 0;
|
|
|
1226
|
+ newTradeStore[j].end_time = 0;
|
|
|
1227
|
+ newTradeStore[j].buy_hit_virtual_sl = false;
|
|
|
1228
|
+ newTradeStore[j].sell_hit_virtual_sl = false;
|
|
|
1229
|
+ }
|
|
|
1230
|
+
|
|
|
1231
|
+ int idx = 0;
|
|
|
1232
|
+ while(!FileIsEnding(fileHandle) && idx < MaxOrders)
|
|
|
1233
|
+ {
|
|
|
1234
|
+ string line = FileReadString(fileHandle);
|
|
|
1235
|
+ if(StringLen(line) == 0)
|
|
|
1236
|
+ continue;
|
|
|
1237
|
+
|
|
|
1238
|
+ string tokens[];
|
|
|
1239
|
+ int total = StringSplit(line, ',', tokens);
|
|
|
1240
|
+
|
|
|
1241
|
+ if(total >= 10)
|
|
|
1242
|
+ {
|
|
|
1243
|
+ newTradeStore[idx].buy_ticket = (ulong)tokens[0];
|
|
|
1244
|
+ newTradeStore[idx].sell_ticket = (ulong)tokens[1];
|
|
|
1245
|
+ newTradeStore[idx].symbol = tokens[2];
|
|
|
1246
|
+ newTradeStore[idx].price = StringToDouble(tokens[3]);
|
|
|
1247
|
+ newTradeStore[idx].stop_loss = StringToDouble(tokens[4]);
|
|
|
1248
|
+ newTradeStore[idx].take_profit = StringToDouble(tokens[5]);
|
|
|
1249
|
+
|
|
|
1250
|
+ string sStart = tokens[6];
|
|
|
1251
|
+ string sEnd = tokens[7];
|
|
|
1252
|
+ newTradeStore[idx].start_time = (StringLen(sStart) > 0) ? StringToTime(sStart) : 0;
|
|
|
1253
|
+ newTradeStore[idx].end_time = (StringLen(sEnd) > 0) ? StringToTime(sEnd) : 0;
|
|
|
1254
|
+
|
|
|
1255
|
+ bool bHit = false;
|
|
|
1256
|
+ bool sHit = false;
|
|
|
1257
|
+
|
|
|
1258
|
+ if(tokens[8] == "true")
|
|
|
1259
|
+ {
|
|
|
1260
|
+ bHit = true;
|
|
|
1261
|
+ }
|
|
|
1262
|
+ else
|
|
|
1263
|
+ {
|
|
|
1264
|
+ bHit = false;
|
|
|
1265
|
+ }
|
|
|
1266
|
+
|
|
|
1267
|
+ if(tokens[9] == "true")
|
|
|
1268
|
+ {
|
|
|
1269
|
+ sHit = true;
|
|
|
1270
|
+ }
|
|
|
1271
|
+ else
|
|
|
1272
|
+ {
|
|
|
1273
|
+ sHit = false;
|
|
|
1274
|
+ }
|
|
|
1275
|
+
|
|
|
1276
|
+ newTradeStore[idx].buy_hit_virtual_sl = bHit;
|
|
|
1277
|
+ newTradeStore[idx].sell_hit_virtual_sl = sHit;
|
|
|
1278
|
+
|
|
|
1279
|
+ // --- simple Print instead of PrintFormat ---
|
|
|
1280
|
+ Print(
|
|
|
1281
|
+ "Loaded newTradeStore[", idx, "]:",
|
|
|
1282
|
+ " buy_ticket=", newTradeStore[idx].buy_ticket,
|
|
|
1283
|
+ " sell_ticket=", newTradeStore[idx].sell_ticket,
|
|
|
1284
|
+ " symbol=", newTradeStore[idx].symbol,
|
|
|
1285
|
+ " price=", DoubleToString(newTradeStore[idx].price, _Digits),
|
|
|
1286
|
+ " \n sl=", DoubleToString(newTradeStore[idx].stop_loss, _Digits),
|
|
|
1287
|
+ " tp=", DoubleToString(newTradeStore[idx].take_profit, _Digits),
|
|
|
1288
|
+ " start=", (newTradeStore[idx].start_time != 0 ? TimeToString(newTradeStore[idx].start_time, TIME_DATE|TIME_SECONDS) : ""),
|
|
|
1289
|
+ " end=", (newTradeStore[idx].end_time != 0 ? TimeToString(newTradeStore[idx].end_time, TIME_DATE|TIME_SECONDS) : ""),
|
|
|
1290
|
+ " buyHit=", newTradeStore[idx].buy_hit_virtual_sl,
|
|
|
1291
|
+ " sellHit=", newTradeStore[idx].sell_hit_virtual_sl
|
|
|
1292
|
+ );
|
|
|
1293
|
+
|
|
|
1294
|
+ idx++;
|
|
|
1295
|
+ }
|
|
|
1296
|
+ else
|
|
|
1297
|
+ {
|
|
|
1298
|
+ Print("loadNewTradeStoreFile(): skipping malformed line (tokens=", total, "): ", line);
|
|
|
1299
|
+ }
|
|
|
1300
|
+ }
|
|
|
1301
|
+
|
|
|
1302
|
+ FileClose(fileHandle);
|
|
|
1303
|
+ Print("loadNewTradeStoreFile() -> loaded ", idx, " entries from '", file_name, "'.");
|
|
|
1304
|
+ }
|
|
|
1305
|
+//+------------------------------------------------------------------+
|
|
|
1306
|
+//| |
|
|
|
1307
|
+//+------------------------------------------------------------------+
|
|
937
|
1308
|
|
|
938
|
1309
|
//+------------------------------------------------------------------+
|