How to Hide Labels in Pine Script?

Introduction to Labels in Pine Script

What are Labels and Why Use Them?

Labels in Pine Script are graphical objects that display text directly on the chart. They’re invaluable for annotating price action, highlighting key events, and providing real-time information about indicator states. While they enhance chart readability, situations arise where hiding labels becomes necessary – decluttering the display, focusing on specific signals, or during automated backtesting.

Basics of Creating Labels with ‘label.new()’

The label.new() function is the cornerstone of label creation. It takes several arguments to define the label’s appearance and position. Key arguments include x (bar index for horizontal placement), y (price level for vertical placement), text (the text to display), color, style, and textcolor.

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

if bar_index % 10 == 0
    label.new(bar_index, high, text="High", color=color.green, style=label.style_labeldown)

This simple script creates a green label at every 10th bar, pointing downwards from the high price.

Understanding Label Properties (text, color, style, location)

Labels are highly customizable. You can control the text content using strings or by converting numerical values using str.tostring(). The color property determines the label’s background color, while textcolor sets the color of the text itself. The style property offers various pre-defined shapes (label.stylelabelup, label.styletriangledown, etc.). x and y arguments define the label’s precise location.

Methods for Hiding Labels

Using Conditional Logic to Prevent Label Creation

The most straightforward way to hide labels is to prevent their creation in the first place using conditional statements. By wrapping the label.new() call within an if statement, you can control when the label is drawn.

//@version=5
indicator("Conditional Label", overlay=true)

show_label = input.bool(true, "Show Label?")

if show_label
    label.new(bar_index, close, text="Close Price", color=color.blue)

Here, the label is only created if the show_label input is true.

Employing ‘na’ Values to Effectively Hide Labels

While you can’t directly “hide” an existing label, assigning na (not a number) to its x or y coordinate effectively removes it from the chart. This approach is valuable when you need to conditionally display/hide a label that was previously created.

//@version=5
indicator("NA Label", overlay=true)

var label mylabel = na

if bar_index % 20 == 0
    mylabel := label.new(bar_index, close, text="Event", color=color.red)

if bar_index % 40 == 0
    label.set_x(mylabel, na) // Hide the label

This example creates a label every 20 bars and then hides it every 40 bars by setting its x coordinate to na.

Leveraging Input Options to Control Label Visibility

Combining conditional logic with input options provides user-friendly control over label visibility. This allows traders to customize the indicator’s output without modifying the code directly.

//@version=5
indicator("Input Controlled Label", overlay=true)

show_labels = input.bool(true, "Show Labels?")

if show_labels
    if ta.crossover(ta.sma(close, 10), ta.sma(close, 20))
        label.new(bar_index, high, text="Crossover", color=color.orange)

This script displays a label when a moving average crossover occurs, but only if the “Show Labels?” input is enabled.

Advanced Techniques for Dynamic Label Control

Hiding Labels Based on Chart Resolution

Sometimes, labels can clutter the chart at lower timeframes. You can hide labels based on the chart’s current resolution using the timeframe.period variable.

//@version=5
indicator("Resolution Dependent Label", overlay=true)

show_labels = timeframe.period == "D" // Show only on Daily timeframe

if show_labels
    label.new(bar_index, high, text="Daily", color=color.purple)

This example only displays the label when the chart is set to the Daily timeframe.

Dynamically Showing/Hiding Labels with User-Defined Conditions

You can create complex conditions for showing or hiding labels based on multiple factors, such as price action, indicator values, or time of day.

//@version=5
indicator("Dynamic Label", overlay=true)

long_condition = ta.crossover(close, ta.sma(close, 50))
short_condition = ta.crossunder(close, ta.sma(close, 50))

var label long_label = na
var label short_label = na

if long_condition
    long_label := label.new(bar_index, low, text="Long", color=color.green)
    label.delete(short_label) // Delete previous short label if exists
    short_label := na

if short_condition
    short_label := label.new(bar_index, high, text="Short", color=color.red)
    label.delete(long_label) // Delete previous long label if exists
    long_label := na

This script displays a “Long” label when the price crosses above its 50-period SMA and a “Short” label when it crosses below, ensuring only one label is visible at a time.

Combining Multiple Conditions for Complex Label Management

For sophisticated strategies, combine resolution-based, user-defined, and time-based conditions to create highly customized label displays.

Best Practices and Considerations

Performance Implications of Creating and Hiding Labels

Creating a large number of labels, especially within loops or on every tick, can impact script performance. Hiding labels using na or deleting and recreating them repeatedly is also computationally expensive. Optimize your code by creating labels only when necessary and reusing them whenever possible.

Maintaining Code Readability When Managing Label Visibility

Use clear and descriptive variable names and comments to explain the logic behind label visibility. This makes your code easier to understand and maintain, especially when dealing with complex conditions.

Avoiding Common Pitfalls When Hiding Labels

  • Forgetting to initialize labels: Always initialize label variables with na to avoid errors.
  • Deleting labels unnecessarily: Avoid deleting and recreating labels if you can simply update their properties or hide them using na.
  • Overlapping labels: Carefully manage label placement to prevent them from overlapping and cluttering the chart.

Examples and Use Cases

Hiding Labels During Specific Trading Sessions

//@version=5
indicator("Session Label", overlay=true)

market_open = input.session("0900-1600", "Trading Session")
show_label = not na(time(timeframe.period, market_open))

if show_label
    label.new(bar_index, close, text="Market Open", color=color.green)

This script shows a label only during the specified trading session.

Conditional Labeling of Breakout Patterns (with Hidden Labels)

//@version=5
indicator("Breakout Label", overlay=true)

long_breakout = close > ta.highest(high, 20)
short_breakout = close < ta.lowest(low, 20)

if long_breakout
    label.new(bar_index, high, text="Long Breakout", color=color.green)

if short_breakout
    label.new(bar_index, low, text="Short Breakout", color=color.red)

This script labels breakouts. It is left as an exercise for the reader to implement a mechanism to hide older labels once a new one appears.

Using Labels for Debugging (and Hiding them in Production)

Use labels to display intermediate calculations or variable values during development. Wrap these debugging labels in conditional statements based on a debug input, allowing you to easily hide them when the script is deployed to production.

//@version=5
indicator("Debugging Label", overlay=true)

debug = input.bool(false, "Enable Debug Mode")

sma_value = ta.sma(close, 14)

if debug
    label.new(bar_index, close, text="SMA: " + str.tostring(sma_value), color=color.yellow)

This helps ensure that the debugging information does not clutter the chart once the script is used for trading purposes.


Leave a Reply