Bladeren bron

Merge branch 'update/tp-sl-and-csv-changes' of huzaifa.hayat/vol_hedge_strategy_mt5 into master

Wajeeh Saqib 4 maanden geleden
bovenliggende
commit
6cab7a1a4a
3 gewijzigde bestanden met toevoegingen van 235 en 123 verwijderingen
  1. 3 2
      data/vol_hedge_data.csv
  2. BIN
      vol_hedge_strategy_mt5.ex5
  3. 232 121
      vol_hedge_strategy_mt5.mq5

+ 3 - 2
data/vol_hedge_data.csv

@@ -1,2 +1,3 @@
1
-EURUSDm,1.08384,1000,1000,2023.03.22 02:05:25,2023.03.24 02:05:25
2
-EURUSDm,1.08734,400,400,2023.03.30 00:00:00,2023.04.04 02:05:25
1
+Symbol,Price,Buy StopLoss,Buy TakeProfit,Sell StopLoss,Sell TakeProfit,StartTime,EndTime
2
+EURUSDm,1.08384,1.07384,1.09384,1.09384,1.07384,2023.03.22 02:05:25,2024.03.24 02:05:25
3
+EURUSDm,1.08734,1.07734,1.09734,1.09734,1.07734,2023.03.30 00:00:00,2024.04.04 02:05:25

BIN
vol_hedge_strategy_mt5.ex5


+ 232 - 121
vol_hedge_strategy_mt5.mq5

@@ -5,7 +5,7 @@
5 5
 //+------------------------------------------------------------------+
6 6
 #property copyright "Copyright 2025, MQL Development"
7 7
 #property link      "https://www.mqldevelopment.com/"
8
-#property version   "1.00"
8
+#property version   "1.10"
9 9
 #define MaxOrders 100
10 10
 #include <Trade\Trade.mqh>
11 11
 CTrade  trade;
@@ -22,6 +22,11 @@ enum selectLine
22 22
    LineOnNewsBar =0,
23 23
    LineOnNewsStop=1
24 24
   };
25
+enum selectDay
26
+  {
27
+   prev,    // Previous Day
28
+   curr,    // Current Day
29
+  };
25 30
 
26 31
 #import "volHedgeNewsFilter.ex5"
27 32
 
@@ -61,8 +66,10 @@ struct new_trade_store
61 66
    ulong             sell_ticket;           // Sell Ticket
62 67
    string            symbol;                // Symbol
63 68
    double            price;                 // Price
64
-   double            stop_loss;             // StopLoss
65
-   double            take_profit;           // TakeProfit
69
+   double            stop_loss_buy;         // StopLoss Buy
70
+   double            take_profit_buy;       // TakeProfit Buy
71
+   double            stop_loss_sell;        // StopLoss Sell
72
+   double            take_profit_sell;      // TakeProfit Sell
66 73
    datetime          start_time;            // Start time
67 74
    datetime          end_time;              // End Time
68 75
    bool              buy_hit_virtual_sl;    // Buy Hit Virtual StopLoss
@@ -83,18 +90,21 @@ new_trade_store newTradeStore[MaxOrders];
83 90
 enum lotcalculator
84 91
   {
85 92
    fix, //Fixed Lot Size
86
-   rsk, //Risk Percentage
93
+   rsk, //Risk in Percentage
87 94
    dollar, // Risk in Dollars
88 95
   };
89 96
 
90 97
 sinput       string                  string_0                   = "<><><><><><> General SETTINGS <><><><><><>";   //__
91 98
 input        int                     magic_no                   = 333;             // Magic no
92
-input        bool                    bothHitsSl                 = false;           // Open after Both Hits StopLoss
93
-input        int                     maxTrades                  = 2;               // Max Concurrent Trades
94
-input        int                     maxSlippage                = 5;               // Max Slippage
99
+input        bool                    useTpSlPips                = false;           // Use Relative Tp/Sl in Pips
100
+input        double                  stopLoss                   = 100;             // Fixed Stop Loss in Pips
101
+input        double                  takeProfit                 = 100;             // Fixed Take Profit in Pips
102
+input        bool                    bothHitsSl                 = false;           // Enable Topped & Tailed Pre-Demand Level
103
+input        int                     maxSlippage                = 5;               // Max Slippage (Points)
95 104
 input        bool                    enableSpreadFilter         = false;           // Enable Spread Filter
96
-input        double                  maximumSpread              = 10;              // Maximum Spread
97
-input        string                  dataFileName               = "vol_hedge_data.csv"; // Data File Name
105
+input        double                  maximumSpread              = 10;              // Maximum Spread (Points)
106
+input        string                  tradeComment               = "Trade Placed";  // Trade Comment Prefix
107
+input        string                  dataFileName               = "vol_hedge_data.csv"; // CSV File Name
98 108
 
