TradePoseResearch
How a backtest avoids look-ahead bias: bar timestamps, shift, and what happens inside one bar
Backtest mechanics

How a backtest avoids look-ahead bias: bar timestamps, shift, and what happens inside one bar

A backtest that sees the future looks better than any strategy can trade. The two most common ways it happens are quiet: a signal reads a bar’s close before that bar has finished, or the backtest assumes the convenient order of events inside a bar. This post walks through both with simulated prices on which no rule can have an edge, and explains the rules TradePose uses to keep them out.

Conclusion

Every decision in TradePose is made at the open of a bar: the current open is known, the current high, low, close and volume are not. Bars are labelled by their start time and other timeframes are joined backward onto the main timeline without any automatic delay, so a strategy that wants a finished higher-timeframe bar declares shift=1 on that timeframe’s own bars. Shifting after the join only moves one row of the main timeframe; on a random walk that mistake turns a coin flip into a 66.6% win rate. The engine does not treat a shift as proof of causality, so checking the lineage of each input stays with the author. Inside a bar, where OHLC data does not record which price came first, the engine always picks the outcome it can prove: a stop reached on the entry bar is taken, a target reached on the entry bar is not, and when a stop and a target are both reached the stop wins. Those rules can understate a strategy, but they never invent profit.

That is the conclusion. Each section below is the evidence for one claim; follow the sections or the evidence links into the strategy report for the data on every parameter set.

When is a decision made?

Every backtest needs one answer to a simple question: at the moment a decision is made, which prices are already known?

TradePose evaluates strategies at the open of each bar. At 10:00, the 10:00 bar’s open is known; its high, low, close and volume are not, because they only exist once the bar has finished at 10:15. Every rule below follows from this line:

Value of the current barKnown when the decision is made?
OpenYes
High, low, close, volumeNo: only after the bar finishes
Any indicator computed from the current high, low, close or volumeNo

A strategy that needs a close therefore uses the close of a bar that has already finished.

Bars are labelled by their start time

Each bar carries the time it starts: the 15-minute bar labelled 10:00 covers 10:00 to 10:15. When a strategy uses a second timeframe, for example 1-hour bars next to 15-minute bars, each 15-minute row is joined to the most recent 1-hour bar whose start time is not later than its own.

The engine does not add a delay to that join. The table below joins simulated 1-hour closes onto a 15-minute timeline:

15-minute row15-minute close1-hour close, shift=01-hour close, shift=1
00:0014,891.214,899.714,891.9
00:1514,887.114,899.714,891.9
00:3014,888.114,899.714,891.9
00:4514,899.714,899.714,891.9
01:0014,890.614,890.814,899.7
01:1514,890.814,890.814,899.7

With shift=0, the 00:00 row already shows 14,899.7, the close of the 00:00–01:00 hour, which is only known at 01:00. With shift=1 declared on the 1-hour bars, the 00:00–01:00 close appears on the 01:00 row, the first moment it is known, and not an hour later.

Why no automatic delay? If the engine delayed every higher-timeframe bar to its end, a strategy that already declared shift=1 would be delayed twice. One rule, stated by the author, keeps direct and resampled data consistent.

Which bars does the shift move?

shift=1 means “one bar earlier” on the bars it is declared on. Declared on daily bars, it reaches the previous day. Applied after the daily values have been joined onto a 15-minute timeline, it only reaches the previous 15-minute row, which usually belongs to the same day.

Simulation 1: one rule, three ways to shift

The test uses 1,500 days of simulated 15-minute prices from a random walk, so no rule can have a real edge. The rule: if the daily bar closed above its open, go long at 09:30 and exit at 16:00; otherwise go short. The only difference between the three versions is where the shift is applied.

How the daily signal is shiftedTradesWin rateAverage per trade
No shift1,50066.6%+16.5 points
Shift after the join (one 15-minute row)1,50066.6%+16.5 points
shift=1 on the daily bars1,49949.0%−1.1 points

The first two versions read the close of the day they are trading. The 09:30–16:00 move is part of that day’s move, so they “predict” it two times out of three. Shifting after the join does not help at all: the 09:15 row it reaches belongs to the same day. Only the shift on the daily bars gives the coin flip a random walk should give.

Simulated data:

Both simulations in this post use randomly generated prices, not market data. A random walk has no edge by construction, so any profit it shows comes from the backtest, not the strategy.

The same mistake is easy to make in pandas:

# Wrong: shift() moves one 15-minute row, which is still the same day
m15 = pd.merge_asof(m15, daily[["ts", "up"]], on="ts")
m15["up_prev"] = m15["up"].shift(1)

# Right: shift the daily bars first, then join
daily["up_prev"] = daily["up"].shift(1)
m15 = pd.merge_asof(m15, daily[["ts", "up_prev"]], on="ts")

In TradePose the shift is declared on the column’s own source, for example tp.DataColumn(column="close", shift=1) on the daily source; the engine shifts the daily bars first and only then joins them onto the main timeline. A Polars .shift() written after the join still counts rows of the main timeline.

Why the engine does not decide causality for you

