Pine Script Bar Close vs Intrabar: Repainting, Alerts and calc_on_every_tick
3 min read · Updated
A Pine Script that looks clean on history can fire signals live that later disappear. Almost every case comes down to one question: is the script working on the closed bar or on the bar that is still forming?
This guide covers each setting that controls it and what I use for level alerts.
Historical bars and the realtime bar
On history, Pine runs your script once per bar, at its close. On the live bar, it runs again on every price update. A condition like close > level can be true at 10:01, false at 10:03 and true again at 10:04. History only keeps the last answer. That gap is what traders call repainting.
barstate.isconfirmed
barstate.isconfirmed is true only on the last update of a bar, when it closes. Adding it to a condition makes the live bar behave like history:
crossUp = ta.crossover(close, level) and barstate.isconfirmed
plotshape(crossUp, style=shape.triangleup, location=location.belowbar)
alertcondition(crossUp, "Close above level")
The cost is speed. You get the signal at the bar close, a full bar later on a slow timeframe.
Alert frequency
When you create an alert, TradingView asks how often it can fire:
- Once Per Bar Close: fires only when the bar closes with the condition true. It matches the backtest.
- Once Per Bar: fires the first time the condition turns true inside the bar, even if the bar closes the other way.
- Once Per Minute and Only Once: limits on how many times it fires.
For level alerts I use Once Per Bar Close on a 1 or 5 minute chart. A wick through a level that closes back inside does not trigger, which cuts most false breaks.
Strategies: calc_on_every_tick and process_orders_on_close
A strategy runs at bar close by default on both history and live bars. Two settings change that:
calc_on_every_tick=trueruns the strategy on every live update. History cannot do this, so live trades start to differ from the backtest.process_orders_on_close=truefills orders at the close of the signal bar. By default they fill at the next bar's open. It suits a close-based exit, but it can flatter entries.
Leave both off until the strategy works at bar close. The indicator vs strategy guide covers the rest of the Strategy Tester's assumptions.
Bar Magnifier
On history, a strategy only knows each bar's open, high, low and close. If both your stop and target sit inside one bar, it has to guess which hit first. use_bar_magnifier=true uses lower timeframe data to settle it, on paid TradingView plans. Turn it on before you trust a tight stop and target.
request.security and higher timeframes
Pulling a daily high into a 5 minute chart is a common source of repainting. On history the daily bar is already complete, so the script sees the final high from the first bar of the day. Live, the high keeps moving. Ask for the previous, finished value:
prevHigh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
The [1] with lookahead_on gives yesterday's high on every bar of today, on history and live alike.
A checklist
- Signals use
barstate.isconfirmedor the alert is Once Per Bar Close. - Higher timeframe values use
[1]withlookahead_on. - The strategy runs at bar close, with
calc_on_every_tickoff. - Bar Magnifier is on when stops and targets are close together.
- You compare a week of live alerts with the backtest before trading it.
Levels that do not move
My Trade Plans levels are set before the session and stay fixed all day, so a script that draws them has nothing to repaint. The Pine Script levels guide shows how to plot them on TradingView.
FAQ
What is the difference between bar close and intrabar in Pine Script?
Bar close means the script acts once, when the bar is finished. Intrabar means it acts on every price update inside the bar, so a signal can appear and vanish before the close.
How do I stop a Pine Script alert from repainting?
Set the alert to Once Per Bar Close, or add barstate.isconfirmed to the condition. For higher timeframe data, use the previous bar's value with lookahead_on.
Does a strategy backtest use intrabar prices?
Only for fills inside a bar, and only with Bar Magnifier on. Signals on history are worked out at each bar's close.
Sources
- TradingView Pine Script User Manual: Execution model, Repainting, Alerts
- TradingView Help Center: Bar Magnifier
Educational content only. Not investment advice.