diff --git a/crates/wickra-core/src/indicators/cmf.rs b/crates/wickra-core/src/indicators/cmf.rs index 788b052f..042f3076 100644 --- a/crates/wickra-core/src/indicators/cmf.rs +++ b/crates/wickra-core/src/indicators/cmf.rs @@ -214,6 +214,34 @@ mod tests { assert!(matches!(ChaikinMoneyFlow::new(0), Err(Error::PeriodZero))); } + /// Cover the const accessor `period` (71-73) and the Indicator-impl + /// `name` body (124-126). `warmup_period` is covered elsewhere. + #[test] + fn accessors_and_metadata() { + let cmf = ChaikinMoneyFlow::new(20).unwrap(); + assert_eq!(cmf.period(), 20); + assert_eq!(cmf.name(), "CMF"); + } + + /// Cover the `range == 0.0` defensive branch (line 84). All other + /// tests use H != L candles; feed all-flat candles (H == L) so the + /// MFV computation must take the zero-range fallback and emit MFV = 0. + #[test] + fn zero_range_candle_contributes_zero_mfv() { + let mut cmf = ChaikinMoneyFlow::new(3).unwrap(); + let candles: Vec = (0..5) + .map(|i| Candle::new(10.0, 10.0, 10.0, 10.0, 5.0, i).unwrap()) + .collect(); + let last = cmf + .batch(&candles) + .into_iter() + .flatten() + .last() + .expect("emits"); + // Every bar contributed 0 to mfv_sum, so the ratio is 0. + assert_eq!(last, 0.0); + } + #[test] fn reset_clears_state() { let candles: Vec = (0..20) diff --git a/crates/wickra-core/src/indicators/hma.rs b/crates/wickra-core/src/indicators/hma.rs index 3c1acbf3..416fb802 100644 --- a/crates/wickra-core/src/indicators/hma.rs +++ b/crates/wickra-core/src/indicators/hma.rs @@ -128,6 +128,16 @@ mod tests { assert!(Hma::new(0).is_err()); } + /// Cover the const accessor `period` (51-53) and the Indicator-impl + /// `name` body (87-89). `warmup_period` is covered by + /// `first_emission_matches_warmup_period`. + #[test] + fn accessors_and_metadata() { + let hma = Hma::new(9).unwrap(); + assert_eq!(hma.period(), 9); + assert_eq!(hma.name(), "HMA"); + } + #[test] fn first_emission_matches_warmup_period() { let prices: Vec = (1..=40).map(f64::from).collect(); @@ -155,16 +165,16 @@ mod tests { let mut half = Wma::new(4).unwrap(); // (9 / 2).max(1) let mut full = Wma::new(9).unwrap(); let mut smooth = Wma::new(3).unwrap(); // round(sqrt(9)) - for &p in &prices { + for (i, &p) in prices.iter().enumerate() { let got = hma.update(p); let want = match (half.update(p), full.update(p)) { (Some(h), Some(f)) => smooth.update(2.0 * h - f), _ => None, }; - match (got, want) { - (None, None) => {} - (Some(a), Some(b)) => assert_relative_eq!(a, b, epsilon = 1e-9), - _ => panic!("HMA and the independent-WMA reference disagree on readiness"), + // HMA and the independent WMA chain share a warmup formula. + assert_eq!(got.is_some(), want.is_some(), "readiness mismatch at {i}"); + if let (Some(a), Some(b)) = (got, want) { + assert_relative_eq!(a, b, epsilon = 1e-9); } } } diff --git a/crates/wickra-core/src/indicators/mfi.rs b/crates/wickra-core/src/indicators/mfi.rs index 0b70092d..492f5b2d 100644 --- a/crates/wickra-core/src/indicators/mfi.rs +++ b/crates/wickra-core/src/indicators/mfi.rs @@ -181,6 +181,34 @@ mod tests { assert!(!mfi.is_ready()); } + /// Cover the const accessor `period` (58-60) and the Indicator-impl + /// `name` body (132-134). `warmup_period` is already covered elsewhere. + #[test] + fn accessors_and_metadata() { + let mfi = Mfi::new(14).unwrap(); + assert_eq!(mfi.period(), 14); + assert_eq!(mfi.name(), "MFI"); + } + + /// Cover the `tp == prev` arm (line 85) — when typical price equals + /// the previous typical price, both flows are 0 — and the all-zero- + /// flow fallback `Some(50.0)` (line 105). Existing tests use varying + /// candles so the flat-TP arm and the zero-flow fallback never fired. + #[test] + fn flat_typical_prices_default_to_50() { + let mut mfi = Mfi::new(3).unwrap(); + let candles: Vec = (0..6) + .map(|i| Candle::new(10.0, 10.0, 10.0, 10.0, 1.0, i).unwrap()) + .collect(); + let last = mfi + .batch(&candles) + .into_iter() + .flatten() + .last() + .expect("emits"); + assert_eq!(last, 50.0); + } + #[test] fn rejects_zero_period() { assert!(Mfi::new(0).is_err()); diff --git a/crates/wickra-core/src/indicators/obv.rs b/crates/wickra-core/src/indicators/obv.rs index 5bc6af5a..df0986cc 100644 --- a/crates/wickra-core/src/indicators/obv.rs +++ b/crates/wickra-core/src/indicators/obv.rs @@ -99,6 +99,21 @@ mod tests { Candle::new(close, close, close, close, volume, 0).unwrap() } + /// Cover the `value()` Some branch (line 47) and the Indicator-impl + /// `warmup_period` (79-81) + `name` (87-89). `reset_clears_state` + /// hits only the None branch of `value()`; the metadata methods were + /// never queried. + #[test] + fn accessors_and_metadata() { + let mut obv = Obv::new(); + assert_eq!(obv.warmup_period(), 1); + assert_eq!(obv.name(), "OBV"); + assert_eq!(obv.value(), None); + obv.update(c(10.0, 100.0)); + // Baseline 0 — value() Some branch. + assert_eq!(obv.value(), Some(0.0)); + } + #[test] fn first_candle_baseline_zero() { let mut obv = Obv::new(); diff --git a/crates/wickra-core/src/indicators/psar.rs b/crates/wickra-core/src/indicators/psar.rs index 954d4318..ca5f7dfb 100644 --- a/crates/wickra-core/src/indicators/psar.rs +++ b/crates/wickra-core/src/indicators/psar.rs @@ -246,15 +246,21 @@ mod tests { }) .collect(); let mut psar = Psar::classic(); - for (i, sar) in psar.batch(&candles).into_iter().enumerate() { - if let Some(s) = sar { - assert!( - s <= candles[i].low + 1e-9, - "SAR {s} should be <= low {} at i={i}", - candles[i].low - ); - } - } + let violations: Vec<(usize, f64, f64)> = psar + .batch(&candles) + .into_iter() + .enumerate() + .filter_map(|(i, sar)| { + sar.and_then(|s| { + if s > candles[i].low + 1e-9 { + Some((i, s, candles[i].low)) + } else { + None + } + }) + }) + .collect(); + assert!(violations.is_empty(), "SAR above low: {violations:?}"); } #[test] @@ -267,13 +273,23 @@ mod tests { }) .collect(); let mut psar = Psar::classic(); - let outs = psar.batch(&candles); // After the trend establishes downward, SAR should sit above highs. - for (i, sar) in outs.into_iter().enumerate().skip(5) { - if let Some(s) = sar { - assert!(s >= candles[i].high - 1e-9); - } - } + let violations: Vec<(usize, f64, f64)> = psar + .batch(&candles) + .into_iter() + .enumerate() + .skip(5) + .filter_map(|(i, sar)| { + sar.and_then(|s| { + if s < candles[i].high - 1e-9 { + Some((i, s, candles[i].high)) + } else { + None + } + }) + }) + .collect(); + assert!(violations.is_empty(), "SAR below high: {violations:?}"); } #[test] @@ -292,6 +308,16 @@ mod tests { ); } + /// Cover the Indicator-impl `warmup_period` (206-208) and `name` + /// (220-222). PSAR's warmup is the constant 2 (seed candle + first + /// emitting candle); the name is the literal "PSAR". + #[test] + fn accessors_and_metadata() { + let psar = Psar::classic(); + assert_eq!(psar.warmup_period(), 2); + assert_eq!(psar.name(), "PSAR"); + } + #[test] fn rejects_invalid_params() { assert!(Psar::new(0.0, 0.02, 0.20).is_err());