Sidechair

max_bars_back()

Browse the reference

Sets how many past bars of a variable's history the script keeps available for the [] operator.

Try it in the editor

max_bars_back(var, num) tells the runtime to keep num bars of history for var. Pine normally works out the history a script needs by itself, and the call is only needed when the lookback depends on a value that is known late, such as close[n] with n computed on a bar.

The largest value is 5000. The argument can also be set once for the whole script with the max_bars_back parameter of indicator() or strategy().

Syntax

Pine Script
max_bars_back(var: any type that is compatible with the `[]` operator, num: const int) -> void

Arguments

NameTypeDefaultDescription
varany type that is compatible with the `[]` operatorrequiredThe series variable (or built-in such as close) whose history buffer you are setting.
numconst intrequiredHow many bars to keep, from 1 to 5000. It must be a constant.

Returns

Nothing.

Remarks

  • A script that reaches back farther than its history buffer stops with a runtime error that names the variable to give to max_bars_back().
  • On Sidechair: the call is accepted and does nothing. History is kept for every bar in the run, so no script fails for lack of history.

Example

Pine ScriptLookback chosen from the dataOpen in editor
//@version=6
indicator("max_bars_back")
max_bars_back(close, 100)
n = bar_index % 50
plot(close[n], "Close n bars ago")

See also

Runs on Sidechair