Skip to content

array ​

How do you keep a list of values across bars, such as the closes of the last five up days? Declare an array with var, so the same array carries from bar to bar, then push to it and read it with array.get or a summary such as array.avg. An array holds values of one type and counts from index 0. It grows and shrinks as you add and remove elements. Most functions here can also be called as a method, as in ups.push(close).

pine
//@version=6
indicator("Mean of the last five up closes")
var array<float> ups = array.new_float() // var: one array, kept from bar to bar
if close > open
    ups.push(close)
    if ups.size() > 5
        ups.shift() // drop the oldest
plot(ups.avg())

References and copies ​

An array variable holds a reference to the array, not its values. b = a gives one array two names, so a change made through either shows in both. array.copy and array.slice make a new array, which you can change without touching the original. The copy is one level deep. An element that is an object, an array or a map is the same one in both, so a change inside it shows in both.

pine
//@version=6
indicator("Shared or copied")
a = array.from(1.0, 2.0)
b = a              // the same array under a second name
c = array.copy(a)  // a new array
b.set(0, 9)
plot(a.get(0), "a")  // 9: changed through b
plot(c.get(0), "c")  // 1: the copy kept its value

An earlier bar's array ​

a[1] is the array as it stood when the previous bar closed, with the size and the elements it had then. On the first bar it is na. The snapshot is one level deep, like a copy. An array, a map or an object held inside it is the same one as today's, so a change made to it since shows in a[1] too.

pine
//@version=6
indicator("Elements added since the last bar")
var array<float> a = array.new_float()
a.push(close)
plot(bar_index > 0 ? a.size() - array.size(a[1]) : na) // 1 on every bar after the first

Indexes ​

Index 0 is the first element and size - 1 the last. A negative index counts from the end, so -1 is the last element. An index past either end stops the script with an error, and so do array.pop, array.shift, array.first and array.last on an empty array. A fractional index drops its fraction, so 1.7 reads index 1. A na index reads na in array.get and array.remove, changes nothing in array.set, and appends in array.insert.

na and infinity in statistics ​

The statistics skip a na element, and some skip an infinity too. An infinity here is a value such as 1.0 / (close - close) worked out at run time: strict Pine refuses a literal division by zero.

FunctionsA na elementAn infinite element
sum, avg, min, max, range, stdev, varianceskippedskipped
median, mode, percentrankskippedkept
percentile_nearest_rank, percentile_linear_interpolationsorted last and countedkept

An empty array, or one holding only na, reads na from each of these, and that includes array.sum, which doesn't read 0. array.standardize and array.abs keep a na in its place. array.covariance reads na if either array holds one.

Equality ​

array.includes, array.indexof and array.lastindexof look for an element that is exactly the value. Two floats match only when they are the same number, although == treats floats within 1e-10 of each other as equal. So 0.1 + 0.2 worked out at run time doesn't match 0.3. Strings, bools and colours match by value. Objects, arrays and maps match only when they are the same one. A na matches a na.

Size limit ​

An array holds at most 100,000 elements. Making or growing one past that stops the script with an error.

array.new ​

pine
array.new<type>(size, initial_value) → array<type>

A new array of size elements, each set to initial_value.

With no initial value each element is na, except in array.new<bool>, where it is false. An object given as the initial value puts that one object in every slot, so a change made to it through one slot shows in all of them.

ParameterDefaultDescription
size
series int
0How many elements.
initial_value
<array_type>
naThe value of every element.

array.new_* ​

A new array of one type, size elements long, each set to initial_value. Left out, initial_value is na, except in array.new_bool, where it is false.

NameTakesGives
array.new_boolsize: series int, initial_value: series boolarray<bool>
array.new_boxsize: series int, initial_value: series boxarray<box>
array.new_colorsize: series int, initial_value: series colorarray<color>
array.new_floatsize: series int, initial_value: series int/floatarray<float>
array.new_intsize: series int, initial_value: series intarray<int>
array.new_labelsize: series int, initial_value: series labelarray<label>
array.new_linesize: series int, initial_value: series linearray<line>
array.new_linefillsize: series int, initial_value: series linefillarray<linefill>
array.new_stringsize: series int, initial_value: series stringarray<string>
array.new_tablesize: series int, initial_value: series tablearray<table>
ParameterDescription
size
series int
How many elements, 0 if left out.
initial_valueThe value of every element.

array.from ​