99 109
 input        string                  string_1                   = "<><><><><><> Lot Management<><><><><><>";   //__
100 110
 input        lotcalculator           lot_calculator             = fix;             // Lot Size Option
@@ -102,10 +112,11 @@ input        double                  lot_amount                 = 0.1;
102 112
 input        double                  risk                       = 0.5;             // Risk in Percentage %
103 113
 input        double                  dollars                    = 10;              // Risk in GBP
104 114
 
105
-input        string                  string_2                   = "<><><><><><> Time Filter Setting <><><><><><> ";//_
106
-input        bool                    enableTimeSession          = false;           // Enable Time Session
107
-input        string                  start_time_session         = "01:00";         // Start Session
108
-input        string                  end_time_session           = "23:59";         // End Session
115
+input        string                  time_setting               = "<><><><><> Time Filter Settings <><><><><>";             //_
116
+input        selectDay               newYorkSessionDay          = curr;            // Select Day for Start Time
117
+input        bool                    enableTimeFilter           = false;           // Enable Time Filter
118
+input        string                  startTime                  = "03:00";         // Start Time Session
119
+input        string                  endTime                    = "09:00";         // End Time Session
109 120
 
110 121
 input        string                  string_0_2                 = "<><><><><><> Trailing Setting<><><><><><>";   //__
111 122
 input        bool                    indivial_trailing          = false;           // Indiviual Trailing
@@ -125,21 +136,23 @@ input        int                     High_Stop_Time             = 15;
125 136
 input        bool                    show_high_line             = true;                 //Show verticle Line when high news comes
126 137
 input        selectLine              Select_News_Line           = 0;                    //News Line
127 138
 input        bool                    mobileAlert                = true;                 //Mobile Alert
128
-input        bool                    Medium_Impact_News         = false;                // Medium Impact News
139
+input        bool                    Medium_Impact_News         = true;                 // Medium Impact News
129 140
 input        int                     Medium_Start_Time          = 60;                   // Stop Trade before medium News (min)
130 141
 input        int                     Medium_Stop_Time           = 15;                   // Stop Trade after medium News (min)
131
-input        bool                    show_medium_line           = false;                // Show vertical Line when medium news comes
132
-input        bool                    Low_Impact_News            = false;                // Low Impact News
142
+input        bool                    show_medium_line           = true;                 // Show vertical Line when medium news comes
143
+input        bool                    Low_Impact_News            = true;                 // Low Impact News
133 144
 input        int                     Low_Start_Time             = 60;                   // Stop Trade before low News (min)
134 145
 input        int                     Low_Stop_Time              = 15;                   // Stop Trade after low News (min)
135
-input        bool                    show_low_line              = false;                // Show vertical Line when low news comes
146
+input        bool                    show_low_line              = true;                 // Show vertical Line when low news comes
136 147
 
137 148
 // Global Variables
138 149
 static double tickCurrentBid = 0;
139 150
 double tickPreviousBid = 0;
140 151
 static double tickCurrentAsk = 0;
141 152
 double tickPreviousAsk = 0;
142
-datetime startSessionTime, endSessionTime;
153
+int newYorkStartTime = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
154
+datetime newYorkStartTrading = 0, newYorkEndTrading = 0;
155
+int maxTrades = 2; // Max Concurrent Trades
143 156
 int GMT_Broker_Time = +2; // GMT_Broker_Time Time of your Broker
144 157
 int                     gmt                        = 0;                   // GMT_Broker_Time Time of your Broker
145 158
 string Lname="newsLabel3";
@@ -179,7 +192,7 @@ int OnInit()
179 192
             Print(" Order Data: ", orderData[i], " i: ", i);
180 193
            }
181 194
 
