Home » Array Functions » Understanding the array.new_line() Function in Pine Script

Understanding the array.new_line() Function in Pine Script

Photo of author
Published on

This article provides a comprehensive tutorial on how to use the array.new_line() function, ensuring you can implement it effectively in your scripts.

Syntax Overview

The array.new_line() function initializes a new array designed to hold line objects. The syntax for this function is:

array.new_line(size, initial_value) → array<line>

Arguments

  • size (series int): This optional argument specifies the initial size of the array. If not provided, the default size is set to 0.
  • initial_value (series line): This optional argument defines the initial value for all elements within the array. The default value is na, representing a not available or undefined state.

Example

Let’s examine a practical example to understand how the array.new_line() function can be utilized within a Pine Script.

//@version=5
indicator("Modified array.new_line example")
// Drawing the last 15 lines
var linesArray = array.new_line()
array.push(linesArray, line.new(bar_index - 1, close[1], bar_index, close))
if array.size(linesArray) > 15
    deletedLine = array.shift(linesArray)
    line.delete(deletedLine)
Example

Step-by-Step Walkthrough

  1. Indicator Declaration:
   //@version=5
   indicator("Modified array.new_line example")

This line sets the script version and names the indicator as “Modified array.new_line example”.

  1. Array Initialization:
   var linesArray = array.new_line()

An array named linesArray is initialized without specifying the size or initial value, thus using the default settings.

  1. Pushing New Lines into the Array:
   array.push(linesArray, line.new(bar_index - 1, close[1], bar_index, close))

A new line object is created with line.new() for each script execution and added to linesArray. This line connects the previous bar’s close price to the current bar’s close price.

  1. Managing Array Size:
   if array.size(linesArray) > 15
       deletedLine = array.shift(linesArray)
       line.delete(deletedLine)

The script ensures that only the last 15 lines are retained in the array. If the array exceeds this size, the oldest line (deletedLine) is removed from the array and then deleted from the chart.

Key Features and Takeaways

  • Function Useability: The array.new_line() function is essential for scripts that dynamically generate and manage line objects on TradingView charts.
  • Syntax and Application: Its syntax allows for initializing arrays with specific sizes and values, offering flexibility in managing collections of line objects.
  • Visual Representation: This function is particularly useful for indicators or strategies that require visual representation of calculations or trends over a specific number of bars.

By understanding and implementing the array.new_line() function, you can enhance your Pine Script indicators and strategies, making them more dynamic and visually informative.

Leave a Comment