last_bar_index - PineScript 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
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@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)Remarks
Section titled “Remarks”- Please note that using this variable can cause indicator repainting.