use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AroonOutput {
pub up: f64,
pub down: f64,
}
#[derive(Debug, Clone)]
pub struct Aroon {
period: usize,
candles: VecDeque<Candle>,
}
impl Aroon {
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
candles: VecDeque::with_capacity(period + 1),
})
}
pub const fn period(&self) -> usize {
self.period
}
}
impl Indicator for Aroon {
type Input = Candle;
type Output = AroonOutput;
fn update(&mut self, candle: Candle) -> Option<AroonOutput> {
if self.candles.len() == self.period + 1 {
self.candles.pop_front();
}
self.candles.push_back(candle);
if self.candles.len() < self.period + 1 {
return None;
}
let (mut hh_idx, mut ll_idx) = (0_usize, 0_usize);
let (mut hh, mut ll) = (f64::NEG_INFINITY, f64::INFINITY);
for (i, c) in self.candles.iter().enumerate() {
if c.high >= hh {
hh = c.high;
hh_idx = i;
}
if c.low <= ll {
ll = c.low;
ll_idx = i;
}
}
let n = self.period as f64;
let up = 100.0 * hh_idx as f64 / n;
let down = 100.0 * ll_idx as f64 / n;
Some(AroonOutput { up, down })
}
fn reset(&mut self) {
self.candles.clear();
}
fn warmup_period(&self) -> usize {
self.period + 1
}
fn is_ready(&self) -> bool {
self.candles.len() == self.period + 1
}
fn name(&self) -> &'static str {
"Aroon"
}
}
#[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 pure_uptrend_aroon_up_100() {
let candles: Vec<Candle> = (1..=15)
.map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
.collect();
let mut a = Aroon::new(14).unwrap();
let last = a.batch(&candles).into_iter().flatten().last().unwrap();
assert_relative_eq!(last.up, 100.0, epsilon = 1e-9);
assert_relative_eq!(last.down, 0.0, epsilon = 1e-9);
}
#[test]
fn pure_downtrend_aroon_down_100() {
let candles: Vec<Candle> = (1..=15)
.rev()
.map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
.collect();
let mut a = Aroon::new(14).unwrap();
let last = a.batch(&candles).into_iter().flatten().last().unwrap();
assert_relative_eq!(last.down, 100.0, epsilon = 1e-9);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..40)
.map(|i| {
let m = 50.0 + (f64::from(i) * 0.3).sin() * 5.0;
c(m + 1.0, m - 1.0, m)
})
.collect();
let mut a = Aroon::new(14).unwrap();
let mut b = Aroon::new(14).unwrap();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn outputs_in_range() {
let candles: Vec<Candle> = (0..200)
.map(|i| {
let m = 50.0 + (f64::from(i) * 0.2).sin() * 5.0;
c(m + 1.0, m - 1.0, m)
})
.collect();
let mut a = Aroon::new(14).unwrap();
for o in a.batch(&candles).into_iter().flatten() {
assert!((0.0..=100.0).contains(&o.up));
assert!((0.0..=100.0).contains(&o.down));
}
}
}