[Canonical HTML article](https://algoway.trade/blog/pine-var-varip-intrabar-state.html)

# **Pine var vs varip: Why Intrabar Counters Lose Updates**

Published November 26, 2026

![Pine var vs varip: Why Intrabar Counters Lose Updates](https://algoway.trade/static/img/blog-img/pine-var-varip-intrabar-state.jpg)

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](https://algoway.trade/blog/automate-tradingview-alerts-to-any-broker-with-algoway.html) belongs after that decision: an execution route cannot reconstruct a signal state that the script discarded.

## What survives the next update?

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](https://www.tradingview.com/pine-script-docs/language/variable-declarations/).

| 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.

## Trace three executions, not three candles

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.

## Memory does not make the script execute

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](https://www.tradingview.com/pine-script-docs/language/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](https://algoway.trade/blog/pine-isnew-versus-isconfirmed.html) 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](https://www.tradingview.com/pine-script-docs/faq/variables-and-operators/) 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.

## A remembered signal is only one part of an alert rule

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](https://www.tradingview.com/pine-script-docs/concepts/alerts/).

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.