fin-primitives 2.14.1

Checked building blocks for Rust trading code: exact decimal price and quantity types, a level-2 order book, ticks to OHLCV candles, 700+ streaming indicators, Black-Scholes Greeks, a position ledger and risk limits.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
//! # Signal Warmup Contracts
//!
//! Formalises the warmup period that every indicator implicitly has, making it
//! queryable, enforceable at the type level, and reportable across a full pipeline.
//!
//! ## Overview
//!
//! Every signal in `fin-primitives` returns [`SignalValue::Unavailable`] until it has
//! accumulated enough bars to produce a meaningful value. Before this module, that
//! behaviour was implicit — callers had to inspect each value and handle `Unavailable`
//! ad-hoc. The warmup contract system makes it explicit and composable:
//!
//! | Type | Purpose |
//! |------|---------|
//! | [`WarmupContract`] | Trait: query warmup period, readiness, and remaining bars |
//! | [`WarmupGuard`] | Wrapper: converts `Unavailable` into a typed `Err(NotReady)` |
//! | [`WarmupReport`] | Report: snapshot of all signal warmup states in a pipeline |
//! | [`SignalWarmupStatus`] | Per-signal status entry within a `WarmupReport` |
//!
//! ## Example
//!
//! ```rust
//! use fin_primitives::signals::indicators::Sma;
//! use fin_primitives::signals::{BarInput, Signal};
//! use fin_primitives::signals::warmup::{WarmupContract, WarmupGuard};
//! use rust_decimal_macros::dec;
//!
//! let sma = Sma::new("sma5", 5).unwrap();
//! let mut guard = WarmupGuard::new(sma);
//!
//! // Feed 4 bars — guard returns Err(NotReady)
//! for _ in 0..4 {
//!     let bar = BarInput::from_close(dec!(100));
//!     let result = guard.update_checked(&bar);
//!     assert!(result.is_err());
//! }
//!
//! // 5th bar — guard returns Ok(SignalValue::Scalar(...))
//! let bar = BarInput::from_close(dec!(100));
//! let result = guard.update_checked(&bar);
//! assert!(result.is_ok());
//! ```

use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};

// ── WarmupContract ────────────────────────────────────────────────────────────

/// A formal contract for inspecting a signal's warmup state.
///
/// Every signal that implements [`Signal`] has an implicit warmup period; this
/// trait exposes that information as a structured, queryable contract. Implement
/// this trait alongside `Signal` to participate in [`WarmupReport`] generation.
pub trait WarmupContract {
    /// Returns the total number of bars required before the signal is ready.
    ///
    /// This is a static property of the indicator: it does not change during
    /// the signal's lifetime. Corresponds to the indicator's `period()`.
    fn warmup_period(&self) -> usize;

    /// Returns `true` if the signal has accumulated enough bars to produce a value.
    ///
    /// After this returns `true`, [`Signal::update`] will no longer return
    /// `SignalValue::Unavailable` due to insufficient data.
    fn is_ready(&self) -> bool;

    /// Returns the number of bars still needed before the signal becomes ready.
    ///
    /// Returns `0` if the signal is already ready.
    fn bars_remaining(&self) -> usize;
}

/// Blanket implementation of [`WarmupContract`] for any type that implements [`Signal`].
///
/// This allows all built-in indicators (`Sma`, `Ema`, `Rsi`, …) to be used with
/// [`WarmupGuard`] and [`WarmupReport`] without additional boilerplate.
///
/// Note: because [`Signal`] already exposes `is_ready()` and `period()`, the blanket
/// impl delegates via fully-qualified syntax to avoid ambiguity.
impl<S: Signal> WarmupContract for S {
    fn warmup_period(&self) -> usize {
        // Fully-qualified call to Signal::period to avoid name collision.
        <S as Signal>::period(self)
    }

    fn is_ready(&self) -> bool {
        // Fully-qualified call to Signal::is_ready to avoid name collision.
        <S as Signal>::is_ready(self)
    }

