Time converstion function slowing down EA
Since the function is to know if market is opened, run it per bar not per tick.
Dua Yong Rew: I noticed that TimeToString and StringToTime is making my EA a lot slower. Is there anything I can improve on the code below? It is a function to check if the market is open for trading
- Why are you converting the date/times into strings?
That makes processing slower. Treat them as datetime directly and mask out the time only. - Why are you reading the sessions on every tick?
They are not going to change suddenly, so read them for an entire week at the beginning of the week or read them once a day at the beginning of the day.
Save the session data in datetime format, or just as time only, and then only check them when necessary, for example when you need to place an order.
void OnTick() { //--- static datetime NewTime=0; bool NewBar=false; if(New_Time!=Time[0]) { NewTime=Time[0]; NewBar=true; TimeTradeServer(time); if(isMarketOpen()) {} } } //+------------------------------------------------------------------+ //| is Market Open | //+------------------------------------------------------------------+ bool isMarketOpen() { //--- datetime start = 0, end = 0, now = StructToTime(time); if(!SymbolInfoSessionTrade(_Symbol, (ENUM_DAY_OF_WEEK) time.day_of_week, 0, start, end)) return(false); string str_start = TimeToString(start, TIME_MINUTES|TIME_SECONDS), str_end = TimeToString(end, TIME_MINUTES|TIME_SECONDS), str_now = TimeToString(now, TIME_MINUTES|TIME_SECONDS); start = StringToTime(str_start); end = StringToTime(str_end); now = StringToTime(str_now); if(start <= now && now <= end) return(true); //--- return(false); }
I noticed that TimeToString and StringToTime is making my EA a lot slower
Is there anything I can improve on the code below?
It is a function to check if the market is open for trading