Arrays
An array holds a list of values. Use one to keep a rolling window, a set of levels, or anything you build up bar by bar.
//@version=5
indicator("Median of the last 9 closes", overlay = true)
var window = array.new_float(0)
window.push(close)
if window.size() > 9
window.shift()
plot(window.median(), "Median", color.orange)
Making arrays
array.new_float / new_int / new_bool / new_string / new_color(size, initial), array.new<float>(...), and array.from(a, b, ...). Typed declarations (array<float> a = ..., float[] a = ...) are accepted.
Every function can be called either way: array.push(a, x) or a.push(x), including on an expression, as in array.from(x, y).max().
Functions
- Access
size get set first last includes indexof lastindexof - Change
push pop shift unshift insert remove clear fill reverse sort(order.descending) concat - New arrays
copy slice(from, to) from - Statistics (skip
na)sum avg min(nth) max(nth) median range variance stdev join(separator), andfor item in aorfor [i, item] in ato loop
Things to know
- A plain
a = array.new_float(0)is a new, empty array on every bar. Usevarto keep one across bars. - An index outside the array, or
pop,shift,firstorlaston an empty one, stops the script with an error on that line, as in Pine. - An array holds at most 100,000 elements.
a[i]is not element access. It is the history operator, so usea.get(i).