ScriptingHow toHow to tell a forming bar from one that has closed

How to tell a forming bar from one that has closed

Read barstate.isrealtime. It is true when the bar under evaluation is still updating and false once that bar has closed.

indicator("Forming bar", "forming", true)
plot(close, title = "Close", color = barstate.isrealtime ? color.orange : color.gray)

The flag is a series bool, so it answers for the bar the script is on, not for the chart as a whole.

If you want the closed bars instead

barstate.ishistory is true on a historical bar. It is the flag to test when your logic should run over settled bars and stay out of the way on the one that is still moving.

If you want the end of the chart

Two flags mark single positions rather than ranges.

FlagMarks
barstate.islastthe last bar of the chart
barstate.islastconfirmedhistorythe last confirmed historical bar

They are separate flags. Do not use one expecting the other.

The name you may be reaching for

barstate.isconfirmed does not exist in Lipi. Use barstate.isrealtime and read it the other way round.

What this recipe does not tell you

It does not tell you how often a script is evaluated while a bar is still updating, what triggers each evaluation, or whether a script is evaluated again over bars it has already seen. Do not infer any of that from the flags. See Recalculation and confirmation.

Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade