quant-indicators 0.7.0

Pure indicator math library for trading — MA, RSI, Bollinger, MACD, ATR, HRP
Documentation
//! Time series representation for indicator output.

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;

/// A time-indexed series of values.
///
/// Represents the output of an indicator computation.
/// Each value is paired with its timestamp.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Series {
    values: Vec<(DateTime<Utc>, Decimal)>,
}

impl Series {
    /// Create a new series from timestamped values.
    #[must_use]
    pub fn new(values: Vec<(DateTime<Utc>, Decimal)>) -> Self {
        Self { values }
    }

    /// Create an empty series.
    #[must_use]
    pub fn empty() -> Self {
        Self { values: Vec::new() }
    }

    /// Get the number of values in the series.
    #[must_use]
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Check if the series is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Get the underlying values.
    #[must_use]
    pub fn values(&self) -> &[(DateTime<Utc>, Decimal)] {
        &self.values
    }

    /// Get just the decimal values without timestamps.
    #[must_use]
    pub fn decimal_values(&self) -> Vec<Decimal> {
        self.values.iter().map(|(_, v)| *v).collect()
    }

    /// Get the value at a specific index.
    #[must_use]
    pub fn get(&self, index: usize) -> Option<(DateTime<Utc>, Decimal)> {
        self.values.get(index).copied()
    }

    /// Get the last value in the series.
    #[must_use]
    pub fn last(&self) -> Option<(DateTime<Utc>, Decimal)> {
        self.values.last().copied()
    }

    /// Get the first value in the series.
    #[must_use]
    pub fn first(&self) -> Option<(DateTime<Utc>, Decimal)> {
        self.values.first().copied()
    }
}

impl IntoIterator for Series {
    type Item = (DateTime<Utc>, Decimal);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.values.into_iter()
    }
}

impl<'a> IntoIterator for &'a Series {
    type Item = &'a (DateTime<Utc>, Decimal);
    type IntoIter = std::slice::Iter<'a, (DateTime<Utc>, Decimal)>;

    fn into_iter(self) -> Self::IntoIter {
        self.values.iter()
    }
}

#[cfg(test)]
#[path = "series_tests.rs"]
mod tests;