Home » Array Functions » Understanding the array.from Function in Pine Script

Understanding the array.from Function in Pine Script

Photo of author
Published on

One of the many powerful utilities it offers to developers is the array.from function. This function is vital for creating and manipulating arrays, which are essential data structures for storing multiple values in a single variable.

What is array.from?

array.from is a versatile function in Pine Script that allows you to create an array and fill it with a variable number of elements of a specific type. The types supported include int, float, bool, string, label, line, color, box, table, and linefill. This capability is incredibly useful in various scenarios, such as handling multiple data points, storing values for calculations, or managing graphical objects on a chart.

Syntax and Overloads

The syntax of array.from is straightforward. It takes multiple arguments of a specific type and returns an array containing those elements. Here is the general syntax:

array.from(arg0, arg1, ...) → type[]

This syntax can be adapted to various types, as shown in the given explanation. For example, to create an array of strings, you can use:

array.from("Hello", "World!") → string[]

In this instance, a string array containing two elements, “Hello” and “World!”, is created.

A Unique Use Case Example

To delve deeper, let’s explore a unique example of using the array.from function to create an array of lines that represent support and resistance levels on a chart.

Creating Support and Resistance Levels
indicator("Support and Resistance Levels", overlay = true)

// Sample prices to create support and resistance levels
levels = array.from(1800, 1850, 1900, 1950, 2000)

// Plotting lines on the chart for each level
for i = 0 to array.size(levels) - 1
    level = array.get(levels, i)
    line.new(x1 = bar_index[10], y1 = level, x2 = bar_index, y2 = level, color=color.red, width=1)

array.from Function
Code Explanation
  1. We start by defining a new indicator using the indicator function. The "Support and Resistance Levels" string is the name of the indicator, and overlay = true ensures that the indicator is plotted over the price chart.
  2. We then create an array of float values named levels, using the array.from function, representing price levels where we want to draw our support and resistance lines.
  3. A for loop is employed to iterate over each element in the levels array. The array.size function is used to get the total number of elements in the array.
  4. Within the loop, the array.get function retrieves each level from the levels array.
  5. The line.new function is used to draw lines on the chart at each level stored in the array. The lines are drawn from 10 bars ago to the current bar, at the y-values specified in the levels array.
  6. Finally, the plot function is used to plot the closing prices, helping visualize the support and resistance lines in context with the price data.

Key Takeaway

The primary takeaway here is the flexibility and utility of the array.from function. It can be adapted for various data types and is instrumental in creating dynamic arrays. The function simplifies the process of handling multiple data elements, thus making data management and manipulation in Pine Script more efficient and less cumbersome.


The array.from function is a quintessential tool for any Pine Script developer. It offers a robust solution for creating arrays of different data types, contributing to the language’s flexibility. By understanding and utilizing this function effectively, developers can optimize data handling, make their code cleaner, and implement more complex logic in their custom indicators and strategies on TradingView. This exploration of array.from, along with the unique use case, aims to provide a concrete foundation for effectively utilizing arrays in Pine Script.

Leave a Comment