182
-         if(ArraySize(orderData) >= 6)
195
+         if(ArraySize(orderData) >= 8)
183 196
            {
184 197
             if(orderData[0] == Symbol())
185 198
               {
@@ -188,11 +201,13 @@ int OnInit()
188 201
                ulong  sell_ticket_local = (ulong)-1;
189 202
                string symbol_local      = orderData[0];
190 203
                double price_local       = StringToDouble(orderData[1]);
191
-               double sl_local          = StringToDouble(orderData[2]);
192
-               double tp_local          = StringToDouble(orderData[3]);
204
+               double sl_buy_local      = StringToDouble(orderData[2]);
205
+               double tp_buy_local      = StringToDouble(orderData[3]);
206
+               double sl_sell_local     = StringToDouble(orderData[4]);
207
+               double tp_sell_local     = StringToDouble(orderData[5]);
193 208
                // if your CSV has extra fields (tp2,tp3, etc.) parse here as needed
194
-               datetime start_local     = StringToTime(orderData[4]);
195
-               datetime end_local       = StringToTime(orderData[5]);
209
+               datetime start_local     = StringToTime(orderData[6]);
210
+               datetime end_local       = StringToTime(orderData[7]);
196 211
 
197 212
                ArrayResize(levelsAre,ArraySize(levelsAre)+1);
198 213
                levelsAre[ArraySize(levelsAre) - 1] = price_local;
@@ -212,7 +227,8 @@ int OnInit()
212 227
                  {
213 228
                   addToNewTradeStore(buy_ticket_local, sell_ticket_local,
214 229
                                      symbol_local, price_local,
215
-                                     sl_local, tp_local,
230
+                                     sl_buy_local, tp_buy_local,
231
+                                     sl_sell_local, tp_sell_local,
216 232
                                      start_local, end_local,
217 233
                                      buy_virtual_tp_hit, sell_virtual_tp_hit);
218 234
                  }
@@ -232,8 +248,19 @@ int OnInit()
232 248
 
233 249
    struct_level_check();
234 250
 
235
-   timeFilter(true,start_time_session, end_time_session, startSessionTime, endSessionTime);
236
-   Print(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
251
+   string time[];
252
+   StringSplit(startTime,':',time);
253
+   newYorkStartTime = (int)StringToInteger(time[0]);
254
+   newYorkStartMin  = (int)StringToInteger(time[1]);
255
+   Print("NewYork Start Time Hour: ",newYorkStartTime,"  Start Time Min: ",newYorkStartMin);
256
+   time[0] = "";
257
+   time[1] = "";
258
+   StringSplit(endTime,':',time);
259
+   newYorkEndHour = (int)StringToInteger(time[0]);
260
+   newYorkEndMin  = (int)StringToInteger(time[1]);
261
+   Print("NewYork End Time Hour: ",newYorkEndHour,"  End Time Min: ",newYorkEndMin);
262
+   timeFilter(true);
263
+
237 264
 
238 265
    int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
239 266
    Print("Time Difference is: ", timeDifference);
@@ -394,6 +421,7 @@ void OnTick()
394 421
 void mainActivity()
395 422
   {
396 423
 //---
424
+   newBar();
397 425
    if(indivial_trailing)
398 426
      {
399 427
       Individual_Trailing();
@@ -427,10 +455,9 @@ void mainActivity()
427 455
       tickCurrentAsk  = Ask;
428 456
      }
429 457
 
430
-// Print(" Time is: ", TimeCurrent());
431
-   timeFilter(false,start_time_session, end_time_session, startSessionTime, endSessionTime);
432
-// Comment(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
433
-   if((!enableTimeSession) || (enableTimeSession && TimeCurrent() >= startSessionTime && TimeCurrent() <= endSessionTime))
458
+   timeFilter(false);
459
+//   Comment(" Session Start  = ", newYorkStartTrading,  " Asian  Session End = ", newYorkEndTrading);
460
+   if((enableTimeFilter && TimeCurrent() >= newYorkStartTrading && TimeCurrent() <= newYorkEndTrading) || !enableTimeFilter)
434 461
      {
435 462
       removeFromStructure();
436 463
       if(bothHitsSl)
@@ -441,6 +468,22 @@ void mainActivity()
441 468
      }
442 469
   }
443 470
 //+------------------------------------------------------------------+
471
+bool newBar()
472
+  {
473
+   static datetime lastbar;
474
+   datetime curbar = iTime(Symbol(), PERIOD_CURRENT, 0);
475
+   if(lastbar != curbar)
476
+     {
477
+      lastbar = curbar;
478
+      Print(" ---------------------- New Bar :: ---------------------- ",lastbar);
479
+      return (true);
480
+     }
481
+   else
482
+     {
483
+      return (false);
484
+     }
485
+  }
486
+
444 487
 //+------------------------------------------------------------------+
445 488
 //|                                                                  |
446 489
 //+------------------------------------------------------------------+
@@ -566,14 +609,16 @@ void struct_level_check()
566 609
                buy_virtual_tp_hit = false;
567 610
                sell_virtual_tp_hit = false;
568 611
               }
569
-            newTradeStore[i].buy_hit_virtual_sl = buy_virtual_tp_hit;
612
+            newTradeStore[i].buy_hit_virtual_sl  = buy_virtual_tp_hit;
570 613
             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;
614
+            newTradeStore[i].symbol           = "";
615
+            newTradeStore[i].price            = 0.0;
616
+            newTradeStore[i].stop_loss_buy    = 0.0;
617
+            newTradeStore[i].take_profit_buy  = 0.0;
618
+            newTradeStore[i].stop_loss_sell   = 0.0;
619
+            newTradeStore[i].take_profit_sell = 0.0;
620
+            newTradeStore[i].start_time       = 0;
621
+            newTradeStore[i].end_time         = 0;
577 622
             return;
578 623
            }
579 624
         }
