Fix Pine Script Repainting: Causes, Code and Checks
To fix repainting in a Pine Script strategy, remove future leakage from request.security() by referencing the previous closed higher-timeframe bar (expr[1] with barmerge.lookahead_on), guard real-time signals with barstate.isconfirmed, keep calc_on_every_tick=false, and avoid synthetic fills on Heikin Ashi charts. Repainting happens whenever historical bars and real-time bars follow different data paths, producing backtests that cannot be reproduced live.
You can run this guide's non-repainting multi-timeframe script in the free browser judge on BTCUSDT 4h candles (0.10% fees per side, final 20% unseen).
Published 29 November 2026. Educational research on past data, not investment advice.
What repainting means in TradingView
TradingView's official Pine Script documentation defines repainting as any discrepancy between historical bars and real-time bars. Historical bars hold only four finalized OHLC values once per candle, while a live real-time bar updates on every tick until it closes.
A moving line during an unclosed candle is normal live behavior. The distortion starts when historical trades assume knowledge of a bar's final close—or a higher timeframe's close—before it actually finished. That gap joins fees and overfitting among the main causes explored in 5 measured reasons a Pine strategy loses live.
| Repainting source | What happens on historical vs live bars | Clean TradingView fix |
|---|---|---|
request.security() with lookahead_on |
Historical bars read the future higher-timeframe close at bar open | Request expr[1] with barmerge.lookahead_on, or shift with barstate.isrealtime |
| Unconfirmed real-time bars | Signals fire and vanish intra-bar before the candle closes | Gate entries, exits, and alerts with barstate.isconfirmed |
ta.pivothigh() plotted at offset=-N |
Markers sit retroactively on peaks even though confirmation lags by N bars |
Store the confirmed pivot in var and trade only subsequent crosses |
calc_on_every_tick=true or Heikin Ashi |
Live ticks or synthetic average prices diverge from historical OHLC fills | Set calc_on_every_tick=false and fill_orders_on_standard_ohlc=true |
Cause 1: request.security() and barmerge.lookahead_on without [1]
Reading a daily trend filter on a 4-hour chart is a frequent source of historical lookahead. Passing lookahead=barmerge.lookahead_on without shifting the expression by [1] hands historical 4-hour bars the final daily close at the opening of the day—information unavailable live.
Before — repaints in TradingView (future leakage on historical bars, do not deploy as written):
//@version=6
// BEFORE (REPAINTS IN TRADINGVIEW): lookahead_on without [1] leaks future 1D close on historical bars
strategy("MTF Daily EMA (Repainting in TradingView)", overlay=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
commission_type=strategy.commission.percent,
commission_value=0.1)
htfEma = request.security(syminfo.tickerid, "1D", ta.ema(close, 20), lookahead=barmerge.lookahead_on)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if close > htfEma and ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
Switching only to barmerge.lookahead_off stops historical lookahead, yet an unshifted daily value still moves live during the open day. TradingView's documentation standardizes two non-repainting patterns: either request the previous completed higher-timeframe bar via expr[1] with barmerge.lookahead_on, or apply the barstate.isrealtime ? 1 : 0 offset with barmerge.lookahead_off.
After — official TradingView non-repainting multi-timeframe pattern:
//@version=6
strategy("MTF Daily EMA (Non-Repainting)", overlay=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
commission_type=strategy.commission.percent,
commission_value=0.1)
// Official TradingView non-repainting HTF pattern: previous closed daily bar ([1] + lookahead_on)
htfEma = request.security(syminfo.tickerid, "1D", ta.ema(close, 20)[1], lookahead=barmerge.lookahead_on)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
crossUp = ta.crossover(fast, slow)
crossDn = ta.crossunder(fast, slow)
if barstate.isconfirmed and close > htfEma and crossUp
strategy.entry("Long", strategy.long)
if barstate.isconfirmed and crossDn
strategy.close("Long")
Run in our production judge on BTCUSDT 4h (10 bps per side), this clean rule logs 20 sealed trades and −16.17% (No evidence of an edge, for now).
- Run this non-repainting MTF script in the free browser judge (BTCUSDT
4h, 0.10% fees per side).
Cause 2: unconfirmed real-time bars and barstate.isconfirmed
A crossover can turn true mid-bar, trigger an alert, and vanish before the candle closes. Adding barstate.isconfirmed waits for the closing update of the chart's current bar (true on historical bars, true once at live bar close). Do not place barstate.isconfirmed inside request.security(), and compute ta.crossover() before if for Pine v6 lazy evaluation (Pine Script v6 migration guide).
//@version=6
strategy("EMA 9/21 Confirmed Close", overlay=true,
calc_on_every_tick=false,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
commission_type=strategy.commission.percent,
commission_value=0.1)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
crossUp = ta.crossover(fast, slow)
crossDn = ta.crossunder(fast, slow)
if barstate.isconfirmed and crossUp
strategy.entry("Long", strategy.long)
if barstate.isconfirmed and crossDn
strategy.close("Long")
In the judge on BTCUSDT 1d (10 bps per side), this script yields 16 sealed trades and +2.87% (No evidence of an edge yet: 3 of 4 steps passed). On BTCUSDT 4h (repaint_04_barstate_confirmed_4h.pine), the same rule yields 32 sealed trades and −3.61% (No evidence of an edge, for now).
Cause 3: ta.pivothigh() and back-plotted markers
ta.pivothigh(high, 5, 5) only returns a confirmed value five bars after the peak. Scripts that plot markers with offset=-5 look predictive on past charts even though live orders fire five bars later. Store the confirmed pivot in a var variable and enter only on a later breakout:
//@version=6
strategy("Confirmed Pivot Breakout", overlay=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
commission_type=strategy.commission.percent,
commission_value=0.1)
// A (5, 5) pivot high is confirmed only after 5 subsequent bars close
ph = ta.pivothigh(high, 5, 5)
var float lastPivot = na
if not na(ph)
lastPivot := ph
breakUp = not na(lastPivot) and ta.crossover(close, lastPivot)
exitDn = ta.crossunder(close, ta.ema(close, 20))
if barstate.isconfirmed and breakUp
strategy.entry("Long", strategy.long)
if barstate.isconfirmed and exitDn
strategy.close("Long")
In the judge on BTCUSDT 1d (10 bps per side), this confirmed-pivot rule produces 13 sealed trades and −2.34% (No evidence of an edge, for now).
Cause 4: calc_on_every_tick=true and synthetic Heikin Ashi fills
Two strategy settings frequently separate historical reports from live fills:
calc_on_every_tick=true: live bars re-evaluate on every tick, while historical bars only supply four OHLC values.- Heikin Ashi without
fill_orders_on_standard_ohlc=true: Heikin Ashi prices are smoothed averages rather than executable market quotes.
Before — diverges between historical and live execution in TradingView:
//@version=6
// BEFORE (DIVERGES LIVE IN TRADINGVIEW): calc_on_every_tick=true + synthetic Heikin Ashi prices
strategy("Heikin Ashi + Every Tick (TradingView Trap)", overlay=true,
calc_on_every_tick=true,
fill_orders_on_standard_ohlc=false,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
commission_type=strategy.commission.percent,
commission_value=0.1)
haClose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
haEma = ta.ema(haClose, 20)
if ta.crossover(haClose, haEma)
strategy.entry("Long", strategy.long)
if ta.crossunder(haClose, haEma)
strategy.close("Long")
After — standard OHLC candles, closed-bar execution, explicit standard fills:
//@version=6
strategy("Standard OHLC EMA 20", overlay=true,
calc_on_every_tick=false,
fill_orders_on_standard_ohlc=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
commission_type=strategy.commission.percent,
commission_value=0.1)
ema20 = ta.ema(close, 20)
crossUp = ta.crossover(close, ema20)
crossDn = ta.crossunder(close, ema20)
if barstate.isconfirmed and crossUp
strategy.entry("Long", strategy.long)
if barstate.isconfirmed and crossDn
strategy.close("Long")
On BTCUSDT 1d (10 bps per side) in the judge, this standard OHLC rule logs 42 sealed trades and −8.07% (No evidence of an edge, for now).
How to verify a backtest and what the browser judge does
Four checks catch repainting inside TradingView:
- Audit every
request.security()call for unshiftedbarmerge.lookahead_onorticker.heikinashi()/ticker.renko(). - Audit the
strategy()header forcalc_on_every_tick=trueand verifyfill_orders_on_standard_ohlc=trueon non-standard charts. - Run TradingView's Bar Replay over 50–100 recent bars, log entries as bars print, then compare against the full historical trade list after exiting replay.
- Cross-check on closed standard OHLC bars and a sealed period.
Exact scope of the Strategy Arena browser judge
Verified on the production WASM engine (sa_pine_vm):
- The judge does not detect repainting automatically. It computes strictly on closed standard OHLC bars (BTCUSDT
1d/4h/1hor your CSV) and holds out the final 20% unseen. - On
request.security(): bothbarmerge.lookahead_onwithout[1]andta.ema(close, 20)[1]withbarmerge.lookahead_onreturned identical4houtputs (20 sealed trades, −16.17%), because the judge does not simulate TradingView's historical future leak. - On
calc_on_every_tick=trueandticker.heikinashi(): both run on closed standard OHLC candles (42 sealed trades, −8.07%); when Heikin Ashi or Renko candles are requested, the judge shows a warning that the verdict is for standard candles.
If a multi-timeframe or Heikin Ashi script looks strong in TradingView while its closed-bar standard-OHLC run in the free browser judge drops sharply on the same market, inspect your data calls first. See also 7 free checks for a reliable TradingView backtest, testing a Pine strategy out of sample for free, and using [1] on ta.dmi and other tuples.
FAQ
What is repainting in Pine Script?
Repainting happens when a script behaves differently on historical bars than on live real-time bars, causing past signals or backtest trades to shift or vanish once reloaded.
Does barmerge.lookahead_off guarantee zero repainting in request.security()?
No. While lookahead_off blocks historical future leakage, an unshifted higher-timeframe close still updates live during the forming real-time bar. TradingView's non-repainting pattern requests the previous closed bar with expr[1] and barmerge.lookahead_on, or uses a barstate.isrealtime offset.
Should I add barstate.isconfirmed to every strategy entry?
Adding barstate.isconfirmed ensures real-time orders and alerts wait for the current bar to close instead of flickering intra-bar. On closed historical bars it always evaluates to true.
Why do Heikin Ashi charts distort TradingView strategy backtests?
Heikin Ashi candles compute synthetic open, high, low, and close averages rather than market quotes. Setting fill_orders_on_standard_ohlc=true tells TradingView to fill orders at standard chart OHLC prices.
Does the Strategy Arena browser judge detect repainting automatically?
No. The browser judge does not flag repainting automatically; it computes strictly on closed standard OHLC bars and holds out the final 20% of candles unseen. Comparing TradingView against a closed-bar test helps spot discrepancies.
Sources
- TradingView Pine Script User Manual, Repainting (
https://www.tradingview.com/pine-script-docs/concepts/repainting/), consulted 29 November 2026. - TradingView Pine Script User Manual, Other timeframes and data (
https://www.tradingview.com/pine-script-docs/concepts/other-timeframes-and-data/), consulted 29 November 2026. - Strategy Arena production judge (
sa_pine_vm) runs on BTCUSDT1dand4h(10 bps per side, final 20% sealed window), 29 November 2026.
Research on past data, not investment advice. Past results do not guarantee future results.
⚠️ Disclaimer — This article is for informational and educational purposes only. It does not constitute investment advice or a buy/sell recommendation. Past performance does not guarantee future results. Strategy Arena is an educational simulator with virtual capital. Always do your own research before making investment decisions.