> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to keep a running total across bars

> Carrying a total from one bar to the next with static, restarting it each session, and the built-in cumulative functions.

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.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Running total of volume", overlay=false)
static float total = 0.0
total := total + volume
plot(total, title="Total")
```

`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:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Volume so far this session", overlay=false)
static float total = 0.0
if session.isfirst {
    total := volume
} else {
    total := total + volume
}
plot(total, title="Session volume")
```

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

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Cumulative volume and delta", overlay=false)
plot(talib.cum(volume), title="Cumulative volume")
plot(orderflow.cvd(), title="Cumulative delta")
```

## 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:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Delta so far", overlay=false)
static float total = 0.0
total := total + nz(orderflow.delta)
plot(total, title="Total delta")
```

## 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](https://gocharting.com/docs/scripting/learn/keep-the-last-values-in-an-array.md).

## The mistake to avoid

> [!WARNING]
>
> Writing `var`, which other languages use for this. Lipi has no `var`. The compiler says: `var` is not a valid type in variable declaration.
>
> ```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
> //@lipi=2
> indicator("Var")
> var total = 0.0
> total := total + volume
> plot(total)
> ```

Order flow values exist only where the chart has footprint data (see [Orderflow](https://gocharting.com/docs/scripting/market-data/orderflow.md)). See also
[Values that persist across bars](https://gocharting.com/docs/scripting/evaluation/persistence.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.