Quellcode durchsuchen

Time Filter Logic Changed

Huzaifa-MQLDev vor 4 Monaten
Ursprung
Commit
c0726e226c
2 geänderte Dateien mit 51 neuen und 71 gelöschten Zeilen
  1. BIN
      vol_hedge_strategy_mt5.ex5
  2. 51 71
      vol_hedge_strategy_mt5.mq5

BIN
vol_hedge_strategy_mt5.ex5


+ 51 - 71
vol_hedge_strategy_mt5.mq5

@@ -22,6 +22,11 @@ enum selectLine
22
    LineOnNewsBar =0,
22
    LineOnNewsBar =0,
23
    LineOnNewsStop=1
23
    LineOnNewsStop=1
24
   };
24
   };
25
+enum selectDay
26
+  {
27
+   prev,    // Previous Day
28
+   curr,    // Current Day
29
+  };
25
 
30
 
26
 #import "volHedgeNewsFilter.ex5"
31
 #import "volHedgeNewsFilter.ex5"
27
 
32
 
@@ -85,19 +90,20 @@ new_trade_store newTradeStore[MaxOrders];
85
 enum lotcalculator
90
 enum lotcalculator
86
   {
91
   {
87
    fix, //Fixed Lot Size
92
    fix, //Fixed Lot Size
88
-   rsk, //Risk Percentage
93
+   rsk, //Risk in Percentage
89
    dollar, // Risk in Dollars
94
    dollar, // Risk in Dollars
90
   };
95
   };
91
 
96
 
92
 sinput       string                  string_0                   = "<><><><><><> General SETTINGS <><><><><><>";   //__
97
 sinput       string                  string_0                   = "<><><><><><> General SETTINGS <><><><><><>";   //__
93
 input        int                     magic_no                   = 333;             // Magic no
98
 input        int                     magic_no                   = 333;             // Magic no
94
-input        bool                    useTpSlPips                = true;            // Use Tp/Sl in Pips
99
+input        bool                    useTpSlPips                = true;            // Use Relative Tp/Sl in Pips
95
 input        double                  stopLoss                   = 100;             // Fixed Stop Loss in Pips
100
 input        double                  stopLoss                   = 100;             // Fixed Stop Loss in Pips
96
 input        double                  takeProfit                 = 100;             // Fixed Take Profit in Pips
101
 input        double                  takeProfit                 = 100;             // Fixed Take Profit in Pips
97
-input        bool                    bothHitsSl                 = false;           // Open after Both Hits StopLoss
98
-input        int                     maxSlippage                = 5;               // Max Slippage
102
+input        bool                    bothHitsSl                 = false;           // Enable Topped & Tailed Pre-Demand Level
103
+input        int                     maxSlippage                = 5;               // Max Slippage (Points)
99
 input        bool                    enableSpreadFilter         = false;           // Enable Spread Filter
104
 input        bool                    enableSpreadFilter         = false;           // Enable Spread Filter
100
-input        double                  maximumSpread              = 10;              // Maximum Spread
105
+input        double                  maximumSpread              = 10;              // Maximum Spread (Points)
106
+input        string                  tradeComment               = "Trade Placed";  // Trade Comment Prefix
101
 input        string                  dataFileName               = "vol_hedge_data.csv"; // Data File Name
107
 input        string                  dataFileName               = "vol_hedge_data.csv"; // Data File Name
102
 
108
 
103
 input        string                  string_1                   = "<><><><><><> Lot Management<><><><><><>";   //__
109
 input        string                  string_1                   = "<><><><><><> Lot Management<><><><><><>";   //__
@@ -106,10 +112,11 @@ input        double                  lot_amount                 = 0.1;
106
 input        double                  risk                       = 0.5;             // Risk in Percentage %
112
 input        double                  risk                       = 0.5;             // Risk in Percentage %
107
 input        double                  dollars                    = 10;              // Risk in GBP
113
 input        double                  dollars                    = 10;              // Risk in GBP
108
 
114
 
