Skip to main content

Understanding label.new in Pine Script for TradingView

· 5 min read

Pine Script, TradingView's proprietary language, allows traders to create custom indicators and strategies. Among its functions, label.new plays a crucial role in enhancing chart visualization. This article will explore how to use the label.new function effectively to improve your scripts and trading strategies.

Defining label.new​

label.new is a function that generates a new label object on a TradingView chart. It uses a set of parameters to define the properties of the label, such as its position, color, text, and style. The basic syntax of the function is as follows:

label.new(x, y, text, xloc, yloc, color, style, textcolor, size, textalign, tooltip, text_font_family) → series label

The function accepts arguments such as x and y coordinates for positioning, text for the label's content, and various styling options to customize the label's appearance.

The Best Pine Script Generator

Parameters of label.new

  • x: The x coordinate of the label.
  • y: The y coordinate of the label.
  • text: The text to display on the label.
  • xloc: Defines how the label is placed on the x-axis.
  • yloc: Defines how the label is placed on the y-axis.
  • color: The background color of the label.
  • style: The style of the label (e.g., square, circle, flag) .
  • textcolor: The color of the text on the label.
  • size: The size of the label.
  • textalign: The alignment of the text within the label.
  • tooltip: Text that appears when you hover over the label.
  • text_font_family: The font family of the label.

Creating and Modifying Labels​

The label.new() function creates a new label. Setter functions allow you to change a label’s properties:

  • label.set_x()
  • label.set_y()
  • label.set_xy()
  • label.set_text()
  • label.set_xloc()
  • label.set_yloc()
  • label.set_color()
  • label.set_style()
  • label.set_textcolor()
  • label.set_size()
  • label.set_textalign()
  • label.set_tooltip()

What is Pineify?​

Pineify is a versatile tool tailored for traders who use TradingView, designed to simplify the creation and management of trading indicators and strategies. It allows users to develop complex indicators and strategies quickly, using visual tools, and requires no programming skills. Pineify supports adding unlimited technical indicators to TradingView charts, overcoming the platform's limitations. It is designed to be error-free and offers a way to save time and money compared to hiring a freelancer.

Pineify | Best Pine Script Editor

Website: Pineify

Click here to view all the features of Pineify.

Practical Application​

Here’s a basic example of how to use label.new in Pine Script:

//@version=5
indicator("Highest High Label Tracker", overlay = true)
highestHigh = ta.highest(high, 10)
var hiLabel = label.new(na, na, "Highest High", xloc = xloc.bar_index, yloc = yloc.price, color=color.green, style=label.style_label_down)
if (high == highestHigh)
label.set_xy(hiLabel, bar_index, highestHigh)
label.set_text(hiLabel, "Highest High: " + tostring(highestHigh))

This script tracks the highest high over the last 10 bars and displays a label indicating the value.

Key Considerations​

  • When including series values in text displayed using label.new(), they will first need to be converted to strings using str.tostring(). The concatenation operator for strings in Pine is +.
  • Labels persist on bars until your script deletes them using label.delete(), or garbage collection removes them.
  • To position a label on the last bar only, it is unnecessary and inefficient to create and delete the label as the script executes on all bars, so that only the last label remains.

Benefits of Using Labels​

  • Improved Visualization: Labels provide clear and direct visual cues on the chart.
  • Dynamic Information: Labels can display real-time data and adapt to changing market conditions.
  • Enhanced Decision Making: Well-organized charts improve the decision-making process in trading.

Improve Your Pine Script Skills​

By using label.new effectively, you can significantly improve the usability and readability of your custom scripts. Experiment with different parameters and styles to create labels that provide valuable insights for your trading strategies.

Take the Next Step: Explore the Pine Script documentation and community forums to deepen your understanding and unlock the full potential of TradingView scripting. Start creating custom indicators and strategies today!