Skip to main content

indicators/
functions.rs

1//! Standalone batch indicator functions and incremental structs.
2//!
3//! Ported from the original `indicators` crate lib. These work on slices
4//! (batch mode) or as incremental O(1)-per-tick structs.
5//!
6//! # Incremental warm-up contract
7//!
8//! Every `Incremental*` struct has the same `update` shape: it returns
9//! `Option<T>`, where `None` means "no value is defined yet". Structs whose
10//! maths seeds from the first tick (EMA, ATR, MACD) return `Some` from the
11//! first call; the others return `None` until their warm-up completes:
12//!
13//! | Struct | `update` returns | First `Some` |
14//! |---|---|---|
15//! | [`IncrementalEma`] | `Option<f64>` | first tick (seeds from it) |
16//! | [`IncrementalAtr`] | `Option<f64>` | first tick (TR = high − low) |
17//! | [`IncrementalRsi`] | `Option<f64>` | second tick (needs a prior price) |
18//! | [`IncrementalMacd`] | `Option<(f64, f64, f64)>` | first tick (all EMAs seed from it) |
19//! | [`IncrementalBollinger`] | `Option<BollingerBandsValue>` | after `period` ticks |
20//! | [`EMA`] / [`ATR`] | `()` — read via `value()` / `is_ready()` | after `period` ticks (SMA seed) |
21//!
22//! Early EMA/MACD values are mathematically defined but still converging
23//! toward the batch equivalents (which warm up with an SMA seed or leading
24//! NaN); gate on your own bar count if you need fully-converged values.
25//!
26//! None of these structs validate their inputs: feeding NaN poisons the
27//! internal state (NaN propagates through every subsequent value) without
28//! panicking. Filter non-finite ticks upstream.
29
30use std::collections::VecDeque;
31
32use crate::error::IndicatorError;
33use crate::types::MacdResult;
34
35// ── Batch functions ───────────────────────────────────────────────────────────
36
37/// Exponential Moving Average over a price slice.
38/// Returns a Vec of the same length; leading values are `NaN` until warm-up.
39pub fn ema(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
40    if period == 0 {
41        return Err(IndicatorError::InvalidParameter {
42            name: "period".into(),
43            value: 0.0,
44        });
45    }
46    if prices.len() < period {
47        return Err(IndicatorError::InsufficientData {
48            required: period,
49            available: prices.len(),
50        });
51    }
52    let mut result = vec![f64::NAN; prices.len()];
53    let alpha = 2.0 / (period as f64 + 1.0);
54    let first_sma: f64 = prices.iter().take(period).sum::<f64>() / period as f64;
55    result[period - 1] = first_sma;
56    for i in period..prices.len() {
57        result[i] = prices[i] * alpha + result[i - 1] * (1.0 - alpha);
58    }
59    Ok(result)
60}
61
62/// EMA that handles leading NaN values, matching Python's `ewm(adjust=False)` behaviour.
63///
64/// Unlike [`ema`], which seeds from the arithmetic mean of the first `period`
65/// values, this function seeds from the **first non-NaN value** and applies
66/// the recursive formula from that point on.  All positions before the first
67/// non-NaN value are left as `NaN`.
68///
69/// This is needed wherever EMA is applied to a derived series (e.g. the MACD
70/// line) that already has a leading NaN warm-up period.  Using the standard
71/// [`ema`] on such a series would propagate NaN through the SMA seed and
72/// produce an all-NaN output.
73pub fn ema_nan_aware(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
74    if period == 0 {
75        return Err(IndicatorError::InvalidParameter {
76            name: "period".into(),
77            value: 0.0,
78        });
79    }
80    let mut result = vec![f64::NAN; prices.len()];
81    let alpha = 2.0 / (period as f64 + 1.0);
82
83    // Seed from the first non-NaN value (adjust=False, no SMA warm-up).
84    let Some(start) = prices.iter().position(|v| !v.is_nan()) else {
85        return Ok(result); // all NaN — nothing to compute
86    };
87
88    result[start] = prices[start];
89    for i in (start + 1)..prices.len() {
90        result[i] = if prices[i].is_nan() {
91            f64::NAN
92        } else {
93            prices[i] * alpha + result[i - 1] * (1.0 - alpha)
94        };
95    }
96    Ok(result)
97}
98
99/// Simple Moving Average over a price slice.
100pub fn sma(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
101    if period == 0 {
102        return Err(IndicatorError::InvalidParameter {
103            name: "period".into(),
104            value: 0.0,
105        });
106    }
107    if prices.len() < period {
108        return Err(IndicatorError::InsufficientData {
109            required: period,
110            available: prices.len(),
111        });
112    }
113    let mut result = vec![f64::NAN; prices.len()];
114    for i in (period - 1)..prices.len() {
115        let sum: f64 = prices[(i + 1 - period)..=i].iter().sum();
116        result[i] = sum / period as f64;
117    }
118    Ok(result)
119}
120
121/// True Range = max(H-L, |H-prevC|, |L-prevC|).
122pub fn true_range(high: &[f64], low: &[f64], close: &[f64]) -> Result<Vec<f64>, IndicatorError> {
123    if high.len() != low.len() || high.len() != close.len() {
124        return Err(IndicatorError::InsufficientData {
125            required: high.len(),
126            available: low.len().min(close.len()),
127        });
128    }
129    let mut result = vec![f64::NAN; high.len()];
130    if !high.is_empty() {
131        result[0] = high[0] - low[0];
132    }
133    for i in 1..high.len() {
134        let tr1 = high[i] - low[i];
135        let tr2 = (high[i] - close[i - 1]).abs();
136        let tr3 = (low[i] - close[i - 1]).abs();
137        result[i] = tr1.max(tr2).max(tr3);
138    }
139    Ok(result)
140}
141
142/// Average True Range (EMA-smoothed).
143///
144/// Note: smoothing is a plain EMA (`span = period`, Python-parity with
145/// `tr.ewm(...).mean()`), **not** Wilder's RMA (`alpha = 1/period`). The two
146/// differ by their decay constant, so values will not match a Wilder-style ATR.
147pub fn atr(
148    high: &[f64],
149    low: &[f64],
150    close: &[f64],
151    period: usize,
152) -> Result<Vec<f64>, IndicatorError> {
153    let tr = true_range(high, low, close)?;
154    ema(&tr, period)
155}
156
157/// Relative Strength Index.
158pub fn rsi(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
159    if prices.len() < period + 1 {
160        return Err(IndicatorError::InsufficientData {
161            required: period + 1,
162            available: prices.len(),
163        });
164    }
165    let mut result = vec![f64::NAN; prices.len()];
166    let mut gains = vec![0.0; prices.len()];
167    let mut losses = vec![0.0; prices.len()];
168    for i in 1..prices.len() {
169        let change = prices[i] - prices[i - 1];
170        if change > 0.0 {
171            gains[i] = change;
172        } else {
173            losses[i] = -change;
174        }
175    }
176    let avg_gains = ema(&gains, period)?;
177    let avg_losses = ema(&losses, period)?;
178    for i in period..prices.len() {
179        if avg_losses[i] == 0.0 {
180            result[i] = 100.0;
181        } else {
182            let rs = avg_gains[i] / avg_losses[i];
183            result[i] = 100.0 - (100.0 / (1.0 + rs));
184        }
185    }
186    Ok(result)
187}
188
189/// MACD — returns (macd_line, signal_line, histogram).
190pub fn macd(
191    prices: &[f64],
192    fast_period: usize,
193    slow_period: usize,
194    signal_period: usize,
195) -> MacdResult {
196    // Use ema_nan_aware to match Python's ewm(span=X, adjust=False), which
197    // seeds from the first value rather than an SMA of the first `period` bars.
198    let fast_ema = ema_nan_aware(prices, fast_period)?;
199    let slow_ema = ema_nan_aware(prices, slow_period)?;
200    let mut macd_line = vec![f64::NAN; prices.len()];
201    for i in 0..prices.len() {
202        if !fast_ema[i].is_nan() && !slow_ema[i].is_nan() {
203            macd_line[i] = fast_ema[i] - slow_ema[i];
204        }
205    }
206    // The macd_line has leading NaN (warm-up from the slow EMA); use the
207    // NaN-aware variant so the signal seeds from the first valid MACD value
208    // rather than an all-NaN SMA, matching Python's ewm(adjust=False).
209    let signal_line = ema_nan_aware(&macd_line, signal_period)?;
210    let mut histogram = vec![f64::NAN; prices.len()];
211    for i in 0..prices.len() {
212        if !macd_line[i].is_nan() && !signal_line[i].is_nan() {
213            histogram[i] = macd_line[i] - signal_line[i];
214        }
215    }
216    Ok((macd_line, signal_line, histogram))
217}
218
219// ── Incremental structs ───────────────────────────────────────────────────────
220
221/// Incremental EMA — O(1) update, SMA warm-up.
222///
223/// Unlike the batch [`ema`] function (which initialises from an SMA over the
224/// first `period` prices), this struct emits its first value *after* it has
225/// accumulated exactly `period` samples and seeds itself from their average.
226/// Both approaches are correct; this one is more natural for streaming use.
227#[derive(Debug, Clone)]
228pub struct EMA {
229    period: usize,
230    alpha: f64,
231    value: f64,
232    initialized: bool,
233    warmup: VecDeque<f64>,
234}
235
236impl EMA {
237    pub fn new(period: usize) -> Self {
238        Self {
239            period,
240            alpha: 2.0 / (period as f64 + 1.0),
241            value: 0.0,
242            initialized: false,
243            warmup: VecDeque::with_capacity(period),
244        }
245    }
246
247    pub fn update(&mut self, price: f64) {
248        if !self.initialized {
249            self.warmup.push_back(price);
250            if self.warmup.len() >= self.period {
251                self.value = self.warmup.iter().sum::<f64>() / self.period as f64;
252                self.initialized = true;
253                self.warmup.clear();
254            }
255        } else {
256            self.value = price * self.alpha + self.value * (1.0 - self.alpha);
257        }
258    }
259
260    pub fn value(&self) -> f64 {
261        if self.initialized {
262            self.value
263        } else {
264            f64::NAN
265        }
266    }
267
268    pub fn is_ready(&self) -> bool {
269        self.initialized
270    }
271
272    pub fn reset(&mut self) {
273        self.value = 0.0;
274        self.initialized = false;
275        self.warmup.clear();
276    }
277}
278
279/// Incremental Wilder ATR.
280#[derive(Debug, Clone)]
281pub struct ATR {
282    #[allow(dead_code)]
283    period: usize,
284    ema: EMA,
285    prev_close: Option<f64>,
286}
287
288impl ATR {
289    pub fn new(period: usize) -> Self {
290        Self {
291            period,
292            ema: EMA::new(period),
293            prev_close: None,
294        }
295    }
296
297    pub fn update(&mut self, high: f64, low: f64, close: f64) {
298        let tr = if let Some(prev) = self.prev_close {
299            (high - low)
300                .max((high - prev).abs())
301                .max((low - prev).abs())
302        } else {
303            high - low
304        };
305        self.ema.update(tr);
306        self.prev_close = Some(close);
307    }
308
309    pub fn value(&self) -> f64 {
310        self.ema.value()
311    }
312
313    pub fn is_ready(&self) -> bool {
314        self.ema.is_ready()
315    }
316}
317
318/// Bundle of per-strategy indicator series.
319#[derive(Debug, Clone)]
320pub struct StrategyIndicators {
321    pub ema_fast: Vec<f64>,
322    pub ema_slow: Vec<f64>,
323    pub atr: Vec<f64>,
324}
325
326/// Multi-period indicator calculator (batch mode).
327#[derive(Debug, Clone)]
328pub struct IndicatorCalculator {
329    pub fast_ema_period: usize,
330    pub slow_ema_period: usize,
331    pub atr_period: usize,
332}
333
334impl Default for IndicatorCalculator {
335    fn default() -> Self {
336        Self {
337            fast_ema_period: 8,
338            slow_ema_period: 21,
339            atr_period: 14,
340        }
341    }
342}
343
344impl IndicatorCalculator {
345    pub fn new(fast_ema: usize, slow_ema: usize, atr_period: usize) -> Self {
346        Self {
347            fast_ema_period: fast_ema,
348            slow_ema_period: slow_ema,
349            atr_period,
350        }
351    }
352
353    pub fn calculate_all(
354        &self,
355        close: &[f64],
356        high: &[f64],
357        low: &[f64],
358    ) -> Result<StrategyIndicators, IndicatorError> {
359        Ok(StrategyIndicators {
360            ema_fast: ema(close, self.fast_ema_period)?,
361            ema_slow: ema(close, self.slow_ema_period)?,
362            atr: atr(high, low, close, self.atr_period)?,
363        })
364    }
365}
366
367/// Incremental EMA — O(1) update per tick that returns the new value each call.
368///
369/// Unlike [`EMA`] (which separates `update` from `value`/`is_ready`), this
370/// seeds from the first sample and returns the EMA on every `update`, which
371/// suits streaming pipelines that consume the value inline.
372#[derive(Debug, Clone)]
373pub struct IncrementalEma {
374    alpha: f64,
375    state: f64,
376    initialized: bool,
377}
378
379impl IncrementalEma {
380    /// Create an incremental EMA for the given period.
381    pub fn new(period: usize) -> Self {
382        Self {
383            alpha: 2.0 / (period as f64 + 1.0),
384            state: 0.0,
385            initialized: false,
386        }
387    }
388
389    /// Feed the next price; returns the updated EMA. Always `Some` — the EMA
390    /// seeds from the first price — but `Option`-shaped to match the warm-up
391    /// contract shared by all incremental structs.
392    pub fn update(&mut self, price: f64) -> Option<f64> {
393        Some(self.step(price))
394    }
395
396    /// Internal non-optional update for composing structs (RSI, MACD, ATR).
397    fn step(&mut self, price: f64) -> f64 {
398        if !self.initialized {
399            self.state = price;
400            self.initialized = true;
401        } else {
402            self.state = self.alpha * price + (1.0 - self.alpha) * self.state;
403        }
404        self.state
405    }
406
407    /// Current EMA value, or `None` before the first `update`.
408    pub fn current(&self) -> Option<f64> {
409        if self.initialized {
410            Some(self.state)
411        } else {
412            None
413        }
414    }
415}
416
417/// Incremental ATR — O(1) per-tick true-range EMA.
418///
419/// Wraps an [`IncrementalEma`] over the true range and returns the smoothed
420/// ATR on each `update`. The first sample's true range is `high - low`.
421pub struct IncrementalAtr {
422    ema: IncrementalEma,
423    prev_close: Option<f64>,
424}
425
426impl IncrementalAtr {
427    /// Create an incremental ATR for the given period.
428    pub fn new(period: usize) -> Self {
429        Self {
430            ema: IncrementalEma::new(period),
431            prev_close: None,
432        }
433    }
434
435    /// Feed the next high/low/close; returns the updated ATR.
436    pub fn update(&mut self, high: f64, low: f64, close: f64) -> Option<f64> {
437        let tr = if let Some(prev) = self.prev_close {
438            let tr1 = high - low;
439            let tr2 = (high - prev).abs();
440            let tr3 = (low - prev).abs();
441            tr1.max(tr2).max(tr3)
442        } else {
443            high - low
444        };
445
446        self.prev_close = Some(close);
447        Some(self.ema.step(tr))
448    }
449}
450
451/// Incremental RSI — O(1) per-tick, EMA-smoothed gains/losses.
452///
453/// Mirrors the batch [`rsi`]: the average gain and loss are tracked with an
454/// [`IncrementalEma`] of the given period and combined as
455/// `100 - 100 / (1 + avg_gain / avg_loss)` (RSI is 100 when the average loss is
456/// zero). Like the other incremental structs it seeds from the first sample
457/// (vs. the batch SMA warm-up), so warm-up values differ slightly from [`rsi`]
458/// but converge — both are valid.
459#[derive(Debug, Clone)]
460pub struct IncrementalRsi {
461    gain_ema: IncrementalEma,
462    loss_ema: IncrementalEma,
463    prev_price: Option<f64>,
464}
465
466impl IncrementalRsi {
467    /// Create an incremental RSI for the given period.
468    pub fn new(period: usize) -> Self {
469        Self {
470            gain_ema: IncrementalEma::new(period),
471            loss_ema: IncrementalEma::new(period),
472            prev_price: None,
473        }
474    }
475
476    /// Feed the next price; returns the updated RSI, or `None` for the very
477    /// first sample (RSI needs a prior price to form the first change).
478    pub fn update(&mut self, price: f64) -> Option<f64> {
479        let prev = self.prev_price.replace(price)?;
480        let change = price - prev;
481        let (gain, loss) = if change > 0.0 {
482            (change, 0.0)
483        } else {
484            (0.0, -change)
485        };
486        let avg_gain = self.gain_ema.step(gain);
487        let avg_loss = self.loss_ema.step(loss);
488        let rsi = if avg_loss == 0.0 {
489            100.0
490        } else {
491            let rs = avg_gain / avg_loss;
492            100.0 - 100.0 / (1.0 + rs)
493        };
494        Some(rsi)
495    }
496}
497
498/// Incremental MACD — O(1) per-tick MACD line, signal, and histogram.
499///
500/// Mirrors the batch [`macd`]: a fast and slow [`IncrementalEma`] give the MACD
501/// line (`fast - slow`), a third EMA over that line is the signal, and the
502/// histogram is `macd - signal`. The EMAs seed from the first sample (matching
503/// the batch's `adjust=false` / first-value seeding via `ema_nan_aware`), so
504/// values are emitted from the first tick rather than after a NaN warm-up.
505#[derive(Debug, Clone)]
506pub struct IncrementalMacd {
507    fast: IncrementalEma,
508    slow: IncrementalEma,
509    signal: IncrementalEma,
510}
511
512impl IncrementalMacd {
513    /// Create an incremental MACD from the fast, slow, and signal periods.
514    pub fn new(fast_period: usize, slow_period: usize, signal_period: usize) -> Self {
515        Self {
516            fast: IncrementalEma::new(fast_period),
517            slow: IncrementalEma::new(slow_period),
518            signal: IncrementalEma::new(signal_period),
519        }
520    }
521
522    /// Feed the next price; returns `(macd, signal, histogram)`. Always `Some`
523    /// — every EMA seeds from the first price — but `Option`-shaped to match
524    /// the warm-up contract shared by all incremental structs.
525    pub fn update(&mut self, price: f64) -> Option<(f64, f64, f64)> {
526        let macd = self.fast.step(price) - self.slow.step(price);
527        let signal = self.signal.step(macd);
528        Some((macd, signal, macd - signal))
529    }
530}
531
532/// Per-tick Bollinger Bands output (see [`IncrementalBollinger`]).
533#[derive(Debug, Clone, Copy, PartialEq)]
534pub struct BollingerBandsValue {
535    /// Middle band — the `period`-SMA.
536    pub middle: f64,
537    /// Upper band — `middle + std_mult * std`.
538    pub upper: f64,
539    /// Lower band — `middle - std_mult * std`.
540    pub lower: f64,
541    /// `(upper - lower) / middle`, or `NaN` when `middle` is 0.
542    pub bandwidth: f64,
543    /// `%b` — `(price - lower) / (upper - lower)`, or `NaN` for a zero-width band.
544    pub percent_b: f64,
545}
546
547/// Incremental Bollinger Bands — per-tick bands over a rolling window.
548///
549/// Mirrors the batch [`BollingerBands`](crate::volatility::BollingerBands):
550/// `middle` is the `period`-SMA, the deviation is the **sample** standard
551/// deviation (ddof = 1) over the window, and `upper`/`lower` are
552/// `middle ± std_mult * std` (2.0 is the common multiplier). Emits `None` until
553/// `period` samples are buffered. Unlike the EMA-based incremental structs this
554/// keeps a `period`-length window, so each `update` is O(period), not O(1).
555#[derive(Debug, Clone)]
556pub struct IncrementalBollinger {
557    window: VecDeque<f64>,
558    period: usize,
559    std_mult: f64,
560}
561
562impl IncrementalBollinger {
563    /// Create incremental Bollinger Bands for the given period and band
564    /// multiplier (`std_mult`, conventionally 2.0).
565    pub fn new(period: usize, std_mult: f64) -> Self {
566        Self {
567            window: VecDeque::with_capacity(period.max(1)),
568            period,
569            std_mult,
570        }
571    }
572
573    /// Feed the next price; returns the bands once `period` samples are buffered
574    /// (and `period >= 2`, so the sample stddev is defined).
575    pub fn update(&mut self, price: f64) -> Option<BollingerBandsValue> {
576        self.window.push_back(price);
577        if self.window.len() > self.period {
578            self.window.pop_front();
579        }
580        if self.window.len() < self.period || self.period < 2 {
581            return None;
582        }
583
584        let mean: f64 = self.window.iter().sum::<f64>() / self.period as f64;
585        // Sample variance (ddof = 1), matching the batch `rolling_std`.
586        let var: f64 =
587            self.window.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / (self.period - 1) as f64;
588        let std = var.sqrt();
589
590        let upper = mean + self.std_mult * std;
591        let lower = mean - self.std_mult * std;
592        let bandwidth = if mean == 0.0 {
593            f64::NAN
594        } else {
595            (upper - lower) / mean
596        };
597        let band_range = upper - lower;
598        let percent_b = if band_range == 0.0 {
599            f64::NAN
600        } else {
601            (price - lower) / band_range
602        };
603
604        Some(BollingerBandsValue {
605            middle: mean,
606            upper,
607            lower,
608            bandwidth,
609            percent_b,
610        })
611    }
612}
613
614#[cfg(test)]
615mod tests {
616    use super::*;
617
618    #[test]
619    fn test_incremental_bollinger_warmup_then_value() {
620        let mut bb = IncrementalBollinger::new(5, 2.0);
621        for p in [10.0, 11.0, 12.0, 13.0] {
622            assert!(bb.update(p).is_none(), "no value before `period` samples");
623        }
624        assert!(bb.update(14.0).is_some(), "value once the window is full");
625    }
626
627    #[test]
628    fn test_incremental_bollinger_constant_prices_zero_width() {
629        let mut bb = IncrementalBollinger::new(4, 2.0);
630        let mut last = None;
631        for _ in 0..4 {
632            last = bb.update(10.0);
633        }
634        let v = last.unwrap();
635        assert!((v.middle - 10.0).abs() < 1e-12);
636        assert!((v.upper - 10.0).abs() < 1e-12);
637        assert!((v.lower - 10.0).abs() < 1e-12);
638        assert!((v.bandwidth - 0.0).abs() < 1e-12);
639        assert!(v.percent_b.is_nan(), "zero-width band → %b undefined");
640    }
641
642    #[test]
643    fn test_incremental_bollinger_matches_sample_stddev() {
644        // window [2,4,4,4,5,5,7,9]: mean 5, sample variance 32/7, std ≈ 2.138.
645        let mut bb = IncrementalBollinger::new(8, 2.0);
646        let mut last = None;
647        for p in [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0] {
648            last = bb.update(p);
649        }
650        let v = last.unwrap();
651        let std = (32.0_f64 / 7.0).sqrt();
652        assert!((v.middle - 5.0).abs() < 1e-9);
653        assert!((v.upper - (5.0 + 2.0 * std)).abs() < 1e-9);
654        assert!((v.lower - (5.0 - 2.0 * std)).abs() < 1e-9);
655    }
656
657    #[test]
658    fn test_incremental_rsi_first_sample_is_none() {
659        let mut rsi = IncrementalRsi::new(14);
660        assert_eq!(rsi.update(10.0), None);
661        assert!(rsi.update(11.0).is_some());
662    }
663
664    #[test]
665    fn test_incremental_rsi_all_gains_saturates_at_100() {
666        let mut rsi = IncrementalRsi::new(14);
667        let mut last = None;
668        for p in [10.0, 11.0, 12.0, 13.0, 14.0, 15.0] {
669            last = rsi.update(p);
670        }
671        // Monotonically rising → average loss is zero → RSI saturates at 100.
672        assert!((last.unwrap() - 100.0).abs() < 1e-9);
673    }
674
675    #[test]
676    fn test_incremental_rsi_stays_in_bounds() {
677        let mut rsi = IncrementalRsi::new(5);
678        let prices = [44.0, 44.3, 44.1, 44.2, 43.6, 44.3, 44.8, 45.0, 44.7, 44.9];
679        let mut produced = 0;
680        for p in prices {
681            if let Some(v) = rsi.update(p) {
682                assert!((0.0..=100.0).contains(&v), "RSI out of bounds: {v}");
683                produced += 1;
684            }
685        }
686        assert_eq!(produced, prices.len() - 1);
687    }
688
689    #[test]
690    fn test_incremental_macd_composes_like_batch() {
691        let mut m = IncrementalMacd::new(12, 26, 9);
692        let mut fast = IncrementalEma::new(12);
693        let mut slow = IncrementalEma::new(26);
694        let mut sig = IncrementalEma::new(9);
695        for p in [10.0, 11.0, 10.5, 12.0, 13.0, 12.5, 11.0, 11.5] {
696            let (macd, signal, hist) = m.update(p).unwrap();
697            let expect_macd = fast.update(p).unwrap() - slow.update(p).unwrap();
698            let expect_sig = sig.update(expect_macd).unwrap();
699            assert!((macd - expect_macd).abs() < 1e-12);
700            assert!((signal - expect_sig).abs() < 1e-12);
701            assert!((hist - (expect_macd - expect_sig)).abs() < 1e-12);
702        }
703    }
704
705    #[test]
706    fn test_ema_sma_seed() {
707        let prices = vec![22.27, 22.19, 22.08, 22.17, 22.18];
708        let result = ema(&prices, 5).unwrap();
709        let expected = (22.27 + 22.19 + 22.08 + 22.17 + 22.18) / 5.0;
710        assert!((result[4] - expected).abs() < 1e-9);
711    }
712
713    #[test]
714    fn test_true_range_first() {
715        let h = vec![50.0, 52.0];
716        let l = vec![48.0, 49.0];
717        let c = vec![49.0, 51.0];
718        let tr = true_range(&h, &l, &c).unwrap();
719        assert_eq!(tr[0], 2.0);
720        assert_eq!(tr[1], 3.0);
721    }
722
723    #[test]
724    fn test_ema_incremental() {
725        let mut e = EMA::new(3);
726        e.update(10.0);
727        assert!(!e.is_ready());
728        e.update(20.0);
729        assert!(!e.is_ready());
730        e.update(30.0);
731        assert!(e.is_ready());
732        assert!((e.value() - 20.0).abs() < 1e-9);
733    }
734
735    #[test]
736    fn test_incremental_ema_returns_value() {
737        let mut e = IncrementalEma::new(3); // alpha = 0.5
738        assert_eq!(e.current(), None);
739        assert_eq!(e.update(10.0), Some(10.0)); // seeds from first sample
740        assert_eq!(e.current(), Some(10.0));
741        let v = e.update(20.0).unwrap(); // 0.5*20 + 0.5*10
742        assert!((v - 15.0).abs() < 1e-9);
743    }
744
745    #[test]
746    fn test_incremental_atr_first_is_range() {
747        let mut a = IncrementalAtr::new(3);
748        // First sample: TR = high - low, EMA seeds to it.
749        assert_eq!(a.update(12.0, 10.0, 11.0), Some(2.0));
750    }
751}