109
-input        string                  string_2                   = "<><><><><><> Time Filter Setting <><><><><><> ";//_
110
-input        bool                    enableTimeSession          = false;           // Enable Time Session
111
-input        string                  start_time_session         = "01:00";         // Start Session
112
-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
113
 
120
 
114
 input        string                  string_0_2                 = "<><><><><><> Trailing Setting<><><><><><>";   //__
121
 input        string                  string_0_2                 = "<><><><><><> Trailing Setting<><><><><><>";   //__
115
 input        bool                    indivial_trailing          = false;           // Indiviual Trailing
122
 input        bool                    indivial_trailing          = false;           // Indiviual Trailing
@@ -143,7 +150,8 @@ static double tickCurrentBid = 0;
143
 double tickPreviousBid = 0;
150
 double tickPreviousBid = 0;
144
 static double tickCurrentAsk = 0;
151
 static double tickCurrentAsk = 0;
145
 double tickPreviousAsk = 0;
152
 double tickPreviousAsk = 0;
146
-datetime startSessionTime, endSessionTime;
153
+int newYorkStartTime = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
154
+datetime newYorkStartTrading = 0, newYorkEndTrading = 0;
147
 int maxTrades = 2; // Max Concurrent Trades
155
 int maxTrades = 2; // Max Concurrent Trades
148
 int GMT_Broker_Time = +2; // GMT_Broker_Time Time of your Broker
156
 int GMT_Broker_Time = +2; // GMT_Broker_Time Time of your Broker
149
 int                     gmt                        = 0;                   // GMT_Broker_Time Time of your Broker
157
 int                     gmt                        = 0;                   // GMT_Broker_Time Time of your Broker
@@ -240,8 +248,19 @@ int OnInit()
240
 
248
 
241
    struct_level_check();
249
    struct_level_check();
242
 
250
 
243
-   timeFilter(true,start_time_session, end_time_session, startSessionTime, endSessionTime);
244
-   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
+
245
 
264
 
246
    int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
265
    int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
247
    Print("Time Difference is: ", timeDifference);
266
    Print("Time Difference is: ", timeDifference);
@@ -435,10 +454,9 @@ void mainActivity()
435
       tickCurrentAsk  = Ask;
454
       tickCurrentAsk  = Ask;
436
      }
455
      }
437
 
456
 
438
-// Print(" Time is: ", TimeCurrent());
439
-   timeFilter(false,start_time_session, end_time_session, startSessionTime, endSessionTime);
440
-// Comment(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
441
-   if((!enableTimeSession) || (enableTimeSession && TimeCurrent() >= startSessionTime && TimeCurrent() <= endSessionTime))
457
+   timeFilter(false);
458
+// Comment(" Session Start  = ", newYorkStartTrading,  " Asian  Session End = ", newYorkEndTrading);
459
+   if((enableTimeFilter && TimeCurrent() >= newYorkStartTrading && TimeCurrent() <= newYorkEndTrading) || !enableTimeFilter)
442
      {
460
      {
443
       removeFromStructure();
461
       removeFromStructure();
444
       if(bothHitsSl)
462
       if(bothHitsSl)
@@ -688,11 +706,11 @@ ulong placeBuyTrade(double stoploss, double takeprofit)
688
 
706
 
689
    if(useTpSlPips)
707
    if(useTpSlPips)
690
      {
708
      {
691
-      if(stoploss != 0)
709
+      if(stopLoss != 0)
692
         {
710
         {
693
          buySL = Ask - (stopLoss * 10 * Point());
711
          buySL = Ask - (stopLoss * 10 * Point());
694
         }
712
         }
695
-      if(takeprofit != 0)
713
+      if(takeProfit != 0)
696
         {
714
         {
697
          buyTp = Ask + (takeProfit * 10 * Point());
715
          buyTp = Ask + (takeProfit * 10 * Point());
698
         }
716
         }
@@ -710,7 +728,7 @@ ulong placeBuyTrade(double stoploss, double takeprofit)
710
      }
728
      }
711
 
729
 
712
    double distance = MathAbs((Ask - buySL) / Point());
730
    double distance = MathAbs((Ask - buySL) / Point());
713
-   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getLot(distance),Ask,buySL,buyTp,"Buy Trade Placed"))
731
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getLot(distance),Ask,buySL,buyTp,tradeComment+" Buy"))
714
      {
732
      {
715
       Print("Buy Trade Placed: ",trade.ResultOrder());
733
       Print("Buy Trade Placed: ",trade.ResultOrder());
716
       return trade.ResultOrder();
734
       return trade.ResultOrder();
@@ -734,11 +752,11 @@ ulong placeSellTrade(double stoploss, double takeprofit)
734
 
752
 
735
    if(useTpSlPips)
753
    if(useTpSlPips)
736
      {
754
      {
737
-      if(stoploss != 0)
755
+      if(stopLoss != 0)
738
         {
756
         {
739
          sellSL = Bid + (stopLoss * 10 * Point());
757
          sellSL = Bid + (stopLoss * 10 * Point());
740
         }
758
         }
741
-      if(takeprofit != 0)
759
+      if(takeProfit != 0)
742
         {
760
         {
743
          sellTp = Bid - (takeProfit * 10 * Point());
761
          sellTp = Bid - (takeProfit * 10 * Point());
744
         }
762
         }
@@ -755,7 +773,7 @@ ulong placeSellTrade(double stoploss, double takeprofit)
755
         }
773
         }
756
      }
774
      }
