Lookahead bias in TradingView means a script uses information on a historical bar that was not available when that bar was live. The backtest then looks great, and live trading does not. The usual culprits are request.security() with lookahead_on, strategies that recalculate inside the bar, and signals read off a candle that has not closed. Below: where the bias comes from, how to test for it, and the exact patterns that fix it.
TL;DR
- Lookahead bias is future data leaking into past decisions. TradingView describes it as peeking into the future, and it produces unrealistically good results.
- The biggest source in Pine Script is
request.security()withbarmerge.lookahead_onand no[1]offset. calc_on_order_fills = truecan also give a strategy information it would never have live.- Test it: run the script live or in replay, compare to history, and watch for a profit drop when you tighten the logic.
- Confirm signals on bar close. Then add slippage and out-of-sample checks.
Table of Contents
- What lookahead bias is, and what it is not
- The three places lookahead hides in TradingView
- A quick table: pattern, risk, fix
- How to test a script for lookahead bias
- Why scalpers get hurt most
- Data discipline beyond Pine Script
- Our perspective: treat a great backtest as a suspect
- A practical fix checklist for your own scripts
- Tools that make this easier
- FAQ
- Sources
What lookahead bias is, and what it is not
Lookahead bias is a narrow error: a decision on bar N uses data from after bar N. A backtest can't tell the difference, because on historical bars every value already exists. In live trading the data doesn't exist yet, so the edge disappears.
It is related to repainting but not identical. Repainting means a signal changes or vanishes after it appeared. Lookahead means the signal was never knowable at the time. A repainting indicator can be built without lookahead (it just flickers on the live bar). A lookahead strategy can look perfectly stable on the chart, which is why it is dangerous. If you want the repainting side, see our guides on non-repainting indicators and non-repainting signals for active traders.
It is also a different problem from other backtest flaws. Survivorship bias, cherry-picked date ranges and ignored costs all inflate results, but only lookahead gives the script information that does not exist yet.

The three places lookahead hides in TradingView
1. Higher-timeframe data with lookahead_on
This is the classic. The Pine Script documentation explains that when you request higher-timeframe data, the lookahead setting decides whether the request can return values from beyond the historical bar it runs on. With lookahead_on and no offset, a 5-minute chart can see the final daily high at 9:35 a.m., hours before that high exists.
The default, barmerge.lookahead_off, avoids the leak on historical bars. The trouble starts when someone switches it on to make the HTF line look "aligned" with the chart, or to get the signal one bar earlier.
The pattern that avoids the leak while keeping the data aligned combines lookahead_on with a history offset on the expression:
htfClose = request.security(syminfo.tickerid, "240", close[1], lookahead = barmerge.lookahead_on)
The [1] makes the script read the last completed higher-timeframe bar. You lose one HTF bar of freshness and gain a number you can trade.
Open-source Pine linting rules flag the same issue from another angle: requesting a still-forming daily bar means history and live disagree, and the fix is to use the previous, closed bar.

