Download MetaTrader 5
To add comments, please log in or register
Track all payments in your profile. It is easy!
chinaski
890
chinaski 2013.03.25 17:36 

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
30298
Alain Verleyen 2013.03.25 18:29  
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
890
chinaski 2013.03.25 19:13  
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
30298
Alain Verleyen 2013.03.25 19:42  
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
890
chinaski 2013.03.25 20:45  
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
30298
Alain Verleyen 2013.03.25 21:03  
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
890
chinaski 2013.03.25 21:38  
angevoyageur:
Which symbol did you use ?
HKIndMar13. Is expired.
Taskin Osman
814
Taskin Osman 2014.07.12 19:33  
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
23012
Benjamin Dixon 2015.08.02 23:31  
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
30298
Alain Verleyen 2015.08.03 20:11  
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