Inserts a value at a chosen position in an array, pushing the items from that position onward up by one.
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
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>) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | any array type | required | The array that receives the new item. Items from the chosen position onward move up by one. |
| 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. |
| value | series <type of the array's elements> | required | The item to store, or the item to look for. It has to match the type of the array's items. |
Returns
Nothing.
Remarks
- To keep a sorted array sorted as it grows, find the position with
array.binary_search_rightmost()and insert next to it.
Example
//@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