is market open/close - page 2

To add comments, please log in or register
Paolo Miocchi
26875
Paolo Miocchi  

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.

Gustavo Hennemann
292
Gustavo Hennemann  
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

Gustavo Hennemann
292
Gustavo Hennemann  
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?

Marco vd Heijden
Moderator
10977
Marco vd Heijden  

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

Or when servertime is frozen ?

foreverLearner
7
foreverLearner  

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.


foreverLearner
7
foreverLearner  

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

foreverLearner
7
foreverLearner  

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

12
To add comments, please log in or register