= starts over on every bar (see Bar-by-bar evaluation). Two keywords
before the declaration keep a value: static across bars and intra across the repeated runs of one forming bar.
static
static type name = initial runs the initialiser when execution first reaches the declaration with no committed value: on the
first bar, or on a later bar for a declaration inside a branch or function not reached before. After that the declaration is skipped
and the variable starts with the value committed at the end of the previous bar. Change it with := or a compound assignment.
- On a forming bar the script runs on every tick, and each run starts from the value committed when the last bar closed, so
total += volumeadds the bar once, not once per tick. staticworks for any type:static label last = nakeeps a drawing handle; a line, label, box or linefill held directly by astaticvariable is never trimmed for being over the drawing limit (one in an array, or a polyline, is not).- An array or object in a
staticvariable is restored the same way on a forming bar, soarray.push(window, close)adds the bar once, not once per tick. Objects nested inside it are shared with the committed copy: change a field of an element of astatic array<Swing>only underbarstate.isconfirmed. - It works inside blocks and functions. In a function it belongs to the call site: two calls keep two values.
intra
intra keeps a value across the repeated runs of one forming bar and forgets it when the bar closes, so the next bar starts from
the initialiser again.
runs counts the ticks of the forming bar (1, 2, 3, …) and returns to 1 on the next bar; with calc_on_every_tick=false the forming bar runs once, when it closes, so it shows 1. On history every bar runs
once, so intra behaves like an ordinary variable and shows 1; a strategy with calc_on_order_fills=true is the exception, because it
reruns a bar after a fill and intra values carry over the reruns. Use it for what must accumulate during a bar but not across bars.
Use a plain variable for a value recomputed from the bar’s own data, static for a running total, counter, last signal or drawing
handle, and x[1] for the value a variable had one bar ago (see Referencing previous bars).