How to detect if the market is closed

Hello,

need a function to detect if a market is closed for FX symbol .

My solution

bool market_closed(string symbol)
{
    if(StringLen(symbol) > 1)
    {
        datetime begin=0;
        datetime end=0;
        datetime now=TimeCurrent();
        uint     session_index=0; // guess latest/current session
        
        
        MqlDateTime today;
        TimeToStruct(now,today);
        if(   SymbolInfoSessionTrade(symbol,(ENUM_DAY_OF_WEEK ) today.day_of_week,session_index,begin,end) == true)
        {
            if(now >= begin && now <= end)
                return false;
            return true; 
        }
    }
    return false;
}

does not work: date begin and end always are 1970-01-01.

Is this because the Symbol is a FX contract ?

Do i something wrong ?

Thank you

Check this article and tell us if that help.

This article is not really focused on my problem: get a simple method which tells you if market is open (ready for placing trades).

If you mean using SymbolInfoSessionQuote instead of SymbolInfoSessionTrade, no, makes no difference.

One section of this article is exactly focused at your problem, there is a mql5 script with a complete example of code for SymbolInfoSessionTrade.

Oh sorry, didn’t see the link.
Thank you. Basically there is a timestamp and date seems not to be important.
My new version works so far (but returns market open for expired contracts as well…)

bool market_closed(string symbol)
{
   if(StringLen(symbol) > 1)
   {
       datetime begin=0;
       datetime end=0;
       datetime now=TimeCurrent();       
       uint     session_index=0;
       
       
       MqlDateTime today;
       TimeToStruct(now,today);
       if(   SymbolInfoSessionTrade(symbol,(ENUM_DAY_OF_WEEK ) today.day_of_week,session_index,begin,end) == true)
       {
           string snow=TimeToString(now,TIME_MINUTES|TIME_SECONDS);
           string sbegin=TimeToString(begin,TIME_MINUTES|TIME_SECONDS);
           string send=TimeToString(end-1,TIME_MINUTES|TIME_SECONDS);

           now=StringToTime(snow);
           begin=StringToTime(sbegin);
           end=StringToTime(send);

           if(now >= begin && now <= end)
               return false;

           return true; 
       }
   }
   return false;
}

Which symbol did you use ?

HKIndMar13. Is expired.

I suggest using now=TimeLocal();, TimeCurrent does not change over weekend. I have made my tests and your routine is well done, tx but you should use TimeLocal(). The problem is the time discrepancy between your time and brokers time.

tyvm for the code saved me much work. I have changed it slightly and seems to be working for me now. checks if market is open.

bool market_open(string symbol)
  {
   if(StringLen(symbol)>1)
     {
      datetime begin=0;
      datetime end=0;
      datetime now=TimeTradeServer();
      uint     session_index=0;

      MqlDateTime today;
      TimeToStruct(now,today);
      if(SymbolInfoSessionTrade(symbol,(ENUM_DAY_OF_WEEK) today.day_of_week,session_index,begin,end)==true)
        {
         string snow=TimeToString(now,TIME_MINUTES|TIME_SECONDS);
         string sbegin=TimeToString(begin,TIME_MINUTES|TIME_SECONDS);
         string send=TimeToString(end-1,TIME_MINUTES|TIME_SECONDS);

         now=StringToTime(snow);
         begin=StringToTime(sbegin);
         end=StringToTime(send);

         if(now>=begin && now<=end)
            return true;

         return false;
        }
      else return false;
     }
   Print("invalid symbol!!!!!");
   return false;
  }

From my experience these data are not always available, it depends of the broker unfortunately.

I use this simple code

you should replace “total index symbol” with yours

bool market_closed(string symbol)
{
    string total_index_symbol="total index symbol";
    long  lastbar_time=SeriesInfoInteger(symbol,PERIOD_D1,SERIES_LASTBAR_DATE);
    long  Tlastbar_time=SeriesInfoInteger(total_index_symbol,PERIOD_D1,SERIES_LASTBAR_DATE);
    
    if(((Tlastbar_time-lastbar_time)/3600/24)>1){ return true;} else {return false;}
}