Skip to content
Algo Trade Analytics Docs
Pine library page — review pending. This page is preserved for compatibility but is not part of the reviewed search index. Check the official Pine Script v6 reference before relying on its explanation, signature, or example.

last_bar_index - Pine Script Variable

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

int

series

  • The value can change as realtime bars arrive. Store it in a var if you need a stable reference.
  • Please note that using this variable can cause indicator repainting.
//@version=6
strategy("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.isrealtime
bgcolor(allowed ? color.new(color.green, 85) : na)