pub struct GarmanKlassVolatility { /* private fields */ }Expand description
Garman-Klass Volatility — an OHLC realised-volatility estimator.
Garman & Klass (1980) extended Parkinson’s high-low estimator by adding an open-to-close term, removing some of the bias introduced when the closing price drifts within the bar. The per-bar sample is
s_t = 0.5 · (ln(H_t / L_t))² − (2·ln 2 − 1) · (ln(C_t / O_t))²and the indicator returns the annualised square root of the rolling
mean of s_t:
out = sqrt(max(mean(s_t over `period`), 0)) · sqrt(trading_periods) · 100Garman & Klass showed the estimator is ~7.4× more statistically efficient than the close-to-close estimator under driftless Geometric Brownian Motion (Parkinson sits at ~5.0×). It is still biased when there is significant overnight drift between bars — use the Yang-Zhang estimator when the dataset has meaningful close-to-open gaps.
The per-bar sample s_t can be slightly negative when the bar’s range
is small relative to its open-to-close move; this matches the original
paper’s algebra and is handled by clamping the rolling mean to zero
before taking the square root.
§Example
use wickra_core::{Candle, GarmanKlassVolatility, Indicator};
let mut indicator = GarmanKlassVolatility::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 GarmanKlassVolatility
impl GarmanKlassVolatility
Sourcepub fn new(period: usize, trading_periods: usize) -> Result<Self>
pub fn new(period: usize, trading_periods: usize) -> Result<Self>
Construct a Garman-Klass 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.
Trait Implementations§
Source§impl Clone for GarmanKlassVolatility
impl Clone for GarmanKlassVolatility
Source§fn clone(&self) -> GarmanKlassVolatility
fn clone(&self) -> GarmanKlassVolatility
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 GarmanKlassVolatility
impl Debug for GarmanKlassVolatility
Source§impl Indicator for GarmanKlassVolatility
impl Indicator for GarmanKlassVolatility
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 GarmanKlassVolatility
impl RefUnwindSafe for GarmanKlassVolatility
impl Send for GarmanKlassVolatility
impl Sync for GarmanKlassVolatility
impl Unpin for GarmanKlassVolatility
impl UnsafeUnpin for GarmanKlassVolatility
impl UnwindSafe for GarmanKlassVolatility
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