Sidechair

array.copy()

Browse the reference

Returns a new array with the same items as the given one, so later changes to either do not affect the other.

Try it in the editor

The copy is shallow. Numbers, strings and colors are duplicated, but arrays of lines, labels, boxes or other objects get a new list that points at the same objects.

Without a copy, b = a only gives a second name for the same array, and a push through either name shows through both.

Syntax

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

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array to duplicate. The copy holds the same items but is a separate array.

Returns

array<type>: A new array with the same item type and the same items.

Example

Pine ScriptSort a copy, keep the original orderOpen in editor
//@version=6
indicator("array.copy: sorted copy", overlay = false)
var recent = array.new_float()
array.push(recent, close)
if array.size(recent) > 5
    array.shift(recent)
ordered = array.copy(recent)
array.sort(ordered)
plot(array.get(ordered, 0), "Lowest of 5")
plot(array.get(recent, 0), "Oldest of 5")

See also

Runs on Sidechair