Sidechair

array.sum()

Browse the reference

Adds up all numbers in an array and returns the total.

Try it in the editor

na items are skipped instead of turning the whole sum into na. An empty array, or one that holds only na, sums to 0.

Divide by array.size() for a simple mean, or call array.avg(), which already ignores na items.

Syntax

Pine Script
array.sum(id: array<int/float>) -> series float
array.sum(id: array<int>) -> series int
array.sum() -> series float
array.sum() -> series int

Arguments

NameTypeDefaultDescription
idarray<int/float> or array<int>requiredThe numeric array to add up. na items are skipped.

Returns

series float or series int: The sum of the numbers in the array.

Example

Pine ScriptVolume over the last 12 barsOpen in editor
//@version=6
indicator("array.sum: rolling volume", overlay = false)
var vol = array.new_float()
array.push(vol, volume)
if array.size(vol) > 12
    array.shift(vol)
plot(array.sum(vol), "Volume, last 12 bars", style = plot.style_columns)

See also

Runs on Sidechair