pub struct YangZhangVolatility { /* private fields */ }Expand description
Yang-Zhang Volatility — combines overnight, open-to-close and Rogers-Satchell volatilities into a single drift- and gap-robust estimator.
Yang & Zhang (2000) showed that the three estimators below are
independent under a driftless GBM with overnight gaps, so a convex
combination of their (sample) variances has minimum estimation variance
at a specific blending factor k:
k = 0.34 / (1.34 + (n + 1) / (n − 1))
σ²_on = sample_var(ln(O_t / C_{t-1}) over n bars) // overnight
σ²_oc = sample_var(ln(C_t / O_t) over n bars) // open-to-close
σ²_rs = mean(ln(H/C)·ln(H/O) + ln(L/C)·ln(L/O) over n bars) // Rogers-Satchell
σ²_YZ = σ²_on + k · σ²_oc + (1 − k) · σ²_rs
out = √max(σ²_YZ, 0) · √trading_periods · 100The “sample” variance uses Bessel’s correction (divisor n − 1), the
same convention as HistoricalVolatility.
This is the gold-standard OHLC estimator for assets with both
overnight gaps and intraday drift — equities, futures, and any
market that doesn’t trade continuously. For pure intraday data
(where C_{t-1} == O_t), the overnight term vanishes and
Rogers-Satchell alone is sufficient.
§Example
use wickra_core::{Candle, Indicator, YangZhangVolatility};
let mut indicator = YangZhangVolatility::new(20, 252).unwrap();
let mut last = None;
for i in 0..40 {
let base = 100.0 + f64::from(i);
let candle = Candle::new(base, base + 2.0, base - 2.0, base + 0.5, 1.0, i64::from(i))
.unwrap();
last = indicator.update(candle);
}
assert!(last.is_some());Implementations§
Source§impl YangZhangVolatility
impl YangZhangVolatility
Sourcepub fn new(period: usize, trading_periods: usize) -> Result<Self>
pub fn new(period: usize, trading_periods: usize) -> Result<Self>
Construct a Yang-Zhang Volatility estimator.
period is the rolling window of bars; trading_periods is the
annualisation factor (252 daily, 52 weekly, 12 monthly, or
1 for raw per-bar volatility).
§Errors
Returns Error::PeriodZero if either parameter is 0, or
Error::InvalidPeriod if period < 2 (the sample variances
inside Yang-Zhang need at least two samples).
Trait Implementations§
Source§impl Clone for YangZhangVolatility
impl Clone for YangZhangVolatility
Source§fn clone(&self) -> YangZhangVolatility
fn clone(&self) -> YangZhangVolatility
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for YangZhangVolatility
impl Debug for YangZhangVolatility
Source§impl Indicator for YangZhangVolatility
impl Indicator for YangZhangVolatility
Source§type Input = Candle
type Input = Candle
f64 for a price, or Candle / Tick).Source§fn update(&mut self, candle: Candle) -> Option<f64>
fn update(&mut self, candle: Candle) -> Option<f64>
None if the indicator is still warming up.Source§fn reset(&mut self)
fn reset(&mut self)
Source§fn warmup_period(&self) -> usize
fn warmup_period(&self) -> usize
None output can be produced.Auto Trait Implementations§
impl Freeze for YangZhangVolatility
impl RefUnwindSafe for YangZhangVolatility
impl Send for YangZhangVolatility
impl Sync for YangZhangVolatility
impl Unpin for YangZhangVolatility
impl UnsafeUnpin for YangZhangVolatility
impl UnwindSafe for YangZhangVolatility
Blanket Implementations§
Source§impl<T> BatchExt for Twhere
T: Indicator,
impl<T> BatchExt for Twhere
T: Indicator,
Source§fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
None during warmup) per input.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more