How to tell if Market is closed?

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

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:

MarketInfo(Symbol(), MODE_TRADEALLOWED)

This method for determining if the market was closed worked perfectly fine prior to the "upgrade", but now it's useless and I'm looking for a good way to check if the market is closed without having to ask the user to input the day of the week and hour+minute+second that the market closes on their broker's server.

Here's what I have now and I'm really hoping there's a better way of doing this because I'd hate for the user to have to manually enter the day of week and time that market closes on their broker's server:

if (MarketInfo(NULL,MODE_TRADEALLOWED) && !isMarketClosed()){
   // TODO
}

bool isMarketClosed() {
   return TimeToStr(TimeCurrent(), TIME_MINUTES|TIME_SECONDS) == "23:59:57"
            && TimeDayOfWeek(TimeCurrent()) == 5;
}

-----------------------------

I also came up with this, although there's a few major drawbacks:

  • it'll have to run the getMarketClose() lookback on every Init
  • it's reliant on the user having data for EURUSD,M1
  • it'll be 1 minute behind of the actual close due to the inability to grab the exact second that the market closes on broker's server
  • Maybe the market doesn't close on Friday for the broker's server


// Global Variable
datetime testTime;

// OnInit()
getMarketClose();

// Regular Program
void getMarketClose() {
   testTime = iTime("EURUSD", PERIOD_M1, 0);    
   if (TimeDayOfWeek(testTime) == 5) {
      testTime -= 60*60*24;
   }
   while (TimeDayOfWeek(testTime) != 5) {
      int temp = iBarShift("EURUSD", PERIOD_M1, testTime)+1;
      testTime = iTime("EURUSD", PERIOD_M1, temp);
   }
}

if (MarketInfo(NULL,MODE_TRADEALLOWED) && !isMarketClosed()){
   // TODO
}

bool isMarketClosed() {
   return TimeToStr(TimeCurrent(), TIME_MINUTES) == TimeToStr(testTime, TIME_MINUTES)
            && TimeDayOfWeek(TimeCurrent()) == TimeDayOfWeek(testTime);
}
To add comments, please log in or register