cumsum
cumsum in ProBuilder returns the cumulative sum of a value from the first loaded bar to the current bar, useful for running totals and custom averages.
Syntax
cumsum(price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| price | numeric expression | required | The value accumulated on each bar, for example close, volume, or any calculation. |
Formula
For bar t, with x the accumulated expression and bar 0 the first loaded bar:
cumsum(x) = x(0) + x(1) + ... + x(t)How it works
cumsum adds the value of its argument on every bar, starting from the oldest bar available in the loaded history, and returns the total up to and including the current bar. The result is a series that can only grow when the argument is positive, shrink when it is negative, and step by the argument's value on each new bar.
The accumulation window is the entire loaded history, not a fixed lookback. This has a practical consequence: the value depends on how many bars the chart or backtest has loaded. The same script on the same instrument returns different cumsum values with 5,000 bars of history than with 500. For a rolling total over the last N bars, Summation[N] is the appropriate tool.
The argument does not have to be a raw price. Any expression works, including comparisons, which evaluate to 1 or 0. That makes cumsum a compact way to count how many times a condition has been true since the start of the data.
Examples
Example 1, Running total of closing prices (Indicator)
// Accumulate every close from the first loaded bar onward
cumulativeClose = cumsum(close)
RETURN cumulativeClose AS "cumulative close"Each bar adds its closing price to the total, producing a monotonically rising curve for any instrument with positive prices.
Example 2, Volume-weighted average over the full history (Indicator)
// Divide cumulative price-volume by cumulative volume
vwap = cumsum(TypicalPrice * Volume) / cumsum(Volume)
RETURN vwap AS "history-wide VWAP"Two cumulative sums combine into a volume-weighted average of the typical price across all loaded bars, a whole-history variant of the VWAP calculation.
Example 3, Trading around the long-run mean (ProBacktest)
// Average of every loaded close: cumulative sum divided by bar count
longRunMean = cumsum(close) / (BarIndex + 1)
IF close CROSSES OVER longRunMean AND NOT ONMARKET THEN
BUY 1 CONTRACT AT MARKET
ELSIF close CROSSES UNDER longRunMean AND LONGONMARKET THEN
SELL AT MARKET
ENDIFcumsum(close) divided by the number of elapsed bars gives the mean of all loaded closes. The strategy goes long when price crosses above that mean and exits on the cross back below.
Common errors and gotchas
- History-dependent values. The total starts at the first loaded bar, so changing the amount of preloaded data changes every cumsum value. Results are not comparable across different history depths.
- Expecting a windowed sum. cumsum has no period parameter.
cumsum(close) - cumsum(close)[10]orSummation[10](close)is required for a 10-bar rolling total. - Unbounded growth. Accumulating a strictly positive series produces a value that grows without limit. Scaling it, for example by bar count or by another cumulative sum, is usually necessary before plotting or comparing.
- Resets on data breaks. The running total restarts if the data series is reloaded or interrupted, so persisted values should not be assumed to survive a chart reset.
Related instructions
Summation, sum of a series over a fixed number of bars.Average, arithmetic mean over a fixed period.BarIndex, number of bars elapsed since the start of loaded data.Volume, per-bar traded volume, a common cumsum argument.OBV, built-in indicator based on cumulative signed volume.ONCE, initializes a variable a single time, useful alongside manual accumulators.RETURN, outputs the computed series from an indicator.
