Files
wickra/crates/wickra-core/src/indicators/aroon.rs
T
kingchencandGitHub d9a1950007 test: 100% coverage for rsi + accelerator_oscillator + aroon + atr_trailing_stop + chaikin_oscillator (#28)
* test(rsi): cover period/value accessors, name, naive flat-series branch

Codecov flagged 7 lines in indicators/rsi.rs (file at 96.42%): const
accessors period (60-62), value (65-67), Indicator-impl name (145-147),
and line 167 in the test-helper rsi_naive's ag==0 fallback. The
proptest reference never lands on a fully flat series so the helper's
50.0 branch was dead.

Add accessors_and_metadata covering period/value/name and
naive_helper_flat_series_yields_50 driving rsi_naive on [42.0; 20] —
both avg_gain and avg_loss converge to 0, hitting the 50.0 branch.
rsi.rs now at 196/196.

* test(accelerator_oscillator): cover params accessor + name metadata

Codecov flagged 6 lines in indicators/accelerator_oscillator.rs (file
at 93.68%): const accessor params (69-71) and Indicator-impl name
(99-101). ac.rs now at 95/95.

* test(aroon): cover period accessor + name metadata

Codecov flagged 6 lines in indicators/aroon.rs (file at 94.28%): const
accessor period (56-58) and Indicator-impl name (104-106). aroon.rs
now at 105/105.

* test(atr_trailing_stop): cover params accessor + name metadata

Codecov flagged 6 lines in indicators/atr_trailing_stop.rs (file at
95.91%): const accessor params (77-79) and Indicator-impl name
(130-132). atr_trailing_stop.rs now at 147/147.

* test(chaikin_oscillator): cover periods accessor + name metadata

Codecov flagged 6 lines in indicators/chaikin_oscillator.rs (file at
95.27%): const accessor periods (76-78) and Indicator-impl name
(109-111). chaikin_oscillator.rs now at 127/127.
2026-05-24 00:47:53 +02:00

195 lines
5.5 KiB
Rust

//! Aroon Up / Down indicator.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Aroon output: up and down strengths in [0, 100].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AroonOutput {
/// Time since the highest high, expressed as a percentage of the window.
pub up: f64,
/// Time since the lowest low, same convention.
pub down: f64,
}
/// Aroon indicator: tracks how many bars since the highest high and lowest low
/// inside a `period + 1`-bar window. Returned as a percentage.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, Aroon};
///
/// let mut indicator = Aroon::new(5).unwrap();
/// let mut last = None;
/// for i in 0..80 {
/// let base = 100.0 + f64::from(i);
/// let candle =
/// Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
/// last = indicator.update(candle);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct Aroon {
period: usize,
candles: VecDeque<Candle>,
}
impl Aroon {
/// # Errors
/// Returns [`Error::PeriodZero`] if `period == 0`.
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
candles: VecDeque::with_capacity(period + 1),
})
}
/// Configured period.
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;
}
// Find the index (0 = oldest) of the highest high and lowest low.
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);
// The lowest low is at the oldest position (index 0).
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));
}
}
#[test]
fn reset_clears_state() {
let candles: Vec<Candle> = (1..=20)
.map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
.collect();
let mut a = Aroon::new(14).unwrap();
a.batch(&candles);
assert!(a.is_ready());
a.reset();
assert!(!a.is_ready());
assert_eq!(a.update(candles[0]), None);
}
/// Cover the const accessor `period` (56-58) and the Indicator-impl
/// `name` body (104-106). `warmup_period` is exercised elsewhere.
#[test]
fn accessors_and_metadata() {
let a = Aroon::new(14).unwrap();
assert_eq!(a.period(), 14);
assert_eq!(a.name(), "Aroon");
}
}