Files
QuanTAlib/lib/statistics/variance/variance.pine
T
Miha Kralj 24e86d762a Add documentation links for various volatility indicators and channels
- 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.
2026-02-18 11:55:48 -08:00

41 lines
1.2 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Variance, Dispersion or Spread (VARIANCE)", "VARIANCE", overlay=false)
//@function variance
//@param src {series float} Source series.
//@param len {int} Lookback length. `len` > 0.
//@returns {series float} Variance of `src` for `len` bars back. Returns 0 if not enough data.
variance(series float src, int len) =>
if len <= 0
runtime.error("Period must be greater than 0")
var int p = math.max(1, len)
var array<float> buffer = array.new_float(p, na)
var int head = 0, var int count = 0
var float sum = 0.0, var float sumSq = 0.0
float oldest = array.get(buffer, head)
if not na(oldest)
sum -= oldest
sumSq -= oldest * oldest
count -= 1
float val = nz(src)
sum += val
sumSq += val * val
count += 1
array.set(buffer, head, val)
head := (head + 1) % p
count > 1 ? math.max(0.0, (sumSq / count) - math.pow(sum / count, 2)) : 0.0
// ---------- Main loop ----------
// Inputs
i_period = input.int(14, "Period", minval=1)
i_source = input.source(close, "Source")
// Calculation
variance_value = variance(i_source, i_period)
// Plot
plot(variance_value, "Var", color=color.yellow, linewidth=2)