Skip to main content
Values that persist between runs. Declared at the top level only. A local variable inside a handler starts fresh on every run. Anything that must persist is declared here, with its lifetime, so the engine can roll it back correctly.

state

<T extends StateValue>(initial: T): Cell<T>
Kept across bars; rolled back before each update of the forming bar (Pine’s var).
<T extends StateValue>(initial: T): Cell<T>
Kept across bars and across updates of the forming bar (Pine’s varip).
<T extends StateValue>(capacity: number, lifetime?: Lifetime): StateList<T>
A list of at most capacity values with a lifetime, default 'bar'.
<T extends StateValue>(capacity: number, lifetime?: Lifetime): StateMap<T>

StateValue

Cell

T

Series

A number with history, made with series(lookback).
(value: number): void
Sets the value for the current bar. Unset bars are na.
number
The current bar’s value.
(n: number): number
The value n bars ago, n at most the declared lookback.

Tally

(amount: number): void
number
The sum so far in the current bar.
number
How many amounts were added in the current bar.

TallyBy

(key: string, amount: number): void
Add to key’s sum for this bar. A new key past the capacity is dropped for the rest of the bar.
(key: string): number
key’s sum so far in this bar, 0 when it has none.
number
How many keys have a sum this bar.
(): IterableIterator<[string, number]>
Every key with its sum, in the order the keys were first added.

StateList

number
(index: number): T
The value at index, from 0 at the oldest.
(index: number, value: T): void
(value: T): void
Add at the end. Past the capacity the script stops (OX2004): shift the oldest first.
(): T
Take from the end.
(): T
Take from the start, the oldest.
(value: T): void
Add at the start.
(): void
(): T[]
A copy as a plain array, oldest first.

StateMap

number
(key: string): T | undefined
The value for key, or undefined when there is none.
(key: string, value: T): void
Set the value for key. A new key past the capacity stops the script (OX2004).
(key: string): boolean
Remove key; true when it was there.
(key: string): boolean
(): void
(): IterableIterator<[string, T]>

Lifetime

series()

A number with history: lookback bars kept, at most 5,000.

tally()

A sum of what event handlers add during a bar. Starts at 0 on each new bar; never rolled back.

tallyBy()

A sum per key (such as a taker order id) during a bar, at most capacity keys. Empty on each new bar.

Next step

Continue with Indicators (ta).
Last modified on October 16, 2026