TradingCharts

Bar States

barstate.* is a namespace of booleans that describe where the script currently is in the per-bar loop.

Core booleans

Name True when
barstate.isfirst The script is on the first bar of the chart's history.
barstate.islast The script is on the current (newest) bar in the chart's data set.
barstate.ishistory The current bar is a confirmed historical bar.
barstate.isrealtime The current bar is being formed in real time.
barstate.isconfirmed The current bar has closed (true on every historical bar; on the live bar only on its very last tick before close).
barstate.isnew The runtime has just advanced to a new bar — true on the first tick of each new bar.
barstate.islastconfirmedhistory The last historical bar BEFORE the realtime pass begins. Useful for one-shot setup that must survive to realtime.

Historical pass vs realtime pass

  • Historical bars run once each. On every one barstate.ishistory = true, barstate.isconfirmed = true.
  • The live bar re-runs on every tick. On most ticks barstate.isrealtime = true, barstate.isconfirmed = false. On its final tick (bar close) barstate.isconfirmed = true and the bar joins the historical pass.

Common patterns

One-time setup at the first bar

if barstate.isfirst
    log.info("Script loaded on " + syminfo.ticker)

Finalise only on confirmed bars

if barstate.isconfirmed and ta.crossover(close, ta.sma(close, 20))
    strategy.entry("Long", strategy.long)

Confirmed-only signals avoid intra-bar flapping — the entry fires once at bar close rather than every tick.

Draw once per bar, update on live

var label l = na
if barstate.isnew
    label.delete(l)
    l := label.new(bar_index, high, "")
if not barstate.isconfirmed
    label.set_text(l, str.tostring(close, "#.##"))

Snapshot the last historical bar

var float lastHistoricalClose = na
if barstate.islastconfirmedhistory
    lastHistoricalClose := close

Values written in islastconfirmedhistory survive into the realtime pass — the snapshot/restore cycle preserves var state.

See also