The MarketOpenHours.mqh file checks the Market Open Hours against the server time at the broker. Its input is only the Symbol name as string type. As a result you get a bool, true - the market is open, false - the market is closed. In the following image i show the Open Hours of my broker DarwinEx for the symbol EURUSD.
You can see how different these open times are on different weekdays. So here is your include file:
//+------------------------------------------------------------------+
//| MarketOpenHours.mqh |
//| Wolfgang Melz, wm1@gmx.de |
//| https://melz.one |
//+------------------------------------------------------------------+
#property copyright "Wolfgang Melz, wm1@gmx.de"
#property link "https://melz.one"
//+------------------------------------------------------------------+
//| MarketOpenHours |
//+------------------------------------------------------------------+
bool MarketOpenHours(string sym) {
bool isOpen = false; // by default market is closed
MqlDateTime mdtServerTime; // declare server time structure variable
datetime dtServerDateTime = TimeTradeServer(); // store server time
if(!TimeToStruct(dtServerDateTime, // is servertime correctly converted to struct?
mdtServerTime)) {
return(false); // no, return market is closed
}
ENUM_DAY_OF_WEEK today = (ENUM_DAY_OF_WEEK) // get actual day and cast to enum
mdtServerTime.day_of_week;
if(today > 0 || today < 6) { // is today in monday to friday?
datetime dtF; // store trading session begin and end time
datetime dtT; // date component is 1970.01.01 (0)
datetime dtServerTime = dtServerDateTime % 86400; // set date to 1970.01.01 (0)
if(!SymbolInfoSessionTrade(sym, today, // do we have values for dtFrom and dtTo?
0, dtF, dtT)) {
return(false); // no, return market is closed
}
switch(today) { // check for different trading sessions
case 1:
if(dtServerTime >= dtF && dtServerTime <= dtT) // is server time in 00:05 (300) - 00:00 (86400)
isOpen = true; // yes, set market is open
break;
case 5:
if(dtServerTime >= dtF && dtServerTime <= dtT) // is server time in 00:04 (240) - 23:55 (86100)
isOpen = true; // yes, set market is open
break;
default:
if(dtServerTime >= dtF && dtServerTime <= dtT) // is server time in 00:04 (240) - 00:00 (86400)
isOpen = true; // yes, set market is open
break;
}
}
return(isOpen);
}
//+------------------------------------------------------------------+
The first part checks for the weekday, Mon - Fri to allow opening trades and trailing stop during the week. In the switch command the actual server time is checked against the broker's symbol session.
Implications:
- If you run this script on every tick or every minute, it will give the exact open hours according to the broker's server like you see in the above image.
- If you run this script every 5 minutes on my broker account, it is correct only on Monday 00:05 - 00:00
- If you run this script on the open of each new bar, the first bar of every weekday get false, market closed also for the last bar on Friday
- If you run this script on PERIOD_D1, PERIOD_W1, PERIOD_MN1 it will not give a true signal for my broker because it runs at 00:00 and market open is at 00:04
Feel free to use it in your EA at your own risk and backtest well.