pine
array.from(arg0, arg1, ...) → array<type>

An array of its arguments, in the order given, such as array.from(1.0, 2.0, 3.0).

Parameter
arg0
<arg..._type>
arg1
<arg..._type>
...
<arg..._type>

array.get ​

pine
array.get(id, index) → series <type>

Also a method: id.get(index).

The element at index. A negative index counts from the end: see indexes.

ParameterDescription
id
any array type
The array.
index
series int
Which element, from 0.

array.set ​

pine
array.set(id, index, value) → void

Also a method: id.set(index, value).

Replaces the element at index with value.

ParameterDescription
id
any array type
The array.
index
series int
Which element, from 0.
value
series <type of the array's elements>
The new value.

array.size ​

pine
array.size(id) → series int

Also a method: id.size().

How many elements the array holds.

Parameter
id
any array type

array.first ​

pine
array.first(id) → series <type>

Also a method: id.first().

The first element. An empty array stops the script with an error.

Parameter
id
any array type

array.last ​

pine
array.last(id) → series <type>

Also a method: id.last().

The last element. An empty array stops the script with an error.

Parameter
id
any array type

array.push ​

pine
array.push(id, value) → void

Also a method: id.push(value).

Adds value after the last element.

Parameter
id
any array type
value
series <type of the array's elements>

array.pop ​

pine
array.pop(id) → series <type>

Also a method: id.pop().

Removes the last element and returns it. An empty array stops the script with an error.

Parameter
id
any array type

array.unshift ​

pine
array.unshift(id, value) → void

Also a method: id.unshift(value).

Adds value before the first element.

Parameter
id
any array type
value
series <type of the array's elements>

array.shift ​

pine
array.shift(id) → series <type>

Also a method: id.shift().

Removes the first element and returns it. An empty array stops the script with an error.

Parameter
id
any array type

array.insert ​

pine
array.insert(id, index, value) → void

Also a method: id.insert(index, value).

Puts value at index and moves the elements from there one place along.

An index equal to the size appends. A negative index counts from the end, so -1 inserts before the last element. An index past the size stops the script with an error, and a na index appends.

ParameterDescription
id
any array type
The array.
index
series int
Where the new element goes.
value
series <type of the array's elements>
The value to insert.

array.remove ​

pine
array.remove(id, index) → series <type>

Also a method: id.remove(index).

Removes the element at index and returns it.

ParameterDescription
id
any array type
The array.
index
series int
Which element, from 0.

array.clear ​

pine
array.clear(id) → void

Also a method: id.clear().

Removes every element, leaving the array empty.

Parameter
id
any array type

array.fill ​

pine
array.fill(id, value, index_from, index_to) → void

Also a method: id.fill(value, index_from, index_to).

Sets every element from index_from up to, but not including, index_to to value.

ParameterDefaultDescription
id
any array type
The array.
value
series <type of the array's elements>
The value to set.
index_from
series int
0The first index to set.
index_to
series int
The index to stop before. Left out, the fill runs to the end.

array.copy ​

pine
array.copy(id) → array<type>

Also a method: id.copy().

A new array holding the same elements, one level deep: see references and copies.

Parameter
id
any array type

array.slice ​

pine
array.slice(id, index_from, index_to) → array<type>

Also a method: id.slice(index_from, index_to).

A new array of the elements from index_from up to, but not including, index_to.

A negative index counts from the end, and an end past the size stops at the last element. The slice is a copy, one level deep, so writing to it leaves the original as it was.

A slice is a copy here

TradingView's reference says a change made to a slice shows in the original array too. Here the slice is a copy, so a script that writes to the original through its slice behaves differently on the two.

ParameterDescription
id
any array type
The array.
index_from
series int
The first index to take.
index_to
series int
The index to stop before. Left out, the slice runs to the end.

array.concat ​

pine
array.concat(id1, id2) → array<type>

Also a method: id1.concat(id2).

Adds every element of id2 after the last element of id1, and returns id1 itself, not a new array.

Concatenating an array with itself adds one copy of its elements.

ParameterDescription
id1
any array type
The array to add to.
id2
any array type
The array whose elements are added.

array.reverse ​

pine
array.reverse(id) → void

Also a method: id.reverse().

Reverses the order of the elements, in place.

Parameter
id
any array type

array.join ​

pine
array.join(id, separator) → series string

Also a method: id.join(separator).

