sum

Total over rolling N-period window

sum(source, periods)

Parameters

Name Type Description
source field Data series to sum (close, volume, or any expression)
periods int Number of bars to include in the sum

Formula

```
sum(x, n) = x[0] + x[1] + x[2] + ... + x[n-1]

where x[0] is the current bar and x[n-1] is n-1 bars ago
```

Examples

// total volume over last 10 days exceeds 50 million
sum(volume, 10) > 50M;
// net change over 5 bars (sum of daily changes)
sum(change(close, 1), 5) > 0;
// ratio of gains to losses over 14 periods
sum(gain(close), 14) / sum(loss(close), 14) > 1;
// these are equivalent: sum = sma * period
sum(close, 20) ~= sma(close, 20) * 20;

Returns

Float (total of all values in the rolling window) ## Notes - The window always contains exactly N values (current bar + N-1 previous) - For cumulative totals from the start of data, use cumsum() instead