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

# Bar-by-bar evaluation

> How a script runs once per bar from the oldest bar to the newest, and what that means for variables.

Lipi has no loop over the chart. The chart runs your whole script from the first line to the last once for each bar, starting
with the oldest bar that is loaded and ending with the newest. In every run `close`, `high`, `bar_index` and the other built-in
series hold the values of that one bar; the script cannot see later bars.

## What a variable is on each bar

A variable you declare with `=` is created again on every bar, so it starts from its initial value each time. Earlier values stay
reachable through the history operator (`x[1]`, see [Referencing previous bars](https://gocharting.com/docs/scripting/evaluation/referencing-past-bars.md)). To
carry a value from one bar to the next, declare it `static` (see [Values that persist across bars](https://gocharting.com/docs/scripting/evaluation/persistence.md)).

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Variables per bar")
int fresh = 0
static int kept = 0
fresh += 1
kept += 1
plot(fresh, title="Fresh each bar")
plot(kept, title="Kept across bars")
```

`fresh` is 1 on every bar; `kept` is 1, 2, 3, ...

## Order inside a run

Statements run top to bottom. A function runs when it is called, and a block only when its condition holds on that bar. A top-level
`plot` is evaluated on every bar and draws nothing where its value is `na`.

## History, then the live bar

Each historical bar runs once. The newest bar of a live chart is still forming, so it runs again on every price tick (see
[Recalculation and confirmation](https://gocharting.com/docs/scripting/evaluation/recalculation.md)). The first bars give `na` for anything that needs earlier
bars, such as a 20-bar average on bar 5.


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