A shift is a number of rows. Whether a value was known at decision time depends on everything it was computed from: an indicator computed from the current bar’s close leaks unless something in its chain selects the previous bar, while an indicator that consumes an input that is already shifted needs no second shift. The engine cannot see that intent from a column name or a shift number, so it does not pretend to:

Guaranteed by the engineOwned by the strategy author
Bars are labelled by their start time and joined backward, with no hidden delayChoosing which inputs need shift=1 to use finished bars
A declared shift moves that source’s own bars, never rows of the main timelineNot shifting a value that is already causal a second time
Market Profile is published after its window closes; the anchor bar is not part of the finished windowReviewing the full lineage of custom columns and expressions

The style checker warns when a shift is missing or cannot be classified, but it does not block a strategy on a guess. Rejecting a valid strategy because of its column name would be as wrong as accepting a leaking one because it carries shift=1.

What happened first inside one bar?

The second kind of look-ahead hides inside a single bar. OHLC data says a bar reached its high and its low, but not in which order. If a stop and a target both lie inside the bar, a backtest has to pick one; picking the target is an assumption that the price went up first.

TradePose always picks the outcome it can prove:

SituationEngine rule
Immediate entryFills at the open of the bar
Stop loss reached on the entry barExits on that bar: the loss is real even if the trade lasts one bar
Take profit reached on the entry barNot filled on that bar; the earliest fill is the next bar
Several orders of the same kind reached, for example two long stops at 97 and 99The one reached first along the price path: 99
Orders of different kinds reachedExits at the open first, then stops, then targets: a stop reached together with a target wins
A delayed entry is pending and the base strategy exits at this bar’s openThe delayed entry is cancelled

Simulation 2: three assumptions against the true order

Here the random walk is generated minute by minute, so the true order of events is known, and then combined into 15-minute bars that hide it. A long enters at 09:30 with a stop loss and a take profit at the same distance and exits at 16:00 if neither is reached; 3,000 simulated days. The truth comes from the minute path; the other three columns only see the 15-minute bars:

Stop and target distanceTruthTarget firstStop firstEngine
5 points50.5%60.8%41.1%30.7%
10 points51.9%52.8%51.1%45.5%
20 points50.9%50.9%50.9%50.4%

Win rates; in this simulation the standard deviation of a 15-minute move is about 12 points. Three things stand out:

  • Assuming the target came first invents profit. With a 5-point distance it turns a fair coin into a 60.8% strategy.
  • The engine errs in the other direction. It never shows profit the bars cannot prove, so with tight distances it understates the strategy: 30.7% against a true 50.5%, mostly because a target reached on the entry bar is not filled and the trade later hits its stop.
  • The error shrinks as orders move away from the bar range. At 20 points every assumption agrees with the truth. When stops and targets are close relative to the bar size, the answer is finer data, not a better assumption.

The engine’s rules are deliberately one-sided: a backtest that understates a strategy can be checked with finer data, while one that overstates it can send a losing strategy live.

The same rule applies to excursion statistics

Maximum adverse and favorable excursion (MAE and MFE) use the same reasoning on the exit bar. When a trade exits at its stop, the favorable side of that bar is excluded, because the price may have reached it only after the stop; when it exits at its target, the adverse side is excluded. The precise definitions of these statistics are covered in the quant research glossary.

Limitations of this post:

Both simulations use random-walk prices with no gaps, spread or slippage; real markets add those, and gap fills follow their own rules. The same-bar comparison uses a single symmetric stop and target on 15-minute bars; how much the engine understates depends on the distance relative to the bar range and on the entry type. Whether an input is causal remains the author’s responsibility: the engine guarantees the timing of bars and shifts, not the meaning of every custom column.

Questions this study answers

How does TradePose keep a strategy from reading a bar's close before the bar has finished?

Decisions are made at each bar’s open, bars are labelled by their start time and joined backward with no hidden delay, and a strategy that needs a finished bar declares shift=1 on that bar’s own timeframe. Whether every input is causal stays with the author, because a shift alone cannot prove it.

What does the engine do when one bar reaches both the stop and the target?

It takes the outcome it can prove: the stop wins over the target, a stop on the entry bar is taken, and a target on the entry bar waits for the next bar. With stops and targets close to the bar range this understates a strategy rather than overstating it.

Questions raised along the way

How do I check my own backtest for these two problems?

Run it on a random walk. Any edge that appears there comes from the backtest. For multi-timeframe signals, make sure the shift is applied to the higher-timeframe bars before they are joined; for stops and targets inside one bar, compare the result with the same test on finer bars.

Method and data

Research setup, data and calculations; the identifiers needed to reproduce the study are under data sources.

Changelog

2026-10-24 20:38 first published
Cite this article

TradePose Research (2026). "How a backtest avoids look-ahead bias: bar timestamps, shift, and what happens inside one bar." TradePose Research Notes.

@article{tradepose2026backtestlookaheadbartiming, title = {How a backtest avoids look-ahead bias: bar timestamps, shift, and what happens inside one bar}, author = {TradePose Research}, journal = {TradePose Research Notes}, year = {2026} }