    fn bars_remaining(&self) -> usize {
        if <S as Signal>::is_ready(self) {
            0
        } else {
            // `period()` is the total warmup needed. Since we cannot observe the
            // internal bar count through the Signal trait alone, we return the full
            // period as an upper bound. `WarmupGuard` tracks `bars_seen` independently.
            <S as Signal>::period(self)
        }
    }
}

// ── NotReady error ────────────────────────────────────────────────────────────

/// Error returned by [`WarmupGuard`] when a signal update is requested before warmup completes.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[error("Signal '{name}' not ready: {bars_remaining} bars still needed (period = {warmup_period})")]
pub struct NotReady {
    /// Name of the signal that is not yet ready.
    pub name: String,
    /// Total bars required before the signal becomes ready.
    pub warmup_period: usize,
    /// Bars still needed.
    pub bars_remaining: usize,
}

// ── WarmupGuard ───────────────────────────────────────────────────────────────

/// Wraps any [`Signal`] and converts `SignalValue::Unavailable` into a typed error.
///
/// Without a guard, callers must match on `SignalValue::Unavailable` manually at
/// every call site. `WarmupGuard` makes the warmup contract explicit and enforceable:
/// any call to [`WarmupGuard::update_checked`] before warmup completes returns
/// `Err(NotReady { … })` rather than silently emitting an incomplete value.
///
/// The inner signal is still updated on every call, so the guard does not delay
/// warmup — it just fails loudly until the signal is ready.
///
/// # Example
///
/// ```rust
/// use fin_primitives::signals::indicators::Ema;
/// use fin_primitives::signals::{BarInput, Signal};
/// use fin_primitives::signals::warmup::WarmupGuard;
/// use rust_decimal_macros::dec;
///
/// let ema = Ema::new("ema3", 3).unwrap();
/// let mut guard = WarmupGuard::new(ema);
///
/// let bar = BarInput::from_close(dec!(50));
/// assert!(guard.update_checked(&bar).is_err()); // not ready yet
/// ```
pub struct WarmupGuard<S: Signal> {
    inner: S,
    /// Number of bars fed into the inner signal since construction or last reset.
    bars_seen: usize,
}

impl<S: Signal> WarmupGuard<S> {
    /// Wraps `signal` in a `WarmupGuard`.
    pub fn new(signal: S) -> Self {
        Self { inner: signal, bars_seen: 0 }
    }

    /// Updates the inner signal and returns its value, or `Err(NotReady)` if not yet ready.
    ///
    /// The inner signal is updated regardless of readiness, so warmup progresses
    /// normally. Once ready, subsequent calls return `Ok(SignalValue::Scalar(_))`.
    ///
    /// # Errors
    ///
    /// - Returns `Err(NotReady)` if the signal has not yet accumulated enough bars.
    /// - Returns `Err(FinError)` (wrapped) if the inner signal returns an arithmetic error.
    pub fn update_checked(&mut self, bar: &BarInput) -> Result<SignalValue, WarmupError> {
        self.bars_seen += 1;
        let value = self.inner.update(bar).map_err(WarmupError::Signal)?;
        match &value {
            SignalValue::Unavailable => {
                let period = self.inner.period();
                let remaining = period.saturating_sub(self.bars_seen);
                Err(WarmupError::NotReady(NotReady {
                    name: self.inner.name().to_owned(),
                    warmup_period: period,
                    bars_remaining: remaining,
                }))
            }
            SignalValue::Scalar(_) => Ok(value),
        }
    }

    /// Returns `true` if the inner signal is ready.
    pub fn is_ready(&self) -> bool {
        self.inner.is_ready()
    }

    /// Returns the number of bars remaining until the signal is ready.
    ///
    /// Returns `0` if already ready.
    pub fn bars_remaining(&self) -> usize {
        self.inner.period().saturating_sub(self.bars_seen)
    }

