Forex and CFD prices keep moving over the weekend while the market is closed. When trading resumes, the first price can be far away from Friday's close. Backtests on bar data often treat that jump like any other move, but live it can trigger entries at prices your logic never expected, and it can push stop orders through their levels.
A small gap check at the start of the week lets the EA make a deliberate choice instead of reacting by accident.
Measuring the gap
The idea is simple: compare the open of the first bar of the new session with the close of the last bar before the break. On the H1 chart the break shows up as a time difference much larger than one bar.
// Returns the gap in points if the current bar is the first after a long break, else 0 double SessionGapPoints(const string sym, ENUM_TIMEFRAMES tf, int minBreakHours = 24) { MqlRates r[]; ArraySetAsSeries(r, true); if(CopyRates(sym, tf, 0, 2, r) < 2) return 0.0; long breakSeconds = (long)(r[0].time - r[1].time); if(breakSeconds < (long)minBreakHours * 3600) return 0.0; double point = SymbolInfoDouble(sym, SYMBOL_POINT); if(point <= 0.0) return 0.0; return MathAbs(r[0].open - r[1].close) / point; } Using the time difference instead of checking for Monday makes it work for holiday breaks too, and for brokers whose server week does not start on Monday in your local time.
Turning the number into a decision
Once you know the size of the gap, there are a few reasonable policies. Pick one on purpose and write it into the strategy rules.
- Skip new entries for the first bar or the first N minutes after a large gap, so the spread and the order book can settle.
- Compare the gap with the ATR. A gap of 0.3 ATR is noise; a gap of 2 ATR means the setup the EA saw on Friday no longer exists.
- Cancel or re validate pending orders that were placed before the break, since their levels were chosen against old prices.
input double InpMaxGapAtr = 1.0; // block entries if gap is larger than this many ATRs input int InpAtrPeriod = 14; int atrHandle = INVALID_HANDLE; int OnInit() { atrHandle = iATR(_Symbol, PERIOD_H1, InpAtrPeriod); return atrHandle == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED; } bool GapAllowsTrading() { double gapPts = SessionGapPoints(_Symbol, PERIOD_H1); if(gapPts == 0.0) return true; double atr[]; if(CopyBuffer(atrHandle, 0, 1, 1, atr) < 1) return false; // no data yet, stay out double atrPts = atr[0] / SymbolInfoDouble(_Symbol, SYMBOL_POINT); bool ok = gapPts <= InpMaxGapAtr * atrPts; PrintFormat("Session gap %.0f pts vs ATR %.0f pts, trading %s", gapPts, atrPts, ok ? "allowed" : "blocked for this bar"); return ok; } Call GapAllowsTrading() inside your new bar logic before any entry. Because it only looks at the current and previous bar, the block lasts exactly one bar on the chosen timeframe, which is usually enough for spreads to normalize.
Test it where the gaps are
The Strategy Tester will reproduce gaps if you test with real tick data across many weekends. Look at the journal lines from the PrintFormat above and check that the blocked bars match the weekends you remember as volatile. If the EA never blocks anything over two years of data, your threshold is too loose to matter.
A gap rule rarely changes the headline backtest numbers much, but it removes a class of live trades that the strategy was never designed to take.
