Skip to main content
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). To carry a value from one bar to the next, declare it static (see Values that persist 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). The first bars give na for anything that needs earlier bars, such as a 20-bar average on bar 5.