2. Strategies that calculate after order fills
By default a strategy calculates once per bar close. With calc_on_order_fills = true, the engine recalculates inside the bar after an order fills. TradingView's support article explains that the script then gets access to extra bar values such as high and low, which lets a strategy post excellent backtest results that cannot be reproduced live. The Pine documentation on strategies repeats the caution: executing after order fills can cause lookahead bias on historical bars.
If a strategy you downloaded sets this flag, treat it as a red flag until you understand why.
3. Signals read from an unconfirmed candle
The quietest form. A script compares the current close to a level and fires a buy. On a closed historical bar, the close is the final value. On the live bar, the close is just the latest tick, and it moves. The backtest sees the final value and credits the signal. Live, the signal flickers on and off.
The fix is to gate every entry on barstate.isconfirmed, or to reference the prior bar's values. Both make the signal reflect what you could have acted on at the time.
A quick table: pattern, risk, fix
| Pattern in Pine Script | Why it leaks | Safer approach |
|---|---|---|
request.security(..., close, lookahead_on) | Returns the HTF bar's final value on earlier chart bars | Use close[1] with lookahead_on, or keep lookahead_off |
request.security(..., high) on current HTF bar | Uses a high or low that has not formed yet | Request the previous closed bar |
calc_on_order_fills = true | Extra intrabar recalculation with data a live script lacks | Leave it off unless you can justify it |
| Entry condition on the live bar's close | Final close known only in history | Require barstate.isconfirmed |
| Pivot or swing functions without delay handling | Pivots are confirmed several bars after the fact | Account for the confirmation lag when plotting and trading |
The pivot row deserves a note. Pivot-style functions need bars to the right of a swing to confirm it. If a script marks the pivot back on the swing bar and then trades from there, it is trading a point it could only identify later.
How to test a script for lookahead bias
Reading code helps, but you will often be testing a closed-source script or a long one. These checks work without understanding every line.
- Tighten, then compare. Change the logic to strict closed-bar values and rerun. If the win rate or profit factor falls sharply, that lost profit was never real, because the strategy was cheating by looking ahead.
- Forward test it. Put the script on a live chart or paper account for a few sessions and log each signal as it prints. Then reload and compare to what the history now shows. Any signal that moved or appeared retroactively is a problem.
- Use Bar Replay. Replay the chart bar by bar and watch whether signals appear at the moment you would have needed them.
- Check the trade list. Entries at the exact low of a bar and exits at the exact high, over and over, are not skill. They are leakage.
- Look for TradingView's own warning. The platform can flag a strategy that may use look-ahead bias. Do not dismiss it.
- Change timeframe and symbol. A leak often collapses when you move one step up or down. A real edge degrades gently.
The same discipline exists outside TradingView. Freqtrade, an open-source crypto bot, ships a dedicated lookahead analysis tool and describes the bias as easy to introduce and hard to detect. The principle carries over: compare results computed on partial data with results computed on full data, and any difference is leakage.
Why scalpers get hurt most
On a 1 to 15 minute chart, the edge per trade is small. A few ticks of leaked information can be the whole backtest profit. A swing system with a 3% average win can absorb a bit of flattering. A scalper targeting a handful of ticks cannot.
Lower timeframes also have more bars, so more chances for a live-bar close or HTF value to sneak in. A backtest with thousands of trades looks statistically strong, but if the signal logic leaks, thousands of trades only make the illusion more convincing.
Then costs stack on top. Even a clean signal faces spreads and fills that differ from the chart, so test those too: our write-up on TradingView slippage models covers three ways to do it.
Data discipline beyond Pine Script
Lookahead can enter through the data, not only the code. Any backtest that uses information stamped with the wrong date, such as fundamentals restated after the fact, has the same flaw. Equity and sector research is a good example: allocation tests need figures as they were known on each date. A walkthrough on backtesting sector allocation with point-in-time data shows how that standard looks in practice, and the idea transfers to any strategy you build in TradingView.
Our perspective: treat a great backtest as a suspect
We build scalping indicators for a living, and our rule is simple: the better a backtest looks, the harder we try to break it.
Here is what that means in practice:
- Signals confirm on candle close. A signal prints when the bar closes and stays. If you can't point to the bar where it became knowable, don't trade it.
- Higher-timeframe inputs use closed bars. Multi-timeframe confluence is only useful if the HTF reading is something you could have had at that moment.
- Open source, so you can check. Our scripts are open-source. You can read how each one requests data, instead of trusting a screenshot.
- Out-of-sample before trust. We split data and judge on the part the settings never saw. Our piece on the 70/30 out-of-sample split for scalping dashboards shows the method.
- Live is the final test. Alerts fire in real time to Discord, so you can compare what you were told then with what history shows now.
A lower number you can trade beats a higher number you can't. When tightening a script cuts the profit, that is not a loss. It is the real number arriving.
— Tran
A practical fix checklist for your own scripts
Run through this before you trust any strategy, yours or someone else's.
- Search the code for
request.security. For each call, check thelookaheadargument and whether the expression has[1]. - Search for
calc_on_order_fills. If it istrue, switch it off and compare results. - Find every entry condition. Does it depend on a value that can change before the bar closes? Add
barstate.isconfirmedor use the previous bar. - Check pivots and swings for confirmation lag.
- Rerun with realistic commission and slippage.
- Split the data. Tune on one part, judge on the other.
- Forward test for at least a couple of weeks of live sessions before sizing up.
After the fix, the equity curve usually looks flatter and less exciting. That is the point.
Tools that make this easier
If you would rather not audit Pine code line by line, start from indicators built around confirmed signals. Smart Scalping Signals prints buy and sell signals confirmed on candle close and is built for the 1m to 15m range. Edge Finder and Momentum Oscillator add regime and momentum context. For stacking checks, see signal confirmation methods every scalper should run.
All plans give access to the full indicator suite, Discord alerts, education and the mentorship community: $79 per month, $799 per year, or $1999 one-off for lifetime. You can see them on the Scalping-Algo plans page.
FAQ
Is lookahead bias the same as repainting?
No. Repainting is a signal that changes or disappears after it appeared. Lookahead bias is a decision that used data not yet available. They often show up together, and the fix for both is acting only on confirmed, closed-bar information.
Does lookahead_on always cause lookahead bias?
Not always. Used with a [1] offset on the expression, it returns the last completed higher-timeframe bar and avoids the leak. Used on the raw expression, it lets historical chart bars see HTF values from the future.
How can I tell if a TradingView strategy has lookahead bias?
Tighten its logic to closed-bar values and see whether profits collapse. Then forward test it live and compare the signals you logged with what history now shows. Entries at the exact low and exits at the exact high across many trades are another warning sign.
Can lookahead bias happen in an indicator, not just a strategy?
Yes. An indicator that plots a value from a higher timeframe or a confirmed pivot back on an earlier bar makes the chart look as if it predicted the move. The strategy tester only makes it worse by turning that chart into a profit figure.
Sources
- TradingView Help: Strategy produces unrealistically good results by peeking into the future
- Pine Script docs: Other timeframes and data
- Pine Script docs: Strategies
- Why Your Pine Backtest Lies: the request.security Fix
- Pine Script strategy skill rules (GitHub)
- Freqtrade lookahead analysis docs
