Sidechair

array.shift()

Browse the reference

Removes the first item of an array and returns it; every other item moves down one position.

Try it in the editor

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

Pine Script
array.shift(id: any array type) -> series <type>
array.shift() -> series <type>

Arguments

NameTypeDefaultDescription
idany array typerequiredThe 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

Pine ScriptFixed-length queue of closesOpen in editor
//@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