ta.cum in MQL5: a running total seeded on the first bar
Short answer
Keep a buffer where each cell is the previous cell plus this bar's value, Cum[i] = (i == 0 ? 0 : Cum[i-1]) + src[i]. The result depends on how much history the chart has.
ta.cum(source) is the cumulative sum of source over every bar from the start of the chart to the current one. In MQL5 it is a buffer where each cell is the previous cell plus this bar's value, with the first bar seeded to its own value. The only subtleties are the seed, how na is treated, and the fact that the result depends on how much history the chart has.
What ta.cum computes
On bar 0 the result is source; on every later bar it is the previous result plus the current source. It is the building block of OBV (ta.cum(math.sign(ta.change(close)) * volume)), of a session-less VWAP numerator and denominator, and of any "total since the beginning" statistic. Because the sum starts at the first bar the platform loaded, two charts with different history depth show different absolute values for the same symbol; the differences between bars match, the levels do not. In Pine a na value of source does not reset or poison the total; it contributes nothing, and the total continues.
The realtime bar is recomputed each tick from the previous bar's total plus the current bar's value, so the sum does not drift; it is not "add every tick".
The MQL5 equivalent
Pine: cumulative volume.
//@version=6
indicator("Cumulative volume")
total = ta.cum(volume)
plot(total, "Total")MQL5. The buffer is the state. Bar 0 is seeded with its own value and every later bar adds to the cell before it; the forming bar is recomputed from Cum[i - 1] on each tick.
//+------------------------------------------------------------------+
//| Cumulative volume: Pine's ta.cum(volume) in MQL5 |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "cum"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
double Cum[];
int OnInit()
{
SetIndexBuffer(0, Cum, INDICATOR_DATA);
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated,
const datetime &time[], const double &open[],
const double &high[], const double &low[],
const double &close[], const long &tick_volume[],
const long &volume[], const int &spread[])
{
int start = (prev_calculated > 1) ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
// ta.cum(x): running total from the first bar on the chart. On the
// first bar the total IS the first value; after that Cum[i-1] + x[i].
// On a tick the forming bar is recomputed from Cum[i-1], so nothing
// is added twice. A missing value would contribute 0 in Pine (nz).
double x = (double)tick_volume[i];
Cum[i] = (i == 0) ? x : Cum[i - 1] + x;
}
return rates_total;
}tick_volume is used because it is what most MetaTrader brokers supply; volume[] (real volume) is zero for them. TradingView's volume for the same CFD or forex symbol is also broker-dependent, so expect the two totals to differ in magnitude even when the logic is identical.
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Seed | Bar 0 equals source | Cum[0] = x; later cells Cum[i-1] + x |
| na in source | Contributes nothing; total continues | Replace a missing or EMPTY_VALUE input with 0 before adding, or the total becomes garbage |
| History depth | Depends on the plan's bar limit and the symbol | Depends on the chart's "Max bars in chart" setting; levels differ, deltas match |
| Realtime bar | Recomputed from the previous total each tick | Same if you read Cum[i - 1]; wrong if you accumulate into a scalar on every call |
| Overflow | Float | double; the long volume arrays need a cast before adding to a double |
What the Stratlate converter emits
ta.cum is a stateful call in the emitter. It emits pine_cum(src, __stN, __pineBar) on a double array. A computed source such as ta.cum(close - open), and volume, which arrives as a long array, are first written into a per-script double array each bar; where no array can be built the emitter uses pine_cum_step(value, __stN, __pineBar) instead. Both runtime helpers replace a na input with 0 via pine_nz, write state[i] = (i == 0) ? x : state[i-1] + x, and return state[i], so the forming bar is recomputed from the previous bar rather than accumulated per tick. The example above converts CLEAN as pine_cum(__ts0, __st0, __pineBar) after the volume array is materialised; the mapping is exact.
Pitfalls
- Accumulating into a global
double totalinsideOnCalculate. Symptom: the live value runs away by the bar's value on every tick and never matches after a reload. - Seeding bar 0 by reading
Cum[-1]. Symptom: array out of range on attach. - Adding
EMPTY_VALUEfrom an upstream buffer that has not warmed up. Symptom: the total jumps to 1.8e308 on the first bars and stays there. - Comparing absolute levels with TradingView. Symptom: the two curves have the same shape but different offsets; the chart histories start on different bars.
- Using
volume[]where the broker provides only tick volume. Symptom: a flat zero line.
FAQ
Why does my cumulative total differ from TradingView even though the formula is right?
Because the sum starts on the first bar each platform loaded. Different history depth means a different starting point; the bar-to-bar changes should still match.
How does ta.cum handle na?
A na value adds nothing and the total continues. The Stratlate runtime reproduces this by replacing na with 0 before adding; in hand-written MQL5 test for EMPTY_VALUE and add 0 instead.
Can I accumulate on every tick to save work?
No. The forming bar is recomputed on each tick, so the total must be rebuilt from the previous bar's value plus the current bar's value; otherwise the same bar is added many times.
Convert the whole script
Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter