Sidechair

bar_index

Browse the reference

The number of the current bar, counting from 0 at the first bar loaded on the chart.

Try it in the editor

bar_index is a series int. The oldest bar loaded has bar_index 0 and every later bar adds one, so on the last bar bar_index equals last_bar_index.

Use it to count bars, to place drawings by bar number, or to run code only on every nth bar with bar_index % n == 0.

Type

series int

Remarks

  • The count starts at the oldest bar the chart has loaded, not at the symbol's first trade, so do not keep a bar number as a permanent id.
  • bar_index[n] is na when n reaches back past bar 0.

Example

Pine ScriptMark every 20th barOpen in editor
//@version=6
indicator("Every 20th bar", overlay = true)
every = bar_index % 20 == 0
plotshape(every, "Marker", shape.circle, location.abovebar, color.blue, size = size.tiny)
plot(bar_index, "bar_index", display = display.data_window)

See also

Runs on Sidechair