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

Understanding barstate.isconfirmed Function in Pine Script

Photo of author
Published on

In this article, we delve into the functionality and application of barstate.isconfirmed in Pine Script, particularly in the context of plotting a Relative Strength Index (RSI) indicator. We’ll explore how this feature helps in managing the peculiarities of real-time versus historical data in trading scripts.

What is barstate.isconfirmed?

barstate.isconfirmed is a built-in boolean variable in Pine Script. Its primary purpose is to differentiate between historical and real-time bars in a chart.

  • Historical Bars: For all historical bars, barstate.isconfirmed is always true. This means that the data for these bars is finalized.
  • Real-Time Bars: For the real-time bar, barstate.isconfirmed becomes true only when the bar closes. This is critical to avoid what is known as ‘repainting’ – a situation where an indicator changes its values as new price data comes in.

Application in Plotting RSI

Now, let’s apply this to plot a Relative Strength Index (RSI), ensuring that it only plots on confirmed bars to avoid repainting:

Pine Script Example:

//@version=5
indicator("My Unique RSI Indicator",overlay = true)
customRSI = ta.rsi(close, 20)
plot(barstate.isconfirmed ? customRSI : na)
Example

Breakdown of the Script:

  1. indicator("My Unique RSI Indicator"): This line initializes our custom indicator and names it “My Unique RSI Indicator”.
  2. customRSI = ta.rsi(close, 20): We declare a variable customRSI and assign it the value of the RSI computed over the last 20 bars. ta.rsi() is a built-in function that calculates the RSI.
  3. plot(barstate.isconfirmed ? customRSI : na): This is where barstate.isconfirmed comes into play. We use a ternary operator to decide what to plot:
    • If barstate.isconfirmed is true (i.e., for historical bars and the last update of a real-time bar), we plot customRSI.
    • If barstate.isconfirmed is false (i.e., during the formation of a real-time bar), we plot na (not available), essentially plotting nothing.

Key Features and Takeaways:

  • Function Usability: barstate.isconfirmed is highly useful for strategies and indicators that need to avoid repainting, ensuring decisions are based on confirmed data.
  • Syntax: The syntax is straightforward, often used in conditional statements to determine whether to execute certain code segments.
  • Application: Particularly useful in time-sensitive indicators like RSI, where real-time data might cause misleading signals if not handled properly.
  • Limitation: It’s important to note that barstate.isconfirmed does not work within request.security() calls, which are used to fetch data from different timeframes or symbols.

In conclusion, barstate.isconfirmed is a valuable tool in Pine Script programming, particularly for ensuring accuracy and reliability in indicators and strategies that rely on real-time data. By understanding and applying this function correctly, traders and programmers can significantly enhance the effectiveness of their trading scripts.

Leave a Comment