Removes the first item of an array and returns it; every other item moves down one position.
After array.shift(queue) the item that was at index 1 is at index 0, and the array is one item shorter. Pairing it with array.push() gives a first-in, first-out queue, which is the usual way to hold the last N values.
Shifting an empty array stops the script with an error saying there is nothing to shift out. Check array.size() first when the array might be empty.
Syntax
array.shift(id: any array type) -> series <type>
array.shift() -> series <type>Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | any array type | required | The array to take the first item from. The item is removed and returned. |
Returns
series <type>: The item that was removed.
Remarks
- It is the mirror of
array.pop(), which takes from the end. - A script can ignore the returned value; the call still removes the item.
Example
//@version=6
indicator("array.shift: queue", overlay = true)
var q = array.new_float()
array.push(q, close)
if array.size(q) > 8
dropped = array.shift(q)
plot(array.avg(q), "Average of last 8")See also
Runs on Sidechair