Home / Blog / Closed vs Live Candles: Which Bar Your EA Should Read

Closed vs Live Candles: Which Bar Your EA Should Read

Trader looking at a computer screen with a financial chart with a thought bubble

An indicator read on a closed candle gives a final value. Read on the live candle, the one still forming, it gives a value that can change with every tick until the bar closes. That choice decides how many signals an Expert Advisor sees, how many of them turn out to be false, how early it acts, and whether its backtest says anything about how it will trade live. At barmenteros FX we treat it as a design decision to make on purpose, in code, not as a side effect of which index a developer happened to type.

This article covers the trade-off, what “closed” and “live” mean in MQL4 code, a closed-bar crossover you can adapt, and why the choice decides which Strategy Tester results you can trust.

Closed candle vs live candle: the trade-off

AspectClosed candle (bar 1)Live candle (bar 0)
Indicator valueFinal: the bar has finishedProvisional: recalculated as prices arrive
SignalsFewer, and they stay once givenMore, and some vanish before the bar closes
TimingAt the close, one bar laterDuring the bar, as soon as the condition holds
Main riskLag when the market moves fastFalse signals when the market chops
Strategy TesterOpen prices only tests the same signalsNeeds Every tick to model the bar’s inside

Take a 20-period and a 50-period moving average. On closed candles, a cross of the 20 above the 50 is a buy signal only once the candle that produced it has finished, so the signal is certain but late. On the live candle, the same cross triggers the moment the averages touch, which is earlier, but a reversal before the close can undo it. An RSI threshold behaves the same way: 70 on a closed candle is a fact, 70 on the live candle is a reading that may be 68 at the close.

What “bar 0” and “bar 1” mean in MQL4

MQL4 indexes price series from the newest bar backwards. The documentation for Close[] (opens in a new tab) says its elements “are indexed in the reverse order” and that “the current bar which is the last in the array is indexed as 0”. The current bar is still forming, so its close is simply the most recent price, and any indicator computed on bar 0 moves with it. Bar 1 is the last bar that finished, and its values no longer change.

Indicator functions take the same index through their last parameter. For iMA() (opens in a new tab), shift is the “index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago)”. A shift of 0 reads the live candle and a shift of 1 reads the last closed one. An EA that passes 0, or that kept a hard-coded 0 from a quick prototype, trades on live candles whether its author meant it to or not.

MetaTrader 5 numbers bars the same way in its timeseries functions, but the arrays an EA copies indicator values into are not series-indexed unless the code says so. That trap is one of the four MT4 to MT5 migration failures that pass the backtest, and the MQL5 version of the logic below is in our post Closed Bar or Forming Bar (opens in a new tab) on the MQL5 blog.

A closed-bar crossover in MQL4

Two pieces make an EA read closed candles: run the signal logic once per bar, and read the bars that have finished.

Time[] (opens in a new tab) holds the “open time of each bar of the current chart”, indexed the same way, so a change in Time[0] marks the first tick of a new bar:

// True on the first tick of each new bar, and on the first tick after the EA starts.
bool IsNewBar()
  {
   static datetime lastBarTime = 0;
   if(Time[0] == lastBarTime)
      return false;
   lastBarTime = Time[0];
   return true;
  }

The crossover compares two consecutive values of each average. The signal bar decides which two: bars 1 and 2 for a closed-candle signal, bars 0 and 1 for a live one.

input int InpSignalBar  = 1;    // Signal bar (0 = forming, 1 = last closed)
input int InpFastPeriod = 20;   // Fast moving average period
input int InpSlowPeriod = 50;   // Slow moving average period

// True when the fast average crossed above the slow one between bar signalBar+1 and bar signalBar.
bool CrossedUp(const int signalBar)
  {
   double fastNow  = iMA(NULL, 0, InpFastPeriod, 0, MODE_SMA, PRICE_CLOSE, signalBar);
   double slowNow  = iMA(NULL, 0, InpSlowPeriod, 0, MODE_SMA, PRICE_CLOSE, signalBar);
   double fastPrev = iMA(NULL, 0, InpFastPeriod, 0, MODE_SMA, PRICE_CLOSE, signalBar + 1);
   double slowPrev = iMA(NULL, 0, InpSlowPeriod, 0, MODE_SMA, PRICE_CLOSE, signalBar + 1);
   return fastPrev <= slowPrev && fastNow > slowNow;
  }

