Sidechair

array.get()

Browse the reference

Returns the item stored at a position in an array, counting from 0, or from the end when the index is negative.

Try it in the editor

array.get(prices, 0) is the first item and array.get(prices, array.size(prices) - 1) is the last. In v6 a negative index counts back from the end, so array.get(prices, -1) is the last item and -2 the one before it.

An index outside the array stops the script with an error that names the index and the array size. Check array.size() first when the length can vary. The index has to be a whole number; na is an error as well.

Syntax

Pine Script
array.get(id: any array type, index: series int) -> series <type>
array.get(index: series int) -> series <type>

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array to read an item from. The array itself is not changed.
indexseries intrequiredPosition of the item. Counts from 0; a negative index counts back from the end, so -1 is the last item.

Returns

series <type>: The item at that position, with the array's item type.

Remarks

  • Reading does not remove the item. Use array.remove() or array.pop() to take it out.
  • The method form, prices.get(i), calls the same function.

Example

Pine ScriptFirst and last of a windowOpen in editor
//@version=6
indicator("array.get: window ends", overlay = true)
var win = array.new<float>()
array.push(win, close)
if array.size(win) > 20
    array.shift(win)
plot(array.get(win, 0), "Oldest", color.orange)
plot(array.get(win, -1), "Newest", color.blue)

See also

Runs on Sidechair