The elements as one string, with separator between each pair, so array.join(array.from(1.0, 2.5)) is "1,2.5".

ParameterDefaultDescription
id
array<float>/array<int>/array<string>
The array.
separator
series string
","What goes between two elements.

array.includes ​

pine
array.includes(id, value) → series bool

Also a method: id.includes(value).

Whether any element is exactly value: see equality.

Parameter
id
any array type
value
series <type of the array's elements>

array.indexof ​

pine
array.indexof(id, value) → series int

Also a method: id.indexof(value).

The index of the first element that is exactly value, or -1 if none is.

Parameter
id
any array type
value
series <type of the array's elements>

array.lastindexof ​

pine
array.lastindexof(id, value) → series int

Also a method: id.lastindexof(value).

The index of the last element that is exactly value, or -1 if none is.

Parameter
id
any array type
value
series <type of the array's elements>
pine
array.binary_search(id, val, sort_field) → series int

Also a method: id.binary_search(val, sort_field).

The index of an element equal to val in an array sorted in ascending order, or -1 if none is.

The array must already be sorted ascending, and on an unsorted array the answer means nothing. With repeated values it finds one of them. The leftmost and rightmost forms find the first or the last one, and say where a missing value would go. For [1, 3, 3, 5]:

valbinary_searchbinary_search_leftmostbinary_search_rightmost
3112
4-12, the 3 before it3, the 5 after it
0-100
9-134, the size
ParameterDescription
id
any array type
The sorted array.
val
series int/float
The value to look for.
sort_field
series string/int
In an array of objects, the field to compare: its name, or its place counting from 0. Left out, the first field.

array.binary_search_leftmost ​

pine
array.binary_search_leftmost(id, val, sort_field) → series int

Also a method: id.binary_search_leftmost(val, sort_field).

The index of the first element equal to val in an array sorted ascending. If none is equal, it gives the index of the last element below val, or 0 when val is below them all.

ParameterDescription
id
any array type
The sorted array.
val
series int/float
The value to look for.
sort_field
series string/int
In an array of objects, the field to compare, as in array.binary_search.

array.binary_search_rightmost ​

pine
array.binary_search_rightmost(id, val, sort_field) → series int

Also a method: id.binary_search_rightmost(val, sort_field).

The index of the last element equal to val in an array sorted ascending. If none is equal, it gives the index of the first element above val, or the array's size when val is above them all.

ParameterDescription
id
any array type
The sorted array.
val
series int/float
The value to look for.
sort_field
series string/int
In an array of objects, the field to compare, as in array.binary_search.

array.sort ​

pine
array.sort(id, order, sort_field) → void

Also a method: id.sort(order, sort_field).

Sorts the array in place.

Numbers sort by value, and strings by their character codes, so "B" comes before "a" and "10" before "9". A na goes last in ascending order and first in descending. Equal elements keep their order.

ParameterDefaultDescription
id
any array type
The array.
order
simple sort_order
order.ascendingorder.ascending or order.descending.
sort_field
series string/int
In an array of objects, the field to sort by: its name, or its place counting from 0. Left out, the first field.

array.sort_indices ​

pine
array.sort_indices(id, order, sort_field) → array<int>

Also a method: id.sort_indices(order, sort_field).

The indexes of the elements in sorted order, leaving the array as it was.

a.get(idx.get(0)) is then the smallest element. Sorting follows the rules of array.sort, except that equal elements come out in reverse order when the order is descending.

ParameterDefaultDescription
id
any array type
The array.
order
series sort_order
order.ascendingorder.ascending or order.descending.
sort_field
series string/int
In an array of objects, the field to sort by, as in array.sort.

array.sum ​

pine
array.sum(id) → series float/int

Also a method: id.sum().

The sum of the numbers, skipping na and infinities: see na and infinity.

Parameter
id
array<float>/array<int>

array.avg ​

pine
array.avg(id) → series float/int

Also a method: id.avg().

The mean of the numbers, skipping na and infinities.

Parameter
id
array<float>/array<int>

array.min ​

pine
array.min(id, nth) → series float/int

Also a method: id.min(nth).

The smallest number, or the nth smallest, skipping na and infinities.

ParameterDefaultDescription
id
array<float>/array<int>
The array.
nth
series int
0Which one: 0 is the smallest, 1 the next. Past the last number it reads na.

array.max ​

pine
array.max(id, nth) → series float/int

