# Execution delay and look-ahead bias in backtests, with an example

Canonical URL: https://marketheist.io/portfolio/learn/execution-delay
By Jeffrey Lin · Published 2026-11-16 · Updated 2026-11-17 · Data through Sep 2026
Category: Validation

## Key takeaways

- A backtest that decides with today's closing price and also collects today's gain is peeking: in real trading the close is only known once the day is over.
- The fix is a delay between the signal and the first return it earns. In MarketHeist a delay of one bar means the signal from a day's close earns from that close onward; two bars means it waits for the next day's close.
- Look-ahead makes weak rules look spectacular. A fast rule that reacts to each day's price can show an extraordinary growth rate with no delay and an ordinary one with the delay in place.
- The faster the rule, the more a one-bar shift matters. A slow trend rule that trades a few times a year barely notices it; a rule that flips every few days can change from a large gain to a loss.
- Look-ahead also hides in data rather than timing: figures revised after their release, index members chosen with hindsight, and settings picked after seeing the whole history. The delay fixes only the timing.

## Definition

Look-ahead bias is the error of letting a backtest act on information that was not available when the decision would have been made, most often by using a bar's closing price to choose a position that also earns that same bar's return. Execution delay is the number of bars between the bar whose data produce a signal and the first return the resulting position earns; MarketHeist uses one bar by default, so a signal earns nothing until after the close that created it.

Also known as: look-ahead bias, lookahead bias, peeking bias, signal lag, next-bar execution, same-bar execution, trade on the close, future leak.

## How it works

A daily backtest works with closing prices. On each bar the rule reads the indicator, which uses that bar's close, and decides whether to be invested. The question is which return the decision earns. A bar's return is the move from the previous close to this close. If the decision made with this close is also given this bar's return, the backtest has used the close twice, once to decide and once to profit. No trader can do that, because the close does not exist until the bar has ended.

Execution delay fixes the timing by shifting the position series forward. With a delay of one bar, the position decided at the close of day t is held over day t + 1, from the close of t to the close of t + 1. That assumes the trade fills at the same closing price the signal was computed from, which in practice needs an order sent just before the close on nearly final prices. A delay of two bars is stricter: the trade fills at the next day's close, a full day after the signal.

Look-ahead bias is like betting on a race after seeing the finish: every bet looks clever, and none of it says anything about the horses.

How much damage it does depends on how closely the signal is tied to the same bar's return. A two-hundred-day average barely moves on one day's close, so letting it collect the same day's return changes little. A rule that compares the price with its five-day average flips on large daily moves, so with no delay it is long on days that ended up and flat on days that ended down, by construction. The backtest then reports gains that are only the market's own daily moves, sorted with hindsight.

Other kinds of look-ahead enter through the data: economic series revised after they were first published, an index's current members tested as if they had been chosen years ago, which is survivorship bias, and parameters chosen because they looked strongest on the full history. Execution delay addresses none of these; it only makes sure a decision never collects a return that was already known when it was made.

## Formula

**The strategy's return with an execution delay of d bars**

```
r_t = \frac{P_t}{P_{t - 1}} - 1, r_t^{strat} = p_{t - d} r_t
```

where:

- Pₜ: adjusted closing price of bar t
- rₜ: the asset's return over bar t, from the close of t − 1 to the close of t
- pₜ: position decided with data up to and including the close of bar t (1 = long, 0 = cash)
- d: execution delay in bars: 0 = look-ahead, 1 = MarketHeist's default, 2 = fill at the next close

With d = 0 the position decided with the close of bar t earns the return that ends at that same close, which no one could have traded.

**Why a zero delay inflates returns**

```
\bar{p_{t - d} r_t} = \bar{p} \bar{r} + Cov (p_{t - d}, r_t)
```

where:

- p̄, r̄: average position (the share of bars invested) and average bar return over the test
- Cov: covariance across bars, population form

A strategy's average return is time in the market times the average return, plus the covariance between position and return. With d = 0 a rule that goes long after up closes has a positive covariance built in. With d of 1 or more, the covariance measures what a rule is meant to find: whether the signal anticipated the next return.

## Worked example

SPY's daily bars from Oct 2006 to Sep 2026, about 20 years, with one fast rule: hold SPY while its close is above its 5-day simple moving average, otherwise cash. The rule is run three times, changing only the execution delay. No trading costs; cash earns nothing.

With no delay, so that each day's position also collected that day's return, the rule grew 120.5% a year with a Sharpe ratio of 6.56, far above the 11.1% a year and Sharpe ratio of 0.64 of holding SPY. 99.6% of its trades ended with a gain. None of it was achievable: the rule was long on days that closed above the average, and those were mostly days that had just gone up.

