use crate::calendar::civil_from_timestamp;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
#[derive(Debug, Clone, PartialEq)]
pub struct IntradayVolatilityProfileOutput {
pub bins: Vec<f64>,
}
#[derive(Debug, Clone)]
pub struct IntradayVolatilityProfile {
buckets: usize,
utc_offset_minutes: i32,
prev_close: Option<f64>,
count: Vec<u64>,
mean: Vec<f64>,
m2: Vec<f64>,
last: Option<IntradayVolatilityProfileOutput>,
}
impl IntradayVolatilityProfile {
pub fn new(buckets: usize, utc_offset_minutes: i32) -> Result<Self> {
if buckets == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
buckets,
utc_offset_minutes,
prev_close: None,
count: vec![0; buckets],
mean: vec![0.0; buckets],
m2: vec![0.0; buckets],
last: None,
})
}
pub const fn params(&self) -> (usize, i32) {
(self.buckets, self.utc_offset_minutes)
}
pub fn value(&self) -> Option<&IntradayVolatilityProfileOutput> {
self.last.as_ref()
}
fn bucket_of(&self, minute_of_day: u32) -> usize {
let raw = (minute_of_day as usize * self.buckets) / 1440;
raw.min(self.buckets - 1)
}
fn snapshot(&self) -> IntradayVolatilityProfileOutput {
let bins = self
.count
.iter()
.zip(&self.m2)
.map(|(n, m2)| {
if *n >= 2 {
(m2 / (*n - 1) as f64).sqrt()
} else {
0.0
}
})
.collect();
IntradayVolatilityProfileOutput { bins }
}
}
impl Indicator for IntradayVolatilityProfile {
type Input = Candle;
type Output = IntradayVolatilityProfileOutput;
fn update(&mut self, candle: Candle) -> Option<IntradayVolatilityProfileOutput> {
let civil = civil_from_timestamp(candle.timestamp, self.utc_offset_minutes);
let result = if let Some(prev) = self.prev_close {
let ret = if prev == 0.0 {
0.0
} else {
candle.close / prev - 1.0
};
let bucket = self.bucket_of(civil.minute_of_day());
self.count[bucket] += 1;
let delta = ret - self.mean[bucket];
self.mean[bucket] += delta / self.count[bucket] as f64;
let delta2 = ret - self.mean[bucket];
self.m2[bucket] += delta * delta2;
let out = self.snapshot();
self.last = Some(out.clone());
Some(out)
} else {
None
};
self.prev_close = Some(candle.close);
result
}
fn reset(&mut self) {
self.prev_close = None;
self.count.iter_mut().for_each(|x| *x = 0);
self.mean.iter_mut().for_each(|x| *x = 0.0);
self.m2.iter_mut().for_each(|x| *x = 0.0);
self.last = None;
}
fn warmup_period(&self) -> usize {
2
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"IntradayVolatilityProfile"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
const HOUR: i64 = 3_600_000;
const DAY: i64 = 24 * HOUR;
fn c(close: f64, ts: i64) -> Candle {
Candle::new(close, close, close, close, 1.0, ts).unwrap()
}
#[test]
fn rejects_zero_buckets() {
assert!(matches!(
IntradayVolatilityProfile::new(0, 0),
Err(Error::PeriodZero)
));
}
#[test]
fn metadata_and_accessors() {
let prof = IntradayVolatilityProfile::new(24, 90).unwrap();
assert_eq!(prof.params(), (24, 90));
assert_eq!(prof.name(), "IntradayVolatilityProfile");
assert_eq!(prof.warmup_period(), 2);
assert!(!prof.is_ready());
assert!(prof.value().is_none());
}
#[test]
fn single_sample_bucket_has_zero_vol() {
let mut prof = IntradayVolatilityProfile::new(24, 0).unwrap();
assert!(prof.update(c(100.0, 0)).is_none());
let out = prof.update(c(101.0, HOUR)).unwrap();
assert_eq!(out.bins.len(), 24);
assert_relative_eq!(out.bins[1], 0.0); assert!(prof.is_ready());
}
#[test]
fn std_matches_manual_two_samples() {
let mut prof = IntradayVolatilityProfile::new(24, 0).unwrap();
prof.update(c(100.0, 0)); prof.update(c(101.0, HOUR)); let out = prof.update(c(101.0 * 1.03, 25 * HOUR)).unwrap();
let mean = 0.02;
let expected = (((0.01_f64 - mean).powi(2) + (0.03 - mean).powi(2)) / 1.0).sqrt();
assert_relative_eq!(out.bins[1], expected, epsilon = 1e-9);
}
#[test]
fn zero_prev_close_uses_zero_return() {
let mut prof = IntradayVolatilityProfile::new(4, 0).unwrap();
prof.update(c(0.0, 0));
let out = prof.update(c(5.0, HOUR)).unwrap();
assert_relative_eq!(out.bins[0], 0.0);
}
#[test]
fn reset_clears_state() {
let mut prof = IntradayVolatilityProfile::new(24, 0).unwrap();
prof.update(c(100.0, 0));
prof.update(c(101.0, HOUR));
prof.reset();
assert!(!prof.is_ready());
assert!(prof.value().is_none());
assert!(prof.update(c(100.0, DAY)).is_none());
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..50)
.map(|i| c(100.0 + f64::from(i % 6), i64::from(i) * HOUR))
.collect();
let mut a = IntradayVolatilityProfile::new(12, 0).unwrap();
let mut b = IntradayVolatilityProfile::new(12, 0).unwrap();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
}