> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Referencing previous bars

> Reading a value from earlier bars with x[n], what it accepts, the 500-bar reach, and the messages for a bad index.

`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.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Close change and range", overlay=false)
float change = close - nz(close[1])
float high3 = math.max(high, high[1], high[2])
float range3 = high3 - math.min(low, low[1], low[2])
plot(change, title="Change")
plot(range3, title="3-bar range")
```

## 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](https://gocharting.com/docs/scripting/evaluation/persistence.md)).

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 any `talib` 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:

```text theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
The script is trying to access historical data outside allowed range (501). The allowed range is 0 to 500.
```

A computed index that is negative stops the script the same way. For a longer look-back keep the values in a `static` variable or
an array (see [Arrays](https://gocharting.com/docs/scripting/language/arrays.md); on an array, `a[1]` is the array one bar ago, not its second element).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.