With the standard one-bar delay, each close's decision earned the following day's return, and the rule grew 0.6% a year with a Sharpe ratio of 0.11; 41.7% of its trades ended with a gain. With two bars, trading at the next day's close, it grew 3.5% a year with a Sharpe ratio of 0.34.

The number of trades barely moved, 670, 671 and 671, because the trades were the same ones shifted by a day. The gap between no delay and one bar, 120.5% against 0.6% a year, is the size of the look-ahead in this test. It is large because the rule is fast: an average of the last five closes is dominated by the latest one.

With the delay in place, this is what a short-term momentum rule on SPY looked like over these years: 0.6% a year against 11.1% for holding the fund, before any trading costs on its 33.6 round trips a year. A slow rule such as the golden cross shifts only a few trades by a day each year and is barely affected by the delay.

Data through Sep 2026. Reproduce id: 297eed5183c59445 (this guide, its lab setup and the data month).

## How MarketHeist computes it

- **Data**: Daily bars (open, high, low, close, volume) of the fund as the MarketHeist data service serves them to the backtester: prices adjusted for splits and dividends, so a dividend counts as reinvested. They come from a snapshot taken each month and cut at the data month of this page, and are not rounded. The fund's own history, not an extension: a fund younger than the window is measured over the years it has.
- **Window**: The worked example and the lab measure the last 20 years of trading days, Oct 2006 to Sep 2026 (5030 days), ending at the data month of this page. Every setup, and buy and hold, is measured over exactly these days; an indicator is warmed up on the days before them, so changing a setting never moves the window, and a setting that needs more history than exists before the window is refused rather than measured over fewer days.
- **Backtest**: The backtester's own engine, in the order it runs it: the indicator, the position rule (or entry and exit triggers) and the optional regime filter produce a position for every day of the fund's whole history; the position is then shifted forward by the execution delay and applied to each day's return over the window. Cash earns 0, there is no leverage, and a one-way cost in basis points is charged on the size of every change of position. Round trips are the closed entries of the trade log (a trade still open on the last day is not counted); the win rate is the share of them with a gain; the payoff ratio is the average win over the size of the average loss; expectancy is the average return per trade; the per-trade profit factor is the sum of the winning trades' returns over the size of the sum of the losing ones, while the per-day profit factor sums days.
- **Years and volatility**: A year is 252 trading days, the engine's convention: trades per year is round trips over days divided by 252, the CAGR is the growth multiple to the power 252 over the days, and volatility is the sample standard deviation of the daily returns times the square root of 252.
- **Indicator statistics**: The indicator's main output over the window: its last value, its 10th, 50th and 90th percentile (linear interpolation between ranks), and the share of days the rule alone says long, before the delay and any regime filter. Every output of the indicator is also summarized by its median and the share of days below 0.
- **Buy and hold**: The fund held every day of the window with dividends reinvested. The largest one-day gain and loss are the highest and lowest daily returns; the CAGR without the 10 or 30 largest gains (or losses) replaces those days' returns with 0 and recompounds.
- **Bar return**: Close-to-close percentage change of the adjusted close: the return of bar t is Pₜ ÷ Pₜ₋₁ − 1. Opening prices are not used. (`pctChange` in packages/shared/engine/utils.ts)
- **Execution delay**: The position series is shifted forward by d bars (as pandas shift), so the position decided with bar t's data earns the return of bar t + d. Default 1; the backtester accepts 0 to 5. Bars where the shifted position or the return is undefined are dropped before any metric is computed. (`shift` in packages/shared/engine/utils.ts)
- **What a one-bar delay assumes**: A fill at the closing price of the bar that produced the signal. Indicators are computed from that close, so the assumption is slightly optimistic for a trader who sees the final close only after the market shuts; a delay of 2 (a fill at the next close) is the conservative check. (`runBacktest` in packages/shared/engine/backtest.ts)
- **Indicators look back only**: Every built-in indicator uses trailing windows that end at the current bar (no centred windows, no future values), so with a delay of 1 or more no position depends on data after the bar it trades. (`compute` in packages/shared/engine/indicators/ma_crossover.ts)
- **Zero delay**: Allowed, to measure how much a result depends on timing. Because every indicator uses the bar's own close, a delay of 0 always lets the position see the return it earns. The MCP tool's description asks AI assistants to pass a delay of 1.
- **Costs**: Transaction costs are charged on the bar where the delayed position changes, so a longer delay moves the cost with the trade. (`runBacktest` in packages/shared/engine/backtest.ts)
- **Engine functions**: `computePosition` (packages/shared/engine/run_basic.ts), `generatePosition` (packages/shared/engine/positions.ts), `applyRegimeFilter` (packages/shared/engine/regime_filter.ts), `generateTriggeredPosition` (packages/shared/engine/triggered_position.ts), `runBacktest` (packages/shared/engine/backtest.ts), `getIndicator` (packages/shared/engine/indicators/registry.ts)

