mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-01 19:27:44 +00:00
24e86d762a
- Updated BBWN, BBWP, CCV, CV, CVI, EWMA, GKV, HLV, HV, Jvolty, JVOLTYN, MASSI, NATR, RSV, RV, RVI, TR, UI, VOV, VR, YZV indicators with documentation links. - Added documentation links for Aberration, Acceleration Bands, Andrews' Pitchfork, Adaptive Price Zone, ATR Bands, Bollinger Bands, Center of Gravity, Donchian Channels, Decay Min-Max Channel, Detrended Synthetic Price, EACP, EBSW, HOMOD, Jurik Volatility Bands, Keltner Channel, MA Envelope, Min-Max Channel, Price Channel, Regression Channels, Standard Deviation Channel, Stoller Average Range Channel, Super Trend Bands, Ultimate Bands, Ultimate Channel, VWAP Bands, and VWAP with Standard Deviation Bands.
58 lines
1.5 KiB
Plaintext
58 lines
1.5 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Rolling Sum", "SUM", overlay=false)
|
|
|
|
//@function Calculates Rolling Sum over a period using Kahan-Babuška algorithm
|
|
//@param source Series to calculate sum from
|
|
//@param length Number of bars to sum
|
|
//@returns Rolling sum of the last 'length' values
|
|
rolling_sum(series float source, simple int length) =>
|
|
// Persistent state
|
|
var float sum = 0.0
|
|
var float c = 0.0 // First-order compensation
|
|
var float cc = 0.0 // Second-order compensation
|
|
|
|
float val = nz(source, 0.0)
|
|
float oldVal = bar_index >= length ? nz(source[length], 0.0) : 0.0
|
|
|
|
// Kahan-Babuška subtract old value
|
|
if bar_index >= length
|
|
float yS = -oldVal - c
|
|
float tS = sum + yS
|
|
c := tS - sum - yS
|
|
sum := tS
|
|
|
|
float zS = c - cc
|
|
float ttS = sum + zS
|
|
cc := ttS - sum - zS
|
|
sum := ttS
|
|
|
|
// Kahan-Babuška add new value
|
|
float yA = val - c
|
|
float tA = sum + yA
|
|
c := tA - sum - yA
|
|
sum := tA
|
|
|
|
float zA = c - cc
|
|
float ttA = sum + zA
|
|
cc := ttA - sum - zA
|
|
sum := ttA
|
|
|
|
sum
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_length = input.int(14, "Length", minval=1)
|
|
i_source = input.source(close, "Source")
|
|
|
|
// Calculation
|
|
sum_value = rolling_sum(i_source, i_length)
|
|
|
|
// Equivalent built-in for comparison (disabled by default)
|
|
// sum_builtin = math.sum(i_source, i_length)
|
|
|
|
// Plot
|
|
plot(sum_value, "Sum", color=color.yellow, linewidth=2)
|