Returns the item stored at a position in an array, counting from 0, or from the end when the index is negative.
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
array.get(id: any array type, index: series int) -> series <type>
array.get(index: series int) -> series <type>Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | any array type | required | The array to read an item from. The array itself is not changed. |
| index | series int | required | Position 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()orarray.pop()to take it out. - The method form, prices.get(i), calls the same function.
Example
//@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