## Execution delay and look-ahead bias compared with its neighbours

What each execution delay means on daily bars

| Setting | Signal computed from | Position earns | Fill it assumes | Tradable? |
| --- | --- | --- | --- | --- |
| Delay 0 | The close of day t | Day t's own return, from the close of t − 1 | At the previous close, before the signal existed | No: the close is used to decide and to profit |
| Delay 1 (default) | The close of day t | From the close of t to the close of t + 1 | At the close of day t | Approximately: needs an order just before the close |
| Delay 2 | The close of day t | From the close of t + 1 onward | At the close of day t + 1 | Yes, with a day's wait |
| Next day's open | The close of day t | From the open of day t + 1 | At the next open | Not modelled: the engine uses closing prices only |

## Limitations

- Closing prices only. The engine fills every trade at a close; opening gaps, intraday stops and the bid-ask spread at the close are not modelled.
- A one-bar delay assumes trading at the very close that produced the signal. That is achievable only approximately, with an order placed on prices a few minutes before the close; a two-bar delay is the conservative version.
- The delay fixes timing look-ahead only. Settings chosen after seeing the results, revised data and survivorship are separate biases with their own checks.
- The example rule was chosen to show the effect at its largest. Slow rules are much less sensitive to the same shift, so the gap here is not a typical size.
- One fund, one window, no costs and no interest on cash.

## Frequently asked questions

**What is look-ahead bias in backtesting?**

Using information in a backtest before it would have been available, so the simulated decisions know something a real trader could not have known. The most common case is deciding with a bar's closing price and also collecting that bar's return; others are revised data, today's index members and settings chosen on the full history.

**Is an execution delay of one bar or two bars more realistic?**

One bar is the usual convention and assumes a fill at the signal's own close. Two bars assumes a fill at the next close and is the conservative case. If a result holds at two bars as well as one, it does not depend on split-second timing; if it collapses, the rule is living on the last bar's move.

**Why does MarketHeist allow a delay of zero at all?**

To measure the look-ahead. Running the same rule at zero and at one bar shows how much of a result comes from timing alone. Every built-in indicator uses the bar's close, so a zero delay is never a tradable setting.

**Does look-ahead bias matter for slow strategies?**

Much less. A rule built on a long average trades rarely and its signal barely depends on any one day, so a one-day shift moves few returns. It matters most for fast rules, short lookbacks and any signal built from the same bar's price change.

**How can I tell whether a backtest has look-ahead bias?**

Warning signs are a win rate far above what similar rules show, a Sharpe ratio that would be extraordinary for any real fund, and results that collapse when the execution delay goes from zero to one bar. Checking the delay and the data dates is the first step for any backtest that looks too good.

## References

1. Chan, E. P. (2009) Quantitative Trading: How to Build Your Own Algorithmic Trading Business. Wiley. Look-ahead bias and other common backtesting errors.
2. López de Prado, M. (2018) Advances in Financial Machine Learning. Wiley. Backtesting pitfalls, including look-ahead and selection bias.
3. Bailey, D. H., Borwein, J. M., López de Prado, M. and Zhu, Q. J. (2014) Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance <https://www.ams.org/notices/201405/rnoti-p458.pdf>. Notices of the American Mathematical Society 61(5).

## Try it

App: https://marketheist.io/backtest/?c=eyJ0aWNrZXIiOiJTUFkiLCJmcmVxdWVuY3kiOiIxZCIsImluZGljYXRvcl9pZCI6Im1hX2Nyb3Nzb3ZlciIsImluZGljYXRvcl9wYXJhbXMiOnsic2hvcnRfd2luZG93IjoxLCJsb25nX3dpbmRvdyI6NSwibWFfdHlwZSI6IlNNQSJ9LCJwb3NpdGlvbl9ydWxlIjp7InR5cGUiOiJ0aHJlc2hvbGQiLCJ0aHJlc2hvbGQiOjEsImRpcmVjdGlvbiI6ImFib3ZlIn0sImV4ZWN1dGlvbl9kZWxheSI6MSwidHJhbnNhY3Rpb25fY29zdHNfYnBzIjowLCJsZXZlcmFnZSI6eyJtb2RlIjoibm9uZSJ9fQ

MCP prompt (MarketHeist MCP server, https://marketheist.io/mcp):

```
Using MarketHeist.io, backtest SPY on daily bars holding while the close is above its 5-day simple moving average (ma_crossover with short window 1, long window 5, threshold 1.0), once with execution delay 0 and once with execution delay 1, and explain how much of the zero-delay result is look-ahead bias.
```

Historical simulation, not advice.
