Appearance
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 valueAn 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 firstIndexes
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.
| Functions | A na element | An infinite element |
|---|---|---|
sum, avg, min, max, range, stdev, variance | skipped | skipped |
median, mode, percentrank | skipped | kept |
percentile_nearest_rank, percentile_linear_interpolation | sorted last and counted | kept |
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.
| Parameter | Default | Description |
|---|---|---|
sizeseries int | 0 | How many elements. |
initial_value<array_type> | na | The 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.
| Name | Takes | Gives |
|---|---|---|
array. | size: series int, initial_value: series bool | array<bool> |
array. | size: series int, initial_value: series box | array<box> |
array. | size: series int, initial_value: series color | array<color> |
array. | size: series int, initial_value: series int/float | array<float> |
array. | size: series int, initial_value: series int | array<int> |
array. | size: series int, initial_value: series label | array<label> |
array. | size: series int, initial_value: series line | array<line> |
array. | size: series int, initial_value: series linefill | array<linefill> |
array. | size: series int, initial_value: series string | array<string> |
array. | size: series int, initial_value: series table | array<table> |
| Parameter | Description |
|---|---|
sizeseries int | How many elements, 0 if left out. |
initial_value | The 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.
| Parameter | Description |
|---|---|
idany array type | The array. |
indexseries int | Which element, from 0. |
array.set
pine
array.set(id, index, value) → voidAlso a method: id.set(index, value).
Replaces the element at index with value.
| Parameter | Description |
|---|---|
idany array type | The array. |
indexseries int | Which element, from 0. |
valueseries <type of the array's elements> | The new value. |
array.size
pine
array.size(id) → series intAlso a method: id.size().
How many elements the array holds.
| Parameter |
|---|
idany 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 |
|---|
idany 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 |
|---|
idany array type |
array.push
pine
array.push(id, value) → voidAlso a method: id.push(value).
Adds value after the last element.
| Parameter |
|---|
idany array type |
valueseries <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 |
|---|
idany array type |
array.unshift
pine
array.unshift(id, value) → voidAlso a method: id.unshift(value).
Adds value before the first element.
| Parameter |
|---|
idany array type |
valueseries <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 |
|---|
idany array type |
array.insert
pine
array.insert(id, index, value) → voidAlso 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.
| Parameter | Description |
|---|---|
idany array type | The array. |
indexseries int | Where the new element goes. |
valueseries <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.
| Parameter | Description |
|---|---|
idany array type | The array. |
indexseries int | Which element, from 0. |
array.clear
pine
array.clear(id) → voidAlso a method: id.clear().
Removes every element, leaving the array empty.
| Parameter |
|---|
idany array type |
array.fill
pine
array.fill(id, value, index_from, index_to) → voidAlso a method: id.fill(value, index_from, index_to).
Sets every element from index_from up to, but not including, index_to to value.
| Parameter | Default | Description |
|---|---|---|
idany array type | The array. | |
valueseries <type of the array's elements> | The value to set. | |
index_fromseries int | 0 | The first index to set. |
index_toseries 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 |
|---|
idany 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.
| Parameter | Description |
|---|---|
idany array type | The array. |
index_fromseries int | The first index to take. |
index_toseries 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.
| Parameter | Description |
|---|---|
id1any array type | The array to add to. |
id2any array type | The array whose elements are added. |
array.reverse
pine
array.reverse(id) → voidAlso a method: id.reverse().
Reverses the order of the elements, in place.
| Parameter |
|---|
idany array type |
array.join
pine
array.join(id, separator) → series stringAlso 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".
| Parameter | Default | Description |
|---|---|---|
idarray<float>/array<int>/array<string> | The array. | |
separatorseries string | "," | What goes between two elements. |
array.includes
pine
array.includes(id, value) → series boolAlso a method: id.includes(value).
Whether any element is exactly value: see equality.
| Parameter |
|---|
idany array type |
valueseries <type of the array's elements> |
array.indexof
pine
array.indexof(id, value) → series intAlso a method: id.indexof(value).
The index of the first element that is exactly value, or -1 if none is.
| Parameter |
|---|
idany array type |
valueseries <type of the array's elements> |
array.lastindexof
pine
array.lastindexof(id, value) → series intAlso a method: id.lastindexof(value).
The index of the last element that is exactly value, or -1 if none is.
| Parameter |
|---|
idany array type |
valueseries <type of the array's elements> |
array.binary_search
pine
array.binary_search(id, val, sort_field) → series intAlso 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]:
val | binary_search | binary_search_leftmost | binary_search_rightmost |
|---|---|---|---|
3 | 1 | 1 | 2 |
4 | -1 | 2, the 3 before it | 3, the 5 after it |
0 | -1 | 0 | 0 |
9 | -1 | 3 | 4, the size |
| Parameter | Description |
|---|---|
idany array type | The sorted array. |
valseries int/float | The value to look for. |
sort_fieldseries 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 intAlso 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.
| Parameter | Description |
|---|---|
idany array type | The sorted array. |
valseries int/float | The value to look for. |
sort_fieldseries 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 intAlso 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.
| Parameter | Description |
|---|---|
idany array type | The sorted array. |
valseries int/float | The value to look for. |
sort_fieldseries 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) → voidAlso 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.
| Parameter | Default | Description |
|---|---|---|
idany array type | The array. | |
ordersimple sort_order | order.ascending | order.ascending or order.descending. |
sort_fieldseries 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.
| Parameter | Default | Description |
|---|---|---|
idany array type | The array. | |
orderseries sort_order | order.ascending | order.ascending or order.descending. |
sort_fieldseries string/int | In an array of objects, the field to sort by, as in array.sort. |
array.sum
pine
array.sum(id) → series float/intAlso a method: id.sum().
The sum of the numbers, skipping na and infinities: see na and infinity.
| Parameter |
|---|
idarray<float>/array<int> |
array.avg
pine
array.avg(id) → series float/intAlso a method: id.avg().
The mean of the numbers, skipping na and infinities.
| Parameter |
|---|
idarray<float>/array<int> |
array.min
pine
array.min(id, nth) → series float/intAlso a method: id.min(nth).
The smallest number, or the nth smallest, skipping na and infinities.
| Parameter | Default | Description |
|---|---|---|
idarray<float>/array<int> | The array. | |
nthseries int | 0 | Which one: 0 is the smallest, 1 the next. Past the last number it reads na. |
array.max
pine
array.max(id, nth) → series float/intAlso a method: id.max(nth).
The largest number, or the nth largest, skipping na and infinities.
| Parameter | Default | Description |
|---|---|---|
idarray<float>/array<int> | The array. | |
nthseries int | 0 | Which one: 0 is the largest, 1 the next. Past the last number it reads na. |
array.median
pine
array.median(id) → series float/intAlso a method: id.median().
The middle number once sorted, skipping na. With an even count it is the mean of the middle two.
| Parameter |
|---|
idarray<float>/array<int> |
array.mode
pine
array.mode(id) → series float/intAlso 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 |
|---|
idarray<float>/array<int> |
array.range
pine
array.range(id) → series float/intAlso a method: id.range().
The largest number minus the smallest, skipping na and infinities.
| Parameter |
|---|
idarray<float>/array<int> |
array.stdev
pine
array.stdev(id, biased) → series float/intAlso 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.
| Parameter | Default | Description |
|---|---|---|
idarray<float>/array<int> | The array. | |
biasedseries bool | true | true divides by the count, false by the count less one. |
array.variance
pine
array.variance(id, biased) → series float/intAlso a method: id.variance(biased).
The variance of the numbers, skipping na and infinities, with biased as in array.stdev.
| Parameter | Default | Description |
|---|---|---|
idarray<float>/array<int> | The array. | |
biasedseries bool | true | true divides by the count, false by the count less one. |
array.covariance
pine
array.covariance(id1, id2, biased) → series floatAlso 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.
| Parameter | Default | Description |
|---|---|---|
id1array<float>/array<int> | The first array. | |
id2array<float>/array<int> | The second array. | |
biasedseries bool | true | true 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 |
|---|
idarray<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 |
|---|
idarray<float>/array<int> |
array.percentile_nearest_rank
pine
array.percentile_nearest_rank(id, percentage) → series float/intAlso 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.
| Parameter | Description |
|---|---|
idarray<float>/array<int> | The array. |
percentageseries int/float | From 0 to 100. |
array.percentile_linear_interpolation
pine
array.percentile_linear_interpolation(id, percentage) → series float/intAlso 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.
| Parameter | Description |
|---|---|
idarray<float>/array<int> | The array. |
percentageseries int/float | From 0 to 100. |
array.percentrank
pine
array.percentrank(id, index) → series float/intAlso 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.
| Parameter | Description |
|---|---|
idarray<float>/array<int> | The array. |
indexseries int | The element to rank. |
array.every
pine
array.every(id) → series boolAlso 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 |
|---|
idarray<bool> |
array.some
pine
array.some(id) → series boolAlso 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 |
|---|
idarray<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 ownta.*state and its own request. This holds only whilearray.mapandarray.sizealone read the name. A write, a reassignment, avaror a type on it makes it an ordinary array. See lists. - Any other array: one loop over the elements. A
ta.*call infnkeeps one state for every element, as it would in aforloop, 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)]).
| Parameter | Description |
|---|---|
listarray<type>/list | An array, or a list written as [a, b, c]. |
fnlambda 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.
| Parameter | Description |
|---|---|
idarray<type> | The array. |
fnlambda 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 positiveSee lambdas.
array.reduce
AmenoScript only
ameno
array.reduce(id, start, fn) → typeAlso 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).
| Parameter | Description |
|---|---|
idarray<type> | The array. |
starttype | What is kept before the first element. |
fnlambda 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.
| Parameter | Default | Description |
|---|---|---|
idarray<type> | The array. | |
fnlambda 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. | |
orderseries sort_order | order.ascending | order.ascending or order.descending. |
See lambdas.
array.any
AmenoScript only
ameno
array.any(id, fn) → series boolAlso 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.
| Parameter | Description |
|---|---|
idarray<type> | The array. |
fnlambda 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 boolAlso 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.
| Parameter | Description |
|---|---|
idarray<type> | The array. |
fnlambda 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) → typeAlso 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.
| Parameter | Description |
|---|---|
idarray<type> | The array. |
fnlambda or fn name | A lambda such as x => x < 0, or the name of a function of yours. |
See lambdas.