Sidechair

array.insert()

Browse the reference

Inserts a value at a chosen position in an array, pushing the items from that position onward up by one.

Try it in the editor

array.insert(a, 1, x) leaves the first item in place and puts x at index 1. An index equal to the array size appends, so inserting at the size behaves like array.push().

In v6 a negative index counts back from the end. Any index beyond the size, or below minus the size, stops the script with an error that names the index and the array size.

Syntax

Pine Script
array.insert(id: any array type, index: series int (optional), value: series <type of the array's elements>) -> void
array.insert(index: series int (optional), value: series <type of the array's elements>) -> void

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array that receives the new item. Items from the chosen position onward move up by one.
indexseries intrequiredPosition of the item. Counts from 0; a negative index counts back from the end, so -1 is the last item.
valueseries <type of the array's elements>requiredThe item to store, or the item to look for. It has to match the type of the array's items.

Returns

Nothing.

Remarks

Example

Pine ScriptSlot a close in next to the first itemOpen in editor
//@version=6
indicator("array.insert: second slot", overlay = false)
var levels = array.from(0.0, 0.0)
if bar_index % 25 == 0 and array.size(levels) < 6
    array.insert(levels, 1, close)
plot(array.size(levels), "Items held")
plot(array.get(levels, 1), "Item at index 1")

See also

Runs on Sidechair