Skip to content

last_bar_index - PineScript Variable

Bar index of the last chart bar. Bar indices begin at zero on the first bar.

int

series

//@version=6strategy("Mark Last X Bars For Backtesting", overlay = true, calc_on_every_tick = true)lastBarsFilterInput = input.int(100, "Bars Count:")// Here, we store the 'last_bar_index' value that is known from the beginning of the script's calculation.// The 'last_bar_index' will change when new real-time bars appear, so we declare 'lastbar' with the 'var' keyword.var lastbar = last_bar_index// Check if the current bar_index is 'lastBarsFilterInput' removed from the last bar on the chart, or the chart is traded in real-time.allowedToTrade = (lastbar - bar_index <= lastBarsFilterInput) or barstate.isrealtimebgcolor(allowedToTrade ? color.new(color.green, 80) : na)
  • Please note that using this variable can cause indicator repainting.