Skip to main content
Declare the total static and add to it with :=. An ordinary variable is created again on every bar, so an ordinary total restarts on every bar; a static variable is initialised once, on the first bar, and keeps its value from bar to bar.
static is the only keyword that persists a value across bars (intra persists only across the runs of one forming bar).

Restart the total each session

Test session.isfirst and seed the total from the bar’s own value on the first bar of a session:

Built-in totals

talib.cum(source) accumulates a series with no declaration of your own, but it cannot be restarted. orderflow.cvd() is the cumulative delta; it restarts each session.

Guard a value that can be na

Adding na to the total gives na from then on. Wrap a value that may be missing in nz, which turns na into 0. Order flow values are na on a bar without footprint data:

A total over the last N bars

talib.sum(source, length) adds the last length values: plot(talib.sum(volume, 20)). To keep the window yourself, see How to keep the last N values in an array.

The mistake to avoid

Writing var, which other languages use for this. Lipi has no var. The compiler says: var is not a valid type in variable declaration.
Order flow values exist only where the chart has footprint data (see Orderflow). See also Values that persist across bars.