Core / Reference
history-operator
expression[n] reads the value expression had n bars ago: close[1] is the previous bar's close, high[10] the high ten bars back. Where there is no such bar — the first bars of the chart — the result is na. The offset may be any non-negative int, also one that changes from bar to bar.
history-operator → the value offset bars back, or naReturns
the value offset bars back, or naExample
history-operator
//@version=6indicator("History in a function")// x[1] inside a function is the value at its previous CALLprevCall(float x) => x[1]float atPrevCall = naif bar_index % 2 == 0 atPrevCall := prevCall(bar_index)plot(atPrevCall, "bar_index at the previous call")plot(bar_index[1], "bar_index one bar ago")Notes
- A local variable or expression — a function's parameters and locals, a variable declared in an if block, an expression or a call's value written inside one, or on one side of a ternary — gets a new value only on the bars where its scope runs. x[n] is the last value it had as of n bars back: on a bar after skipped ones that is the value of the scope's last run, never na just because the bar before skipped it. If the scope last ran 10 bars ago, x[1] to x[9] all give that value. A function called on every bar sees no difference; one called only on some bars looks back to its previous call at x[1]. A script-level variable read inside keeps its bar history.
- A ta.* call that reads past values keeps its own history of its arguments wherever it runs on some bars only — in such a function, an if block, one side of a ternary: it is filled on the bars where the call runs. ta.sma(close, 3) there averages close at its last three runs, not the last three bars, even though close is the chart's own series. The same holds inside a request.security expression, on the requested bars. Two readings stay on the chart's bars: a true range compares each high and low with the previous bar's close (ta.tr) — in ta.atr, ta.supertrend, ta.kc / ta.kcw and ta.dmi — and ta.barssince counts bars. AlertaScript's own built-ins that read past values (ta.trix, ta.stc, ta.adx, ta.stochrsi, ta.dc …) follow the same rule; the series written as their arguments are theirs, so ta.adx(high, low, close, 14) compares each run with the run before. A top-level variable read in a request.security expression is the script's global code, computed on every requested bar, except a call inside its own definition's if block or ternary side, which runs where that part runs.
- The history operator is not an array index: array.get reads an element of an array, history reads earlier bars.
Names and aliases
Guides and navigation are localized. Built-in reference descriptions and code examples are maintained in English.