    /// Returns the total warmup period of the inner signal.
    pub fn warmup_period(&self) -> usize {
        self.inner.period()
    }

    /// Returns the number of bars fed into this guard since construction or last reset.
    pub fn bars_seen(&self) -> usize {
        self.bars_seen
    }

    /// Resets the inner signal and the bar counter.
    pub fn reset(&mut self) {
        self.inner.reset();
        self.bars_seen = 0;
    }

    /// Returns a reference to the inner signal.
    pub fn inner(&self) -> &S {
        &self.inner
    }

    /// Consumes the guard and returns the inner signal.
    pub fn into_inner(self) -> S {
        self.inner
    }
}

// ── WarmupError ───────────────────────────────────────────────────────────────

/// Errors that can be returned by [`WarmupGuard::update_checked`].
#[derive(Debug, thiserror::Error)]
pub enum WarmupError {
    /// The signal has not yet accumulated enough bars to produce a value.
    #[error("{0}")]
    NotReady(NotReady),

    /// The inner signal returned a `FinError` (e.g., arithmetic overflow).
    #[error("signal error: {0}")]
    Signal(FinError),
}

// ── WarmupReport ─────────────────────────────────────────────────────────────

/// Warmup status for a single signal within a [`WarmupReport`].
#[derive(Debug, Clone, PartialEq)]
pub struct SignalWarmupStatus {
    /// The name of the signal.
    pub name: String,
    /// The number of bars required for this signal to become ready.
    pub warmup_period: usize,
    /// Whether the signal is currently ready.
    pub is_ready: bool,
    /// Bars still needed (0 if ready).
    pub bars_remaining: usize,
}

impl SignalWarmupStatus {
    /// Returns a human-readable summary line for this status entry.
    pub fn summary(&self) -> String {
        if self.is_ready {
            format!("[READY]   {} (period={})", self.name, self.warmup_period)
        } else {
            format!(
                "[WARMING] {} (period={}, remaining={})",
                self.name, self.warmup_period, self.bars_remaining
            )
        }
    }
}

/// A snapshot of warmup states for all signals tracked by a [`WarmupReporter`].
///
/// Use [`WarmupReporter::report`] to generate a `WarmupReport` after each bar.
/// The report lets callers determine which signals are ready and which are still
/// warming up, without inspecting each signal individually.
///
/// # Example
///
/// ```rust
/// use fin_primitives::signals::indicators::{Sma, Rsi};
/// use fin_primitives::signals::warmup::{WarmupContract, WarmupReporter};
///
/// let sma = Sma::new("sma10", 10).unwrap();
/// let rsi = Rsi::new("rsi14", 14).unwrap();
/// let reporter = WarmupReporter::new(vec![
///     sma.warmup_period(),
///     rsi.warmup_period(),
/// ], vec!["sma10".into(), "rsi14".into()]);
/// let report = reporter.report(0);
/// assert!(!report.all_ready());
/// ```
#[derive(Debug, Clone)]
pub struct WarmupReport {
    /// Per-signal status entries, in the order they were registered.
    pub statuses: Vec<SignalWarmupStatus>,
    /// Total bars consumed by the reporting pipeline so far.
    pub bars_consumed: usize,
}

impl WarmupReport {
    /// Returns `true` if every tracked signal is ready.
    pub fn all_ready(&self) -> bool {
        self.statuses.iter().all(|s| s.is_ready)
    }

    /// Returns `true` if at least one tracked signal is not yet ready.
    pub fn any_warming(&self) -> bool {
        self.statuses.iter().any(|s| !s.is_ready)
    }

    /// Returns the number of signals that are currently ready.
    pub fn ready_count(&self) -> usize {
        self.statuses.iter().filter(|s| s.is_ready).count()
    }

    /// Returns the number of signals still warming up.
    pub fn warming_count(&self) -> usize {
        self.statuses.iter().filter(|s| !s.is_ready).count()
    }

