Sidechair

last_bar_index

Browse the reference

The bar_index of the last bar on the chart, available on every bar of the run.

Try it in the editor

last_bar_index is a series int that holds the same number on every bar: the index of the newest bar. Subtract bar_index from it to know how many bars remain.

The value changes only when a new bar is added to the end of the chart.

Type

series int

Remarks

  • A common use is to run expensive code only near the end, with bar_index > last_bar_index - 100.

Example

Pine ScriptShade the last 50 barsOpen in editor
//@version=6
indicator("Last 50 bars", overlay = true)
recent = bar_index > last_bar_index - 50
bgcolor(recent ? color.new(color.blue, 90) : na)
plot(last_bar_index - bar_index, "Bars to go", display = display.data_window)

See also

Runs on Sidechair