Look-Ahead Bias in Backtesting
How a backtest can use data it would not have had when the trade was placed, and how to find and fix it in AFL
Look-ahead bias — also called a future leak — occurs when a backtest uses information that wasn't actually available at the moment a trade was placed. The strategy appears profitable because it's secretly using tomorrow's data to make today's decisions. In live trading, that data doesn't exist yet, and results collapse.
This is one of the most dangerous errors in strategy development. Unlike a crash or a wrong calculation, a future leak is invisible: the backtest runs cleanly, the equity curve looks real, and the win rate looks great. You only discover the problem after trading it with real money.
When Is Data "Settled"?
A bar covers a fixed time period (one minute, five minutes, one day, etc.). It has five values: Open, High, Low, Close, Volume. The Open is available the instant the bar begins. High, Low, Close, and Volume are only finalized when the bar ends. Any indicator computed from the close — moving averages, Bollinger Bands, RSI — is only settled when the bar closes.
| Moment | What you know | What you do NOT know yet |
|---|---|---|
| Bar i opens | O[i], and everything from bar i−1 and earlier | H[i], L[i], C[i], V[i]; any indicator using C[i] |
| During bar i | O[i], running H/L/V (not final) | Where bar i will close; final indicator values |
| Bar i closes | Everything through bar i; all indicators updated | O[i+1] (bar i+1 hasn't opened) |
| Bar i+1 opens | Everything through bar i, plus O[i+1] | H[i+1], L[i+1], C[i+1] |
Entry Types and What Data Is Available
Different entry styles have different contracts about what data is legally available. Being clear about which type you're implementing is the first step to avoiding leaks.
What a Leak Looks Like
The most common form of leak in next-bar-open strategies: an indicator computed from the current bar's close is used as the stop or entry price for a trade that fills at the next bar's open. The indicator only exists after the bar closes — but the trade is placed before the bar closes.
The Fix
Shift the indicator reference back by one bar. Use Ref(Indicator, -1) — the
previous bar's value — which is fully settled before the entry bar opens. If using AFL,
also delay the trigger with Ref(Signal, -1) so the entry fires one bar after
the signal condition, and the signal bar index stays consistent.
Common Sources of Leaks in AFL
1. Indicator at bar i used for a next-bar entry
// LEAK: MA[i] uses C[i], but entry is at open of bar i
Trigger = C < MA(C, 20); // uses C[i]
Short[i] = Trigger[i];
StopPrice = MA(C, 20) + 0.10; // MA[i] — not known at bar i open
// FIX: use prior bar's indicator for stop on next-bar entry
Signal = C < MA(C, 20); // uses C[i] — fires on signal bar
Trigger = Ref(Signal, -1); // fire one bar later
Short[i] = Trigger[i];
StopPrice = Ref(MA(C, 20), -1) + 0.10; // MA[i-1] — settled before bar i opens
2. Ref(array, 1) — explicit look-ahead
In AFL, Ref(x, -N) looks N bars back (safe).
Ref(x, N) with positive N looks N bars forward — a direct future leak.
// LEAK: tomorrow's open used to set today's stop
TomorrowOpen = Ref(O, 1);
Stop = TomorrowOpen * 1.02;
// Ref(x, 1) is occasionally valid in exploration display (e.g., color-coding
// the bar before an event) — but never in any value that feeds a trade decision.
3. SignalBarIndex / trigger mismatch
SignalBarIndex is used to capture metric columns at the time the setup formed.
If it doesn't match the bar where the trigger actually fires, not only are the columns
wrong — it's a sign the trigger and pricing are inconsistent, often hiding a leak.
// MISMATCH: trigger fires using C[i] but code claims signal was bar i-1
if (Trigger[i]) // Trigger[i] uses C[i]
{
SignalBarIndex = i - 1; // ← wrong: claims signal was prior bar
Short[i] = 1;
stop_price = StopPrice[i]; // StopPrice[i] = f(C[i]) — leaked
}
// CONSISTENT next-bar entry:
if (Trigger[i]) // Trigger[i] = Signal[i-1] via Ref(Signal,-1)
{
SignalBarIndex = i - 1; // ✓ signal was prior bar
Short[i] = 1;
stop_price = StopPrice[i]; // StopPrice[i] = Ref(Indicator,-1)[i] = Indicator[i-1] ✓
}
4. Daily indicator leaking into intraday
// LEAK: today's daily close used during intraday (bar closes at 4pm, but
// you're making decisions at 10am using that same day's close)
DailyClose = TimeFrameExpand(TimeFrameGetPrice("C", inDaily), inDaily, expandLast);
Trigger = C > DailyClose; // "today's" daily close not known until 4pm
// FIX: use yesterday's settled daily close
YesterdayClose = Ref(DailyClose, -1);
Trigger = C > YesterdayClose; // prior day settled ✓
5. HOD/LOD comparisons (the always-false trap)
// LEAK / BUG: HOD includes bar i's own high, so H > HOD is always false
HOD = Highest(H, BarsSince(newday) + 1);
NewHigh = H > HOD; // can never be true
// FIX: compare today's HOD against previous bar's HOD
NewHigh = HOD > Ref(HOD, -1); // new high when today's HOD exceeds prior bar's HOD
How to Audit a Strategy
Work backwards from the entry. For every value that feeds into a trading decision, ask: "What bar's data does this require, and is that bar fully closed before the order fires?"
- What bar does
Trigger[i]depend on? Trace every array in its expression. - Does
Short[i] = 1mean "fill during bar i" or "fill at bar i+1's open"? - Does
ShortPrice[i]reference any value computed fromC[i]? - Does
StopPrice[i]reference any value computed fromC[i]? - Does
SignalBarIndexmatch the bar where the trigger actually fires? - Is
Ref(x, 1)used anywhere in trading logic (not just display)? - For multi-timeframe: is today's daily close used during today's intraday bars?
Ref(SomeIndicator, -1) gives you bar i−1's value,
which is fully settled before bar i opens.
Clean Pattern Reference
// ── Pattern A: same-bar close entry ────────────────────────────
// Signal and fill both at bar i's close. Any bar-i value is OK.
Short[i] = Trigger[i]; // Trigger uses C[i] — OK
ShortPrice[i] = C[i]; // fill at close — OK
stop_price = SomeIndicator[i]; // uses C[i] — OK here
// ── Pattern B: next-bar open entry ─────────────────────────────
// Signal on bar i, fill at bar i+1's open. Use Ref for all indicators.
Signal = <condition using C[i]>;
Trigger = Ref(Signal, -1); // Trigger[i] = Signal[i-1]
ShortPrice = O; // O[i] — known at entry ✓
StopPrice = Ref(SomeIndicator, -1) + margin; // Indicator[i-1] ✓
InitialStopDistance = StopPrice - ShortPrice;
TargetPrice = ShortPrice - InitialStopDistance * TARGET_R;
PositionSize = IIF(InitialStopDistance > 0,
(RISK / InitialStopDistance) * ShortPrice, 0);
SignalBarIndex = i - 1; // consistent with above ✓
// ── Pattern C: stop at prior bar's high/low ────────────────────
// Always safe for next-bar entries.
stop_price = Ref(H, -1)[i] + 0.01; // prior bar's high — settled ✓
stop_price = Ref(L, -1)[i] - 0.01; // prior bar's low — settled ✓
Ref(x, -1). The hard part is
noticing the leak exists in the first place — especially when the indicator changes slowly
and the difference between Indicator[i] and Indicator[i-1]
looks small on any individual bar.