Also a method: id.max(nth).

The largest number, or the nth largest, skipping na and infinities.

ParameterDefaultDescription
id
array<float>/array<int>
The array.
nth
series int
0Which one: 0 is the largest, 1 the next. Past the last number it reads na.

array.median ​

pine
array.median(id) → series float/int

Also a method: id.median().

The middle number once sorted, skipping na. With an even count it is the mean of the middle two.

Parameter
id
array<float>/array<int>

array.mode ​

pine
array.mode(id) → series float/int

Also a method: id.mode().

The number that occurs most often, skipping na. On a tie it gives the smallest of them, so with no repeats at all it gives the smallest number.

Parameter
id
array<float>/array<int>

array.range ​

pine
array.range(id) → series float/int

Also a method: id.range().

The largest number minus the smallest, skipping na and infinities.

Parameter
id
array<float>/array<int>

array.stdev ​

pine
array.stdev(id, biased) → series float/int

Also a method: id.stdev(biased).

The standard deviation of the numbers, skipping na and infinities.

By default it divides by the count, as for a whole population. With biased = false it divides by the count less one, as for a sample. A single number then reads 0 by default and na with biased = false.

ParameterDefaultDescription
id
array<float>/array<int>
The array.
biased
series bool
truetrue divides by the count, false by the count less one.

array.variance ​

pine
array.variance(id, biased) → series float/int

Also a method: id.variance(biased).

The variance of the numbers, skipping na and infinities, with biased as in array.stdev.

ParameterDefaultDescription
id
array<float>/array<int>
The array.
biased
series bool
truetrue divides by the count, false by the count less one.

array.covariance ​

pine
array.covariance(id1, id2, biased) → series float

Also a method: id1.covariance(id2, biased).

The covariance of two arrays, element by element.

It reads na if either array holds a na, or if they have fewer than two elements. Arrays of different sizes are read up to the shorter one's size.

ParameterDefaultDescription
id1
array<float>/array<int>
The first array.
id2
array<float>/array<int>
The second array.
biased
series bool
truetrue divides by the count, false by the count less one.

array.standardize ​

pine
array.standardize(id) → array<float>/array<int>

Also a method: id.standardize().

A new array of each element's distance from the mean, in standard deviations, with the deviation taken over the whole population.

A na stays na in its place. If every number is equal, each reads 0.

Parameter
id
array<float>/array<int>

array.abs ​

pine
array.abs(id) → array<float>/array<int>

Also a method: id.abs().

A new array of each element's absolute value, with na kept in its place.

Parameter
id
array<float>/array<int>

array.percentile_nearest_rank ​

pine
array.percentile_nearest_rank(id, percentage) → series float/int

Also a method: id.percentile_nearest_rank(percentage).

The element at the given percentile, taken from the sorted array without interpolating.

The array sorts with each na last, and the na counts toward the size. The call reads element ceil(n × percentage / 100), counting from 1, where n is the size. So [na, 1, 3] at 50 reads 3, and a percentile that lands on a na reads na. This matches TradingView.

ParameterDescription
id
array<float>/array<int>
The array.
percentage
series int/float
From 0 to 100.

array.percentile_linear_interpolation ​

pine
array.percentile_linear_interpolation(id, percentage) → series float/int

Also a method: id.percentile_linear_interpolation(percentage).

The value at the given percentile, read between the two nearest elements.

The ith of n sorted elements, counting from 0, sits at percentile 100 × (i + 0.5) / n, and a percentage between two of them reads proportionally between their values. So [1, 2, 3, 4] at 25 reads 1.5. Below the first position the call reads the smallest element, and above the last it reads the largest. A na anywhere in the array makes any percentage that falls between two positions read na. This matches TradingView.

ParameterDescription
id
array<float>/array<int>
The array.
percentage
series int/float
From 0 to 100.

array.percentrank ​

pine
array.percentrank(id, index) → series float/int

Also a method: id.percentrank(index).

The percentage of the array's numbers that are at or below the element at index, skipping na.

An element that is na reads na.

ParameterDescription
id
array<float>/array<int>
The array.
index
series int
The element to rank.

array.every ​

pine
array.every(id) → series bool

Also a method: id.every().

Whether every element is true. An empty array reads true.

A number counts as true when it isn't 0, and a na counts as false. TradingView takes an array of bools only.

Parameter
id
array<bool>

