Returns how many items an array holds right now.
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
array.size(id: any array type) -> series int
array.size() -> series intArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | any array type | required | The 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
//@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