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
use std::f64::INFINITY;
use std::fmt;

use crate::errors::*;
use crate::{Calculate, Low, Next, Reset};

/// Returns the lowest value in a given time frame.
///
/// # Parameters
///
/// * _n_ - size of the time frame (integer greater than 0). Default value is 14.
///
/// # Example
///
/// ```
/// use ta::indicators::Minimum;
/// use ta::{Calculate, Next};
///
/// let mut min = Minimum::new(3).unwrap();
/// assert_eq!(min.calc(10.0), 10.0);
/// assert_eq!(min.calc(11.0), 10.0);
/// assert_eq!(min.calc(12.0), 10.0);
/// assert_eq!(min.calc(13.0), 11.0);
/// ```
#[derive(Debug, Clone)]
pub struct Minimum {
    n: usize,
    vec: Vec<f64>,
    min_index: usize,
    cur_index: usize,
}

impl Minimum {
    pub fn new(n: u32) -> Result<Self> {
        let n = n as usize;

        if n <= 0 {
            return Err(Error::from_kind(ErrorKind::InvalidParameter));
        }

        let indicator = Self {
            n: n,
            vec: vec![INFINITY; n],
            min_index: 0,
            cur_index: 0,
        };

        Ok(indicator)
    }

    fn find_min_index(&self) -> usize {
        let mut min = ::std::f64::INFINITY;
        let mut index: usize = 0;

        for (i, &val) in self.vec.iter().enumerate() {
            if val < min {
                min = val;
                index = i;
            }
        }

        index
    }
}

impl Calculate for Minimum {
    fn calc(&mut self, input: f64) -> f64 {
        self.cur_index = (self.cur_index + 1) % (self.n as usize);
        self.vec[self.cur_index] = input;

        if input < self.vec[self.min_index] {
            self.min_index = self.cur_index;
        } else if self.min_index == self.cur_index {
            self.min_index = self.find_min_index();
        }

        self.vec[self.min_index]
    }
}

impl<T: Low> Next<T> for Minimum {
    fn next(&mut self, input: &T) -> f64 {
        self.calc(input.low())
    }
}

impl Reset for Minimum {
    fn reset(&mut self) {
        for i in 0..self.n {
            self.vec[i] = INFINITY;
        }
    }
}

impl Default for Minimum {
    fn default() -> Self {
        Self::new(14).unwrap()
    }
}

impl fmt::Display for Minimum {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "MIN({})", self.n)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_helper::*;

    test_indicator!(Minimum);

    #[test]
    fn test_new() {
        assert!(Minimum::new(0).is_err());
        assert!(Minimum::new(1).is_ok());
    }

    #[test]
    fn test_next() {
        let mut min = Minimum::new(3).unwrap();

        assert_eq!(min.calc(4.0), 4.0);
        assert_eq!(min.calc(1.2), 1.2);
        assert_eq!(min.calc(5.0), 1.2);
        assert_eq!(min.calc(3.0), 1.2);
        assert_eq!(min.calc(4.0), 3.0);
        assert_eq!(min.calc(6.0), 3.0);
        assert_eq!(min.calc(7.0), 4.0);
        assert_eq!(min.calc(8.0), 6.0);
        assert_eq!(min.calc(-9.0), -9.0);
        assert_eq!(min.calc(0.0), -9.0);
    }

    #[test]
    fn test_next_with_bars() {
        fn bar(low: f64) -> Bar {
            Bar::new().low(low)
        }

        let mut min = Minimum::new(3).unwrap();

        assert_eq!(min.next(&bar(4.0)), 4.0);
        assert_eq!(min.next(&bar(4.0)), 4.0);
        assert_eq!(min.next(&bar(1.2)), 1.2);
        assert_eq!(min.next(&bar(5.0)), 1.2);
    }

    #[test]
    fn test_reset() {
        let mut min = Minimum::new(10).unwrap();

        assert_eq!(min.calc(5.0), 5.0);
        assert_eq!(min.calc(7.0), 5.0);

        min.reset();
        assert_eq!(min.calc(8.0), 8.0);
    }

    #[test]
    fn test_default() {
        Minimum::default();
    }

    #[test]
    fn test_display() {
        let indicator = Minimum::new(10).unwrap();
        assert_eq!(format!("{}", indicator), "MIN(10)");
    }
}