mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-03 19:57: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.
44 lines
1.5 KiB
Plaintext
44 lines
1.5 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Theil's U Statistic", "TheilU", overlay=false)
|
|
|
|
//@function Calculates Theil's U Statistic (U1)
|
|
//@param actual Series of actual values
|
|
//@param predicted Series of predicted/forecast values
|
|
//@param length Rolling window for calculation
|
|
//@returns Theil's U value (0 to 1+ range)
|
|
theil_u(series float actual, series float predicted, simple int length) =>
|
|
float epsilon = 1e-10
|
|
|
|
// Compute squared values for current bar
|
|
float error = nz(predicted, 0.0) - nz(actual, 0.0)
|
|
float sqError = error * error
|
|
float sqActual = nz(actual, 0.0) * nz(actual, 0.0)
|
|
float sqPred = nz(predicted, 0.0) * nz(predicted, 0.0)
|
|
|
|
// Rolling sums
|
|
float sumSqError = ta.sum(sqError, length)
|
|
float sumSqActual = ta.sum(sqActual, length)
|
|
float sumSqPred = ta.sum(sqPred, length)
|
|
|
|
// TheilU = √(Σ(pred-act)²) / √(Σact² + Σpred²)
|
|
float denom = math.sqrt(sumSqActual + sumSqPred)
|
|
float result = denom > epsilon ? math.sqrt(sumSqError) / denom : 0.0
|
|
result
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_length = input.int(14, "Length", minval=1)
|
|
i_actual = input.source(close, "Actual")
|
|
i_predicted = input.source(open, "Predicted")
|
|
|
|
// Calculation
|
|
theilu_value = theil_u(i_actual, i_predicted, i_length)
|
|
|
|
// Plot
|
|
plot(theilu_value, "Theil's U", color=color.yellow, linewidth=2)
|
|
hline(0, "Perfect", color=color.green, linestyle=hline.style_dotted)
|
|
hline(1, "Naive", color=color.red, linestyle=hline.style_dotted)
|