x[n] is the value that x had n bars ago. x[0] is the current value and x[1] the previous bar’s. The operator is written
after the value and works on built-in series (close[1]), on your own variables, on function results (talib.sma(close, 5)[1])
and on parenthesised expressions. It does not work on a literal or on na: History Indexing cannot be used on literal or built-in
non series values.
What the previous value of a variable is
x[1] is the value x had when the previous bar’s run finished, after all its := assignments. A variable you do not
index keeps no history. For a value that must be updated from its own previous value, use static instead of x[1] (see
Values that persist across bars).
When the bars do not reach far enough back, the result is na (close[1] on the first bar). Wrap it in nz(close[1]) to get 0, or
nz(close[1], close) to get another default.
The index
- It is a number; a series value is accepted (
close[bar_index % 3]), and a fraction is rounded down. - A string is refused: History Index must be a number. Got string.
- A negative literal is refused by the editor: History index must be >= 0. Got -1. There is no way to read a later bar.
The 500-bar reach
The index, and the length of anytalib window, may be at most 500. A larger value passes the editor’s check, and the script stops when
it runs with an error on the chart. talib.ema (and macd, bbw, kc, kcw, which use it) is refused already when the script is added to
the chart, with the same message:
static variable or
an array (see Arrays; on an array, a[1] is the array one bar ago, not its second element).