Checking If Market Opened or Closed - Mql4

 

Hello everyone,


For the past few days I've been looking for a reliant solution for this issue as many of the solutions I found online have many drawbacks.

Market Open or Close - how can I find it? - Day Trading Strategies - MQL4 and MetaTrader 4 - MQL4 programming forum (mql5.com)

First of all, I look at the solutions provided in the link above. Some of are the following:

1. Using Market Info TRADEALLOWED:

   MarketInfo(Symbol(), MODE_TRADEALLOWED);

The thing with this method that it is broker dependent, and I believe it reads from the times mentioned in the symbol specification in MT4. 

So, there were times when the market was supposed to be opened as per the listed times, but for some unknown reason, the market was shut down at that time.

At these times calling MODE_TRADEALLOWED will result in 1 (true) value, which is misleading.




2. Computing the difference between TimeLocal() and TimeCurrent()

   int TimeDifference = TimeLocal() - TimeCurrent();
   return TimeDifference;

The issue here is that TimeCurrent() is not related to a given simple rather to all the symbols in the Market Watch list. For example, on weekends, and if a broker provides 24/7 Crypto trading, this method will be updated almost instantly. 

However, if you call this method on EURUSD for example, it will return a misleading result, since the time returned is not for EURUSD, rather for BTCUSD (24h/7d trading) for instance.


3. Sending a stop order to the market and seeing if it gets executed or not.

This solution is the closest one to solve this problem, however, calling OrderSend 10 times a seconds will result in a big load on the broker server, which I suspect that any broker will allow it.


Any ideas?

Thanks!

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

The MarketInfofunction is the old MQL4 function. Have you considered looking at the newer MQL4+ functions which are also equivalent to the MQL5 functions?

For example ...

Forum on trading, automated trading systems and testing trading strategies

How to recall stop trading time

Fernando Carreiro, 2023.04.15 21:51

At first I thought it was only possible in MQL5, but after checking MQL4 documentation, it seems it is also possible in MQL4+.

Use SymbolInfoSessionTrade ...

SymbolInfoSessionQuote

Allows receiving time of beginning and end of the specified quoting sessions for a specified symbol and day of week.

SymbolInfoSessionTrade

Allows receiving time of beginning and end of the specified trading sessions for a specified symbol and day of week.


And here some equivalent information for MQL5, which also works for MQL4+ ...

Forum on trading, automated trading systems and testing trading strategies

`IsTradeAllowed()`: undeclared identifier.

Fernando Carreiro, 2023.02.25 19:14

Please read up on ...

SymbolInfoSessionQuote

Allows receiving time of beginning and end of the specified quoting sessions for a specified symbol and day of week.

SymbolInfoSessionTrade

Allows receiving time of beginning and end of the specified trading sessions for a specified symbol and day of week.

And also see the following as well:

bool  bAccountTradeAllowed       = AccountInfoInteger(  ACCOUNT_TRADE_ALLOWED  ),
      bAutomatedTradeAllowed     = AccountInfoInteger(  ACCOUNT_TRADE_EXPERT   ),
      bTerminalTradeAllowed      = TerminalInfoInteger( TERMINAL_TRADE_ALLOWED ),
      bProgramTradeAllowed       = MQLInfoInteger(      MQL_TRADE_ALLOWED      );
 
Fernando Carreiro #:

The MarketInfofunction is the old MQL4 function. Have you considered looking at the newer MQL4+ functions which are also equivalent to the MQL5 functions?

For example ...

I looked at this function, there may be many similarities to MarketInfo.

Which depends on the data the broker has recorded under the symbol specifications. Not all brokers do so though.

However, I noticed that it requires something called session_index, what is that?


bool  SymbolInfoSessionQuote(
   string            name,                // symbol name
   ENUM_DAY_OF_WEEK  day_of_week,         // day of the week
   uint              session_index,       // session index
   datetime&         from,                // time of the session beginning
   datetime&         to                   // time of the session end
   );
 
Mansour Fahad M Almogaiteeb #: I looked at this function, there may be many similarities to MarketInfo. Which depends on the data the broker has recorded under the symbol specifications. Not all brokers do so though. However, I noticed that it requires something called session_index, what is that?

It is so you can access the start and end time of each session of that week day. There can be multiple sessions per day.

As per the documentation ...

uint

[in]  Ordinal number of a session, whose beginning and end time we want to receive. Indexing of sessions starts with 0.


You loop, with a starting session index of 0, and increment the session index until the function returns false.

That will allow you to collect the information of each session for that week day.

 
Fernando Carreiro #:

It is so you can access the start and end time of each session of that week day. There can be multiple sessions per day.

As per the documentation ...

uint

[in]  Ordinal number of a session, whose beginning and end time we want to receive. Indexing of sessions starts with 0.


You loop, with a starting session index of 0, and increment the session index until the function returns false.

That will allow you to collect the information of each session for that week day.

I understand the logic but how can I tell the difference if that "false" is generated due to no trading or due to no such session at all?

I was thinking of sending test orders to the market and see if any ticket is returned, what do you think about this approach?

 
Mansour Fahad M Almogaiteeb #: I understand the logic but how can I tell the difference if that "false" is generated due to no trading or due to no such session at all?

No, the boolean (true/false) return value of the functions SymbolInfoSessionQuote and SymbolInfoSessionTrade, is not about if trading is allowed or not.

It is about whether information for the session was retrieved or not. Please read the documentation ...

Return Value

If data for the specified session, symbol and day of the week are received, returns true, otherwise returns false.

You would then use the session time to decide on whether trading is allowed or not.

Also remember to check the other trading status functions I mentioned.

Mansour Fahad M Almogaiteeb #: I was thinking of sending test orders to the market and see if any ticket is returned, what do you think about this approach?

Personally, I don't think that is ideal, when there are already several ways of verifying the condition.

That should be reserved for when you have decided to place the order and then check if it was successful or not.

 

if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) || !MQLInfoInteger(MQL_TRADE_ALLOWED)) return(0);