How to find out if the market is closed? (mql4) - page 3

 

What is the practical need for all these checks?

Why check, for example, whether the expert is allowed to trade? Who is the target? A down user? The flag of trade permission is set in the terminal once, that's all. Why should we check it on each tick? Or check it every n seconds? Does the Expert Advisor have nothing else to do?

Why check if the market is open? Does the market open when it wants to? Or some instrument trades at 16 o'clock today and at 14 o'clock tomorrow and this mess happens all the time? If the trading schedule is known and never changes, why should you check every tick or every n-second if the market is open?

And this funny tip about sending a trade request to find out if the market is open? For those who like to have a laugh? Or for a satisfied user?

No tick, no trade.

 
abolk:

What is the practical need for all these checks?

Why check, for example, whether the expert is allowed to trade? Who is the target? A down user? The flag of trade permission is set in the terminal once, that's all. Why should we check it on each tick? Or check it every n seconds? Does the Expert Advisor have nothing else to do?

Why check if the market is open? Does the market open when it wants to? Or some instrument trades at 16 o'clock today and at 14 o'clock tomorrow and this mess happens all the time? Why, if the trading schedule is known and never changes, should we check every tick or every n-second if the market is open?

And this trick with sending a trade request to see if the market is open?

No tick - no trade.

The tasks are different. For all I cannot say, but my task was to open orders for different symbols from one EA and some of them had different trade sessions (I mean the possibility to work with any set of symbols available for the account).

Therefore, all checking has been done not in OnTick but in OnTimer. I described my version on the previous page. Although, in principle, it is a kind of "no tick - no trade" check.

 
abolk:

... Does the market open when it wants to? Or some instrument trades from 4pm today and from 2pm tomorrow and it's a mess all the time?

...

It happens... There are different holidays... Different countries. Forex worked, but gold and silver did not.

And if you have an opportunity to use futures on top of Forex, early closing of sessions, and cut-offs for exceeding a limit and other tricks happen.

 
papaklass:

Here's another option for the four:

bool flag;
//-----------------------------------------------------------------------------+

bool RealSymbol(string str)
{
   return(MarketInfo(str, MODE_BID) != 0);
}//----------------------------------------------------------------------------+
 
void init()
{
   flag = RealSymbol(Symbol());
         
   return;  
}//----------------------------------------------------------------------------+
 
void deinit()
{
   return;
}//----------------------------------------------------------------------------+
 
void start()
{
   if (!flag)
   {
      return;
   }
   
   //дальше Ваш рабочий код
     
   return;  
}//----------------------------------------------------------------------------+

amazing option -- the main thing is to run the advisor before the markets open -- and if you don't turn it off, don't overload, don't change the timeframe -- then the advisor will never work, because the flag will always be in the "no" state

p.s. Funny advice thread

p.s.2. Especially strange when they say that in a closed market

MarketInfo(Symbol(), MODE_BID)

gives ZERO.

 

Checking the lag between ticks and inferring a close trade on that basis is a bad decision.

 
avtomat:

Checking the lag between ticks and inferring a close trade on that basis is a bad decision.

However:
1) if the answer is not "are trades closed", but "is it possible to open a trade at this price" (under current conditions);
2) knowing in advance that if a quote is older than xx minutes, then the server is guaranteed to return off quotes on an attempt to open an order;
then it would be acceptable. IMHO, of course.
 

There has been some discussion on this issue for MT5.

The correct option for me is to use SymbolInfoSessionTrade() to determine if trades are available.


//+------------------------------------------------------------------+
//|Открыта ли торговая сессия                                        |
//|                                                 Copyright,Sergeev|
//|                           https://login.mql5.com/ru/users/sergeev|                 
//+------------------------------------------------------------------+
bool IsTradeSessionOpen()
  {
   MqlDateTime _DateTime;
   datetime _TimeCurrent=TimeTradeServer(_DateTime);

// проверяем время торгов по инструменту
   datetime _TradeEnd=(datetime)SymbolInfoInteger(_Symbol,SYMBOL_EXPIRATION_TIME);
   if(_TimeCurrent>_TradeEnd && _TradeEnd>0) return(false);

   datetime _TradeStart=(datetime)SymbolInfoInteger(_Symbol,SYMBOL_START_TIME);
   if(_TimeCurrent<_TradeStart && _TradeStart>0) return(false);

// проверяем сессии инструмента
   datetime _DayStart=_TimeCurrent/(60*60*24); _DayStart*=(60*60*24); // начало текущего дня
   datetime _sec=_TimeCurrent-_DayStart; // число секунд от начала дня

   for(int i=0; i<10; i++) // подразумеваем, что админ сделал не более 10 сессий в сутки
     {
      datetime _from,_to;
      if(!SymbolInfoSessionTrade(_Symbol,(ENUM_DAY_OF_WEEK)_DateTime.day_of_week,i,_from,_to)) break; // данные сессии
      if(_sec>=_from && _sec<=_to) return(true);
     }
   return(false);
  }
Как узнать, что по выходным нет торговли?
Как узнать, что по выходным нет торговли?
  • www.mql5.com
Пытаюсь дать понять советнику, что по выходным дням торговать не нужно :). - - Категория: общее обсуждение
 
Kino:

Switched the Expert Advisor to OnTimer() mode and now when the market is closed it does not understand this and tries to trade, in general it contacts the server and of course receives errors and clogs logs. GetLastError() = Market is closed. What other conditions can be used to check if the market is closed or on a weekend?

Duration = (set value in milliseconds. e.g. 6 hours = 1000*60*60*6)

if(GetLastError()==132(market is closed) ) Sleep (Duration);

 

Once I experimented withSymbolInfoSessionTrade() and Quote and it turned out that the data from these functions only approximately correspond to reality, and in some cases do not.

Perhaps a combination of several checks will give an unambiguous answer to the question whether the trade is over?

SymbolInfoSessionTrade - Документация на MQL4
  • docs.mql4.com
SymbolInfoSessionTrade - Документация на MQL4
 

Wow, that's a lot of text.

MarketInfo(Symbol(),MODE_TRADEALLOWED)