Skip to main content

quant_indicators/
close.rs

1//! Close price passthrough indicator.
2//!
3//! Simply returns close prices with timestamps. Useful for signal generators
4//! that don't need technical indicators but still need access to timestamps.
5
6use quant_primitives::Candle;
7
8use crate::error::IndicatorError;
9use crate::indicator::Indicator;
10use crate::series::Series;
11
12/// Passthrough indicator that returns close prices.
13///
14/// This is useful for signal generators that don't use technical indicators
15/// but still need timestamps (like BuyAndHoldSignal).
16#[derive(Debug, Clone, Default)]
17pub struct Close;
18
19impl Close {
20    /// Create a new Close indicator.
21    pub fn new() -> Self {
22        Self
23    }
24}
25
26impl Indicator for Close {
27    fn name(&self) -> &str {
28        "Close"
29    }
30
31    fn warmup_period(&self) -> usize {
32        1 // Just need one candle
33    }
34
35    fn compute(&self, candles: &[Candle]) -> Result<Series, IndicatorError> {
36        if candles.is_empty() {
37            return Err(IndicatorError::InsufficientData {
38                required: 1,
39                actual: 0,
40            });
41        }
42
43        let values: Vec<_> = candles.iter().map(|c| (c.timestamp(), c.close())).collect();
44
45        Ok(Series::new(values))
46    }
47}
48
49#[cfg(test)]
50#[path = "close_tests.rs"]
51mod tests;