How do you handle weekend gaps when backtesting a crypto EA in MT5?
I’d be interested in approaches that preserve a reproducible backtest without treating two different price feeds as interchangeable.
The most holistic solution would be to build a custom chart in MT5 that either:
- Prints your broker-dealer's data during your trading days and prints your "exchange" data during the weekend days via WebRequest()/API, or
- Simply prints your "exchange" data during all days of the week (assuming that the data is close enough for technical analysis purposes only).
- (If you have access to "exchange" data natively in MT5, then you don't need a custom chart).
And then code your trading EA's technical analysis elements to reference your custom/"exchange" symbol, and code your trading EA's order management elements to reference the underlying tradable symbol.
Keep the broker data unchanged and find the gap from the bar times, not the calendar. After the reopen the indicator has fewer samples, so wait for a fixed number of bars before you trust a signal.
//--- OnCalculate, rates[] is indexed as series
datetime prev_bar_time = 0;
int bars_since_gap = 0;
//--- on H1, more than 2 hours between bars means the weekend gap
if(prev_bar_time > 0 && rates[0].time - prev_bar_time > PeriodSeconds(_Period) * 2)
bars_since_gap = 0;
else
bars_since_gap++;
prev_bar_time = rates[0].time;
if(bars_since_gap < 20)
return; // indicator not warm yet
//--- normal signal logic from here
I’m looking at a common issue in crypto EA testing: a broker’s MT5 symbol may have its own trading sessions, while the underlying spot market continues to move over the weekend.
Suppose an EA uses a 20-bar moving average on H1 data. If the broker provides no weekend bars, the indicator covers a different amount of calendar time from a continuous spot series. A signal immediately after the symbol reopens may therefore reflect both the strategy and the missing data.
An exchange such as BYDFi can provide a spot-market reference, but I would not substitute its prices for the broker’s historical ticks in an execution backtest. The instruments, quotes and trading costs may differ.
How do other MQL5 developers handle this?
I’d be interested in approaches that preserve a reproducible backtest without treating two different price feeds as interchangeable.