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

 
Vasyl Nosal:

It's a persistent desire to deceive and deceive yourself :) .

Added: Have you tried displaying all the symbols and showing the "Time" column in the "Market Overview" window and discovering the bitcoin symbol that's trading on a non-stop basis? Also, such a basic check also helps:

1

2

3

 

Not a one-size-fits-all solution, but works for me


bool CanTrade()
{

bool res1=IsTradeAllowed(Symbol(), TimeCurrent());
bool res2=IsTradeAllowed(Symbol(), TimeCurrent()+4000); //с запасом,

if (!res1) return false; //сейчас торговать запрещено
if (res1 && res2) return true; //и сейчас и через час торговать можно
return false; //если зависло разрешение от TimeCurrent, то через час (или с запасом в несколько минут) покажет фалсе.
// Но наверняка есть ситуации когда реально сейчас можно торговать а через час нельзя - тогда неправильный ответ :(
}

 
Igor-san:

But surely there are situations where you can really trade now and not in an hour

Of course there are - every time an hour before market close ))))
 
Ihor Herasko:
Of course it does - every time an hour before market close ))))

I disagree. The last date frozen on the screen is Friday 21:59. If I set shift at 3600 sec - the second parameter IsTradeAllowed(Symbol(), TimeCurrent()+3600 returns trude. I added some additional seconds to make it work. Well, if it is Friday at 21:50, everything should work.

To be honest, I didn't check it, I just checked connection to broker Reconnector (found it somewhere on forum) which always reconnected at weekends. So it's not significant for me.

 
/// Ждём разрешения торговать
void TradeAllowed()
{
symb=Symbol();/// Текущий символ или символ на котором нужно произвести проверку

if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
   {Print("Automated trading is forbidden at the trade server side");return;}
if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED)) 
   {Print("Trading is forbidden!");return;}
if (!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))  
   {Print("Check if automated trading is allowed in the terminal settings!");return;}
if(!IsTradeAllowed(symb,TimeLocal())
   {Print("Trading on the symbol "+symb+" is forbidden!");return;}

while(IsTradeContextBusy())
   {
   if(IsStopped()) {Print("Forced shutdown ");break;}
   Print("Trading is occupied. Wait.");
   Sleep(1000);
   RefreshRates(); 
   }  
}
 

bool MarketOpen(string Symbl)
{
   if(IsTradeAllowed(Symbl,TimeCurrent()+1)==false)return(false); //рынок закрыт

   return(true);

}

 
Renat Akhtyamov:

bool MarketOpen(string Symbl)
{
   if(IsTradeAllowed(Symbl,TimeCurrent()+1)==false)return(false); //рынок закрыт

   return(true);

}

It varies, so this check does not guarantee anything.

So trading by experts is forbidden, but the market is open.


Now, Monday, the market is open and the experts are banned by the office:


 
On the contrary, it happens that in some other offices on weekends, when the market is closed, these checks indicate that the market is open and experts have been given permission to trade.
 
Олег avtomat:
On the contrary, it happens that in some other offices on weekends, when the market is closed, these checks indicate that the market is open and experts have been given permission to trade.

yesterday.

so far it's working,

This one doesn't:

Forum on trading, automated trading systems and trading strategy testing

How do I know there is no trading on weekends?

Renat Akhtyamov, 2018.02.11 05:27

THANK YOU!

For MT4.

//------------------------------------------------------------------    IsSessionOpen
bool IsSessionOpen(string smb)
{
     //MqlDateTime dt; datetime tc=TimeTradeServer(dt);
      datetime tc= TimeCurrent();
     // проверяем время торгов по инструменту
     datetime t=(datetime)SymbolInfoInteger(smb, SYMBOL_EXPIRATION_TIME);
     if (tc>t && t>0) return(false);
     t=(datetime)SymbolInfoInteger(smb, SYMBOL_START_TIME);
     if (tc<t && t>0) return(false);
     
     // проверяем сессии инструмента
     datetime ds=tc/(60*60*24); ds*=(60*60*24); // начало сегодня
     datetime sec=tc-ds; // число секунд от начала дня
     bool b=false;
     for (int l=0; l<10; l++) // подразумеваем, что админ сделал не более 10 сессий в сутки
     {
             datetime fs, ts;
             if (!SymbolInfoSessionTrade(smb, TimeDayOfWeek(TimeCurrent()), l, fs, ts)) break; // данные сессии
             if (sec>=fs && sec<=ts) return(true);
     }
     return(false);
}



 

there's something about the 5p function that doesn't work:

(I wonder why ???)

https://www.mql5.com/ru/docs/standardlibrary/tradeclasses/cterminalinfo/cterminalinfoistradeallowed

Документация по MQL5: Стандартная библиотека / Торговые классы / CTerminalInfo / IsTradeAllowed
Документация по MQL5: Стандартная библиотека / Торговые классы / CTerminalInfo / IsTradeAllowed
  • www.mql5.com
Стандартная библиотека / Торговые классы / CTerminalInfo / IsTradeAllowed - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5