void OnTick()
  {
   if(Bars < InpSlowPeriod + InpSignalBar + 2)
      return;                      // not enough history for the slow average
   if(InpSignalBar >= 1 && !IsNewBar())
      return;                      // closed-candle mode: evaluate once per bar
   if(CrossedUp(InpSignalBar))
     {
      // entry logic
     }
  }

With the signal bar set to 1, the EA looks at the market once per bar and only at values that can no longer change. Set to 0, it re-evaluates on every tick and can act on a crossover that the close then undoes. A live-candle EA also needs its own guard against firing again on every tick while the same crossover holds; that latch is left out here.

Why the choice decides which backtests you can trust

MetaTrader 4’s Strategy Tester offers three modelling modes, and its help (opens in a new tab) describes them in terms of exactly this choice:

  • Every tick is “the most accurate method of modeling prices within a bar”.
  • Control points “is intended for a crude estimate of experts efficiency that trade within the bar”.
  • Open prices only exists because “some automated trading systems do not depend on properties of modeling within a bar, they trade on completed bars”.

A closed-candle EA belongs to that last group. It only decides when a bar opens, so the fast mode tests the same signal decisions it will make on a live chart. A live-candle EA does not. Its signals depend on prices inside the bar, so a result from Open prices only, or even from Control points, tests decisions the EA will never make for real. Only Every tick models the movement those signals react to, and even that movement is rebuilt from history, “based on all available least timeframes” in the words of the MT4 help (opens in a new tab).

MetaTrader 5 documents the same split. In its Open prices only mode (opens in a new tab) “the OnTick() function of the Expert Advisor runs only at the beginning of the bar”, and the mode is “well suited for testing strategies, which process deals only at the opening of the bar”. It is still an approximation for order handling: “stop levels and pending may trigger at a price that differs from the specified one”. For the documentation’s own Moving Average example, where “all of the decisions are made at the opening of the bar”, the documentation reports that “the graphs on different testing modes are exactly the same”.

So when an EA’s live trades do not match its backtest, check which bar it reads and which mode it was tested in before looking anywhere else. Other causes, such as a broker spread that changed after the test period or settings tuned to the history, are slower to rule out.

Repainting is a separate problem

Reading bar 1 protects a signal from the forming bar. It does not protect it from an indicator that rewrites its values on bars that have already closed. Such an indicator draws clean historical signals that it could not have shown in real time, and no choice of shift fixes that. We explain what a one-line “remove the repainting” request really asks for in why simple MT4 tools are not simple. Check an indicator for it in the tester’s visual mode before you trust it in either setting.

How to choose between closed and live candles

  • Choose closed candles when the strategy is defined on completed bars, when a false entry costs more than a late one, or when you need fast backtests you can compare.
  • Choose the live candle when entry timing inside the bar is the edge. Accept that the EA then needs Every tick testing, a re-entry guard, and tolerance for signals that vanish.
  • In either case, make the signal bar an input, as the Signal bar setting above does. Run both settings over the same period in Every tick mode, and compare the number of signals as well as the equity curves.

What we would rather not see is the choice made by accident, because a prototype happened to read index 0.

Frequently Asked Questions

Should an EA trade on the closed candle or the live candle?

Neither is right for every system. Closed candles give final values, fewer false signals and backtests that the fast Open prices only mode can run faithfully, at the cost of acting one bar later. The live candle reacts sooner but produces signals that can vanish before the bar closes, and its backtests need Every tick modelling. Make the choice an input and test both settings on the same data.

What is the difference between Close[0] and Close[1] in MQL4?

Close[0] belongs to the current bar, which is still forming, so its value is the latest price and changes with every tick. Close[1] belongs to the last bar that closed, so its value is final. Indicator functions such as iMA() follow the same numbering through their shift parameter.

Why does my EA trade differently in the Strategy Tester than on a live chart?

One cause to rule out first is a signal read on the live candle and tested in Open prices only or Control points mode. Those modes do not reproduce the price movement inside each bar, so the backtest evaluates signals the EA will not see live. Test such an EA in Every tick mode, or move its signal to the last closed bar. Spread changes and over-optimization are other causes worth checking.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get a free quote

Fixed price within 24–48 hours. No subscriptions, no demos.