Guides, manuals and platform references.
Published November 26, 2026 · View Markdown
A counter can remember yesterday and still forget what happened a moment ago. In Pine Script, persistence across bars and persistence inside an open bar are different promises. var supplies the first; varip can supply both.
This matters when TradingView automation depends on several updates, or on remembering that a signal already fired during the current candle. Before connecting such a condition to an order, settle what the script is meant to remember. In an AlgoWay workflow, the TradingView alert automation guide belongs after that decision: an execution route cannot reconstruct a signal state that the script discarded.
For an ordinary scalar variable, a declaration without either keyword initializes the variable each time its scope executes. A var declaration preserves confirmed state across bars, but changes made during an open realtime bar are subject to rollback before the next execution. varip preserves changes across those intrabar executions too. Neither keyword means “reset when a new candle starts.” See TradingView’s variable declaration rules.
| Declaration | Starting point on another realtime update |
|---|---|
| Ordinary variable | Its initialization runs again when its scope executes |
var scalar |
The last committed state, before applying this execution’s changes |
varip scalar |
The state retained from the previous execution |
Think of rollback as reopening the last saved document. Typing one extra character on each reopened copy does not produce a growing paragraph.
Here is a deliberately small arithmetic example, not a measured market result. Assume two counters have both committed the value 10 at the previous bar’s close. One uses var, the other varip. On every execution, the script adds one. There is no reset and no conditional branch.
| Execution on the open bar | var after adding one |
varip after adding one |
|---|---|---|
| First update | 11 | 11 |
| Second update | 11 | 12 |
| Third update, which closes the bar | 11 | 13 |
The var counter is not refusing arithmetic. It repeatedly starts from 10. At the close, 11 becomes its committed value. The varip counter carries each increment forward and finishes at 13. On the next bar, neither automatically starts from zero.
For a per-bar update counter, explicitly reset the intrabar counter on the opening execution, then increment it on subsequent executions. Decide whether the opening execution counts as zero or one. Otherwise two correct implementations can disagree by one while counting the same updates.
Indicators normally execute on realtime feed updates. Strategies normally calculate on the closing update unless their execution settings change that behavior. Declaring a variable with varip does not itself enable intrabar strategy calculations. TradingView documents those scheduling rules in its execution model.
A reset based on barstate.isnew therefore needs a script that actually executes at the opening update. The related isnew versus isconfirmed guide explains those flags. Choose the execution schedule first, then the reset and increment rules. Counting executions also does not count elapsed seconds or individual exchange trades.
Historical candles cannot replay the full sequence of realtime updates that built them. An intrabar counter can consequently differ after a script reload, when those bars are recalculated as history. TradingView’s variables FAQ warns that varip calculations cannot be reproduced on historical bars in the same way. A smooth historical plot is not evidence that an intrabar rule has been tested.
Suppose the intended rule is “send once after three qualifying updates during this bar.” Define whether those updates must be consecutive, whether a failed update clears the count, and whether the count resets each bar. These choices describe different signals.
A remembered Boolean can record that the event has already occurred, but it must use appropriate persistence and reset logic. Alert frequency is a separate control: TradingView distinguishes once-per-bar, once-per-bar-close and all qualifying alert() calls. Match it to the intended event using the official alerts documentation.
Before routing orders, observe the counter and event on realtime updates, record their values around a bar transition, and compare the alert log with the intended rule. Treat a reload as a separate test. A flag stored in a running script is not a durable broker position record, and resetting that flag is not evidence that an earlier order was canceled.