array.some ​

pine
array.some(id) → series bool

Also a method: id.some().

Whether at least one element is true. An empty array reads false. Numbers and na count as in array.every.

Parameter
id
array<bool>

array.map ​

AmenoScript only
ameno
array.map(list, fn) → array<type>

A new array holding the value of fn for each element, in order.

How fn runs depends on where the list comes from:

  • A list written in the text, such as [10, 20, 50], or a top-level name holding one: each row is a call of its own, with its own ta.* state and its own request. This holds only while array.map and array.size alone read the name. A write, a reassignment, a var or a type on it makes it an ordinary array. See lists.
  • Any other array: one loop over the elements. A ta.* call in fn keeps one state for every element, as it would in a for loop, and the run warns.

A tuple from fn gives one array per element: [fast, slow] = array.map([5, 10], n => [ta.sma(close, n), ta.ema(close, n)]).

ParameterDescription
list
array<type>/list
An array, or a list written as [a, b, c].
fn
lambda or fn name
A lambda such as x => x * 2, or the name of a function of yours. It can read anything its block can see.
ameno
indicator("Three averages, one call each")
array<float> mas = array.map([10, 20, 50], n => ta.sma(close, n)) // each row keeps its own sma
plot(mas.get(0), "10")
plot(mas.get(1), "20")
plot(mas.get(2), "50")

See lambdas.

array.filter ​

AmenoScript only
ameno
array.filter(id, fn) → array<type>

Also a method: id.filter(fn).

A new array of the elements for which fn is true, in their order. The array itself is left as it was.

ParameterDescription
id
array<type>
The array.
fn
lambda or fn name
A lambda such as x => x > 0, or the name of a function of yours.
ameno
indicator("Sum of the positive changes")
array<float> moves = array.from(close - close[1], close[1] - close[2], close[2] - close[3])
array<float> ups = moves.filter(m => m > 0)
plot(ups.reduce(0.0, (total, m) => total + m)) // 0.0 when none is positive

See lambdas.

array.reduce ​

AmenoScript only
ameno
array.reduce(id, start, fn) → type

Also a method: id.reduce(start, fn).

Folds the elements into one value. fn takes what is kept so far and the next element, and returns what to keep.

The fold starts from start, so an empty array returns start. The method form is xs.reduce(start, fn).

ParameterDescription
id
array<type>
The array.
start
type
What is kept before the first element.
fn
lambda or fn name
A lambda of two parameters, such as (total, x) => total + x, or the name of a function of yours that takes two.

See lambdas.

array.sort_by ​

AmenoScript only
ameno
array.sort_by(id, fn, order) → array<type>

Also a method: id.sort_by(fn, order).

A new array of the elements, sorted by the number fn gives for each. The array itself is left as it was.

An element whose key is na goes last in ascending order.

ParameterDefaultDescription
id
array<type>
The array.
fn
lambda or fn name
A lambda such as x => math.abs(x), or the name of a function of yours, giving a number for each element.
order
series sort_order
order.ascendingorder.ascending or order.descending.

See lambdas.

array.any ​

AmenoScript only
ameno
array.any(id, fn) → series bool

Also a method: id.any(fn).

Whether fn is true for at least one element. An empty array reads false.

It stops at the first element that is true, so a ta.* call in fn doesn't run for the elements after it.

ParameterDescription
id
array<type>
The array.
fn
lambda or fn name
A lambda such as x => x > 0, or the name of a function of yours.

See lambdas.

array.all ​

AmenoScript only
ameno
array.all(id, fn) → series bool

Also a method: id.all(fn).

Whether fn is true for every element. An empty array reads true.

It stops at the first element that is false, so a ta.* call in fn doesn't run for the elements after it.

ParameterDescription
id
array<type>
The array.
fn
lambda or fn name
A lambda such as x => x > 0, or the name of a function of yours.

See lambdas.

array.find ​

AmenoScript only
ameno
array.find(id, fn) → type

Also a method: id.find(fn).

The first element for which fn is true, or na if there is none.

It stops at that element, so a ta.* call in fn doesn't run for the elements after it.

ParameterDescription
id
array<type>
The array.
fn
lambda or fn name
A lambda such as x => x < 0, or the name of a function of yours.

See lambdas.

AmenoScript is compatible with Pine Script v6. Pine Script is a trademark of TradingView, Inc., which has no part in AmenoScript.