is market open/close - page 2

 

A solution could be this one... though not so elegant :-) ....

 

bool IsMarketOpen() {
  datetime from,to,now;
  datetime servertime;
 
  // put here the GMT offset (in hours) of the server time zone
  const int servergmt=0;
  //  
  servertime=TimeLocal()+TimeGMTOffset()-servergmt*3600;
  if (TimeDayOfWeek(servertime)==6) return false;
  now=StringToTime(TimeToStr(servertime,TIME_DATE));
  if (SymbolInfoSessionTrade(Symbol(),TimeDayOfWeek(servertime),0,from,to)) {
    to = to+now;
    from = from+now;
    return (servertime<to && servertime>from); 
  } else {
    return false;
  }

}

 The drawback is that you have to insert manually the server location GMT offset (in hours) in the servergmt assignment.

 
Tucu Titus Corneliu:

Ohhh, come ooonnn ... you are, too ... when you are present and responsive ! :D

Anyway, I looked over the suggested code in the links and the solution is far from elegant. I mean, I have to loop through the sessions of the day, pick the last one (I presume), check if I am between it's open and close time and get the answer. I wonder if I should test for connection as well. Ain't that too much ? Really, guyz, you are "heavy weight" in MQL and in this forum, maybe a little lobby towards MetaQuotes to introduce "MarketInfo(Symbol(), MODE_MARKETOPEN)" isn't such a bad ideea !

Thank you, both, very much indeed !

Titus 

I think I'm a litle late, but I agree. Is there not something better?

Also, theses solutions work for holidays and days off?

Thanks

 
Paolo Miocchi:

A solution could be this one... though not so elegant :-) ....

 

 The drawback is that you have to insert manually the server location GMT offset (in hours) in the servergmt assignment.

Hi Paolo,

This solution works for holidays and days off too?

 

Maybe when there's no tick's coming in ??

Or when servertime is frozen ?

 

Hi everyone, my first contribution :-D

Same issue, different approach: since there is a trade server error "ERR_MARKET_CLOSED", why not try to use it? Idea: try to create a pending order with correct values, so that if it fails, we get this error as a return code. Tried, seems to work fine:

bool isMarketClosed() {
// try to create a pending order far enough from the current price
int ticket = OrderSend(
                      NULL,                               // symbol
                      OP_BUYSTOP,                         // operation
                      MarketInfo(NULL, MODE_MINLOT),      // volume
                      NormalizeDouble(Bid * 2, _Digits),  // price
                      0,                                  // slippage
                      0,                                  // stop loss
                      0                                   // take profit
);
// the values in order send mean it should fail mostly if market is closed
if( -1 == ticket ) return (ERR_MARKET_CLOSED == GetLastError());
// if the order was created, delete it!
OrderDelete(ticket);
// market is open
return false;

}

I personally find it elegant, but I may be a bit biased :-D

Feedback welcome.


 

Own feedback:

  1. got a timeout once, cannot figure out why
  2. it is a bit slow, so make sure you use it once in a while, but not like onTick or similar

 

Found this in another thread: MarketInfo(Symbol(), MODE_TRADEALLOWED)

0 if closed, 1 if open

Seems to work, but compiled literally 1 minute too late to check on my market of choice :-D

 
fx_ta:

Hi all

I would like to check in the expert advisor if the markt is open/close. What's the easiest way to do that?

I don't want do check that over time. I also need to know when the market open again after the short close every day at 11pm.

Thanks

 FX_TA 

datetime server_t0;

int OnInit() {

        EventSetTimer(2);
        server_t0 = TimeCurrent();

}
void OnTimer() {

   server_t1 = TimeCurrent();
   
   if (server_t0!=server_t1) {
      Print("Market Open");
   }
   else {
      Print("Market Close");
   }
   EventKillTimer();

}
Reason: