1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Historical Volatility.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::traits::Indicator;
/// Historical Volatility — the annualised standard deviation of log returns.
///
/// This is the realised (backward-looking) volatility used to price options
/// and size risk:
///
/// ```text
/// r_t = ln(price_t / price_{t−1})
/// HV = stddev_sample(r over period) · √trading_periods · 100
/// ```
///
/// The log returns over the window are measured with the **sample** standard
/// deviation (divisor `n − 1`, the unbiased estimator), then scaled to an
/// annual figure by `√trading_periods` — `252` for daily bars, `52` for
/// weekly, `12` for monthly — and expressed as a percentage.
///
/// # Example
///
/// ```
/// use wickra_core::{Indicator, HistoricalVolatility};
///
/// // 20-bar window, 252 trading days per year.
/// let mut indicator = HistoricalVolatility::new(20, 252).unwrap();
/// let mut last = None;
/// for i in 0..80 {
/// last = indicator.update(100.0 + (f64::from(i) * 0.3).sin() * 5.0);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct HistoricalVolatility {
period: usize,
trading_periods: usize,
prev_price: Option<f64>,
/// Rolling window of the last `period` log returns.
window: VecDeque<f64>,
sum: f64,
sum_sq: f64,
last: Option<f64>,
}
impl HistoricalVolatility {
/// Construct a new Historical Volatility indicator.
///
/// `period` is the number of log returns in the rolling window;
/// `trading_periods` is the annualisation factor (`252` daily, `52`
/// weekly, `12` monthly).
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `period` or `trading_periods` is `0`,
/// or [`Error::InvalidPeriod`] if `period == 1` (the sample standard
/// deviation needs at least two returns).
pub fn new(period: usize, trading_periods: usize) -> Result<Self> {
if period == 0 || trading_periods == 0 {
return Err(Error::PeriodZero);
}
if period < 2 {
return Err(Error::InvalidPeriod {
message: "historical volatility period must be >= 2",
});
}
Ok(Self {
period,
trading_periods,
prev_price: None,
window: VecDeque::with_capacity(period),
sum: 0.0,
sum_sq: 0.0,
last: None,
})
}
/// Configured `(period, trading_periods)`.
pub const fn periods(&self) -> (usize, usize) {
(self.period, self.trading_periods)
}
/// Current value if available.
pub const fn value(&self) -> Option<f64> {
self.last
}
}
impl Indicator for HistoricalVolatility {
type Input = f64;
type Output = f64;
fn update(&mut self, input: f64) -> Option<f64> {
// Non-finite *and* non-positive prices are both ignored: state is left
// untouched and `self.last` is returned. The log-return `ln(input /
// prev)` is undefined for non-positive prices, and silently
// substituting `0.0` (the previous behaviour, audit finding R13) would
// underreport realised volatility by treating bad ticks as "no
// movement". Skipping them entirely is consistent with how the rest
// of the library handles invalid inputs (see SMA / EMA / ROC).
if !input.is_finite() || input <= 0.0 {
return self.last;
}
let Some(prev) = self.prev_price else {
self.prev_price = Some(input);
return None;
};
// `prev` was assigned from `self.prev_price`, which only ever holds
// valid (finite, positive) inputs because the guard above gates every
// assignment to it — so `(input / prev).ln()` is always well-defined.
self.prev_price = Some(input);
let log_return = (input / prev).ln();
if self.window.len() == self.period {
let old = self.window.pop_front().expect("window is non-empty");
self.sum -= old;
self.sum_sq -= old * old;
}
self.window.push_back(log_return);
self.sum += log_return;
self.sum_sq += log_return * log_return;
if self.window.len() < self.period {
return None;
}
let n = self.period as f64;
let mean = self.sum / n;
// Sample variance (Bessel's correction): Σ(x−mean)² / (n−1).
let variance = ((self.sum_sq - n * mean * mean) / (n - 1.0)).max(0.0);
let hv = variance.sqrt() * (self.trading_periods as f64).sqrt() * 100.0;
self.last = Some(hv);
Some(hv)
}
fn reset(&mut self) {
self.prev_price = None;
self.window.clear();
self.sum = 0.0;
self.sum_sq = 0.0;
self.last = None;
}
fn warmup_period(&self) -> usize {
// The first log return needs a previous price, then the window fills.
self.period + 1
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"HistoricalVolatility"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
#[test]
fn new_rejects_zero_period() {
assert!(matches!(
HistoricalVolatility::new(0, 252),
Err(Error::PeriodZero)
));
assert!(matches!(
HistoricalVolatility::new(20, 0),
Err(Error::PeriodZero)
));
}
/// Cover the const accessors `periods` / `value` (80-88) and the
/// Indicator-impl `name` body (153-155). Existing tests inspect HV
/// output but never query the metadata.
#[test]
fn accessors_and_metadata() {
let mut hv = HistoricalVolatility::new(20, 252).unwrap();
assert_eq!(hv.periods(), (20, 252));
assert_eq!(hv.name(), "HistoricalVolatility");
assert_eq!(hv.value(), None);
for i in 1..=hv.warmup_period() {
hv.update(100.0 + f64::from(u32::try_from(i).unwrap()));
}
assert!(hv.value().is_some());
}
#[test]
fn new_rejects_period_one() {
assert!(matches!(
HistoricalVolatility::new(1, 252),
Err(Error::InvalidPeriod { .. })
));
}
#[test]
fn first_emission_at_warmup_period() {
let mut hv = HistoricalVolatility::new(5, 252).unwrap();
assert_eq!(hv.warmup_period(), 6);
let out = hv.batch(&(1..=20).map(f64::from).collect::<Vec<_>>());
for v in out.iter().take(5) {
assert!(v.is_none());
}
assert!(out[5].is_some());
}
#[test]
fn constant_series_yields_zero() {
// Flat prices -> all log returns are 0 -> zero volatility.
let mut hv = HistoricalVolatility::new(10, 252).unwrap();
let out = hv.batch(&[100.0; 40]);
for v in out.iter().skip(10).flatten() {
assert_relative_eq!(*v, 0.0, epsilon = 1e-12);
}
}
#[test]
fn geometric_series_yields_zero() {
// A constant growth factor gives a constant log return -> zero stddev.
// The mathematical result is exactly zero, but `1.01_f64.powi(i)` and
// the subsequent log / std-dev cascade accumulate platform-sensitive
// floating-point drift on the order of 1e-7 (observed on x86_64 Linux
// and macOS; Windows happens to round closer to zero). The 1e-6
// tolerance stays four decimal places below any realistic volatility
// value while absorbing this drift across every supported platform.
let mut hv = HistoricalVolatility::new(10, 252).unwrap();
let prices: Vec<f64> = (0..40).map(|i| 100.0 * 1.01_f64.powi(i)).collect();
let out = hv.batch(&prices);
for v in out.iter().skip(10).flatten() {
assert_relative_eq!(*v, 0.0, epsilon = 1e-6);
}
}
#[test]
fn output_is_non_negative() {
let mut hv = HistoricalVolatility::new(20, 252).unwrap();
let prices: Vec<f64> = (1..=200)
.map(|i| 100.0 + (f64::from(i) * 0.3).sin() * 12.0)
.collect();
for v in hv.batch(&prices).into_iter().flatten() {
assert!(v >= 0.0, "volatility must be non-negative, got {v}");
}
}
#[test]
fn ignores_non_finite_input() {
let mut hv = HistoricalVolatility::new(5, 252).unwrap();
let out = hv.batch(&(1..=20).map(f64::from).collect::<Vec<_>>());
let last = *out.last().unwrap();
assert!(last.is_some());
assert_eq!(hv.update(f64::NAN), last);
assert_eq!(hv.update(f64::INFINITY), last);
}
/// Audit finding R13. Non-positive prices are now skipped (state left
/// untouched) instead of silently treated as a `0.0` log-return — the old
/// behaviour underreported realised volatility by treating bad ticks as
/// "no movement".
#[test]
fn skips_non_positive_prices() {
let mut hv = HistoricalVolatility::new(5, 252).unwrap();
// Warm up with positive prices.
let warmup_prices = (1..=20).map(f64::from).collect::<Vec<_>>();
let warmup = hv.batch(&warmup_prices);
let baseline = warmup
.last()
.copied()
.flatten()
.expect("warmed up by index 5");
// A negative tick must be ignored: returned value equals the previous
// baseline, and the next real positive tick must use the previous
// valid price as `prev` (not the bad one), so the next log return is
// exactly `ln(21 / 20)`, not `ln(21 / -5)` or anything else.
assert_eq!(hv.update(-5.0), Some(baseline));
assert_eq!(hv.update(0.0), Some(baseline));
// Snapshot the indicator's state, then advance with a real positive
// tick on a clone. The clone must agree with a from-scratch run that
// simply skipped the bad ticks — proving the state was untouched.
let mut control = hv.clone();
let after_real = hv.update(21.0).expect("ready");
assert_eq!(control.update(21.0).expect("ready"), after_real);
}
#[test]
fn reset_clears_state() {
let mut hv = HistoricalVolatility::new(5, 252).unwrap();
hv.batch(&(1..=20).map(f64::from).collect::<Vec<_>>());
assert!(hv.is_ready());
hv.reset();
assert!(!hv.is_ready());
assert_eq!(hv.update(1.0), None);
}
#[test]
fn batch_equals_streaming() {
let prices: Vec<f64> = (1..=120)
.map(|i| 100.0 + (f64::from(i) * 0.25).sin() * 9.0)
.collect();
let batch = HistoricalVolatility::new(20, 252).unwrap().batch(&prices);
let mut b = HistoricalVolatility::new(20, 252).unwrap();
let streamed: Vec<_> = prices.iter().map(|p| b.update(*p)).collect();
assert_eq!(batch, streamed);
}
}