Ver Fonte

Time Filter Logic Changed

Huzaifa-MQLDev há 9 meses atrás
pai
commit
c0726e226c
2 ficheiros alterados com 51 adições e 71 exclusões
  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
    LineOnNewsBar =0,
    LineOnNewsStop=1
   };
+enum selectDay
+  {
+   prev,    // Previous Day
+   curr,    // Current Day
+  };
 
 #import "volHedgeNewsFilter.ex5"
 
@@ -85,19 +90,20 @@ new_trade_store newTradeStore[MaxOrders];
 enum lotcalculator
   {
    fix, //Fixed Lot Size
-   rsk, //Risk Percentage
+   rsk, //Risk in Percentage
    dollar, // Risk in Dollars
   };
 
 sinput       string                  string_0                   = "<><><><><><> General SETTINGS <><><><><><>";   //__
 input        int                     magic_no                   = 333;             // Magic no
-input        bool                    useTpSlPips                = true;            // Use Tp/Sl in Pips
+input        bool                    useTpSlPips                = true;            // Use Relative Tp/Sl in Pips
 input        double                  stopLoss                   = 100;             // Fixed Stop Loss in Pips
 input        double                  takeProfit                 = 100;             // Fixed Take Profit in Pips
-input        bool                    bothHitsSl                 = false;           // Open after Both Hits StopLoss
-input        int                     maxSlippage                = 5;               // Max Slippage
+input        bool                    bothHitsSl                 = false;           // Enable Topped & Tailed Pre-Demand Level
+input        int                     maxSlippage                = 5;               // Max Slippage (Points)
 input        bool                    enableSpreadFilter         = false;           // Enable Spread Filter
-input        double                  maximumSpread              = 10;              // Maximum Spread
+input        double                  maximumSpread              = 10;              // Maximum Spread (Points)
+input        string                  tradeComment               = "Trade Placed";  // Trade Comment Prefix
 input        string                  dataFileName               = "vol_hedge_data.csv"; // Data File Name
 
 input        string                  string_1                   = "<><><><><><> Lot Management<><><><><><>";   //__
@@ -106,10 +112,11 @@ input        double                  lot_amount                 = 0.1;
 input        double                  risk                       = 0.5;             // Risk in Percentage %
 input        double                  dollars                    = 10;              // Risk in GBP
 
-input        string                  string_2                   = "<><><><><><> Time Filter Setting <><><><><><> ";//_
-input        bool                    enableTimeSession          = false;           // Enable Time Session
-input        string                  start_time_session         = "01:00";         // Start Session
-input        string                  end_time_session           = "23:59";         // End Session
+input        string                  time_setting               = "<><><><><> Time Filter Settings <><><><><>";             //_
+input        selectDay               newYorkSessionDay          = curr;            // Select Day for Start Time
+input        bool                    enableTimeFilter           = false;           // Enable Time Filter
+input        string                  startTime                  = "03:00";         // Start Time Session
+input        string                  endTime                    = "09:00";         // End Time Session
 
 input        string                  string_0_2                 = "<><><><><><> Trailing Setting<><><><><><>";   //__
 input        bool                    indivial_trailing          = false;           // Indiviual Trailing
@@ -143,7 +150,8 @@ static double tickCurrentBid = 0;
 double tickPreviousBid = 0;
 static double tickCurrentAsk = 0;
 double tickPreviousAsk = 0;
-datetime startSessionTime, endSessionTime;
+int newYorkStartTime = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
+datetime newYorkStartTrading = 0, newYorkEndTrading = 0;
 int maxTrades = 2; // Max Concurrent Trades
 int GMT_Broker_Time = +2; // GMT_Broker_Time Time of your Broker
 int                     gmt                        = 0;                   // GMT_Broker_Time Time of your Broker
@@ -240,8 +248,19 @@ int OnInit()
 
    struct_level_check();
 
-   timeFilter(true,start_time_session, end_time_session, startSessionTime, endSessionTime);
-   Print(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
+   string time[];
+   StringSplit(startTime,':',time);
+   newYorkStartTime = (int)StringToInteger(time[0]);
+   newYorkStartMin  = (int)StringToInteger(time[1]);
+   Print("NewYork Start Time Hour: ",newYorkStartTime,"  Start Time Min: ",newYorkStartMin);
+   time[0] = "";
+   time[1] = "";
+   StringSplit(endTime,':',time);
+   newYorkEndHour = (int)StringToInteger(time[0]);
+   newYorkEndMin  = (int)StringToInteger(time[1]);
+   Print("NewYork End Time Hour: ",newYorkEndHour,"  End Time Min: ",newYorkEndMin);
+   timeFilter(true);
+
 
    int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
    Print("Time Difference is: ", timeDifference);
@@ -435,10 +454,9 @@ void mainActivity()
       tickCurrentAsk  = Ask;
      }
 
