barstate - PineScript Variable
barstate
Section titled “barstate”Overview
Section titled “Overview”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.
Fields
Section titled “Fields”| Field | Type | Description |
|---|---|---|
barstate.isfirst | series bool | true on the first bar of the dataset (or requested series). |
barstate.islast | series bool | true when the script processes the most recent realtime bar (still updating). |
barstate.islastconfirmedhistory | series bool | true on the last historical bar that has finished forming. The next update will be realtime. |
barstate.isnew | series bool | true during the very first calculation of a new bar. |
barstate.isrealtime | series bool | true while executing on realtime data; false on historical backfill. |
barstate.ishistory | series bool | true when processing historical data before realtime begins. |
barstate.isconfirmed | series bool | true once the current bar closes (its final update). |
Remarks
Section titled “Remarks”- Many flags are mutually exclusive. For example,
barstate.isrealtimebecomestrueonly after historical bars are exhausted. - Avoid using
barstate.isconfirmedinsiderequest.security()expressions; TradingView warns that the value is unpredictable in that context. - Combine these flags with
bar_indexand time-based variables to control when code executes, reducing repainting risk.
Example
Section titled “Example”//@version=5indicator("Barstate demo", overlay=true)
isFirst = barstate.isfirstisNewRealtimeBar = 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))