Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Check Market Opening Hours - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 5 reads | 0 comments |

Check for Market Open Hours - library for MetaTrader 5

Check for Market Open Hours - library for MetaTrader 5

The MarketOpenHours.mqh file checks market opening hours based on the broker's server time. Its input is just a symbolic name of type string. As a result you get a boolean value, true - the market is open, false - the market is closed. In the image below I show the opening hours of the symbol EURUSD for my broker DarwinEx.

Check for Market Open Hours - library for MetaTrader 5

You can see how different the opening times are on different weekdays. This is your include file:

//+------------------------------------------------------------------+
//| market opening hours.mqh |
//| Wolfgang Merz, wm1@gmx.de |
//| https://melz.one |
//+------------------------------------------------------------------+
#Property Copyright "Wolfgang Merz, wm1@gmx.de"
#property associated with "https://melz.one"

//+------------------------------------------------------------------+
//|Market opening hours |
//+------------------------------------------------------------------+
boolean market opening hours ( string symbol) {
boolean opening = false ; // Default market closed Mql date time mdt server time; // Declare server time structure variable date time dt server date time = time trade server (); // Store server time if (! Build time (dt server date time, // servertime is converted to a structure correctly? mdtServerTime)) {
return ( false ); // No, the returns market is closed }

ENUM_DAY_OF_WEEK today = ( ENUM_DAY_OF_WEEK ) // Get the actual date and convert it to an enumeration mdtServerTime.day_of_week;

if (today > 0 || today < 6 ) { // Is today Monday to Friday? datetime dtF; // Store the start and end time of the trading session datetime dtT; // The date part is 1970.01.01 (0) datetime dt server time = dt server datetime % 86400 ; // Set the date to 1970.01.01 (0) if (! symbolinfo session (symbol, today, // do we have the values ​​of dtFrom and dtTo? 0 , dtF, dtT)) {
return ( false ); // No, the returns market is closed }
shift (today) { // Check different trading sessions case 1 :
if (dtServerTime >= dtF && dtServerTime <= dtT) // Server time is 00:05 (300) - 00:00 (86400) opening = true ; // Yes, set market open break ;
Case 5 :
if (dtServerTime >= dtF && dtServerTime <= dtT) // Server time is 00:04 (240) - 23:55 (86100) opening = true ; // Yes, set market open break ;
default :
if (dtServerTime >= dtF && dtServerTime <= dtT) // Server time is 00:04 (240) - 00:00 (86400) opening = true ; // Yes, set market open break ;
    }
  }
return (open);
} //+------------------------------------------------------------------+

The first part checks the working days (Monday to Friday) to allow opening trades and trailing stops during the week. In the switch command, check the actual server time against the agent's symbolic session.


Attachment download

📎 marketopenhours.mqh (2.77 KB)

📎 test_marketopenhours.mq5 (1.75 KB)

Source: MQL5 #46597

Verification code Refresh