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

# Calls that must run on every bar

> Why talib functions and your own functions must run on every bar, once per call site, and how to keep a condition from breaking their history.

A function that looks at earlier bars, such as `talib.sma`, `talib.ema` or `talib.rsi`, does not read the chart's bars. It keeps
its own history, one per place in your script where you call it (the call site), and that history grows by one entry only on the bars
where the call actually runs. Its "previous bar" is the previous time the call ran.

So a `talib` call is correct only when it runs once on every bar. A call that is skipped on some bars gives wrong numbers and no error.

## Where a call is skipped

A call does not run on a bar when it sits in an `if` or `switch` block whose condition is false, in the unchosen side of `c ? a : b`
(only one side runs; both sides of `and` and `or` always run), or in a function that is itself called on some bars only.

A call in a loop body is one call site run several times a bar: the passes overwrite each other and only the last is kept. Do not
call `talib` functions inside a loop.

## Call it on every bar, then choose

Make the call at the top level, then use its result in the condition:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average on up bars", overlay=true)
float avg = talib.sma(close, 20)
float shown = close > open ? avg : na
plot(shown, title="Average")
```

This is the opposite of the following script, which compiles but averages the last 20 up bars only, because the call runs on up bars only:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average of up bars", overlay=true)
float shown = na
if close > open {
    shown := talib.sma(close, 20)
}
plot(shown, title="Average")
```

## The same holds for your own functions

A history reference inside a function (`x[1]`), a `talib` call and a `static` variable all belong to the call site: two calls of one
function keep separate histories, and a call skipped on some bars sees only the bars it ran on.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Smoothed change")
func smooth(float x) => (x + nz(x[1])) / 2
plot(smooth(close - open), title="Smoothed body")
```

`smooth` runs on every bar, so `x[1]` is the previous bar.

## Index

A history index counts recorded entries back. For a built-in series (`close[1]`) every bar is recorded; for your own variable, every
bar on which the line that sets it ran (a top-level variable is set on every bar). The reach is 500 entries (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.