Download MetaTrader 5

Detect Market for symbol closed

To add comments, please log in or register
chinaski
948
chinaski  

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 

Alain Verleyen
Moderator
35040
Alain Verleyen  
chinaski:

Hello,

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

My solution

 

 

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.
chinaski
948
chinaski  
angevoyageur:
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. 

Alain Verleyen
Moderator
35040
Alain Verleyen  
chinaski:

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.
chinaski
948
chinaski  
angevoyageur:
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;
}
Alain Verleyen
Moderator
35040
Alain Verleyen  
chinaski:

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...)

 

 

Which symbol did you use ?
chinaski
948
chinaski  
angevoyageur:
Which symbol did you use ?
HKIndMar13. Is expired.
Taskin Osman
838
Taskin Osman  
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.
Benjamin Dixon
24179
Benjamin Dixon  
chinaski:

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...)

 

 

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;
  }
Alain Verleyen
Moderator
35040
Alain Verleyen  
Benjamin Dixon:

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.

From my experience these data are not always available, it depends of the broker unfortunately.
To add comments, please log in or register