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

# How to reference a value from a previous bar

> Reading an earlier bar's value with the index suffix, computing the index, and the limits on how far back it can reach.

Put the number of bars back in square brackets after the value. `close[1]` is the close one bar before the bar the script is on;
`close[0]` is the current bar.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Change since the previous bar", overlay=false)
plot(close - close[1], title="Change")
```

The index may be a variable or any expression that gives a number, and `[]` works on built-in series, your own variables and call
results:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Close three bars back", overlay=true)
int back = 3
plot(close[back], title="Close 3 bars back")
```

## How far back

* At most 500 bars. A larger index passes the editor's check and stops the script when it runs, with the message "The script is trying to
  access historical data outside allowed range (501). The allowed range is 0 to 500." The same limit bounds the length of a `talib` window (`talib.ema` refuses a longer one when the script is added to the chart).
* On the first bars of the chart, where fewer than `n` earlier bars exist, `close[n]` is `na`.

## The last time something happened

To read a value from the last bar where a condition was true, or to count the bars since, use `talib.valuewhen(condition, source, occurrence)` (occurrence 0 is the latest, the current bar included) and `talib.barssince(condition)`:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Close at the last up bar", overlay=true)
lastUp = talib.valuewhen(close > open, close, 0)
sinceUp = talib.barssince(close > open)
plot(lastUp, title="Close of the last up bar")
plot(sinceUp, title="Bars since an up bar")
```

To keep several earlier values, put them in an array (see [How to keep the last N values in an array](https://gocharting.com/docs/scripting/learn/keep-the-last-values-in-an-array.md)).

## The mistakes to avoid

> [!WARNING]
>
> A negative index. The compiler says: History index must be >= 0. Got -1.
>
> ```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
> //@lipi=2
> indicator("Negative index", overlay=true)
> plot(close[-1])
> ```

Also, `[]` on an array is the history operator, not an element index: use `array.get(a, i)` (see [Arrays](https://gocharting.com/docs/scripting/language/arrays.md)).
Square brackets after a call that returns a tuple are refused (see
[How to return more than one value from a function](https://gocharting.com/docs/scripting/how-to/return-more-than-one-value-from-a-function.md)). See
[Referencing previous bars](https://gocharting.com/docs/scripting/evaluation/referencing-past-bars.md).


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