Sidechair

math.sum()

Browse the reference

Returns the sum of the last length values of source, na until the window is full.

Try it in the editor

math.sum adds up the latest length values of source, including the current one. It is ta.sma(source, length) multiplied by length.

The window needs length bars and no na values before it gives a number. For a total over the whole chart use ta.cum.

Syntax

Pine Script
math.sum(source: series int/float, length: series int) -> series float

Arguments

NameTypeDefaultDescription
sourceseries int/floatrequiredThe series whose latest length values are added together. na makes the result na.
lengthseries intrequiredNumber of bars in the sum. A whole number of 1 or more.

Returns

series float: the rolling sum of the window.

Remarks

  • Use it for counts: math.sum(close > open ? 1 : 0, 10) counts up bars in the last ten.

Example

Pine ScriptCount of up barsOpen in editor
//@version=6
indicator("Up bars in 10")
ups = math.sum(close > open ? 1 : 0, 10)
plot(ups, "Up bars", color = color.green, style = plot.style_columns)
hline(5)

See also

Runs on Sidechair