array.new_label() - Pine Script Function
array.new_label()
Section titled “array.new_label()”Overview
Section titled “Overview”The function creates a new array object of label type elements.
Syntax
Section titled “Syntax”array.new_label(size, initial_value) → array<label>Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| size | series int | Initial size of an array. Optional. The default is 0. |
Returns
Section titled “Returns”The ID of an array object which may be used in other array.*() functions.
Remarks
Section titled “Remarks”- An array index starts from 0.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6indicator("array.new_label example", overlay = true, max_labels_count = 500)//@variable The number of labels to show on the chart.int labelCount = input.int(50, "Labels to show", 1, 500)//@variable An array of `label` objects.var array<label> labelArray = array.new_label()//@variable A `chart.point`for the new label.labelPoint =chart.point.from_index(bar_index, close)//@variable The text in the new label.string labelText = na//@variable The color of the new label.color labelColor = na//@variable The style of the new label.string labelStyle = na// Set the label attributesfor rising bars.if close > open labelText := "Rising" labelColor := color.green labelStyle := label.style_label_down// Set the label attributesfor falling bars.elseif close < open labelText := "Falling" labelColor := color.red labelStyle := label.style_label_up// Add a new label to the `labelArray` when the chart bar closed at a new value.if close != openlabelArray.push( label.new(labelPoint, labelText, color = labelColor, style = labelStyle))// Remove the first element and delete its label when the size of the `labelArray` exceeds the `labelCount`.if labelArray.size() > labelCountlabel.delete(labelArray.shift())