Files
wickra/crates/wickra-core/src/indicators/adl.rs
T
kingchencandGitHub d55d3db3d1 test: 100% coverage for vertical_horizontal_filter + z_score + vpt + csv + adl (#31)
* test(vertical_horizontal_filter): cover period accessor + name metadata

Codecov flagged 6 lines (file at 94.44%): period (61-63) + name (119-121).

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

Codecov flagged 6 lines (file at 93.75%): period (59-61) + name (106-108).

* test(vpt): cover value() Some branch, name, zero-prev fallback

Codecov flagged 5 lines (file at 94.38%): value() Some branch (57),
prev==0.0 ROC fallback (77), and Indicator-impl name (100-102).
Add accessors_and_metadata covering value()/name and zero_previous_
close_contributes_zero — feeding a 0.0 baseline + non-zero candle
proves the divide-by-zero guard yields a 0 contribution rather than NaN.

* test(csv): cover from_csv_reader + kill rejects_header dead panic arm

Codecov flagged 5 lines in csv.rs (file at 96.98%): from_csv_reader
(201-204) — never called by existing tests which use from_reader /
open — and the cold  arm in
rejects_header_missing_a_column (279). Add from_csv_reader_accepts_a_
prebuilt_reader (demonstrates the API by building a custom-delimited
csv::Reader and passing it in), and refactor the header-missing test
to use a single matches!() assertion so the panic arm is gone.

* test(adl): cover name metadata

Codecov flagged 3 lines (file at 96.84%): Indicator-impl name body (94-96).
2026-05-24 00:48:02 +02:00

194 lines
5.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Accumulation/Distribution Line.
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Accumulation/Distribution Line — Marc Chaikin's cumulative volume-flow
/// indicator.
///
/// Each bar contributes a *money-flow volume*: the bar's volume weighted by
/// where the close fell within the bar's range.
///
/// ```text
/// MFM_t = ((close low) (high close)) / (high low) (the money-flow multiplier, 1..+1)
/// MFV_t = MFM_t · volume_t
/// ADL_t = ADL_{t1} + MFV_t
/// ```
///
/// A close near the high makes the multiplier near `+1` (accumulation), near
/// the low near `1` (distribution). The running total is unbounded and drifts
/// with cumulative volume — what matters is its slope and its divergence from
/// price. A bar with `high == low` contributes `0`.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, Adl};
///
/// let mut indicator = Adl::new();
/// 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, Default)]
pub struct Adl {
total: f64,
has_emitted: bool,
}
impl Adl {
/// Construct a new Accumulation/Distribution Line starting at zero.
pub const fn new() -> Self {
Self {
total: 0.0,
has_emitted: false,
}
}
/// Current cumulative value if at least one candle has been ingested.
pub const fn value(&self) -> Option<f64> {
if self.has_emitted {
Some(self.total)
} else {
None
}
}
}
impl Indicator for Adl {
type Input = Candle;
type Output = f64;
fn update(&mut self, candle: Candle) -> Option<f64> {
let range = candle.high - candle.low;
let mfv = if range == 0.0 {
// A zero-range bar carries no positional information.
0.0
} else {
let mfm = ((candle.close - candle.low) - (candle.high - candle.close)) / range;
mfm * candle.volume
};
self.total += mfv;
self.has_emitted = true;
Some(self.total)
}
fn reset(&mut self) {
self.total = 0.0;
self.has_emitted = false;
}
fn warmup_period(&self) -> usize {
1
}
fn is_ready(&self) -> bool {
self.has_emitted
}
fn name(&self) -> &'static str {
"ADL"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn candle(open: f64, high: f64, low: f64, close: f64, volume: f64, ts: i64) -> Candle {
Candle::new(open, high, low, close, volume, ts).unwrap()
}
#[test]
fn reference_values() {
// bar 1: close at high -> MFM = +1 -> MFV = +100; ADL = 100.
// bar 2: h=12 l=8 c=9 -> MFM = ((9-8)-(12-9))/4 = -0.5 -> MFV = -100;
// ADL = 100 - 100 = 0.
let mut adl = Adl::new();
let out = adl.batch(&[
candle(8.0, 10.0, 8.0, 10.0, 100.0, 0),
candle(10.0, 12.0, 8.0, 9.0, 200.0, 1),
]);
assert_relative_eq!(out[0].unwrap(), 100.0, epsilon = 1e-12);
assert_relative_eq!(out[1].unwrap(), 0.0, epsilon = 1e-12);
}
#[test]
fn emits_from_first_candle() {
let mut adl = Adl::new();
assert_eq!(adl.warmup_period(), 1);
assert!(adl.update(candle(8.0, 10.0, 8.0, 9.0, 50.0, 0)).is_some());
}
/// Cover the Indicator-impl `name` body (94-96). The other accessors
/// are exercised by existing tests; `name` was never queried.
#[test]
fn accessors_and_metadata() {
let adl = Adl::new();
assert_eq!(adl.name(), "ADL");
}
#[test]
fn close_at_high_accumulates_full_volume() {
// Every bar closes at its high: MFM = +1, so ADL grows by `volume`.
let mut adl = Adl::new();
let mut expected = 0.0;
for i in 0..10 {
let c = candle(8.0, 10.0, 8.0, 10.0, 25.0, i);
expected += 25.0;
assert_relative_eq!(adl.update(c).unwrap(), expected, epsilon = 1e-9);
}
}
#[test]
fn zero_range_bar_contributes_nothing() {
let mut adl = Adl::new();
adl.update(candle(8.0, 10.0, 8.0, 10.0, 100.0, 0));
let before = adl.value().unwrap();
// A flat candle (high == low) adds zero.
let after = adl.update(candle(9.0, 9.0, 9.0, 9.0, 999.0, 1)).unwrap();
assert_relative_eq!(after, before, epsilon = 1e-12);
}
#[test]
fn reset_clears_state() {
let mut adl = Adl::new();
adl.batch(&[
candle(8.0, 10.0, 8.0, 9.0, 100.0, 0),
candle(9.0, 11.0, 9.0, 10.0, 100.0, 1),
]);
assert!(adl.is_ready());
adl.reset();
assert!(!adl.is_ready());
assert_eq!(adl.value(), None);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..60)
.map(|i| {
let mid = 100.0 + (i as f64 * 0.3).sin() * 8.0;
candle(
mid,
mid + 2.0,
mid - 2.0,
mid + 0.5,
10.0 + (i % 5) as f64,
i,
)
})
.collect();
let batch = Adl::new().batch(&candles);
let mut b = Adl::new();
let streamed: Vec<_> = candles.iter().map(|c| b.update(*c)).collect();
assert_eq!(batch, streamed);
}
}