Returns the sum of the last length values of source, na until the window is full.
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
math.sum(source: series int/float, length: series int) -> series floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| source | series int/ | required | The series whose latest length values are added together. na makes the result na. |
| length | series int | required | Number 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
//@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