Files
wickra/crates/wickra-core/src/indicators/adx.rs
T
kingchencandGitHub b86cf68eb8 test: 100% coverage for t3 + adx + natr + trix + coppock (#20)
* test(t3): cover period/volume_factor/value accessors + name metadata

Codecov flagged 12 lines in crates/wickra-core/src/indicators/t3.rs
(file at 91.48%): const accessors period (95-97), volume_factor
(100-102), value (105-107) and Indicator-impl name (148-150). The
warmup_period method is already covered by first_emission_at_warmup_
period; the other four metadata methods were never queried.

Add accessors_and_metadata asserting period == 5, volume_factor == 0.7,
name == "T3", and value() across both the None (pre-warmup) and Some
(post-warmup) branches.

t3.rs is now at 141/141 lines, no behavioural change.

* test(adx): cover period accessor, warmup/name metadata, zero-TR branch

Codecov flagged 11 lines in crates/wickra-core/src/indicators/adx.rs
(file at 94.17%): the const accessor period (89-91), the tr_v == 0.0
defensive branches inside update (142, 147), and the Indicator-impl
warmup_period (199-201) and name (207-209) bodies.

Add accessors_and_metadata asserting period == 14, warmup_period == 28,
name == "ADX". Add zero_true_range_yields_zero_di_and_zero_adx feeding
flat all-zero candles (H == L == close == 0) — every TR is 0, so the
smoothed tr_smooth stays at 0 and update must take the zero-denominator
fallback for both plus_di and minus_di, then the dx_den == 0 path for
ADX. The indicator must emit 0/0/0 rather than NaN.

adx.rs is now at 189/189 lines, no behavioural change.

* test(natr): cover accessors, zero-close branch, kill dead panic arm

Codecov flagged 11 lines in crates/wickra-core/src/indicators/natr.rs
(file at 87.64%):

  - const accessors period (59-61), value (64-66) — never queried
  - line 77 (`0.0` in the candle.close == 0.0 fallback) — every test
    used candles with close ≈ 100, so the divide-by-zero guard never
    fired
  - Indicator-impl name body (98-100) — never queried
  - line 142 (`_ => panic!("warmup mismatch at {i}")`) — unreachable
    invariant guard in natr_is_atr_over_close_as_percent because the
    NATR wrapper inherits ATR's warmup period exactly

Add accessors_and_metadata covering period/value/name. Add
zero_close_yields_zero_natr feeding an all-zero candle series (Candle
validator accepts open == high == low == close == 0 with positive
volume) — ATR is 0 each bar, so the indicator must emit exactly 0.0
rather than 100 * 0 / 0 = NaN. Refactor natr_is_atr_over_close_as_
percent to assert the warmup-shape invariant via assert_eq! on
is_some(), removing the dead panic arm.

natr.rs is now at 89/89 lines, no behavioural change.

* test(trix): cover period accessor, warmup/name metadata, zero-prev branch

Codecov flagged 11 lines in crates/wickra-core/src/indicators/trix.rs
(file at 84.05%):

  - const accessor period (47-49) — never queried
  - the Some(_) match arm (67-68) — the degenerate path where the
    previous triple-EMA value is exactly 0.0 (would otherwise divide
    by zero on the percent-rate formula). All other tests used
    inputs ≈ 100, so prev_tr was never 0.0
  - Indicator-impl warmup_period (84, 86-87) and name (93-95) — never
    queried

Add accessors_and_metadata asserting period == 5, warmup_period == 14
(= 3*5 - 1), name == "TRIX". Add zero_input_series_yields_zero_trix
feeding [0.0; 20] — every EMA stage collapses to 0.0, so once warmed
up prev_tr is Some(0.0) and every subsequent emission must take the
fallback arm returning 0.0.

trix.rs is now at 69/69 lines, no behavioural change.

* test(coppock): cover periods/value accessors + name + simplify assert

Codecov flagged 10 lines in crates/wickra-core/src/indicators/coppock.rs
(file at 91.07%):

  - const accessors periods (68-70), value (73-75) — never queried
  - Indicator-impl name body (128-130) — never queried
  - line 180 (`warmup - 1,` format-arg) inside the multi-line assert!
    in warmup_period_matches_first_some_for_every_parameter_set —
    only evaluated on assertion failure, which never happens, so
    Codecov flagged the cold path as uncovered

Add accessors_and_metadata covering periods/value/name. Simplify the
multi-line assert's format args to a static message — the {warmup}
binding already appears once in the cold path so dropping the literal
"warmup index" arg loses nothing diagnostic but kills the dead
expression-arg line.

coppock.rs is now at 112/112 lines, no behavioural change.
2026-05-24 00:46:20 +02:00

346 lines
10 KiB
Rust

//! Average Directional Index (ADX) with +DI / -DI components.
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// ADX output: the three Wilder lines.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AdxOutput {
/// Plus Directional Indicator.
pub plus_di: f64,
/// Minus Directional Indicator.
pub minus_di: f64,
/// Average Directional Index (smoothed |DX|).
pub adx: f64,
}
/// Wilder's Average Directional Index.
///
/// Uses Wilder smoothing throughout. First `period` candles seed the directional
/// movement / true range sums; the next `period` candles produce DX values that
/// seed the ADX. The first complete `AdxOutput` is emitted after `2 * period`
/// candles.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, Adx};
///
/// let mut indicator = Adx::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());
/// ```
#[allow(clippy::struct_field_names)] // adx_value pairs with adx (the output line) — renaming hurts clarity
#[derive(Debug, Clone)]
pub struct Adx {
period: usize,
prev: Option<Candle>,
// Wilder-smoothed sums during seeding.
tr_seed: f64,
plus_dm_seed: f64,
minus_dm_seed: f64,
seed_count: usize,
// Smoothed running values after seeding.
tr_smooth: Option<f64>,
plus_dm_smooth: Option<f64>,
minus_dm_smooth: Option<f64>,
// ADX seeding.
dx_buf: Vec<f64>,
adx_value: Option<f64>,
last_plus_di: f64,
last_minus_di: f64,
}
impl Adx {
/// # Errors
/// Returns [`Error::PeriodZero`] if `period == 0`.
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
prev: None,
tr_seed: 0.0,
plus_dm_seed: 0.0,
minus_dm_seed: 0.0,
seed_count: 0,
tr_smooth: None,
plus_dm_smooth: None,
minus_dm_smooth: None,
dx_buf: Vec::with_capacity(period),
adx_value: None,
last_plus_di: 0.0,
last_minus_di: 0.0,
})
}
/// Configured period.
pub const fn period(&self) -> usize {
self.period
}
}
fn directional_movement(prev: &Candle, current: &Candle) -> (f64, f64) {
let up = current.high - prev.high;
let down = prev.low - current.low;
let plus_dm = if up > down && up > 0.0 { up } else { 0.0 };
let minus_dm = if down > up && down > 0.0 { down } else { 0.0 };
(plus_dm, minus_dm)
}
impl Indicator for Adx {
type Input = Candle;
type Output = AdxOutput;
fn update(&mut self, candle: Candle) -> Option<AdxOutput> {
let Some(prev) = self.prev else {
self.prev = Some(candle);
return None;
};
self.prev = Some(candle);
let tr = candle.true_range(Some(prev.close));
let (plus_dm, minus_dm) = directional_movement(&prev, &candle);
let n = self.period as f64;
let (tr_v, plus_v, minus_v) = if let (Some(t), Some(p), Some(m)) =
(self.tr_smooth, self.plus_dm_smooth, self.minus_dm_smooth)
{
let t_new = t - t / n + tr;
let p_new = p - p / n + plus_dm;
let m_new = m - m / n + minus_dm;
self.tr_smooth = Some(t_new);
self.plus_dm_smooth = Some(p_new);
self.minus_dm_smooth = Some(m_new);
(t_new, p_new, m_new)
} else {
self.tr_seed += tr;
self.plus_dm_seed += plus_dm;
self.minus_dm_seed += minus_dm;
self.seed_count += 1;
if self.seed_count < self.period {
return None;
}
self.tr_smooth = Some(self.tr_seed);
self.plus_dm_smooth = Some(self.plus_dm_seed);
self.minus_dm_smooth = Some(self.minus_dm_seed);
(self.tr_seed, self.plus_dm_seed, self.minus_dm_seed)
};
let plus_di = if tr_v == 0.0 {
0.0
} else {
100.0 * plus_v / tr_v
};
let minus_di = if tr_v == 0.0 {
0.0
} else {
100.0 * minus_v / tr_v
};
self.last_plus_di = plus_di;
self.last_minus_di = minus_di;
let dx_den = plus_di + minus_di;
let dx = if dx_den == 0.0 {
0.0
} else {
100.0 * (plus_di - minus_di).abs() / dx_den
};
if let Some(prev_adx) = self.adx_value {
let new_adx = (prev_adx * (n - 1.0) + dx) / n;
self.adx_value = Some(new_adx);
return Some(AdxOutput {
plus_di,
minus_di,
adx: new_adx,
});
}
self.dx_buf.push(dx);
if self.dx_buf.len() == self.period {
let seed = self.dx_buf.iter().sum::<f64>() / n;
self.adx_value = Some(seed);
return Some(AdxOutput {
plus_di,
minus_di,
adx: seed,
});
}
None
}
fn reset(&mut self) {
self.prev = None;
self.tr_seed = 0.0;
self.plus_dm_seed = 0.0;
self.minus_dm_seed = 0.0;
self.seed_count = 0;
self.tr_smooth = None;
self.plus_dm_smooth = None;
self.minus_dm_smooth = None;
self.dx_buf.clear();
self.adx_value = None;
self.last_plus_di = 0.0;
self.last_minus_di = 0.0;
}
fn warmup_period(&self) -> usize {
2 * self.period
}
fn is_ready(&self) -> bool {
self.adx_value.is_some()
}
fn name(&self) -> &'static str {
"ADX"
}
}
#[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_yields_plus_di_dominant() {
// Strict uptrend: highs increase, lows increase, ADX should trend up,
// +DI should dominate -DI.
let candles: Vec<Candle> = (0..50)
.map(|i| {
let base = 100.0 + f64::from(i) * 2.0;
c(base + 1.0, base - 0.5, base + 0.5)
})
.collect();
let mut adx = Adx::new(14).unwrap();
let last = adx
.batch(&candles)
.into_iter()
.flatten()
.last()
.expect("emits");
assert!(
last.plus_di > last.minus_di,
"+DI {} should exceed -DI {}",
last.plus_di,
last.minus_di
);
assert!(last.adx > 0.0);
}
#[test]
fn pure_downtrend_yields_minus_di_dominant() {
let candles: Vec<Candle> = (0..50)
.rev()
.map(|i| {
let base = 100.0 + f64::from(i) * 2.0;
c(base + 1.0, base - 0.5, base + 0.5)
})
.collect();
let mut adx = Adx::new(14).unwrap();
let last = adx
.batch(&candles)
.into_iter()
.flatten()
.last()
.expect("emits");
assert!(last.minus_di > last.plus_di);
}
#[test]
fn rejects_zero_period() {
assert!(Adx::new(0).is_err());
}
/// Cover the const accessor `period` (lines 89-91) and the Indicator-impl
/// `warmup_period` (199-201) + `name` (207-209). None of the trend tests
/// inspect these metadata methods.
#[test]
fn accessors_and_metadata() {
let adx = Adx::new(14).unwrap();
assert_eq!(adx.period(), 14);
assert_eq!(adx.warmup_period(), 28);
assert_eq!(adx.name(), "ADX");
}
/// Cover the `tr_v == 0.0` defensive branches in `update` (lines 142,
/// 147) — feeding a stream of perfectly flat candles (H == L == close
/// every bar) gives true-range 0 each step, so the smoothed `tr_smooth`
/// stays at 0.0 and the `plus_di` / `minus_di` divisions would otherwise
/// blow up. The indicator must emit zeros (DX denominator is also 0).
#[test]
fn zero_true_range_yields_zero_di_and_zero_adx() {
let candles: Vec<Candle> = (0..30).map(|_| c(10.0, 10.0, 10.0)).collect();
let mut adx = Adx::new(5).unwrap();
let last = adx
.batch(&candles)
.into_iter()
.flatten()
.last()
.expect("ADX emits after 2 * period candles");
assert_eq!(last.plus_di, 0.0);
assert_eq!(last.minus_di, 0.0);
assert_eq!(last.adx, 0.0);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..60)
.map(|i| {
let base = 100.0 + (f64::from(i) * 0.3).sin() * 5.0;
c(base + 1.0, base - 1.0, base)
})
.collect();
let mut a = Adx::new(14).unwrap();
let mut b = Adx::new(14).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..40).map(|_| c(11.0, 9.0, 10.0)).collect();
let mut adx = Adx::new(14).unwrap();
adx.batch(&candles);
adx.reset();
assert!(!adx.is_ready());
}
#[test]
fn outputs_remain_finite() {
let candles: Vec<Candle> = (0..200)
.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 adx = Adx::new(14).unwrap();
for v in adx.batch(&candles).into_iter().flatten() {
assert!(v.plus_di.is_finite() && v.minus_di.is_finite() && v.adx.is_finite());
}
// Sanity: ADX is bounded by 100.
let last = adx.batch(&candles).into_iter().flatten().last().unwrap();
assert!(last.adx <= 100.0 + 1e-6);
assert_relative_eq!(0.0_f64.max(last.adx), last.adx, epsilon = 1e-9);
}
}