Home » Labels Functions » label.delete Function in Pine Script

label.delete Function in Pine Script

Photo of author
Published on

Understanding and Utilizing the label.delete Function in Pine Script

In this tutorial, we will be delving into the workings of the label.delete function within Pine Script. This function is an essential tool for managing on-chart labels, a key feature of Pine Script’s graphics capabilities

Function Overview

Pine Script is a domain-specific language designed by TradingView for backtesting and trading algorithms. Among its many features, Pine Script allows users to display custom labels on a chart through the label function, which can then be manipulated using various other functions. One such function is label.delete.

The label.delete function serves the purpose of deleting a specified label object from a chart. Its syntax is straightforward:

label.delete(id)

In this syntax, id refers to the label object that is to be deleted. This function will return nothing (void), and if the label object has already been deleted, the function will do nothing.

Practical Use Case

To provide a practical example of the label.delete function, let’s consider a use case where we use labels to identify specific points of interest on a trading chart and need to remove them after a certain point.

Let’s assume that we want to mark the highest price of each day with a label and then delete these labels as soon as the price drops below a certain level.

//@version=5
indicator('My Script')

bullishPattern = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
bearishPattern = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))

var label[] labels = array.new_label(0)

if bullishPattern
    array.push(labels, label.new(bar_index, high, text='Bullish', color=color.green))

if bearishPattern
    array.push(labels, label.new(bar_index, low, text='Bearish', color=color.red))

if array.size(labels) > 3
    label.delete(array.get(labels, 0))
    array.shift(labels)
label.delete Function

Here’s what each line does:

  1. //@version=5: This specifies the version of Pine Script being used, which is version 5 in this case.
  2. indicator('My Script'): This function declares the script as an indicator and names it ‘My Script’.
  3. bullishPattern = ta.crossover(ta.sma(close, 14), ta.sma(close, 28)): This line defines a bullish pattern as a crossover between a 14-period simple moving average (SMA) and a 28-period SMA.
  4. bearishPattern = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28)): This line defines a bearish pattern as a crossunder between the same SMAs used for the bullish pattern.
  5. var label[] labels = array.new_label(0): Here we initialize a new empty array of labels. This array will store the label objects we create.
  6. if bullishPattern: If a bullish crossover pattern is detected…
  7. array.push(labels, label.new(bar_index, high, text='Bullish', color=color.green)): …a new green ‘Bullish’ label is created at the high of the current bar and added to the labels array.
  8. if bearishPattern: If a bearish crossunder pattern is detected…
  9. array.push(labels, label.new(bar_index, low, text='Bearish', color=color.red)): …a new red ‘Bearish’ label is created at the low of the current bar and added to the labels array.
  10. if array.size(labels) > 3: If the number of labels in the labels array exceeds three…
  11. label.delete(array.get(labels, 0)): …the oldest label (the first one in the array) is deleted from the chart.
  12. array.shift(labels): …and also removed from the labels array, ensuring that only the last three labels remain on the chart.

This script identifies bullish and bearish patterns and labels them accordingly, while ensuring the chart is not cluttered by deleting old labels, leaving only the most recent three. This helps traders keep track of recent market trends and patterns.

Key Takeaways

The label.delete function in Pine Script offers a powerful way to manage the labels on your charts. Understanding and leveraging this function allows you to create cleaner, more readable charts, which are particularly important when using labels to mark key price levels, trends, or trading signals.

Conclusion

Mastering Pine Script’s graphical functions, including label.delete, opens up numerous possibilities for customization, helping you visualize and interpret complex trading data more effectively. Through the careful application of these functions, you can create a trading tool that is both highly personalized and efficient.

Remember that practice makes perfect. Experimenting with different use cases and situations will help you get the hang of using the label.delete function, and before long, you’ll be a whizz at manipulating labels on your charts.

Leave a Comment