Error [Market Closed] when backtesting in MT5 on a custom symbol - page 2

 

To check if market is close  (brokers trading hours / trading holiday etc.) I suggest this code:

if (SymbolInfoInteger(Symbol(), SYMBOL_TRADE_MODE) != SYMBOL_TRADE_MODE_FULL)

return;

Place this first in the OnTick() function.

I had this similar problem, and it boiled down to the IsNewBar() function when running on the daily, weekly and monthly, all new bars starting at 00:00, hence none of my trades will get processed because this fell outside my brokers trading sessions.

Solution was to check IsNewBar() for the daily, weekly and monthly on the 12hr timeframe, hence the IsNewBar() function will look like this:

//+------------------------------------------------------------------+

//| Function: Execute code only on a new bar, and not every tick     |

//+------------------------------------------------------------------+

bool isNewBar()

  {

   static datetime last_time = 0;

   ENUM_TIMEFRAMES timeframe = inp_timeframe;

   if(timeframe == PERIOD_D1 || timeframe == PERIOD_W1 || timeframe == PERIOD_MN1)

     {

      timeframe = PERIOD_H12;

     }

   datetime lastbar_time = (datetime) SeriesInfoInteger(Symbol(), timeframe, SERIES_LASTBAR_DATE);

   if(last_time != lastbar_time)

     {

      last_time = lastbar_time;

      return(true);

     }

   return(false);

  }

So in other words the daily check will occur at 12:00 instead of 00:00...

Reason: