Appearance
barstate
How do you run code only once, on the last bar, or only when a bar has closed? Test a barstate.* value. Each is a bool that the engine sets before your script runs on each bar.
pine
//@version=6
indicator("Last close, once", overlay = true)
// a label drawn on every bar would pile up; draw it on the last one only
if barstate.islast
label.new(bar_index, high, str.tostring(close))
plot(close)A chart runs its history once, then each update of the live bar. A bar still forming is the last one, and its values move until it closes. In leak-safe AmenoScript your code never runs on the forming bar, so the last bar your script sees is the last closed one. See the forming bar.
barstate.isconfirmed
pine
series bool barstate.isconfirmedTrue on a closed bar, false on the forming bar while its values can still move.
A signal that waits for barstate.isconfirmed can't repaint: the bar it fires on has closed.
barstate.isfirst
pine
series bool barstate.isfirstTrue on the run's first bar, where bar_index is 0.
barstate.ishistory
pine
series bool barstate.ishistoryTrue on bars that were already there when the script started, false on the bars that arrive live after it.
barstate.islast
pine
series bool barstate.islastTrue on the run's last bar. On a live chart, every bar that arrives after the script started reads true in turn.
In leak-safe AmenoScript the last bar is the last closed one, so barstate.islast is true on that bar and never on a forming one.
It looks ahead on history
A bar in the past couldn't know it was the last. A decision on barstate.islast reads differently on old bars than it did live, so draw with it, but don't trade on it.
barstate.islastconfirmedhistory
pine
series bool barstate.islastconfirmedhistoryTrue on the last bar of history that has closed: the bar before a forming last bar, or the last bar itself when it has closed.
barstate.isnew
pine
series bool barstate.isnewTrue on a bar's first run, false on the later updates of the same live bar.
barstate.isrealtime
pine
series bool barstate.isrealtimeTrue on the bars that arrive live after the script started, false on history.