Repainting in Pine Script: Confirmed Bars and HTF Data
Repainting means a script can behave differently on historical and realtime bars. Some changes are normal, such as an unfinished candle's close changing; other behavior, such as using future values in a historical calculation, can invalidate a test. Identify the cause before applying a fix.

Choose Which Information Is Allowed
For a signal evaluated at the chart bar's close, use data that is available at that moment. Waiting for confirmation changes timing; it does not make a prediction more accurate. Record that delay as part of the rule.
TradingView's repainting documentation distinguishes realtime changes, higher-timeframe behavior and misleading historical calculations. Provider revisions or changes to the available dataset are additional reasons old results may differ after a reload.
Confirm a Chart-Bar Signal
This complete example calculates its cross on each execution but displays and alerts it only when the chart bar is confirmed. It makes no claim about profitability.
//@version=6
indicator("Confirmed Chart Signal", overlay=true)
average = ta.sma(close, 20)
crossUp = ta.crossover(close, average)
signal = crossUp and barstate.isconfirmed
plot(average, "SMA", color.blue)
plotshape(signal, "Confirmed cross", shape.triangleup, location.belowbar, color.green)
if signal
alert("Confirmed cross above SMA", alert.freq_once_per_bar_close)
This does not automatically confirm data requested from another timeframe. Nor does it correct future leakage or a script that deliberately places a later-known result on an earlier bar.
Request Confirmed Higher-Timeframe Data
For a strictly higher timeframe, request the previous completed value with a one-bar expression offset and barmerge.lookahead_on. The two parts belong together: using lookahead without the offset can leak future information into historical bars.
//@version=6
indicator("Confirmed Higher-Timeframe High", overlay=true)
higherTimeframe = input.timeframe("1D", "Higher timeframe")
if timeframe.in_seconds(higherTimeframe) <= timeframe.in_seconds()
runtime.error("Choose a timeframe higher than the chart timeframe.")
confirmedHigh = request.security(syminfo.tickerid, higherTimeframe, high[1], lookahead=barmerge.lookahead_on)
plot(confirmedHigh, "Previous HTF high", color.orange)
The value is intentionally delayed until the higher-timeframe bar has completed. Do not replace the fixed expression offset with a dynamic barstate.isconfirmed test inside the request. This example does not cover lower-timeframe aggregation.
Configure and Check Alerts
Create the alert in TradingView after adding the script; source code alone does not activate a running alert. TradingView's alert documentation explains that running alerts execute on its servers and retain a snapshot of the script and inputs. Recreate the alert after changes that must affect its behavior.
Your browser disconnecting is not the same event as the server ceasing to evaluate an alert. Delivery channels and execution should be diagnosed separately.
Test Realtime Behavior
- Record the source version, inputs, symbol, interval and session.
- Observe signals during an open bar and again after its close.
- Save timestamps and values, then reload and compare the same bars.
- Check higher-timeframe boundaries separately.
- Investigate differences in data, configuration and execution before attributing every discrepancy to one cause.
A short observation period can reveal a problem but cannot prove that every possible state is safe. Test each data request and condition before combining them.
Generated Code Still Needs Review
If you use Pineify to build a script, inspect the generated data requests and confirmation logic. No blanket claim that a generator automatically prevents all repainting is justified by syntax validation alone.
Previous-bar volume or ATR may be appropriate for a rule that explicitly uses completed bars. They are not mandatory for every calculation. Decide whether the rule uses intrabar or close-time information, then test that exact design.
Does a previous-bar reference prevent all repainting?
It removes dependence on that value's current open bar. It does not fix every other request, future leak or later data revision in the script.
Can barstate.isconfirmed fix higher-timeframe requests?
Chart-bar confirmation does not confirm a still-open higher-timeframe bar. Use a documented request pattern suited to the requested timeframe.
Is a changing realtime value always a defect?
No. Open-bar values normally change. The defect is misrepresenting when information was available or evaluating a different rule in historical and live use.



