barstate
Bar state flags — first bar, last bar, new bar, etc.
barstate
The barstate namespace provides boolean flags indicating the current bar’s position in the historical data and real-time status. Use these to identify first/last bars, detect new bars, or check whether the script is running on confirmed, historical, or real-time data.
Quick Example
from pynecore.lib import (
close, high, label, bar_index, barstate, script
)
@script.indicator(title="Bar State Detector", overlay=True)
def main():
# Mark the first and last bars
if barstate.isfirst:
label.new(bar_index, high, "First")
if barstate.islast:
label.new(bar_index, high, "Last")
# Log bar state information
if barstate.isconfirmed:
# This is the final update for the current bar
pass
if barstate.ishistory:
# We are on a historical bar
passVariables
barstate.isfirst
Returns true if the current bar is the first bar in the dataset, false otherwise.
Type: bool
if barstate.isfirst:
label.new(bar_index, high, "Start")barstate.islast
Returns true if the current bar is the last bar in the dataset, false otherwise. For real-time bars, this is always true.
Type: bool
if barstate.islast:
label.new(bar_index, high, "End")barstate.isconfirmed
Returns true if the script is calculating the final update of the current bar (at bar close). The next script execution will be on a new bar.
Type: bool
if barstate.isconfirmed:
# Execute only once per bar, at close
passbarstate.ishistory
Returns true if the current bar is a historical bar (not real-time), false otherwise.
Type: bool
if barstate.ishistory:
# Processing historical data
passbarstate.islastconfirmedhistory
Returns true if the script is executing on the dataset’s last bar when the market is closed, or on the bar immediately before the real-time bar when the market is open.
Type: bool
if barstate.islastconfirmedhistory:
# Last historical bar before real-time data begins
passbarstate.isnew
Returns true if the script is currently calculating on a new bar, false otherwise.
Type: bool
if barstate.isnew:
# New bar detected
passbarstate.isrealtime
Returns true if the current bar is a real-time bar (live market data), false otherwise.
Type: bool
if barstate.isrealtime:
# Real-time bar processing
passCompatibility Notes
Backtesting (historical data only): every bar is a closed historical bar.
barstate.ishistoryandbarstate.isconfirmedaretrueon every bar.barstate.isnewistrueon every bar: each historical bar is calculated once.barstate.isrealtimeisfalseon every bar.barstate.islastconfirmedhistoryisfalseon every bar, including the dataset’s last bar.
Live mode (pyne run --live, or pyne run --broker): the historical phase behaves like a
backtest, except that barstate.islast is false on the historical bars and
barstate.islastconfirmedhistory is true on the final historical bar. On the streamed bars
barstate.ishistory is false, barstate.isrealtime and barstate.islast are true,
barstate.isconfirmed is true only on the bar-closing update, and barstate.isnew marks the
first execution of a new bar. See Live Mode for the
per-tick values.