Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

What Is Look-Ahead Bias – Amibroker Tutorial

1 min read

When you backtest a trading strategy, it’s tempting to use the closing price to make decisions.
But doing that can make your results look unrealistically good. That mistake is called look-ahead bias.

Let’s understand it in plain English – and then fix it with a simple AmiBroker example.


What Is Look-Ahead Bias?

Look-ahead bias happens when your trading strategy uses future information that wouldn’t have been available at the time of trading.

Example:

“Buy when the candle closes above the 20 EMA.”

The problem?
You don’t know where the candle will close until the bar is finished.
So, if your code triggers a buy at that same close, it’s cheating – because it’s using data from the future.


Why It’s a Problem

Look-ahead bias makes your backtest look like a genius – high profits, perfect entries, and zero lag.
But when you trade live, those same signals can’t be executed in real time.

That’s why your live performance never matches your backtest – because the backtest had an unfair advantage.


The Correct Way: Use the Next Bar’s Open

The fix is simple:

Wait for the candle to close, confirm your signal, and take action at the next bar’s open.

This is how real trading works – you only act after the previous bar has completed.


Example: 10–20 EMA Crossover Strategy

Below are two versions of the same strategy — the first one wrong, the second one correct using ValueWhen().


❌ Wrong Version (Look-Ahead Bias Present)

// --- Wrong: Uses same bar's Close ---
ema10 = EMA(Close, 10);
ema20 = EMA(Close, 20);

Buy  = Cross(ema10, ema20);
Sell = Cross(ema20, ema10);

BuyPrice  = Close;   // ❌ Close not known in real time
SellPrice = Close;

Plot(Close, "Close", colorDefault, styleCandle);
Plot(ema10, "EMA10", colorBlue);
Plot(ema20, "EMA20", colorRed);
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, Low, -15);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, High, -15);

This version assumes you could somehow know the closing price and act instantly at that same close — which is impossible.


✅ Correct Version (Bias-Free, Uses Next Bar’s Open)

// --- Correct: Entry at next bar's open ---
ema10 = EMA(Close, 10);
ema20 = EMA(Close, 20);

BuySignal  = Cross(ema10, ema20);
SellSignal = Cross(ema20, ema10);

// Trigger confirmed only after bar closes
Buy  = Ref(BuySignal, -1);
Sell = Ref(SellSignal, -1);

// Use ValueWhen() to capture next bar's open after signal
BuyPrice  = ValueWhen(Buy, Open);
SellPrice = ValueWhen(Sell, Open);

Plot(Close, "Close", colorDefault, styleCandle);
Plot(ema10, "EMA10", colorBlue);
Plot(ema20, "EMA20", colorRed);
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorGreen, 0, Low, -15);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, High, -15);

Here’s what happens:

  • The crossover signal (BuySignal) is confirmed after the bar closes.
  • The actual trade executes at the next bar’s open.
  • ValueWhen() ensures the correct open price is recorded for that signal.

Quick Comparison

VersionEntry Based OnTrade PriceLook-Ahead Bias
WrongSame bar closeClose❌ Yes
CorrectPrevious bar closeOpen (next bar)✅ No

Final Takeaway

Look-ahead bias is the silent killer of backtests.
It gives you a false sense of accuracy and destroys your confidence when live results don’t match.

The golden rule:

If your signal depends on the candle close, trade at the next bar’s open.

So next time you’re backtesting in AmiBroker (or any platform), remember —
real traders wait for confirmation, not prediction.

Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More