last_bar_index - Pine Script Variable
last_bar_index
Section titled “last_bar_index”Overview
Section titled “Overview”Bar index of the last chart bar. Bar indices begin at zero on the first bar.
intQualifier
Section titled “Qualifier”series
Remarks
Section titled “Remarks”- The value can change as realtime bars arrive. Store it in a
varif you need a stable reference. - Please note that using this variable can cause indicator repainting.
Example
Section titled “Example”//@version=6strategy("Mark last N bars", overlay=true, calc_on_every_tick=true)
barsToTrade = input.int(100, "Bars Count")var lastBar = last_bar_index
allowed = (lastBar - bar_index <= barsToTrade) or barstate.isrealtimebgcolor(allowed ? color.new(color.green, 85) : na)