    /// Returns the maximum number of bars remaining across all warming signals.
    ///
    /// This is the number of additional bars needed before the *entire* pipeline
    /// is ready. Returns `0` if all signals are already ready.
    pub fn pipeline_bars_remaining(&self) -> usize {
        self.statuses.iter().map(|s| s.bars_remaining).max().unwrap_or(0)
    }

    /// Returns an iterator over signals that are ready.
    pub fn ready_signals(&self) -> impl Iterator<Item = &SignalWarmupStatus> {
        self.statuses.iter().filter(|s| s.is_ready)
    }

    /// Returns an iterator over signals that are still warming up.
    pub fn warming_signals(&self) -> impl Iterator<Item = &SignalWarmupStatus> {
        self.statuses.iter().filter(|s| !s.is_ready)
    }

    /// Returns a multi-line human-readable summary of the pipeline warmup state.
    pub fn display(&self) -> String {
        let mut lines = vec![format!(
            "WarmupReport [bars_consumed={}, ready={}/{}, pipeline_remaining={}]",
            self.bars_consumed,
            self.ready_count(),
            self.statuses.len(),
            self.pipeline_bars_remaining(),
        )];
        for status in &self.statuses {
            lines.push(format!("  {}", status.summary()));
        }
        lines.join("\n")
    }
}

// ── WarmupReporter ────────────────────────────────────────────────────────────

/// Tracks warmup progress for a set of named signals and produces [`WarmupReport`]s.
///
/// `WarmupReporter` stores the static warmup period for each signal by name, along
/// with the current bar count, allowing it to generate accurate [`WarmupReport`]s
/// at any point without holding mutable references to the signals themselves.
///
/// Construct via [`WarmupReporter::new`], then call [`WarmupReporter::tick`] after
/// each bar update and [`WarmupReporter::report`] whenever you need a snapshot.
///
/// # Example
///
/// ```rust
/// use fin_primitives::signals::warmup::WarmupReporter;
///
/// let mut reporter = WarmupReporter::new(
///     vec![5, 14],
///     vec!["sma5".into(), "rsi14".into()],
/// );
///
/// for _ in 0..5 {
///     reporter.tick();
/// }
///
/// let report = reporter.report(reporter.bars_consumed());
/// assert!(report.statuses[0].is_ready);   // sma5 ready
/// assert!(!report.statuses[1].is_ready);  // rsi14 still warming
/// ```
pub struct WarmupReporter {
    /// Registered signal names, in registration order.
    names: Vec<String>,
    /// Warmup periods for each signal, parallel to `names`.
    periods: Vec<usize>,
    /// Total bars consumed.
    bars_consumed: usize,
}

impl WarmupReporter {
    /// Creates a new `WarmupReporter` for the given named signals.
    ///
    /// `periods` and `names` must have the same length. The i-th entry in `periods`
    /// corresponds to the warmup period for the i-th name in `names`.
    ///
    /// # Panics
    /// Panics in debug builds if `periods.len() != names.len()`.
    pub fn new(periods: Vec<usize>, names: Vec<String>) -> Self {
        debug_assert_eq!(periods.len(), names.len(), "periods and names must have equal length");
        Self { names, periods, bars_consumed: 0 }
    }

    /// Advances the bar counter by one.
    ///
    /// Call this after each bar is processed to keep the reporter in sync with
    /// the indicator pipeline.
    pub fn tick(&mut self) {
        self.bars_consumed += 1;
    }

    /// Advances the bar counter by `n` bars at once.
    pub fn tick_n(&mut self, n: usize) {
        self.bars_consumed += n;
    }

    /// Returns the total number of bars consumed so far.
    pub fn bars_consumed(&self) -> usize {
        self.bars_consumed
    }

    /// Resets the bar counter to zero.
    pub fn reset(&mut self) {
        self.bars_consumed = 0;
    }

