DEV Community

Omais Ali
Omais Ali

Posted on

Detecting the Monday open gap in MQL5 and deciding what your EA should do

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;
}
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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.
  2. 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.
  3. 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;
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
alicexixi85 profile image
Dejun (Alice) Chen •

Keying off the time difference instead of the weekday is a nice touch. Two things I'd add. The gap bar's true range includes the jump from Friday's close, so H1 ATR(14) stays inflated for the next 14 bars, and anything that sizes stops off ATR runs wider than the market warrants for most of Monday. And the rule only gates new entries: a position held over the weekend takes the gap directly, past its stop. On a prop account that loss lands at the start of a new day, so it can eat most of the daily limit before the EA runs a single check. A Friday-close check that sizes carried positions against the largest recent weekend gap covers that side.

Collapse
 
verdictfx profile image
VerdictFX •

Using the time delta instead of checking for Monday is a good call, since it also covers holiday breaks and odd server-week starts. One thing worth considering is measuring the gap against ATR taken from before the break, as you do, but also logging the spread on the first bar. Weekend-open spreads are often several times normal, so a gap that looks acceptable in ATR terms can still make a stop-loss or market entry fill badly. It may also help to widen the block from one bar to a fixed number of minutes, so the rule behaves the same on M5 and H1.