-// Print(" Time is: ", TimeCurrent());
-   timeFilter(false,start_time_session, end_time_session, startSessionTime, endSessionTime);
-// Comment(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
-   if((!enableTimeSession) || (enableTimeSession && TimeCurrent() >= startSessionTime && TimeCurrent() <= endSessionTime))
+   timeFilter(false);
+// Comment(" Session Start  = ", newYorkStartTrading,  " Asian  Session End = ", newYorkEndTrading);
+   if((enableTimeFilter && TimeCurrent() >= newYorkStartTrading && TimeCurrent() <= newYorkEndTrading) || !enableTimeFilter)
      {
       removeFromStructure();
       if(bothHitsSl)
@@ -688,11 +706,11 @@ ulong placeBuyTrade(double stoploss, double takeprofit)
 
    if(useTpSlPips)
      {
-      if(stoploss != 0)
+      if(stopLoss != 0)
         {
          buySL = Ask - (stopLoss * 10 * Point());
         }
-      if(takeprofit != 0)
+      if(takeProfit != 0)
         {
          buyTp = Ask + (takeProfit * 10 * Point());
         }
@@ -710,7 +728,7 @@ ulong placeBuyTrade(double stoploss, double takeprofit)
      }
 
    double distance = MathAbs((Ask - buySL) / Point());
-   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getLot(distance),Ask,buySL,buyTp,"Buy Trade Placed"))
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getLot(distance),Ask,buySL,buyTp,tradeComment+" Buy"))
      {
       Print("Buy Trade Placed: ",trade.ResultOrder());
       return trade.ResultOrder();
@@ -734,11 +752,11 @@ ulong placeSellTrade(double stoploss, double takeprofit)
 
    if(useTpSlPips)
      {
-      if(stoploss != 0)
+      if(stopLoss != 0)
         {
          sellSL = Bid + (stopLoss * 10 * Point());
         }
-      if(takeprofit != 0)
+      if(takeProfit != 0)
         {
          sellTp = Bid - (takeProfit * 10 * Point());
         }
@@ -755,7 +773,7 @@ ulong placeSellTrade(double stoploss, double takeprofit)
         }
      }
    double distance = MathAbs((Bid - sellSL) / Point());
-   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getLot(distance),Bid,sellSL,sellTp,"Sell Trade Placed"))
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getLot(distance),Bid,sellSL,sellTp,tradeComment+ " Sell"))
      {
       Print("Sell Trade PLaced: ",trade.ResultOrder());
       return trade.ResultOrder();
@@ -826,49 +844,11 @@ double getLot(double stop_loss)
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+
-void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionStart,datetime & sessionEnd)
+void timeFilter(bool onInit)
   {
-   int newYorkStartHour = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
-
-   datetime newYorkStartTrading,newYorkEndTrading;
-
-   string time[];
-   StringSplit(startTime,':',time);
-   newYorkStartHour = (int)StringToInteger(time[0]);
-   newYorkStartMin  = (int)StringToInteger(time[1]);
-
-   EventSetMillisecondTimer(500);
-   time[0] = "";
-   time[1] = "";
-   StringSplit(endTime,':',time);
-   newYorkEndHour = (int)StringToInteger(time[0]);
-   newYorkEndMin  = (int)StringToInteger(time[1]);
-
-// Print(" Start Time Hour: ",newYorkStartHour,"  Start Time Min: ",newYorkStartMin);
-// Print(" End Time Hour: ",newYorkEndHour,"  End Time Min: ",newYorkEndMin);
-
-
-   datetime startDateTime;
-   MqlDateTime st;
-   TimeCurrent(st); // get current date
-   st.hour   = newYorkStartHour;
-   st.min    = newYorkStartMin;
-   st.sec    = 0;
-   startDateTime = StructToTime(st);
-
-
-   datetime endDateTime;
-   MqlDateTime et;
-   TimeCurrent(et); // get current date
-   et.hour   = newYorkEndHour;
-   et.min    = newYorkEndMin;
-   et.sec    = 0;
-   endDateTime = StructToTime(et);
-
-
    MqlDateTime sdate,edate;
    datetime start_Time = 0, end_Time = 0;
-   if(startDateTime > endDateTime)
+   if(newYorkSessionDay == prev)
      {
       if(onInit)
         {
@@ -877,9 +857,9 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
         }
       else
         {
-         start_Time = sessionStart;
-         end_Time = sessionEnd;
-         if(TimeCurrent() >= sessionEnd && sessionEnd != 0)
+         start_Time = newYorkStartTrading;
+         end_Time = newYorkEndTrading;
+         if(TimeCurrent() >= newYorkEndTrading && newYorkEndTrading != 0)
            {
             start_Time = iTime(Symbol(),PERIOD_D1,0);
             end_Time = start_Time + 86400;
@@ -891,7 +871,6 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
       start_Time = iTime(Symbol(),PERIOD_D1,0);
       end_Time = iTime(Symbol(),PERIOD_D1,0);
      }
-
    if(TimeToStruct(end_Time,edate))
      {
       edate.hour = newYorkEndHour;
@@ -904,7 +883,7 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
 
    if(TimeToStruct(start_Time,sdate))
      {
-      sdate.hour = newYorkStartHour;
+      sdate.hour = newYorkStartTime;
       sdate.min  = newYorkStartMin;
       sdate.sec  = 0;
      }
@@ -912,8 +891,9 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
       Print("Error in Converting Time: ",GetLastError());
    newYorkStartTrading = StructToTime(sdate);
 
-   sessionStart = newYorkStartTrading;
-   sessionEnd   = newYorkEndTrading;
+// if(onInit)
+//Print("NewYork Start Time ",newYorkStartTrading,"End Date: ",newYorkEndTrading);
+//Print("Edate: ",edate.hour," ",edate.min," Sdate: ",sdate.hour," ",sdate.min);
   }
 //+------------------------------------------------------------------+
 //|                                                                  |
@@ -992,8 +972,8 @@ void virtualSLHitCheck()
               }
             else
               {
-               buy_sl = newTradeStore[i].price - (stopLoss * 10 * Point());
-               sell_sl = newTradeStore[i].price + (stopLoss * 10 * Point());
+               buy_sl = stopLoss != 0 ? newTradeStore[i].price - (stopLoss * 10 * Point()) : 0;
+               sell_sl = stopLoss != 0 ? newTradeStore[i].price + (stopLoss * 10 * Point()) : 0;
               }
             if(newTradeStore[i].buy_hit_virtual_sl == false)
               {