Sidechair

array.set()

Browse the reference

Replaces the item at a position in an array with a new value.

Try it in the editor

array.set(levels, 0, close) overwrites the first item. A negative index counts back from the end in v6, so array.set(levels, -1, close) changes the last item.

The index must point at an item that exists; set cannot grow an array. Use array.push() or array.insert() to add items.

Syntax

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

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array to write into. Only the item at the chosen index is replaced.
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

  • The value must have the type of the array's items.
  • The method form, levels.set(i, x), calls the same function.

Example

Pine ScriptKeep the latest value in slot 0Open in editor
//@version=6
indicator("array.set: running extremes", overlay = true)
var ext = array.from(0.0, 0.0)
if bar_index == 0
    array.set(ext, 0, low)
    array.set(ext, 1, high)
array.set(ext, 0, math.min(array.get(ext, 0), low))
array.set(ext, 1, math.max(array.get(ext, 1), high))
plot(array.get(ext, 0), "Lowest low", color.red)
plot(array.get(ext, 1), "Highest high", color.green)

See also

Runs on Sidechair