How to check Market Open/Closed in MT5 ? - page 3

To add comments, please log in or register
Anthony Garot
3508
Anthony Garot  
Alain Verleyen:

The only conclusion for everyone is try it yourself according to your needs, there is no universal solution by using a single function call.

Alain is correct—brokers have all sorts of wonky settings! I found one broker who specifies forex hours between two separate sessions.

As this thread is the top one to show up in Google, I will place this code here. It seems to work for two separate brokers for forex and odd symbols like an index fund that takes a 15 minute break on weekdays.

Caveat emptor.

// Checks if market is currently open for specified symbol
bool IsMarketOpen(const string symbol, const bool debug = false)
{
    datetime from = NULL;
    datetime to = NULL;
    datetime serverTime = TimeTradeServer();

    // Get the day of the week
    MqlDateTime dt;
    TimeToStruct(serverTime,dt);
    const ENUM_DAY_OF_WEEK day_of_week = (ENUM_DAY_OF_WEEK) dt.day_of_week;

    // Get the time component of the current datetime
    const int time = (int) MathMod(serverTime,HR2400);

    if ( debug ) PrintFormat("%s(%s): Checking %s", __FUNCTION__, symbol, EnumToString(day_of_week));

    // Brokers split some symbols between multiple sessions.
    // One broker splits forex between two sessions (Tues thru Thurs on different session).
    // 2 sessions (0,1,2) should cover most cases.
    int session=2;
    while(session > -1)
    {
        if(SymbolInfoSessionTrade(symbol,day_of_week,session,from,to ))
        {
            if ( debug ) PrintFormat(    "%s(%s): Checking %d>=%d && %d<=%d",
                                        __FUNCTION__,
                                        symbol,
                                        time,
                                        from,
                                        time,
                                        to );
            if(time >=from && time <= to )
            {
                if ( debug ) PrintFormat("%s Market is open", __FUNCTION__);
                return true;
            }
        }
        session--;
    }
    if ( debug ) PrintFormat("%s Market not open", __FUNCTION__);
    return false;
}
FXreedom
4092
FXreedom  
Anthony Garot:

Alain is correct—brokers have all sorts of wonky settings! I found one broker who specifies forex hours between two separate sessions.

As this thread is the top one to show up in Google, I will place this code here. It seems to work for two separate brokers for forex and odd symbols like an index fund that takes a 15 minute break on weekdays.

Caveat emptor.

hello Anthony, what is HR2400?

thanks

William Roeder
19654
William Roeder  
FXreedom: hello Anthony, what is HR2400?
#define  HR2400 (PERIOD_D1 * 60) // 86400 = 24 * 3600 = 1440 * 60
FXreedom
4092
FXreedom  
William Roeder:

thanks for your answer William.

In MT5, PERIOD_D1=16408, so I think in MT5 the code should be HR2400=PeriodSeconds(PERIOD_D1)

regards

123
To add comments, please log in or register