Series, state and history
Every top level let and var is a series. The script executes once per bar, oldest to newest, and stores one value per bar. Function parameters and function local declarations are plain scalars for that call and have no history.
| Form | Meaning |
|---|---|
| let name: type = expression | Recomputed on every bar and immutable. |
| var name: type = expression | Initialised on the first bar, then carries its previous value forward. |
| name := expression | Updates a top level var, or a local inside a function or loop body. |
| name += expression | Short for name := name + expression. -=, *= and /= work the same way. |
| name[offset] | The value offset bars back. 0 is the current bar, 1 the previous one. |
The history offset must be a whole number from 0 through 200,000. It may be computed. Reading before the first bar gives nan.
var cumulative: float = 0
cumulative := nz(cumulative[1], 0) + tape.delta()
let previous_close: float = close[1]
let gap: float = open - close[1]Chart globals
| Name | Type | Meaning |
|---|---|---|
| open | float | Bar open. |
| high | float | Bar high. |
| low | float | Bar low. |
| close | float | Bar close, and the current price on the forming bar. |
| volume | float | Bar volume. |
| time | float | Bar start, epoch milliseconds. |
| time_end | float | Bar end, epoch milliseconds. |
| bar_index | int | Position of the bar in the loaded window, starting at 0. |
| bar_count | int | How many bars this run received. |
| last_bar | bool | True only on the newest bar in the window. This is how a drawing is pinned to the right edge. |
| is_forming | bool | True on the bar the chart is still building, which is the newest bar of the window. |
| nan | float | The absent numeric value. na is the same value. |
| hl2 | float | (high + low) / 2 |
| hlc3 | float | (high + low + close) / 3 |
| ohlc4 | float | (open + high + low + close) / 4 |
| hlcc4 | float | (high + low + close + close) / 4 |
Globals carry history too, so close[1], high[3] and hlc3[2] are valid. A script that declares its own series, function, parameter or local named hl2, hlc3, ohlc4, hlcc4 or na keeps its own meaning for that name everywhere in the script. They work on every bar type the chart offers, including range, volume, tick, delta and seconds bars, because a script receives bars rather than a chart type.
