September 10, 2020
Arrays Are Now Available in Pine

Pine scripts can now use a powerful new arrays feature to build custom datasets. Arrays significantly extend the modelling capabilities of Pine by allowing coders to populate, manage and calculate on one-dimensional data structures containing elements of one of the following types: float, int, bool, or color.

This line creates an array called levels containing three float elements initialized with the value na:

levels = array.new_float(size = 3, initial_value = na)

Arrays support dynamic resizing. Special functions allow various transformations on arrays, including copying, sorting and slicing. Special-purpose math functions also operate on them: array.min(), array.max(), array.stdev(), array.avg(), array.median(), etc.

This example uses an array to build a dataset holding price levels of only the bars where volume increases. It then plots the average of those values. The array is used here to implement a FIFO (first in, first out) structure:

//@version=4 
study("Price Avg Of Increasing Volume Bars", "", true) 
i_levels = input(20,   "Levels") 
i_src    = input(hlc3, "Source") 

// Initialize array of user-selected size with `na` values. 
var levels = array.new_float(i_levels) 

if rising(volume, 1)     
    // Remove the oldest level from the beginning of the array.
    array.shift(levels)

    // Add the new level to the end of the array. 
    array.push(levels, i_src) 

// Calculate the avg of levels in the array, excluding any `na` values. 
level = array.avg(levels)
plot(level, "Level", close > level ? color.lime : color.fuchsia)

The Pine User Manual page on arrays will help you get started. Also see arrays in action in these scripts by the PineCoders who helped us the most in testing arrays, and whom we thank sincerely: RicardoSantos and Duyck:

Graph style – 4th Dimension RSI

Function – Multi Dimension Indexer

Function – K-Means Clustering

Function – Linear Regression

Function – Polynomial Regression

Trendlines – JD

Average Pivot Range – JD

Matrix functions – JD

These scripts give us a glimpse of how arrays redefine what’s possible in Pine, and how they will pave the way for more powerful indicators and strategies than ever, for all TradingView traders. We love to see it.

We hope this new feature you requested is useful. Please continue sending us your feedback for improvement. We build TradingView for you and love hearing what you think of these highly requested updates to our platform.

The fastest way to follow markets

Launch Chart