In this article, we’ll dive into how to use the label.copy()
function effectively, with an illustrative example and a detailed breakdown of each code component.
What is the label.copy()
Function?
Syntax
label.copy(id) → series label
Arguments
id
(series label): This is the label object that you wish to clone.
Example Explained
Let’s break down the example provided to understand how label.copy()
is used in practice.
//@version=5 indicator('Last 100 bars highest/lowest', overlay = true) var LOOKBACK_PERIOD = 100 var highestValue = ta.highest(LOOKBACK_PERIOD) var highestValueBarsAgo = ta.highestbars(LOOKBACK_PERIOD) var lowestValue = ta.lowest(LOOKBACK_PERIOD) var lowestValueBarsAgo = ta.lowestbars(LOOKBACK_PERIOD) if barstate.islastconfirmedhistory var labelHigh = label.new(bar_index + highestValueBarsAgo, highestValue, str.tostring(highestValue), color = color.green) var labelLow = label.copy(labelHigh) label.set_xy(labelLow, bar_index + lowestValueBarsAgo, lowestValue) label.set_text(labelLow, str.tostring(lowestValue)) label.set_color(labelLow, color.red) label.set_style(labelLow, label.style_label_up)
Walkthrough
- Indicator Declaration:
//@version=5 indicator('Last 100 bars highest/lowest', overlay = true)
This line defines the script as a version 5 indicator with a title. Theoverlay = true
argument means the indicator will be drawn over the price chart. - Variable Initialization:
var LOOKBACK_PERIOD = 100 var highestValue = ta.highest(LOOKBACK_PERIOD) var highestValueBarsAgo = ta.highestbars(LOOKBACK_PERIOD) var lowestValue = ta.lowest(LOOKBACK_PERIOD) var lowestValueBarsAgo = ta.lowestbars(LOOKBACK_PERIOD)
Here, variables are defined for the lookback period and to find the highest and lowest values within that period, along with their positions relative to the current bar. - Label Creation and Cloning:
if barstate.islastconfirmedhistory var labelHigh = label.new(bar_index + highestValueBarsAgo, highestValue, str.tostring(highestValue), color = color.green) var labelLow = label.copy(labelHigh)
A new label is created at the position of the highest value over the lookback period. Thelabel.copy()
function is then used to clone this label, which will be modified to represent the lowest value. - Modifying the Cloned Label:
pine label.set_xy(labelLow, bar_index + lowestValueBarsAgo, lowestValue) label.set_text(labelLow, str.tostring(lowestValue)) label.set_color(labelLow, color.red) label.set_style(labelLow, label.style_label_up)
The cloned label’s position, text, color, and style are updated to reflect the lowest value’s information.
Key Features and Takeaways
- Function Usability: The
label.copy()
function allows for efficient duplication of labels, reducing the need for repetitive code and easing the management of multiple similar labels with minor differences. - Syntax and Application: It accepts a single argument, the label ID to be cloned, and returns a new label ID that can be modified independently of the original.
- Practical Usage: Especially useful in scenarios where multiple labels share common properties but differ in their specific details, such as text, color, or position.
Understanding and utilizing the label.copy()
function can significantly enhance the readability and efficiency of your Pine Script code, especially in complex charting applications.