ta_statistics/
minimum.rs

1use num_traits::Float;
2
3use crate::utils::{Min, MonotonicQueue};
4
5/// # Minimum Value Calculation for Rolling Windows
6///
7/// A specialized module for tracking minimum values in rolling windows of financial time-series data.
8///
9/// Uses an efficient data structure to maintain the minimum value at all times, providing
10/// constant-time lookups and amortized constant-time updates. This approach is particularly
11/// valuable for technical analysis indicators that require identifying price extremes,
12/// such as Donchian channels, volatility measures, and support/resistance level detection.
13///
14/// The implementation is optimized for financial time-series analysis where
15/// identifying minimum values within specific lookback periods is essential
16/// for decision-making processes.
17#[derive(Debug)]
18pub struct Minimum<T>(MonotonicQueue<T, Min>);
19
20impl<T: Default + Clone + Float> Minimum<T> {
21    /// Creates a new Minimum instance with the specified period
22    ///
23    /// # Arguments
24    ///
25    /// * `period` - The size of the rolling window
26    ///
27    /// # Returns
28    ///
29    /// A new Minimum instance
30    pub fn new(period: usize) -> Self {
31        Self(MonotonicQueue::new(period))
32    }
33
34    /// Pushes a new value into the rolling window
35    ///
36    /// # Arguments
37    ///
38    /// * `value` - The new value to be added to the rolling window
39    ///
40    /// # Returns
41    ///
42    /// None if the window is not yet full, otherwise returns the minimum value
43    pub fn push(&mut self, value: T) {
44        self.0.push(value)
45    }
46
47    /// Returns the minimum value in the rolling window
48    ///
49    /// # Returns
50    ///
51    /// None if the window is not yet full, otherwise returns the minimum value
52    pub fn get(&self) -> Option<T> {
53        self.0.front()
54    }
55
56    /// Resets the rolling window
57    pub fn reset(&mut self) {
58        self.0.reset();
59    }
60}