So you are basically checking if the current time is within the allowed trading session time for that symbol.Centaur wrote: Thu Oct 27, 2022 1:25 am Really interesting approach, I've also done some reading and they suggest using the function SymbolInfoSessionTrade, so I came up with the following function (seems to be working so far):Code: Select all
//+------------------------------------------------------------------+ //| Function: Check if market is Open | //+------------------------------------------------------------------+ bool MarketOpen() { datetime from = 0; datetime to = 0; datetime now = TimeCurrent(); uint session_index = 0; MqlDateTime today; TimeToStruct(now, today); if(SymbolInfoSessionTrade(_Symbol, (ENUM_DAY_OF_WEEK) today.day_of_week, session_index, from, to)) { MqlDateTime from_time; MqlDateTime to_time; TimeToStruct(from, from_time); TimeToStruct(to, to_time); int from_time_seconds = from_time.sec + from_time.min * 60 + from_time.hour * 3600; int to_time_seconds = to_time.sec + to_time.min * 60 + to_time.hour * 3600; int now_seconds = today.sec + today.min * 60 + today.hour * 3600; if(now_seconds >= from_time_seconds && now_seconds <= to_time_seconds) return(true); return(false); } return(false); }
That is a nice idea. I can extend my code to first check if trading is allowed and then call OrderSend. If trading is not allowed, I can do what I have mentioned in my post above.