Skip to content

barstate - PineScript Variable

barstate is a record that exposes booleans describing the current bar’s evaluation state. These flags help you gate calculations to the first or last update of a bar, identify realtime execution, and avoid double-counting logic on historical data.

FieldTypeDescription
barstate.isfirstseries booltrue on the first bar of the dataset (or requested series).
barstate.islastseries booltrue when the script processes the most recent realtime bar (still updating).
barstate.islastconfirmedhistoryseries booltrue on the last historical bar that has finished forming. The next update will be realtime.
barstate.isnewseries booltrue during the very first calculation of a new bar.
barstate.isrealtimeseries booltrue while executing on realtime data; false on historical backfill.
barstate.ishistoryseries booltrue when processing historical data before realtime begins.
barstate.isconfirmedseries booltrue once the current bar closes (its final update).
  • Many flags are mutually exclusive. For example, barstate.isrealtime becomes true only after historical bars are exhausted.
  • Avoid using barstate.isconfirmed inside request.security() expressions; TradingView warns that the value is unpredictable in that context.
  • Combine these flags with bar_index and time-based variables to control when code executes, reducing repainting risk.
//@version=5
indicator("Barstate demo", overlay=true)
isFirst = barstate.isfirst
isNewRealtimeBar = barstate.isnew and barstate.isrealtime
plotshape(isFirst, title="First bar", style=shape.circle, size=size.tiny,
location=location.bottom, color=color.new(color.blue, 0))
plotshape(isNewRealtimeBar, title="Realtime open", style=shape.triangleup,
location=location.belowbar, color=color.new(color.green, 0))
if barstate.islastconfirmedhistory
label.new(bar_index, high,
text="Last confirmed history",
textcolor=color.white,
bgcolor=color.new(color.orange, 70))