How to fix the EA sleep during time filter ?

 

Hi want to ask, i'm using time filter to trade on mt4.. Here is the code related to it

extern string  TradingTime       = "------------ Trading Time Settings ------------";
extern bool    Trading_On_Monday = true;
extern string  Monday_StartTime  = "00:01"; // Start Trade Time
extern string  Monday_StopTime   = "23:59"; // End Trade Time
extern bool    Trading_On_Tuesday = true;
extern string  Tuesday_StartTime  = "00:01"; // Start Trade Time
extern string  Tuesday_StopTime   = "23:59"; // End Trade Time
extern bool    Trading_On_Wednesday = true;
extern string  Wednesday_StartTime  = "00:01"; // Start Trade Time
extern string  Wednesday_StopTime   = "23:59"; // End Trade Time
extern bool    Trading_On_Thursday = true;
extern string  Thursday_StartTime  = "00:01"; // Start Trade Time
extern string  Thursday_StopTime   = "23:59"; // End Trade Time
extern bool    Trading_On_Friday = true;
extern string  Friday_StartTime  = "00:01"; // Start Trade Time
extern string  Friday_StopTime   = "23:59"; // End Trade Time
   datetime candleTime   = iTime(Symbol(), Period(), 0);
   static datetime timeTrade;
   bool     isTradeAllowed       = true;
   if(TradingHours() && isTradeAllowed && candleTime && (Continue_Trade))
     {
      if(((Signal_Type==0)&&((TotalOrder(OP_BUY)==0)&& (TotalOrder(OP_SELL)==0))) ||
         ((!Hedge_Mode)&&(Signal_Type==1)&&((TotalOrder(OP_BUY)==0)|| (TotalOrder(OP_SELL)==0))) ||
         ((Hedge_Mode)&&((TotalOrder(OP_BUY)==0)&& (TotalOrder(OP_SELL)==0))))
        {
         if(buyCondition && TradeBuy && TotalOrder(OP_BUY)==0)
           {
            Order(OP_BUY, NamaEA + " [1st]");
            trail_mode = 0;
            timeTrade = candleTime;
            Trend_Buy = true;
            Trend_Sell = false;
           }
         else if(sellCondition && TradeSell && TotalOrder(OP_SELL)==0)
           {
            Order(OP_SELL, NamaEA + " [1st]");
            trail_mode = 0;
            timeTrade = candleTime;
            Trend_Sell = true;
            Trend_Buy = false;
           }
        }
     }
  }
bool TradingHours()
{
    if ((DayOfWeek() == 1) && TimeCurrent() > StrToTime(Monday_StartTime) && TimeCurrent() < StrToTime(Monday_StopTime))
        return true;
    else if ((DayOfWeek() == 2) && TimeCurrent() > StrToTime(Tuesday_StartTime) && TimeCurrent() < StrToTime(Tuesday_StopTime))
        return true;
    else if ((DayOfWeek() == 3) && TimeCurrent() > StrToTime(Wednesday_StartTime) && TimeCurrent() < StrToTime(Wednesday_StopTime))
        return true;
    else if ((DayOfWeek() == 4) && TimeCurrent() > StrToTime(Thursday_StartTime) && TimeCurrent() < StrToTime(Thursday_StopTime))
        return true;
    else if ((DayOfWeek() == 5) && TimeCurrent() > StrToTime(Friday_StartTime) && TimeCurrent() < StrToTime(Friday_StopTime))
        return true;
    else
        return false;
}


So the problem is, when the market close during early morning on my timezone, which is around 5 a.m, the ea keep sleep during that time until it open back using the setting above.. which is 00:01

But my setting is almost 24 hours run right.. so what is the issue here ??

 
Mohd Hakim Johari:

So the problem is, when the market close during early morning on my timezone, which is around 5 a.m, the ea keep sleep during that time until it open back using the setting above.. which is 00:01

