Home » Symbol Information Functions » Understanding barstate.islastconfirmedhistory Function in Pine Script

Understanding barstate.islastconfirmedhistory Function in Pine Script

Photo of author
Published on

Pine Script is a domain-specific programming language primarily used for custom technical analysis in trading. One of the unique features of Pine Script is its ability to handle real-time market data and historical data efficiently. In this context, the barstate.islastconfirmedhistory attribute plays a critical role.

What is barstate.islastconfirmedhistory?

Definition and Use Cases

  • barstate.islastconfirmedhistory is a boolean attribute in Pine Script.
  • It returns true under two conditions:
    1. When the Market is Closed: If the script is executing on the last available bar of the dataset.
    2. When the Market is Open: On the bar immediately preceding the current real-time bar.

This attribute is particularly useful in scenarios where differentiating between real-time and historical data is crucial. Some common use cases include:

  • Detecting the First Real-time Bar: By using barstate.islastconfirmedhistory[1], scripts can identify the transition from historical to real-time data.
  • Optimizing Server Load: Postponing intensive calculations to be executed only on the last historical bar, which is essential when markets are open and data is continuously updating.

Code Example

Let’s create a simple script to demonstrate the usage of barstate.islastconfirmedhistory.

//@version=5
indicator("My BarState Indicator", overlay=true)

// Variable to store the state
var bool isLastConfirmedHist = false

if (barstate.islastconfirmedhistory)
    isLastConfirmedHist := true

// Plotting an indicator
plotshape(series=isLastConfirmedHist, location=location.abovebar, color=color.green, style=shape.triangleup, title="Last Confirmed Historical Bar")

// Detecting the first real-time bar
bool isFirstRealtimeBar = not barstate.islastconfirmedhistory[1] and barstate.islastconfirmedhistory

plotshape(series=isFirstRealtimeBar, location=location.belowbar, color=color.red, style=shape.triangledown, title="First Real-time Bar")

Walkthrough of the Code

  1. Indicator Setup: We begin by defining a new indicator with indicator() function and setting overlay=true to display it on the price chart.
  2. Variable Initialization: isLastConfirmedHist is initialized to store the state of barstate.islastconfirmedhistory.
  3. Checking the Last Historical Bar: The script checks if barstate.islastconfirmedhistory is true, and if so, sets isLastConfirmedHist to true.
  4. Plotting the Indicator: We use plotshape() to plot a green triangle above bars where isLastConfirmedHist is true.
  5. Detecting the First Real-time Bar: By comparing the previous ([1]) and current bar states, the script identifies the first real-time bar and plots a red triangle below it.

Key Features and Takeaways

  • Function Usability: barstate.islastconfirmedhistory is vital for distinguishing between historical and real-time data, optimizing calculations, and triggering actions at specific data points.
  • Syntax and Application: It’s a boolean attribute used in conditional statements within Pine Script. Its primary use is in scenarios where the distinction between real-time and historical data impacts the script’s behavior.
  • Optimization Benefits: Ideal for reducing server load and ensuring that certain calculations or actions are executed only at the end of historical data or at the beginning of real-time data.

This feature of Pine Script allows traders and developers to create more efficient and responsive scripts, tailored to the dynamic nature of financial markets.

Leave a Comment