Sidechair

array.size()

Browse the reference

Returns how many items an array holds right now.

Try it in the editor

The count is 0 for an empty array and goes up by one for every push, unshift or insert. The last valid index is always size minus one.

Use it to guard reads, to keep a rolling window at a fixed length, and as the upper bound of a for loop over the array.

Syntax

Pine Script
array.size(id: any array type) -> series int
array.size() -> series int

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array to count. Returns 0 for an array that exists but holds nothing.

Returns

series int: The number of items, as an integer.

Remarks

  • Calling it on an array that is na stops the script with an error that says the array is na.
  • The method form, values.size(), calls the same function.

Example

Pine ScriptCap a window at 10 itemsOpen in editor
//@version=6
indicator("array.size: capped window", overlay = false)
var vol = array.new<float>()
array.push(vol, volume)
if array.size(vol) > 10
    array.shift(vol)
plot(array.size(vol), "Items held")
plot(array.avg(vol), "Average volume")

See also

Runs on Sidechair