Sidechair

array.slice()

Browse the reference

Returns the items from index_from up to, but not including, index_to as a smaller array.

Try it in the editor

array.slice(a, 0, 3) holds the first three items. Omit index_to to run to the end of the array. The start may equal the size, which yields an empty slice.

In standard Pine the slice is a view: setting an item in it also changes the parent array. Reads, such as array.avg() or array.max() on the slice, behave the same either way.

Syntax

Pine Script
array.slice(id: any array type, index_from: series int (optional), index_to: series int (optional)) -> array<type>
array.slice(index_from: series int (optional), index_to: series int (optional)) -> array<type>

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array to cut a piece from. On Sidechair the piece is a copy, so later changes do not reach it.
index_fromseries int0Position of the first item to include. Counts from 0.
index_toseries intthe size of the arrayPosition just after the last item to include.

Returns

array<type>: An array holding the chosen items.

Remarks

  • On Sidechair: the slice is an independent copy. Writing to it does not change the parent array, so copy items back with array.set() when you need that.
  • A start outside the array, or an end before the start, stops the script with an error.

Example

Pine ScriptAverage of the last 3 of 10Open in editor
//@version=6
indicator("array.slice: last three", overlay = true)
var win = array.new_float()
array.push(win, close)
if array.size(win) > 10
    array.shift(win)
n = array.size(win)
last3 = array.slice(win, math.max(0, n - 3), n)
plot(array.avg(last3), "Avg last 3")
plot(array.avg(win), "Avg last 10", color.gray)

See also

Runs on Sidechair