use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::indicators::sma::Sma;
use crate::ohlcv::Candle;
use crate::traits::Indicator;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HurstChannelOutput {
pub upper: f64,
pub middle: f64,
pub lower: f64,
}
#[derive(Debug, Clone)]
pub struct HurstChannel {
period: usize,
multiplier: f64,
sma: Sma,
highs: VecDeque<f64>,
lows: VecDeque<f64>,
}
impl HurstChannel {
pub fn new(period: usize, multiplier: f64) -> Result<Self> {
if !multiplier.is_finite() || multiplier <= 0.0 {
return Err(Error::NonPositiveMultiplier);
}
Ok(Self {
period,
multiplier,
sma: Sma::new(period)?,
highs: VecDeque::with_capacity(period),
lows: VecDeque::with_capacity(period),
})
}
pub const fn period(&self) -> usize {
self.period
}
pub const fn multiplier(&self) -> f64 {
self.multiplier
}
}
impl Indicator for HurstChannel {
type Input = Candle;
type Output = HurstChannelOutput;
fn update(&mut self, candle: Candle) -> Option<HurstChannelOutput> {
if self.highs.len() == self.period {
self.highs.pop_front();
self.lows.pop_front();
}
self.highs.push_back(candle.high);
self.lows.push_back(candle.low);
let middle = self.sma.update(candle.close)?;
let hi = self.highs.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let lo = self.lows.iter().copied().fold(f64::INFINITY, f64::min);
let range = hi - lo;
Some(HurstChannelOutput {
upper: middle + self.multiplier * range,
middle,
lower: middle - self.multiplier * range,
})
}
fn reset(&mut self) {
self.sma.reset();
self.highs.clear();
self.lows.clear();
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.sma.is_ready()
}
fn name(&self) -> &'static str {
"HurstChannel"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn c(h: f64, l: f64, cl: f64) -> Candle {
Candle::new(cl, h, l, cl, 1.0, 0).unwrap()
}
#[test]
fn rejects_zero_period() {
assert!(matches!(HurstChannel::new(0, 0.5), Err(Error::PeriodZero)));
}
#[test]
fn rejects_non_positive_multiplier() {
assert!(matches!(
HurstChannel::new(10, 0.0),
Err(Error::NonPositiveMultiplier)
));
assert!(matches!(
HurstChannel::new(10, -0.5),
Err(Error::NonPositiveMultiplier)
));
assert!(matches!(
HurstChannel::new(10, f64::NAN),
Err(Error::NonPositiveMultiplier)
));
}
#[test]
fn accessors_and_metadata() {
let h = HurstChannel::new(10, 0.5).unwrap();
assert_eq!(h.period(), 10);
assert_relative_eq!(h.multiplier(), 0.5, epsilon = 1e-12);
assert_eq!(h.warmup_period(), 10);
assert_eq!(h.name(), "HurstChannel");
}
#[test]
fn flat_market_collapses_bands() {
let candles: Vec<Candle> = (0..20).map(|_| c(10.0, 10.0, 10.0)).collect();
let mut h = HurstChannel::new(5, 0.5).unwrap();
let last = h.batch(&candles).into_iter().flatten().last().unwrap();
assert_relative_eq!(last.upper, 10.0, epsilon = 1e-9);
assert_relative_eq!(last.middle, 10.0, epsilon = 1e-9);
assert_relative_eq!(last.lower, 10.0, epsilon = 1e-9);
}
#[test]
fn upper_above_middle_above_lower() {
let candles: Vec<Candle> = (0..50)
.map(|i| {
let m = 100.0 + (f64::from(i) * 0.2).sin() * 5.0;
c(m + 1.0, m - 1.0, m)
})
.collect();
let mut h = HurstChannel::new(10, 0.5).unwrap();
for o in h.batch(&candles).into_iter().flatten() {
assert!(o.upper >= o.middle);
assert!(o.middle >= o.lower);
}
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..40)
.map(|i| c(f64::from(i) + 2.0, f64::from(i), f64::from(i) + 1.0))
.collect();
let mut a = HurstChannel::new(10, 0.5).unwrap();
let mut b = HurstChannel::new(10, 0.5).unwrap();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn reset_clears_state() {
let candles: Vec<Candle> = (0..10)
.map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
.collect();
let mut h = HurstChannel::new(5, 0.5).unwrap();
h.batch(&candles);
assert!(h.is_ready());
h.reset();
assert!(!h.is_ready());
assert_eq!(h.update(candles[0]), None);
}
#[test]
fn reference_values() {
let candles: Vec<Candle> = (0..5).map(|_| c(12.0, 8.0, 10.0)).collect();
let mut h = HurstChannel::new(5, 0.5).unwrap();
let out = h.batch(&candles);
assert!(out[0].is_none() && out[3].is_none());
let v = out[4].unwrap();
assert_relative_eq!(v.middle, 10.0, epsilon = 1e-9);
assert_relative_eq!(v.upper, 12.0, epsilon = 1e-9);
assert_relative_eq!(v.lower, 8.0, epsilon = 1e-9);
}
}