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

Understanding barstate.islast Function in Pine Script

Photo of author
Published on

In this article, we’re diving into the use of barstate.islast in Pine Script, an essential component for those looking to create dynamic and efficient scripts in trading platforms. We’ll explore how to use this feature to update a label on the last bar of a chart, a common requirement for many trading strategies and visual indicators.

Introduction to barstate.islast

barstate.islast is a boolean value in Pine Script that becomes true when the script is executing on the last bar of the chart. This could be the final historical bar or any realtime bar that appears as the last bar temporarily. The functionality is especially useful for operations that need to be restricted to the last bar, such as drawing lines, labels, or tables.

Example: Updating a Label on the Last Bar

Script Overview

Let’s walk through a Pine Script example where we utilize barstate.islast:

//@version=5
indicator("BarState isLast Example", overlay = true)

// Create label on the first bar only.
var label myLabel = label.new(na, na, "")

// Update the label's position and text on the last bar,
// including on all realtime bar updates.
if barstate.islast
    label.set_xy(myLabel, bar_index, high)
    label.set_text(myLabel, str.tostring(high, format.mintick))
Example

Detailed Walkthrough

  1. Script Initialization
    • //@version=5: Specifies the version of Pine Script being used.
    • indicator("BarState isLast Example", overlay = true): Declares the script as an indicator and overlays it on the main chart.
  2. Label Creation
    • var label myLabel = label.new(na, na, ""): Initializes a label (myLabel) only once due to var. The label is created with undefined (na) coordinates and no text.
  3. Condition and Label Update
    • if barstate.islast: This condition checks if the script is executing on the last bar.
    • label.set_xy(myLabel, bar_index, high): Moves myLabel to the current bar’s index and the high price level of that bar.
    • label.set_text(myLabel, str.tostring(high, format.mintick)): Sets the label’s text to the high price of the current bar, formatted to the minimum tick size.

Key Features and Takeaways

  • Functionality: barstate.islast is pivotal for scripts that need to execute code exclusively on the last bar of the chart.
  • Efficiency: By updating a label instead of creating it repeatedly, the script becomes more efficient and resource-friendly.
  • Application: This method is particularly useful for dynamic indicators where information on the last bar needs constant updating.
  • Syntax: The usage of label.set_*() functions for updating label properties dynamically.

This explanation of barstate.islast in Pine Script demonstrates its utility in creating more dynamic and efficient trading scripts. By understanding and applying these concepts, script writers can enhance their trading strategies and visual indicators on the platform.

Leave a Comment