757
    double distance = MathAbs((Bid - sellSL) / Point());
775
    double distance = MathAbs((Bid - sellSL) / Point());
758
-   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getLot(distance),Bid,sellSL,sellTp,"Sell Trade Placed"))
776
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getLot(distance),Bid,sellSL,sellTp,tradeComment+ " Sell"))
759
      {
777
      {
760
       Print("Sell Trade PLaced: ",trade.ResultOrder());
778
       Print("Sell Trade PLaced: ",trade.ResultOrder());
761
       return trade.ResultOrder();
779
       return trade.ResultOrder();
@@ -826,49 +844,11 @@ double getLot(double stop_loss)
826
 //+------------------------------------------------------------------+
844
 //+------------------------------------------------------------------+
827
 //|                                                                  |
845
 //|                                                                  |
828
 //+------------------------------------------------------------------+
846
 //+------------------------------------------------------------------+
829
-void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionStart,datetime & sessionEnd)
847
+void timeFilter(bool onInit)
830
   {
848
   {
831
-   int newYorkStartHour = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
832
-
833
-   datetime newYorkStartTrading,newYorkEndTrading;
834
-
835
-   string time[];
836
-   StringSplit(startTime,':',time);
837
-   newYorkStartHour = (int)StringToInteger(time[0]);
838
-   newYorkStartMin  = (int)StringToInteger(time[1]);
839
-
840
-   EventSetMillisecondTimer(500);
841
-   time[0] = "";
842
-   time[1] = "";
843
-   StringSplit(endTime,':',time);
844
-   newYorkEndHour = (int)StringToInteger(time[0]);
845
-   newYorkEndMin  = (int)StringToInteger(time[1]);
846
-
847
-// Print(" Start Time Hour: ",newYorkStartHour,"  Start Time Min: ",newYorkStartMin);
848
-// Print(" End Time Hour: ",newYorkEndHour,"  End Time Min: ",newYorkEndMin);
849
-
850
-
851
-   datetime startDateTime;
852
-   MqlDateTime st;
853
-   TimeCurrent(st); // get current date
854
-   st.hour   = newYorkStartHour;
855
-   st.min    = newYorkStartMin;
856
-   st.sec    = 0;
857
-   startDateTime = StructToTime(st);
858
-
859
-
860
-   datetime endDateTime;
861
-   MqlDateTime et;
862
-   TimeCurrent(et); // get current date
863
-   et.hour   = newYorkEndHour;
864
-   et.min    = newYorkEndMin;
865
-   et.sec    = 0;
866
-   endDateTime = StructToTime(et);
867
-
868
-
869
    MqlDateTime sdate,edate;
849
    MqlDateTime sdate,edate;
870
    datetime start_Time = 0, end_Time = 0;
850
    datetime start_Time = 0, end_Time = 0;
871
-   if(startDateTime > endDateTime)
851
+   if(newYorkSessionDay == prev)
872
      {
852
      {
873
       if(onInit)
853
       if(onInit)
874
         {
854
         {
@@ -877,9 +857,9 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
877
         }
857
         }
878
       else
858
       else
879
         {
859
         {
880
-         start_Time = sessionStart;
881
-         end_Time = sessionEnd;
882
-         if(TimeCurrent() >= sessionEnd && sessionEnd != 0)
860
+         start_Time = newYorkStartTrading;
861
+         end_Time = newYorkEndTrading;
862
+         if(TimeCurrent() >= newYorkEndTrading && newYorkEndTrading != 0)
883
            {
863
            {
884
             start_Time = iTime(Symbol(),PERIOD_D1,0);
864
             start_Time = iTime(Symbol(),PERIOD_D1,0);
885
             end_Time = start_Time + 86400;
865
             end_Time = start_Time + 86400;
@@ -891,7 +871,6 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
891
       start_Time = iTime(Symbol(),PERIOD_D1,0);
871
       start_Time = iTime(Symbol(),PERIOD_D1,0);
892
       end_Time = iTime(Symbol(),PERIOD_D1,0);
872
       end_Time = iTime(Symbol(),PERIOD_D1,0);
893
      }
873
      }
894
-
895
    if(TimeToStruct(end_Time,edate))
874
    if(TimeToStruct(end_Time,edate))
896
      {
875
      {
897
       edate.hour = newYorkEndHour;
876
       edate.hour = newYorkEndHour;
@@ -904,7 +883,7 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
904
 
883
 
905
    if(TimeToStruct(start_Time,sdate))
884
    if(TimeToStruct(start_Time,sdate))
906
      {
885
      {
907
-      sdate.hour = newYorkStartHour;
886
+      sdate.hour = newYorkStartTime;
908
       sdate.min  = newYorkStartMin;
887
       sdate.min  = newYorkStartMin;
909
       sdate.sec  = 0;
888
       sdate.sec  = 0;
910
      }
889
      }
@@ -912,8 +891,9 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
912
       Print("Error in Converting Time: ",GetLastError());
891
       Print("Error in Converting Time: ",GetLastError());
913
    newYorkStartTrading = StructToTime(sdate);
892
    newYorkStartTrading = StructToTime(sdate);
914
 
893
 
915
-   sessionStart = newYorkStartTrading;
916
-   sessionEnd   = newYorkEndTrading;
894
+// if(onInit)
895
+//Print("NewYork Start Time ",newYorkStartTrading,"End Date: ",newYorkEndTrading);
896
+//Print("Edate: ",edate.hour," ",edate.min," Sdate: ",sdate.hour," ",sdate.min);
917
   }
897
   }
918
 //+------------------------------------------------------------------+
898
 //+------------------------------------------------------------------+
919
 //|                                                                  |
899
 //|                                                                  |
@@ -992,8 +972,8 @@ void virtualSLHitCheck()
992
               }
972
               }
993
             else
973
             else
994
               {
974
               {
995
-               buy_sl = newTradeStore[i].price - (stopLoss * 10 * Point());
996
-               sell_sl = newTradeStore[i].price + (stopLoss * 10 * Point());
975
+               buy_sl = stopLoss != 0 ? newTradeStore[i].price - (stopLoss * 10 * Point()) : 0;
976
+               sell_sl = stopLoss != 0 ? newTradeStore[i].price + (stopLoss * 10 * Point()) : 0;
997
               }
977
               }
998
             if(newTradeStore[i].buy_hit_virtual_sl == false)
978
             if(newTradeStore[i].buy_hit_virtual_sl == false)
999
               {
979
               {