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

48 lines
1.4 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Theil Index (THEIL)", "THEIL", overlay=false, precision=6)
//@function Calculates Theil's T Index for a series over a lookback period.
//@param src series float Input data series (must be positive values).
//@param len simple int Lookback period (must be > 0).
//@returns series float The Theil's T Index, or na if data is not suitable.
theil_t_index(series float src, simple int len) =>
if len <= 0
float(na)
float sum_src = 0.0
int n_valid = 0
float[] values_arr = array.new_float()
for i = 0 to len - 1
val = src[i]
if not na(val) and val > 0
array.push(values_arr, val)
sum_src += val
n_valid += 1
if n_valid == 0
float(na)
float mean_val = sum_src / n_valid
if mean_val <= 0
float(na)
float theil_sum = 0.0
for i = 0 to n_valid - 1
xi = array.get(values_arr, i)
ratio = xi / mean_val
if ratio > 0
theil_sum += ratio * math.log(ratio)
float theil_t = na
if n_valid > 0
theil_t := theil_sum / n_valid
theil_t
// Inputs
i_source = input.source(close, title="Source (must be positive values)")
i_length = input.int(14, title="Lookback Period", minval=1)
// Calculation
theil_value = theil_t_index(i_source, i_length)
// Plot
plot(theil_value, "Theil T Index", color=color.yellow, linewidth=2)