Files
QuanTAlib/lib/statistics/covariance/covariance.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

56 lines
1.9 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Covariance (COVARIANCE)", "COVARIANCE", overlay=false)
//@function Calculates covariance using single pass with circular buffer
//@param src1 series float First series to analyze
//@param src2 series float Second series to analyze
//@param len simple int Lookback period for calculation
//@returns float Covariance between src1 and src2
//@optimized for performance using circular buffer
covariance(series float src1, series float src2, simple int len) =>
if len <= 0
runtime.error("Period must be greater than 0")
var int p = math.max(1, len)
var array<float> buffer1 = array.new_float(p, na)
var array<float> buffer2 = array.new_float(p, na)
var int head = 0, var int count = 0
var float sum1 = 0.0, var float sum2 = 0.0
var float sumProd = 0.0
float oldest1 = array.get(buffer1, head)
float oldest2 = array.get(buffer2, head)
if not na(oldest1) and not na(oldest2)
sum1 -= oldest1
sum2 -= oldest2
sumProd -= oldest1 * oldest2
count -= 1
if not na(src1) and not na(src2)
sum1 += src1
sum2 += src2
sumProd += src1 * src2
count += 1
array.set(buffer1, head, src1)
array.set(buffer2, head, src2)
else
array.set(buffer1, head, na)
array.set(buffer2, head, na)
head := (head + 1) % p
count > 1 ? (sumProd / count) - (sum1 / count) * (sum2 / count) : na
// ---------- Main loop ----------
// Inputs
i_source1 = input.source(close, "Source 1")
i_source2_ticker = input.symbol("SPY", "Source 2 Ticker (e.g., SPY, AAPL)")
i_period = input.int(20, "Period", minval=2)
i_source2 = request.security(i_source2_ticker, timeframe.period, close, lookahead=barmerge.lookahead_off)
// Calculation
variance_value = covariance(i_source1, i_source2, i_period)
// Plot
plot(variance_value, "Covariance", color=color.yellow, linewidth=2)