@@ -587,7 +632,8 @@ void struct_level_check()
587 632
 //+------------------------------------------------------------------+
588 633
 void addToNewTradeStore(ulong r_buy_ticket, ulong r_sell_ticket,
589 634
                         string r_symbol, double r_price,
590
-                        double r_stop_loss, double r_take_profit,
635
+                        double r_stop_loss_buy, double r_take_profit_buy,
636
+                        double r_stop_loss_sell, double r_take_profit_sell,
591 637
                         datetime r_start_time, datetime r_end_time, bool r_buy_hit_sl, bool r_sell_hit_sl)
592 638
   {
593 639
    Print(" Tier 1. ");
@@ -602,8 +648,10 @@ void addToNewTradeStore(ulong r_buy_ticket, ulong r_sell_ticket,
602 648
             newTradeStore[i].sell_ticket = r_sell_ticket;
603 649
             newTradeStore[i].symbol      = r_symbol;
604 650
             newTradeStore[i].price       = r_price;
605
-            newTradeStore[i].stop_loss   = r_stop_loss;
606
-            newTradeStore[i].take_profit = r_take_profit;
651
+            newTradeStore[i].stop_loss_buy   = r_stop_loss_buy;
652
+            newTradeStore[i].take_profit_buy = r_take_profit_buy;
653
+            newTradeStore[i].stop_loss_sell   = r_stop_loss_sell;
654
+            newTradeStore[i].take_profit_sell = r_take_profit_sell;
607 655
             newTradeStore[i].start_time  = r_start_time;
608 656
             newTradeStore[i].end_time    = r_end_time;
609 657
             newTradeStore[i].buy_hit_virtual_sl = r_buy_hit_sl;
@@ -612,8 +660,10 @@ void addToNewTradeStore(ulong r_buy_ticket, ulong r_sell_ticket,
612 660
             Print("Stored -> idx: ", i,
613 661
                   " | Symbol: ", newTradeStore[i].symbol,
614 662
                   " | price: ", DoubleToString(newTradeStore[i].price, Digits()),
615
-                  " | sl: ", DoubleToString(newTradeStore[i].stop_loss, Digits()),
616
-                  " | tp: ", DoubleToString(newTradeStore[i].take_profit, Digits()),
663
+                  " | Sl Buy: ", DoubleToString(newTradeStore[i].stop_loss_buy, Digits()),
664
+                  " | Tp Buy: ", DoubleToString(newTradeStore[i].take_profit_buy, Digits()),
665
+                  "\n | Sl Sell: ", DoubleToString(newTradeStore[i].stop_loss_sell, Digits()),
666
+                  " | Tp Sell: ", DoubleToString(newTradeStore[i].take_profit_sell, Digits()),
617 667
                   " | start: ", TimeToString(newTradeStore[i].start_time, TIME_DATE|TIME_SECONDS),
618 668
                   " | end: ", TimeToString(newTradeStore[i].end_time, TIME_DATE|TIME_SECONDS),
619 669
                   " | Buy Virtal Sl Hit: ", newTradeStore[i].buy_hit_virtual_sl,
@@ -642,12 +692,12 @@ void tradePlacingCheck()
642 692
                   if((tickPreviousBid > levelPriceIs && tickCurrentBid < levelPriceIs) ||
643 693
                      (tickPreviousBid < levelPriceIs && tickCurrentBid > levelPriceIs))
644 694
                     {
645
-                     if(countLiveTrades() < maxTrades)
695
+                     if(countLiveTrades() == 0)
646 696
                        {
647 697
                         if((enableSpreadFilter && spreadFilter()) || !enableSpreadFilter)
648 698
                           {
649
-                           ulong buyTicket = placeBuyTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
650
-                           ulong sellTicket = placeSellTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
699
+                           ulong buyTicket = placeBuyTrade(newTradeStore[i].stop_loss_buy, newTradeStore[i].take_profit_buy);
700
+                           ulong sellTicket = placeSellTrade(newTradeStore[i].stop_loss_sell, newTradeStore[i].take_profit_sell);
651 701
 
652 702
                            newTradeStore[i].buy_ticket = buyTicket;
653 703
                            newTradeStore[i].sell_ticket = sellTicket;
@@ -671,17 +721,31 @@ ulong placeBuyTrade(double stoploss, double takeprofit)
671 721
    double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
672 722
    double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
673 723
 
674
-   if(stoploss != 0)
724
+   if(useTpSlPips)
675 725
      {
676
-      buySL = Ask - (stoploss * Point());
726
+      if(stopLoss != 0)
727
+        {
728
+         buySL = Ask - (stopLoss * 10 * Point());
729
+        }
730
+      if(takeProfit != 0)
731
+        {
732
+         buyTp = Ask + (takeProfit * 10 * Point());
733
+        }
677 734
      }
678
-   if(takeprofit != 0)
735
+   else
679 736
      {
680
-      buyTp = Ask + (takeprofit * Point());
737
+      if(stoploss > 0)
738
+        {
739
+         buySL = stoploss;
740
+        }
741
+      if(takeprofit > 0)
742
+        {
743
+         buyTp = takeprofit;
744
+        }
681 745
      }
682 746
 
683 747
    double distance = MathAbs((Ask - buySL) / Point());
684
-   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getLot(distance),Ask,buySL,buyTp,"Buy Trade Placed"))
748
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getLot(distance),Ask,buySL,buyTp,tradeComment+" Buy"))
685 749
      {
686 750
       Print("Buy Trade Placed: ",trade.ResultOrder());
687 751
       return trade.ResultOrder();
@@ -703,16 +767,30 @@ ulong placeSellTrade(double stoploss, double takeprofit)
703 767
    double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
704 768
    double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
705 769
 
706
-   if(stoploss != 0)
770
+   if(useTpSlPips)
707 771
      {
708
-      sellSL = Bid + (stoploss * Point());
772
+      if(stopLoss != 0)
773
+        {
774
+         sellSL = Bid + (stopLoss * 10 * Point());
775
+        }
776
+      if(takeProfit != 0)
777
+        {
778
+         sellTp = Bid - (takeProfit * 10 * Point());
779
+        }
709 780
      }
710
-   if(takeprofit != 0)
781
+   else
711 782
      {
712
-      sellTp = Bid - (takeprofit * Point());
783
+      if(stoploss > 0)
784
+        {
785
+         sellSL = stoploss;
786
+        }
787
+      if(takeprofit > 0)
788
+        {
789
+         sellTp = takeprofit;
790
+        }
713 791
      }
714 792
    double distance = MathAbs((Bid - sellSL) / Point());
715
-   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getLot(distance),Bid,sellSL,sellTp,"Sell Trade Placed"))
793
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getLot(distance),Bid,sellSL,sellTp,tradeComment+ " Sell"))
716 794
      {
717 795
       Print("Sell Trade PLaced: ",trade.ResultOrder());
718 796
       return trade.ResultOrder();
@@ -783,49 +861,11 @@ double getLot(double stop_loss)
783 861
 //+------------------------------------------------------------------+
784 862
 //|                                                                  |
785 863
 //+------------------------------------------------------------------+
786
-void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionStart,datetime & sessionEnd)
864
+void timeFilter(bool onInit)
787 865
   {
788
-   int newYorkStartHour = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
789
-
790
-   datetime newYorkStartTrading,newYorkEndTrading;
791
-
792
-   string time[];
793
-   StringSplit(startTime,':',time);
794
-   newYorkStartHour = (int)StringToInteger(time[0]);
795
-   newYorkStartMin  = (int)StringToInteger(time[1]);
796
-
797
-   EventSetMillisecondTimer(500);
798
-   time[0] = "";
799
-   time[1] = "";
800
-   StringSplit(endTime,':',time);
801
-   newYorkEndHour = (int)StringToInteger(time[0]);
802
-   newYorkEndMin  = (int)StringToInteger(time[1]);
803
-
804
-// Print(" Start Time Hour: ",newYorkStartHour,"  Start Time Min: ",newYorkStartMin);
805
-// Print(" End Time Hour: ",newYorkEndHour,"  End Time Min: ",newYorkEndMin);
806
-
807
-
808
-   datetime startDateTime;
809
-   MqlDateTime st;
810
-   TimeCurrent(st); // get current date
811
-   st.hour   = newYorkStartHour;
812
-   st.min    = newYorkStartMin;
813
-   st.sec    = 0;
814
-   startDateTime = StructToTime(st);
815
-
816
-
817
-   datetime endDateTime;
818
-   MqlDateTime et;
819
-   TimeCurrent(et); // get current date
820
-   et.hour   = newYorkEndHour;
821
-   et.min    = newYorkEndMin;
822
-   et.sec    = 0;
823
-   endDateTime = StructToTime(et);
824
-
825
-
826 866
    MqlDateTime sdate,edate;
827 867
    datetime start_Time = 0, end_Time = 0;
828
-   if(startDateTime > endDateTime)
868
+   if(newYorkSessionDay == prev)
829 869
      {
830 870
       if(onInit)
831 871
         {
@@ -834,9 +874,9 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
834 874
         }
835 875
       else
836 876
         {
837
-         start_Time = sessionStart;
838
-         end_Time = sessionEnd;
839
-         if(TimeCurrent() >= sessionEnd && sessionEnd != 0)
877
+         start_Time = newYorkStartTrading;
878
+         end_Time = newYorkEndTrading;
879
+         if(TimeCurrent() >= newYorkEndTrading && newYorkEndTrading != 0)
840 880
            {
841 881
             start_Time = iTime(Symbol(),PERIOD_D1,0);
842 882
             end_Time = start_Time + 86400;
@@ -848,7 +888,6 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
848 888
       start_Time = iTime(Symbol(),PERIOD_D1,0);
849 889
       end_Time = iTime(Symbol(),PERIOD_D1,0);
850 890
      }
851
-
852 891
    if(TimeToStruct(end_Time,edate))
853 892
      {
854 893
       edate.hour = newYorkEndHour;
@@ -861,7 +900,7 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
861 900
 
862 901
    if(TimeToStruct(start_Time,sdate))
863 902
      {
864
-      sdate.hour = newYorkStartHour;
903
+      sdate.hour = newYorkStartTime;
865 904
       sdate.min  = newYorkStartMin;
866 905
       sdate.sec  = 0;
867 906
      }
@@ -869,8 +908,9 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
869 908
       Print("Error in Converting Time: ",GetLastError());
870 909
    newYorkStartTrading = StructToTime(sdate);
871 910
 
872
-   sessionStart = newYorkStartTrading;
873
-   sessionEnd   = newYorkEndTrading;
911
+// if(onInit)
912
+//Print("NewYork Start Time ",newYorkStartTrading,"End Date: ",newYorkEndTrading);
913
+//Print("Edate: ",edate.hour," ",edate.min," Sdate: ",sdate.hour," ",sdate.min);
874 914
   }
875 915
 //+------------------------------------------------------------------+
876 916
 //|                                                                  |
@@ -941,8 +981,17 @@ void virtualSLHitCheck()
941 981
         {
942 982
          if(TimeCurrent() > newTradeStore[i].start_time && TimeCurrent() < newTradeStore[i].end_time)
943 983
            {
944
-            double buy_sl = newTradeStore[i].price - (newTradeStore[i].stop_loss * Point());
945
-            double sell_sl = newTradeStore[i].price + (newTradeStore[i].stop_loss * Point());
984
+            double buy_sl = 0, sell_sl = 0;
985
+            if(!useTpSlPips)
986
+              {
987
+               buy_sl = newTradeStore[i].stop_loss_buy;
988
+               sell_sl = newTradeStore[i].stop_loss_sell;
989
+              }
990
+            else
991
+              {
992
+               buy_sl = stopLoss != 0 ? newTradeStore[i].price - (stopLoss * 10 * Point()) : 0;
993
+               sell_sl = stopLoss != 0 ? newTradeStore[i].price + (stopLoss * 10 * Point()) : 0;
994
+              }
946 995
             if(newTradeStore[i].buy_hit_virtual_sl == false)
947 996
               {
948 997
                if((tickPreviousBid < buy_sl && tickCurrentBid > buy_sl))
@@ -1017,6 +1066,11 @@ void breakEven()
1017 1066
         {
1018 1067
          if(PositionGetString(POSITION_SYMBOL)==Symbol()  && PositionGetInteger(POSITION_MAGIC) == magic_no)
1019 1068
            {
1069
+            if(isCounterpartOpen(ticket))
1070
+              {
1071
+               // counterpart still open -> skip trailing for this position
1072
+               continue;
1073
+              }
1020 1074
             //========================================================Buy Condition=========================================================================
1021 1075
             if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
1022 1076
               {
@@ -1061,6 +1115,48 @@ void breakEven()
1061 1115
 //+------------------------------------------------------------------+
1062 1116
 //|                                                                  |
1063 1117
 //+------------------------------------------------------------------+
1118
+bool isCounterpartOpen(ulong ticket)
1119
+  {
1120
+// scan stored pairs
1121
+   for(int k=0; k<MaxOrders; k++)
1122
+     {
1123
+      // check if this ticket is the buy side in the pair
1124
+      if(newTradeStore[k].buy_ticket == ticket)
1125
+        {
1126
+         ulong other = newTradeStore[k].sell_ticket;
1127
+         if(other <= 0)
1128
+            return false; // no counterpart recorded -> treat as closed
1129
+         // check if counterpart ticket exists in current positions
1130
+         for(int p = PositionsTotal()-1; p >= 0; p--)
1131
+           {
1132
+            ulong t = PositionGetTicket(p);
1133
+            if(t == other)
1134
+               return true; // counterpart still open
1135
+           }
1136
+         return false; // counterpart not found -> closed
1137
+        }
1138
+
1139
+      // check if this ticket is the sell side in the pair
1140
+      if(newTradeStore[k].sell_ticket == ticket)
1141
+        {
1142
+         ulong other = newTradeStore[k].buy_ticket;
1143
+         if(other <= 0)
1144
+            return false; // no counterpart recorded -> treat as closed
1145
+         for(int p = PositionsTotal()-1; p >= 0; p--)
1146
+           {
1147
+            ulong t = PositionGetTicket(p);
1148
+            if(t == other)
1149
+               return true; // counterpart still open
1150
+           }
1151
+         return false; // counterpart not found -> closed
1152
+        }
1153
+     }
1154
+// ticket not found in the stored pairs -> treat as no counterpart open
1155
+   return false;
1156
+  }
1157
+//+------------------------------------------------------------------+
1158
+//|                                                                  |
1159
+//+------------------------------------------------------------------+
1064 1160
 void Individual_Trailing()
1065 1161
   {
1066 1162
    int openedpositions;
@@ -1075,13 +1171,19 @@ void Individual_Trailing()
1075 1171
          ulong ticket = PositionGetTicket(i);
1076 1172
          if(PositionSelectByTicket(ticket))
1077 1173
            {
1078
-            if(PositionGetString(POSITION_SYMBOL)==Symbol()  && PositionGetInteger(POSITION_MAGIC) ==magic_no) // only look if mygrid and symbol. ..
1174
+            if(PositionGetString(POSITION_SYMBOL)==Symbol()  && PositionGetInteger(POSITION_MAGIC) == magic_no) // only look if mygrid and symbol. ..
1079 1175
               {
1176
+               if(isCounterpartOpen(ticket))
1177
+                 {
1178
+                  // counterpart still open -> skip trailing for this position
1179
+                  continue;
1180
+                 }
1080 1181
 
1081 1182
                double SymbolAsk           = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_ASK);
1082 1183
                double SymbolBid           = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_BID);
1083 1184
                int SymbolDigits           = (int)SymbolInfoInteger(PositionGetString(POSITION_SYMBOL), SYMBOL_DIGITS);
1084 1185
                double SymbolTickSize      = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_TRADE_TICK_SIZE);
1186
+
1085 1187
                int type= (int)PositionGetInteger(POSITION_TYPE);
1086 1188
 
1087 1189
                if(type==POSITION_TYPE_BUY) // its a long position
@@ -1154,9 +1256,11 @@ void saveNewTradeStoreFile()
1154 1256
       // convert values to strings
1155 1257
       string buyTicketStr  = IntegerToString((long)newTradeStore[i].buy_ticket);
1156 1258
       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);
1259
+      string priceStr      = DoubleToString(newTradeStore[i].price, Digits());
1260
+      string slBuyStr      = DoubleToString(newTradeStore[i].stop_loss_buy, Digits());
1261
+      string tpBuyStr      = DoubleToString(newTradeStore[i].take_profit_buy, Digits());
1262
+      string slSellStr      = DoubleToString(newTradeStore[i].stop_loss_sell, Digits());
1263
+      string tpSellStr      = DoubleToString(newTradeStore[i].take_profit_sell, Digits());
1160 1264
 
1161 1265
       string startTimeStr  = (newTradeStore[i].start_time != 0) ? TimeToString(newTradeStore[i].start_time, TIME_DATE|TIME_SECONDS) : "";
1162 1266
       string endTimeStr    = (newTradeStore[i].end_time   != 0) ? TimeToString(newTradeStore[i].end_time, TIME_DATE|TIME_SECONDS)   : "";
@@ -1185,7 +1289,8 @@ void saveNewTradeStoreFile()
1185 1289
       // build CSV line and write
1186 1290
       string line = buyTicketStr + "," + sellTicketStr + "," +
1187 1291
                     newTradeStore[i].symbol + "," +
1188
-                    priceStr + "," + slStr + "," + tpStr + "," +
1292
+                    priceStr + "," + slBuyStr + "," + tpBuyStr + "," +
1293
+                    slSellStr + "," + tpSellStr + "," +
1189 1294
                     startTimeStr + "," + endTimeStr + "," +
1190 1295
                     buyHitStr + "," + sellHitStr + "\r\n";
1191 1296
 
@@ -1220,8 +1325,10 @@ void loadNewTradeStoreFile()
1220 1325
       newTradeStore[j].sell_ticket          = (ulong)-1;
1221 1326
       newTradeStore[j].symbol               = "";
1222 1327
       newTradeStore[j].price                = 0.0;
1223
-      newTradeStore[j].stop_loss            = 0.0;
1224
-      newTradeStore[j].take_profit          = 0.0;
1328
+      newTradeStore[j].stop_loss_buy        = 0.0;
1329
+      newTradeStore[j].take_profit_buy      = 0.0;
1330
+      newTradeStore[j].stop_loss_sell       = 0.0;
1331
+      newTradeStore[j].take_profit_sell     = 0.0;
1225 1332
       newTradeStore[j].start_time           = 0;
1226 1333
       newTradeStore[j].end_time             = 0;
1227 1334
       newTradeStore[j].buy_hit_virtual_sl   = false;
@@ -1238,24 +1345,26 @@ void loadNewTradeStoreFile()
1238 1345
       string tokens[];
1239 1346
       int total = StringSplit(line, ',', tokens);
1240 1347
 
1241
-      if(total >= 10)
1348
+      if(total >= 12)
1242 1349
         {
1243 1350
          newTradeStore[idx].buy_ticket  = (ulong)tokens[0];
1244 1351
          newTradeStore[idx].sell_ticket = (ulong)tokens[1];
1245 1352
          newTradeStore[idx].symbol      = tokens[2];
1246 1353
          newTradeStore[idx].price       = StringToDouble(tokens[3]);
1247
-         newTradeStore[idx].stop_loss   = StringToDouble(tokens[4]);
1248
-         newTradeStore[idx].take_profit = StringToDouble(tokens[5]);
1354
+         newTradeStore[idx].stop_loss_buy   = StringToDouble(tokens[4]);
1355
+         newTradeStore[idx].take_profit_buy = StringToDouble(tokens[5]);
1356
+         newTradeStore[idx].stop_loss_sell  = StringToDouble(tokens[6]);
1357
+         newTradeStore[idx].take_profit_sell = StringToDouble(tokens[7]);
1249 1358
 
1250
-         string sStart = tokens[6];
1251
-         string sEnd   = tokens[7];
1359
+         string sStart = tokens[8];
1360
+         string sEnd   = tokens[9];
1252 1361
          newTradeStore[idx].start_time = (StringLen(sStart) > 0) ? StringToTime(sStart) : 0;
1253 1362
          newTradeStore[idx].end_time   = (StringLen(sEnd)   > 0) ? StringToTime(sEnd)   : 0;
1254 1363
 
1255 1364
          bool bHit = false;
1256 1365
          bool sHit = false;
1257 1366
 
1258
-         if(tokens[8] == "true")
1367
+         if(tokens[10] == "true")
1259 1368
            {
1260 1369
             bHit = true;
1261 1370
            }
@@ -1264,7 +1373,7 @@ void loadNewTradeStoreFile()
1264 1373
             bHit = false;
1265 1374
            }
1266 1375
 
1267
-         if(tokens[9] == "true")
1376
+         if(tokens[11] == "true")
1268 1377
            {
1269 1378
             sHit = true;
1270 1379
            }
@@ -1282,9 +1391,11 @@ void loadNewTradeStoreFile()
1282 1391
             " buy_ticket=", newTradeStore[idx].buy_ticket,
1283 1392
             " sell_ticket=", newTradeStore[idx].sell_ticket,
1284 1393
             " 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),
1394
+            " price=", DoubleToString(newTradeStore[idx].price, Digits()),
1395
+            " \n sl buy=", DoubleToString(newTradeStore[idx].stop_loss_buy, Digits()),
1396
+            " tp buy=", DoubleToString(newTradeStore[idx].take_profit_buy, Digits()),
1397
+            " sl sell=", DoubleToString(newTradeStore[idx].stop_loss_sell, Digits()),
1398
+            " tp sell=", DoubleToString(newTradeStore[idx].take_profit_sell, Digits()),
1288 1399
             " start=", (newTradeStore[idx].start_time != 0 ? TimeToString(newTradeStore[idx].start_time, TIME_DATE|TIME_SECONDS) : ""),
1289 1400
             " end=", (newTradeStore[idx].end_time   != 0 ? TimeToString(newTradeStore[idx].end_time,   TIME_DATE|TIME_SECONDS) : ""),
1290 1401
             " buyHit=", newTradeStore[idx].buy_hit_virtual_sl,