Cumulative Sum (CUMSUM)
Summary
Cumulative Sum: the running total of a series from the anchor bar forward. A math primitive rather than an indicator — it is the one operation between the shipped corpus and three named classical breadth indicators: the A/D Line is CUMSUM(SUB(advances, declines)), the A/D Volume Line is CUMSUM(SUB(advancingVolume, decliningVolume)), and the McClellan Summation Index is CUMSUM of the McClellan Oscillator.
SUM is a rolling window over optInTimePeriod bars; CUMSUM has no window — every bar since the anchor contributes.
Chart
Chart metadata i
Outputs
outReal:line·zero·positive·negative
Formula
out[j] = inReal[startIdx] + inReal[startIdx+1] + … + inReal[startIdx+j]
Left-to-right in one double, no compensation — the same plain += convention the shipped accumulators (AD, OBV) use.
The accumulator re-seeds at the anchor. CUMSUM(3, 7, x) starts its total at x[3]; it does not warm up from x[0]. This is the published contract of the indicators built on it (StockCharts: only the A/D Line's shape carries meaning, the first value is "simply Net Advances for one period") and the convention of every shipped path-dependent function. The path_dependent flag declares exactly this class.
Inputs
inReal— Source series (canonically a per-bar net figure, e.g. advances − declines)
Outputs
outReal— Running total since the anchor bar
Notes
- Lookback 0:
outBegIdx = startIdx, one output per input bar. Streaming state is a single accumulator, so a peek commits nothing by construction. - The sum is uncompensated. A Kahan or Neumaier variant would diverge from
AD's own convention, which this follows.
Properties
Numerical Stability: Path-Dependent
| ☐ Overlap Input |
| ✅ Independent Y-Axis i |
| ☐ Candlestick |
| ☐ Display Shift |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double series[252]; /* your data, oldest first */
double out[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_CUMSUM( 0, n - 1, series, &outBegIdx, &outNBElement, out );
printf( "%d values from bar %d: [%.4f, ..., %.4f]\n", outNBElement, outBegIdx, out[0], out[outNBElement - 1] );use ta_lib::Core;
let series = vec![0.0; 252]; // your data, oldest first
let mut out = vec![0.0; 252];
let core = Core::new();
let range = core.cumsum(0, series.len() - 1, &series, &mut out)?;
println!("{} values from bar {}: [{:.4}, ..., {:.4}]", range.count, range.beg_idx, out[0], out[range.count - 1]);import io.github.talib.Core;
import io.github.talib.OutRange;
import java.util.Locale;
double[] series = new double[252]; // your data, oldest first
double[] out = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.cumsum(0, series.length - 1, series, out);
System.out.printf(Locale.ROOT, "%d values from bar %d: [%.4f, ..., %.4f]%n", r.count(), r.begIdx(), out[0], out[r.count() - 1]);using System;
using TALib;
var series = new double[252]; // your data, oldest first
var outReal = new double[252];
var core = Core.Default;
OutRange r = core.Cumsum(0, series.Length - 1, series, outReal);
Console.WriteLine(FormattableString.Invariant($"{r.Count} values from bar {r.BegIdx}: [{outReal[0]:F4}, ..., {outReal[r.Count - 1]:F4}]"));import { cumsum } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const { begIdx, values } = cumsum(series);
console.log(values.length + ' values from bar ' + begIdx + ': [' + values[0].toFixed(4) + ', ..., ' + values[values.length - 1].toFixed(4) + ']');import (
"fmt"
"log"
talib "ta-lib.github.io/go"
)
series := make([]float64, 252) // your data, oldest first
out := make([]float64, 252)
core := talib.NewCore()
r, err := core.CUMSUM(0, len(series)-1, series, out)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d values from bar %d: [%.4f, ..., %.4f]\n", r.NBElement, r.BegIdx, out[0], out[r.NBElement-1])Implementation
Added in 0.8.1 (ta-lib-python 0.8.1).
TA-Lib Definition: cumsum.c · cumsum.yaml
| Native | File |
|---|---|
| C | ta_CUMSUM.c |
| Rust | cumsum.rs |
| Java | Core_CUMSUM.java |
| C# | Core_CUMSUM.cs |
| TypeScript | cumsum.ts |
| Go | cumsum.go |
Python: talib.CUMSUM (ta-lib-python 0.8.1 or later), see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.