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

62 lines
2.4 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Kendall Rank Correlation (KENDALL)", "KENDALL", overlay=false, precision=4)
//@function Calculates Kendall's Tau-a rank correlation coefficient.
//@param source1 series float The first input series.
//@param source2 series float The second input series.
//@param length int The lookback period. Min 2, Max 60.
//@returns series float Kendall's Tau-a coefficient, ranging from -1 to +1.
kendall(series float source1, series float source2, simple int length) =>
if length < 2
float(na)
else
float[] src1_window = array.new_float(length)
float[] src2_window = array.new_float(length)
bool window_has_na = false
for k = 0 to length - 1
val1_k = source1[length - 1 - k]
val2_k = source2[length - 1 - k]
if na(val1_k) or na(val2_k)
window_has_na := true
break
array.set(src1_window, k, val1_k)
array.set(src2_window, k, val2_k)
if window_has_na
float(na)
else
concordant_pairs = 0
discordant_pairs = 0
for i = 0 to length - 2
for j = i + 1 to length - 1
val1_i = array.get(src1_window, i)
val2_i = array.get(src2_window, i)
val1_j = array.get(src1_window, j)
val2_j = array.get(src2_window, j)
diff_val1 = val1_i - val1_j
diff_val2 = val2_i - val2_j
product_of_signs = diff_val1 * diff_val2
if product_of_signs > 0
concordant_pairs += 1
else if product_of_signs < 0
discordant_pairs += 1
denominator = length * (length - 1) / 2.0
if denominator == 0.0
float(na)
else
(concordant_pairs - discordant_pairs) / denominator
// 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
kendall_value = kendall(i_source1, i_source2, i_period)
// Plot
plot(kendall_value, "Kendall's Tau", color=color.yellow, linewidth=2)