cumsum
Running total from start of data
cumsum(source)
Calculates a running total that accumulates from the beginning of the data series. Each bar's value equals the sum of all values from the first bar up to and including the current bar. This is useful for tracking lifetime totals, counting events over time, and building cumulative metrics like On-Balance Volume.
Parameters
- source (field): Data series to accumulate (can be any numeric expression)
Formula
cumsum[n] = source[0] + source[1] + source[2] + ... + source[n]
where source[0] is the oldest bar in the series
Examples
Cumulative Volume
Track total volume traded over the stock's history:
// lifetime volume exceeds 1 billion shares
cumsum(volume) > 1B;
Cumulative Gains vs Losses
Compare total accumulated gains to losses over all time:
// more lifetime gains than losses (long-term uptrend)
cumsum(gain(close)) > cumsum(loss(close));
Counting Events
Count how many times a condition has occurred using cond():
// stocks with at least 100 bullish days in history
cumsum(cond(close > open, 1, 0)) >= 100;
Net Price Change from Beginning
Calculate total price movement since the start of data:
// net change from first recorded price
cumsum(change(close, 1)) > 0;
Returns
Float (running total from start of available data)
Notes
This function scans the entire price history for each symbol, making it computationally expensive. Use additional filters (price, volume, exchange) to limit the symbol universe, or the scan may time out.
- Unlike sum(source, n) which uses a rolling window of N bars, cumsum() has no period parameter and always accumulates from the start of data
- The result grows continuously; for monotonic series like volume, cumsum is always increasing
- Combine with cond() to count occurrences of any boolean condition