ScriptingHow scripts are evaluatedValues that persist across bars

Values that persist across bars

Your script is evaluated again on every bar, so an ordinary declaration is made again on every bar. To keep a value from one bar to the next, declare it with static.

static <data_type> <variable_name> = <expression>

Reassign a static value with :=. The value carries into the next bar.

indicator("Cumulative close")

static float total = 0.0
total := total + close

plot(total)

static is the keyword

static is how a value persists across bars. There is no other spelling.

A declaration beginning var does not compile. This is worth stating because the mistake is common.

If a declaration is rejected, check the keyword first.

The two keywords

static and intra are the only two persistence keywords the grammar accepts.

intra <data_type> <variable_name> = <expression>

intra is accepted. What intra retains, and how that differs from static, is not documented here. Prefer static.

What this page does not tell you

This page does not state when a persistent value is initialised relative to the first bar, nor what happens to it when a chart is reloaded or a setting is changed.

Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade