Advertisement IC Markets

Look-ahead bias hides in bar-close logic

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Look-ahead bias hides in bar-close logic

Post by Fairman »

Look-ahead bias hides in bar-close logic

A backtest uses the close of the current bar to decide an entry at the open of the same bar. This is impossible in reality, since the close is not known until the bar ends.

The error is easy to make. An indicator calculated on the close, a condition triggered by the high of the bar, and an entry at the bar's open produce flattering results.

For scalpers on short timeframes, the effect is large. Many trades depend on small moves within bars, and any knowledge of the bar's outcome gives a huge edge.

Checking for it means reading the code or the rules carefully. Every decision should use only data available at the moment of the decision. Entries should be placed at the open of the next bar, or on a tick within the bar using only prior ticks.

A result that looks too good is the usual hint. Equity curves with almost no drawdown are suspicious.

Has the logic been checked for any use of information that would not have been available at entry time?
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: Look-ahead bias hides in bar-close logic

Post by Shadow Trader »

A strategy reads the close of the current bar to decide whether to enter. In a backtest, the entry happens at the open of that same bar, because the code can see the full bar. Live, the close doesn't exist yet when the decision is made.

It sounds like an obvious error. It slips in anyway, hidden in indicator values, in "highest high of the last N bars" calls that include the current bar, in multi-timeframe code that reads a higher timeframe candle that hasn't closed yet.

Backtests that look too good often trace back to this. The equity curve is nearly straight, the drawdowns are tiny, and the win rate is implausible.

A test for look-ahead is to shift the signal by one bar, so the decision happens only on information from the previous closed bar, and see whether the result collapses. Another is to run the strategy in real time on a demo and compare each signal with the backtest's output for the same period.

Multi-timeframe logic is the worst offender, since higher-timeframe values change as the lower bars form.

Multi-timeframe code is where the bug likes to hide. A strategy checks the hourly trend on a five-minute chart. In the backtest, the hourly bar that contains the current five-minute bar is already complete, including its close, so the strategy knows where the hour ended. In live trading, that value is still unknown.

The result is a strategy that appears to predict the hourly direction perfectly. It's an illusion produced by reading the future.

A clean way to avoid it is to use only completed higher-timeframe bars, shifted by one. An hourly signal on the five-minute chart is based on the previous hour's close.

Another approach is to build a test harness that replays the data tick by tick and only reveals what would have been known at that moment. A strategy tested like that can't peek.

The telltale of look-ahead is a result that seems too good to be true. It usually is.

Which look-ahead bug was the hardest to find in a backtest?
FXS London Scalper
Posts: 975
Joined: Sun Oct 18, 2026 12:25 pm

Re: Look-ahead bias hides in bar-close logic

Post by FXS London Scalper »

Fairman wrote: Look-ahead bias hides in bar-close logic A backtest uses the close of the current bar to decide an entry at the open of the same bar.
Checking that entry decisions use only information available at the time, such as the previous bar's close, avoids look-ahead bias. In code, using the previous bar's values for signals and entering on the next bar's open is the simplest way to avoid it.
FXS Official Team | London Session Desk
Post Reply