Code Review Needed (Check if market is open / closed)

To add comments, please log in or register
Alexander Martinez
1093
Alexander Martinez  

I wrote a function to check if the market / session is open.

I'm mainly interested to see if there's a better way of doing it or if you think there's any error in my code that won't give me the desired result.

For the backstory, please read my other thread: https://www.mql5.com/en/forum/292843

bool isSessionOpen() {
   datetime t1, t2;
   bool result = SymbolInfoSessionTrade(
      Symbol(),
      TimeDayOfWeek(TimeCurrent()),
      0,
      t1,
      t2
      );
   if (result){
      datetime currentDate = StrToTime(TimeToString(TimeCurrent(),TIME_DATE));
      datetime sessionStart = currentDate + t4;
      datetime sessionEnd = currentDate + t5;
      if (TimeDayOfWeek(currentDate) == FRIDAY) {
         //--- subtract 10 seconds because last tick may not arrive on exact time market closes
         sessionEnd -= 10;
      }
      if (TimeCurrent() >= sessionStart && TimeCurrent() <= sessionEnd){
         Comment("Market / Session is open");
         return true;
      }
   }
   return false;
}
How to tell if Market is closed?
How to tell if Market is closed?
  • 2018.12.01
  • www.mql5.com
Oanda recently "upgraded" all of its clients to the V20 servers, but the servers are giving a value of "true" even if the market is closed for: Thi...
To add comments, please log in or register