mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-05 12:37:43 +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.7 KiB
Plaintext
44 lines
1.7 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Yang-Zhang Volatility (YZV)", shorttitle="YZV", overlay=false)
|
|
|
|
//@function Calculates Yang-Zhang Volatility (YZV)
|
|
//@param length Lookback period for smoothing daily variance estimates (> 0)
|
|
//@returns Yang-Zhang Volatility value for the current bar
|
|
//@optimized Uses bias-corrected RMA with OHLC prices for O(1) complexity per bar
|
|
yzv(int length) =>
|
|
if length <= 0
|
|
runtime.error("Length must be greater than 0 for YZV calculation.")
|
|
float(na)
|
|
o=open,h=high,l=low,c=close,pc=na(close[1])?open:close[1]
|
|
ro=math.log(o/pc),rc=math.log(c/o),rh=math.log(h/o),rl=math.log(l/o)
|
|
s_o_sq=ro*ro,s_c_sq=rc*rc
|
|
s_rs_sq=rh*(rh-rc)+rl*(rl-rc)
|
|
ratio_N=length<=1?1.0:(float(length)+1.0)/(float(length)-1.0)
|
|
k_yz=0.34/(1.34+ratio_N)
|
|
s_sq_daily=s_o_sq+k_yz*s_c_sq+(1.0-k_yz)*s_rs_sq
|
|
var float EPSILON_YZV = 1e-10 // Consistent with VR's EPSILON_ATR
|
|
var float raw_rma_val = 0.0
|
|
var float e_comp_val = 1.0
|
|
float smoothed_s_sq = na
|
|
if not na(s_sq_daily)
|
|
rma_alpha = 1.0 / float(length)
|
|
if na(raw_rma_val[1]) and e_comp_val == 1.0 // First valid calculation for RMA
|
|
raw_rma_val := s_sq_daily
|
|
else
|
|
raw_rma_val := (nz(raw_rma_val[1]) * (length - 1) + s_sq_daily) / length
|
|
e_comp_val := (1.0 - rma_alpha) * e_comp_val
|
|
smoothed_s_sq := e_comp_val > EPSILON_YZV ? raw_rma_val / (1.0 - e_comp_val) : raw_rma_val
|
|
result = math.sqrt(smoothed_s_sq)
|
|
result
|
|
|
|
// Inputs
|
|
i_length = input.int(20, title="Length", minval=1, tooltip="The lookback period for smoothing Yang-Zhang daily variance estimates.")
|
|
|
|
// Calculation
|
|
yzvValue = yzv(i_length)
|
|
|
|
// Plot
|
|
plot(yzvValue, title="YZV", color=color.yellow, linewidth=2)
|