Market Open or Close - how can I find it? - page 2

 
MassimoMondo:

Hello,

I found a simple solution with OnTimer routine. Let's see the MQL code:

Hope this will help you.

Massimo

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if (TimeCurrent() == prevTimeCurrent){
      Comment ("Market is CLOSED - Local Time : ",TimeLocal());
      infoMarket=FALSE;
   }
   else {
      Comment ("Market is OPEN - Local Time : ",TimeLocal());
      infoMarket=TRUE;
      }
  }


If it was working ( it doesn't as prevTimeCurrent is never updated)

This will only tell you that there have been no ticks for 5 seconds.

Pavel Soukup:

Yes. I know it. But I need trading more markets at the same time and ALL MARKETS MUST BE OPEN (gold, oil, S&P, fx)

It will not tell you if any particular market is open.

 
Especially during holidays in december these times will change a lot and this might confuse your strategy. The fact is that these times depend on your broker and the markets you trade AND the major holidays. There is no simple super solution for your problem.
 
Pavel Soukup:

I find solution for me: MarketInfo(Symbol(), MODE_TRADEALLOWED);

return 1 - if market open else return 0

Thank You for share the solution.


MQL4 Code:

   if((MarketInfo(Symbol(),MODE_TRADEALLOWED))>0)
     {
      Print(" Market is Open and Trade is Allowed on This Symbol.");
     }
   else
     {
      Print ("Trade is not Allowed on This Symbol RightNow.");
     }
 
if(GetLastError()==ERR_MARKET_CLOSED)
  {
   Print("Market is closed!");
  }

You can also analyze the error when your ordersend call fails.

 
Pavel Soukup:

Hi,

How can I find market Open or Close?

dunction GetLastError()? What value return function? Or other function/way? Other solution?

Thank you.

You can to use this reference... to check day by day the time to beggin and time to end the market session.

bool  SymbolInfoSessionTrade( 
   string            name,                // symbol name 
   ENUM_DAY_OF_WEEK  day_of_week,         // day of the week 
   uint              session_index,       // session index 
   datetime&         from,                // session beginning time 
   datetime&         to                   // session end time 
   );

 
if(!MarketClosed()) {  
  //... do something
}

bool MarketClosed()
{  
    bool closed=false;
    MqlDateTime STime;
    datetime time=TimeGMT();    
    TimeToStruct(time,STime);    
         
    //Test for Friday after 5pm EST
    if(STime.day_of_week==5){     
      if(STime.hour==21 || STime.hour==22 || STime.hour==23 || STime.hour==24 || 
      STime.hour==1 || STime.hour==2 || STime.hour==3)
      { closed=true; }
    }
    
    //Test for Saturday
    if(STime.day_of_week==6)
      { closed=true; }
    
    //Test for Sunday before 5pm EST
    if(STime.day_of_week==0){ 
      if(STime.hour==4 || STime.hour==5 || STime.hour==6 || STime.hour==7 || STime.hour==8 || STime.hour==9 ||
         STime.hour==10 || STime.hour==11 || STime.hour==12 || STime.hour==13 || STime.hour==14 || 
         STime.hour==15 || STime.hour==16 || STime.hour==17 || STime.hour==18 || STime.hour==19 || STime.hour==20)
        { closed=true; }                          
    }    
   return closed;                          
}
 
   //Test for Friday after 5pm EST
  1. This is totally broker dependent.
              Time zone - Trading Systems - MQL5 programming forum #2 (2020.11.13)
  2. Breaks on market holidays
  3. If the market is closed, there will be no ticks. The code will not run in OnTick.
 
William Roeder #:
  1. This is totally broker dependent.
              Time zone - Trading Systems - MQL5 programming forum #2 (2020.11.13)
  2. Breaks on market holidays
  3. If the market is closed, there will be no ticks. The code will not run in OnTick.



It is just an example based on his premise. I think it was his premise, or it may have been what I read in another thread where they were asking about oil markets and gold or something. The point being it is platform, culture and timezone independent (it is coded in GMT). Obviously you have to code in your own holidays as people in China and Saudi Arabia have different times and holidays. I didnt offer it as an out of the box solution, just a termplate
 
William Roeder #:
  1. This is totally broker dependent.
              Time zone - Trading Systems - MQL5 programming forum #2 (2020.11.13)
  2. Breaks on market holidays
  3. If the market is closed, there will be no ticks. The code will not run in OnTick.

what about an ea that uses OnTimer to do it's duty?

why there is no library in mql to check for it?

 
Paul B #:
Your EA will only work if the market is open. How can something that is not receiving ticks to enable processing carry out any checks???
I shouldn't receive tickets in a closed market, I think just like you. That's why I would like to know why I get "market closed" if I am within the onTick function. I can't find the answer, do you know it? I am doing tests in MT5.
Reason: