MT4 Time filter

 

Hi,

I've been trying to put together a time filter with multiple starts and finishes. the parameter starts and finishes once, but when i repeat the procedure there is no response.

//String for the start trading time
input string StartTradingTime="08:00";

//String fo the stop trading time
input string StopTradingTime="08:13";

///////

 //set time to local time
      datetime time=TimeLocal();
      //Convert local time to a formatted string
      CurrentTime=TimeToString(time,TIME_MINUTES);

// if it is trading time
    {
      if(CheckTradingTime()==true)

///////

bool CheckTradingTime()
  {
   if(StringSubstr(CurrentTime,0,5)==StartTradingTime)
      TradingIsAllowed=true;
   if(StringSubstr(CurrentTime,0,5)==StopTradingTime)
      TradingIsAllowed=false;
   return TradingIsAllowed;
  }
 
 if(StringSubstr(CurrentTime,0,5)==StartTradingTime)

      TradingIsAllowed=true;

Print CurrentTime and you will see your substr call is bogus.

There can be minutes between ticks in the Asian session; your boolean may not trigger.

Convert your two strings to a datetime and then just compare.

 
William Roeder #:

Print CurrentTime and you will see your substr call is bogus.

There can be minutes between ticks in the Asian session; your boolean may not trigger.

Convert your two strings to a datetime and then just compare.

Hi William, the substr call is bogus, your right their, but the CurrentTime==StartTradingTime shows the correct time. I don't see the issue? the parameter isn't replicating?
 
OTC #:
Hi William, the substr call is bogus, your right their, but the CurrentTime==StartTradingTime shows the correct time. I don't see the issue? the parameter isn't replicating?

I worked it, there shouldnt be a double up of if statements.

 

change == to >=

bool CheckTradingTime()
  {
   if(StringSubstr(CurrentTime,0,5)>=StartTradingTime)
      TradingIsAllowed=true;
   if(StringSubstr(CurrentTime,0,5)>=StopTradingTime)
      TradingIsAllowed=false;
   return TradingIsAllowed;
  }
 
Daudi Venance Shija #:
  1. Simplified
    bool CheckTradingTime()
      {
       return StringSubstr(CurrentTime,0,5)>=StartTradingTime
          &&  StringSubstr(CurrentTime,0,5)< StopTradingTime);
      }
  2. When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours and fraction can be inputs.

    See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
    Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)
    MQL5 Programming Basics: Time - MQL5 Articles (2013.04.26)

    Remember to handle the case where start > end (e.g. 2100 … 0200)