    /// Generates a [`WarmupReport`] given the current `bars_consumed`.
    ///
    /// Pass `self.bars_consumed()` for live reporting, or any integer for
    /// hypothetical "how ready would we be after N bars?" queries.
    pub fn report(&self, bars_consumed: usize) -> WarmupReport {
        let statuses = self
            .names
            .iter()
            .zip(self.periods.iter())
            .map(|(name, &period)| {
                let is_ready = bars_consumed >= period;
                let bars_remaining = period.saturating_sub(bars_consumed);
                SignalWarmupStatus {
                    name: name.clone(),
                    warmup_period: period,
                    is_ready,
                    bars_remaining,
                }
            })
            .collect();
        WarmupReport { statuses, bars_consumed }
    }

    /// Returns the number of signals registered with this reporter.
    pub fn signal_count(&self) -> usize {
        self.names.len()
    }

    /// Returns the maximum warmup period across all registered signals.
    ///
    /// This is the minimum number of bars before the full pipeline can be considered
    /// ready. Returns `0` if no signals are registered.
    pub fn max_warmup_period(&self) -> usize {
        self.periods.iter().copied().max().unwrap_or(0)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::signals::indicators::{Ema, Rsi, Sma};
    use rust_decimal_macros::dec;

    fn bar(close: &str) -> BarInput {
        BarInput::from_close(close.parse().unwrap())
    }

    // ── WarmupContract blanket impl ──────────────────────────────────────────

    #[test]
    fn test_warmup_contract_sma_period() {
        let sma = Sma::new("sma10", 10).unwrap();
        assert_eq!(sma.warmup_period(), 10);
    }

    #[test]
    fn test_warmup_contract_rsi_period() {
        let rsi = Rsi::new("rsi14", 14).unwrap();
        assert_eq!(rsi.warmup_period(), 14);
    }

    #[test]
    fn test_warmup_contract_ema_not_ready_initially() {
        let ema = Ema::new("ema5", 5).unwrap();
        assert!(!WarmupContract::is_ready(&ema));
    }

    #[test]
    fn test_warmup_contract_sma_ready_after_period() {
        let mut sma = Sma::new("sma3", 3).unwrap();
        sma.update(&bar("10")).unwrap();
        sma.update(&bar("20")).unwrap();
        assert!(!WarmupContract::is_ready(&sma));
        sma.update(&bar("30")).unwrap();
        assert!(WarmupContract::is_ready(&sma));
        assert_eq!(sma.bars_remaining(), 0);
    }

    // ── WarmupGuard ──────────────────────────────────────────────────────────

    #[test]
    fn test_warmup_guard_returns_err_before_ready() {
        let sma = Sma::new("sma5", 5).unwrap();
        let mut guard = WarmupGuard::new(sma);
        for _ in 0..4 {
            let result = guard.update_checked(&bar("100"));
            assert!(
                matches!(result, Err(WarmupError::NotReady(_))),
                "expected NotReady error"
            );
        }
    }

    #[test]
    fn test_warmup_guard_returns_ok_after_warmup() {
        let sma = Sma::new("sma3", 3).unwrap();
        let mut guard = WarmupGuard::new(sma);
        guard.update_checked(&bar("10")).ok();
        guard.update_checked(&bar("20")).ok();
        let result = guard.update_checked(&bar("30"));
        assert!(result.is_ok(), "expected Ok after warmup");
        assert!(matches!(result.unwrap(), SignalValue::Scalar(_)));
    }

    #[test]
    fn test_warmup_guard_is_ready_tracks_correctly() {
        let sma = Sma::new("sma2", 2).unwrap();
        let mut guard = WarmupGuard::new(sma);
        assert!(!guard.is_ready());
        guard.update_checked(&bar("10")).ok();
        assert!(!guard.is_ready());
        guard.update_checked(&bar("20")).ok();
        assert!(guard.is_ready());
    }

    #[test]
    fn test_warmup_guard_bars_remaining_decrements() {
        let sma = Sma::new("sma4", 4).unwrap();
        let mut guard = WarmupGuard::new(sma);
        assert_eq!(guard.bars_remaining(), 4);
        guard.update_checked(&bar("1")).ok();
        assert_eq!(guard.bars_remaining(), 3);
        guard.update_checked(&bar("1")).ok();
        assert_eq!(guard.bars_remaining(), 2);
    }

    #[test]
    fn test_warmup_guard_bars_remaining_zero_when_ready() {
        let sma = Sma::new("sma2", 2).unwrap();
        let mut guard = WarmupGuard::new(sma);
        guard.update_checked(&bar("10")).ok();
        guard.update_checked(&bar("20")).ok();
        assert_eq!(guard.bars_remaining(), 0);
    }

    #[test]
    fn test_warmup_guard_reset_restarts_warmup() {
        let sma = Sma::new("sma2", 2).unwrap();
        let mut guard = WarmupGuard::new(sma);
        guard.update_checked(&bar("10")).ok();
        guard.update_checked(&bar("20")).ok();
        assert!(guard.is_ready());
        guard.reset();
        assert!(!guard.is_ready());
        assert_eq!(guard.bars_seen(), 0);
        let result = guard.update_checked(&bar("10"));
        assert!(matches!(result, Err(WarmupError::NotReady(_))));
    }

    #[test]
    fn test_warmup_guard_not_ready_error_has_correct_name() {
        let sma = Sma::new("my_sma", 5).unwrap();
        let mut guard = WarmupGuard::new(sma);
        match guard.update_checked(&bar("100")) {
            Err(WarmupError::NotReady(e)) => {
                assert_eq!(e.name, "my_sma");
                assert_eq!(e.warmup_period, 5);
            }
            _ => panic!("expected NotReady"),
        }
    }

    #[test]
    fn test_warmup_guard_rsi_warmup_period() {
        let rsi = Rsi::new("rsi14", 14).unwrap();
        let guard = WarmupGuard::new(rsi);
        assert_eq!(guard.warmup_period(), 14);
    }

    #[test]
    fn test_warmup_guard_into_inner() {
        let sma = Sma::new("sma3", 3).unwrap();
        let guard = WarmupGuard::new(sma);
        let inner = guard.into_inner();
        assert_eq!(inner.name(), "sma3");
    }

    // ── WarmupReporter ───────────────────────────────────────────────────────

    #[test]
    fn test_warmup_reporter_all_warming_at_zero_bars() {
        let reporter = WarmupReporter::new(vec![5, 14], vec!["sma5".into(), "rsi14".into()]);
        let report = reporter.report(0);
        assert!(!report.all_ready());
        assert_eq!(report.warming_count(), 2);
        assert_eq!(report.ready_count(), 0);
    }

    #[test]
    fn test_warmup_reporter_partial_ready() {
        let reporter = WarmupReporter::new(vec![5, 14], vec!["sma5".into(), "rsi14".into()]);
        let report = reporter.report(5);
        assert!(!report.all_ready());
        assert_eq!(report.ready_count(), 1);
        assert_eq!(report.warming_count(), 1);
        assert!(report.statuses[0].is_ready);
        assert!(!report.statuses[1].is_ready);
    }

    #[test]
    fn test_warmup_reporter_all_ready() {
        let reporter = WarmupReporter::new(vec![5, 14], vec!["sma5".into(), "rsi14".into()]);
        let report = reporter.report(14);
        assert!(report.all_ready());
        assert_eq!(report.pipeline_bars_remaining(), 0);
    }

    #[test]
    fn test_warmup_reporter_tick_advances_count() {
        let mut reporter = WarmupReporter::new(vec![3], vec!["sma3".into()]);
        reporter.tick();
        reporter.tick();
        reporter.tick();
        assert_eq!(reporter.bars_consumed(), 3);
        let report = reporter.report(reporter.bars_consumed());
        assert!(report.all_ready());
    }

    #[test]
    fn test_warmup_reporter_tick_n() {
        let mut reporter = WarmupReporter::new(vec![10], vec!["sma10".into()]);
        reporter.tick_n(10);
        let report = reporter.report(reporter.bars_consumed());
        assert!(report.statuses[0].is_ready);
    }

    #[test]
    fn test_warmup_reporter_pipeline_bars_remaining() {
        let reporter = WarmupReporter::new(vec![5, 14, 20], vec!["a".into(), "b".into(), "c".into()]);
        let report = reporter.report(10);
        // c needs 20 bars, 10 consumed → 10 remaining
        assert_eq!(report.pipeline_bars_remaining(), 10);
    }

    #[test]
    fn test_warmup_reporter_max_warmup_period() {
        let reporter = WarmupReporter::new(vec![5, 14, 200], vec!["a".into(), "b".into(), "c".into()]);
        assert_eq!(reporter.max_warmup_period(), 200);
    }

    #[test]
    fn test_warmup_report_display_contains_signal_names() {
        let reporter = WarmupReporter::new(vec![5], vec!["mysig".into()]);
        let report = reporter.report(0);
        let display = report.display();
        assert!(display.contains("mysig"));
        assert!(display.contains("WARMING"));
    }

    #[test]
    fn test_warmup_report_display_shows_ready() {
        let reporter = WarmupReporter::new(vec![5], vec!["mysig".into()]);
        let report = reporter.report(5);
        let display = report.display();
        assert!(display.contains("READY"));
    }

    #[test]
    fn test_signal_warmup_status_summary_ready() {
        let status = SignalWarmupStatus {
            name: "sma5".into(),
            warmup_period: 5,
            is_ready: true,
            bars_remaining: 0,
        };
        assert!(status.summary().contains("READY"));
        assert!(status.summary().contains("sma5"));
    }

    #[test]
    fn test_signal_warmup_status_summary_warming() {
        let status = SignalWarmupStatus {
            name: "rsi14".into(),
            warmup_period: 14,
            is_ready: false,
            bars_remaining: 7,
        };
        let s = status.summary();
        assert!(s.contains("WARMING"));
        assert!(s.contains("remaining=7"));
    }

    #[test]
    fn test_warmup_reporter_reset() {
        let mut reporter = WarmupReporter::new(vec![3], vec!["sma3".into()]);
        reporter.tick_n(10);
        reporter.reset();
        assert_eq!(reporter.bars_consumed(), 0);
        let report = reporter.report(reporter.bars_consumed());
        assert!(!report.all_ready());
    }

    #[test]
    fn test_warmup_reporter_empty_reports_all_ready() {
        let reporter = WarmupReporter::new(vec![], vec![]);
        let report = reporter.report(0);
        // vacuously all ready (no signals)
        assert!(report.all_ready());
        assert_eq!(report.pipeline_bars_remaining(), 0);
    }

    #[test]
    fn test_warmup_guard_period_1_immediate() {
        let sma = Sma::new("sma1", 1).unwrap();
        let mut guard = WarmupGuard::new(sma);
        let result = guard.update_checked(&bar("42"));
        assert!(result.is_ok());
    }

    #[test]
    fn test_not_ready_error_display() {
        let err = NotReady {
            name: "sma10".into(),
            warmup_period: 10,
            bars_remaining: 5,
        };
        let msg = err.to_string();
        assert!(msg.contains("sma10"));
        assert!(msg.contains("5 bars"));
    }

    #[test]
    fn test_warmup_guard_bars_seen_tracks_all_updates() {
        let sma = Sma::new("sma20", 20).unwrap();
        let mut guard = WarmupGuard::new(sma);
        for i in 0..7 {
            guard.update_checked(&bar("10")).ok();
            assert_eq!(guard.bars_seen(), i + 1);
        }
    }

    #[test]
    fn test_warmup_contract_ema_warmup_period_matches_period() {
        let ema = Ema::new("ema20", 20).unwrap();
        assert_eq!(ema.warmup_period(), ema.period());
    }
}