But my setting is almost 24 hours run right.. so what is the issue here ??

Read the Documentation! Everything is explained there, and you'll find the answer you need.

If you want your EA to run continuously for 24 hours, place the code inside the OnTimer function and remove any checks related to TimeCurrent.
TimeCurrent - Date and Time - MQL4 Reference
TimeCurrent - Date and Time - MQL4 Reference
  • docs.mql4.com
TimeCurrent - Date and Time - MQL4 Reference
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Mohd Hakim Johari:

Hi want to ask, i'm using time filter to trade on mt4.. Here is the code related to it


So the problem is, when the market close during early morning on my timezone, which is around 5 a.m, the ea keep sleep during that time until it open back using the setting above.. which is 00:01

But my setting is almost 24 hours run right.. so what is the issue here ??

what do you mean by market close ?

if the market closes then its not tradable 

you can check the trading sessions with this 

SymbolInfoSessionTrade - Market Info - MQL4 Reference
SymbolInfoSessionTrade - Market Info - MQL4 Reference
SymbolInfoSessionTrade - Market Info - MQL4 Reference
  • docs.mql4.com
SymbolInfoSessionTrade - Market Info - MQL4 Reference
 
Lorentzos Roussos #:

what do you mean by market close ?

if the market closes then its not tradable 

you can check the trading sessions with this 

SymbolInfoSessionTrade - Market Info - MQL4 Reference

during early morning, market closed, then the price start moving back..

 
Mohd Hakim Johari #: during early morning, market closed, then the price start moving back..

Yes, that is normal. So what is the issue?

Only start trading once the market has opened again.

As @Lorentzos Roussos explained, use the function SymbolInfoSessionTrade to find out the time of the market sessions.

 
Fernando Carreiro #:

Yes, that is normal. So what is the issue?

Only start trading once the market has opened again.

As @Lorentzos Roussos explained, use the function SymbolInfoSessionTrade to find out the time of the market sessions.

the issue is, during that time my ea on sleep mode. no trade at all.. it open on vps time on 00:01. on my timezone, it close during 5 am. and reopen back on 4 p.m.. how many hours were wasted.. almost 11 hours.. Not sure is this related to vps 12 hours since my setting is 24 hours.. 

 
Mohd Hakim Johari #: the issue is, during that time my ea on sleep mode. no trade at all.. it open on vps time on 00:01. so from 5 pm to 00:01am. more than 6 hours were wasted..

Is that not obvious? If the market is closed, then no trade operations are allowed. Also, no new ticks arrive so the the EA has no need to do anything.

Once the market opens and the new ticks start arriving, then take action.

Don't make your code logic depend on an exact starting time. Make it so that it detects the first tick arrives after the start time you have set, and only then place your trades.

 
Fernando Carreiro #:

Is that not obvious? If the market is closed, then no trade operations are allowed. Also, no new ticks arrive so the the EA has no need to do anything.

Once the market opens and the new ticks start arriving, then take action.

Don't make your code logic depend on an exact starting time. Make it so that it detects the first tick arrives after the start time you have set, and only then place your trades.

most broker reopen after a few minute.. 

 
Mohd Hakim Johari #: it open on vps time on 00:01. on my timezone, it close during 5 am. and reopen back on 4 p.m.. how many hours were wasted.. almost 11 hours.. Not sure is this related to vps 12 hours since my setting is 24 hours.. 

Brokers can use different time-zones. Don't just assume that it is the same time-zone as your VPS or your computer.

Adjust your start and end times based on the trade server's time zone.

An I repeat, use the SymbolInfoSessionTrade to find out the session times and adjust accordingly. Don't just hard-code the start and end times.

 
Mohd Hakim Johari #: most broker reopen after a few minute.. 

Yes, that is when they calculate the swaps. That is normal. Adjust your strategy logic accordingly.

Make your code so as to take that into consideration. Program the logic in your EA.