Language
Bar states
barstate.* tells the script where it is in the run: the first bar, the last, history or live, and whether the bar has closed.
Because a script runs once per bar, it sometimes needs to know which bar it is on.
| Variable | true when |
|---|---|
barstate.isfirst | The first bar of the run |
barstate.islast | The last bar on the chart (the forming one, when live) |
barstate.ishistory | A bar that had closed before the chart loaded |
barstate.isrealtime | A bar forming while the chart is open |
barstate.isnew | The first update of a new bar |
barstate.isconfirmed | The bar has closed |
barstate.islastconfirmedhistory | The last closed bar before live bars began |
The two you will use most#
Draw once, at the end. Wrap drawings and tables in if barstate.islast so they are made once rather than on every bar of history.
Signal only on closed bars. On the live bar, prices move until it closes. A condition can be true mid-bar and false at the close. Require barstate.isconfirmed for signals that must not disappear:
//@version=6indicator("Confirmed crosses", overlay = true)f = ta.ema(close, 9)s = ta.ema(close, 21)plotshape(ta.crossover(f, s) and barstate.isconfirmed, "Confirmed", style = shape.triangleup, location = location.belowbar, color = color.teal)On history every bar is confirmed, so this only changes behaviour live. See Repainting.