quantaxis_rs/indicators/
hhv.rs1use std::f64::INFINITY;
2use std::fmt;
3
4use crate::{High, Next, Reset, Update};
5use crate::errors::*;
6
7#[derive(Debug, Clone)]
27pub struct HHV {
28 n: usize,
29 vec: Vec<f64>,
30 max_index: usize,
31 cur_index: usize,
32 pub cached: Vec<f64>
33}
34
35impl HHV {
36 pub fn new(n: u32) -> Result<Self> {
37 let n = n as usize;
38
39 if n == 0 {
40 return Err(Error::from_kind(ErrorKind::InvalidParameter));
41 }
42
43 let indicator = Self {
44 n: n,
45 vec: vec![-INFINITY; n],
46 max_index: 0,
47 cur_index: 0,
48 cached: vec![-INFINITY; n]
49 };
50 Ok(indicator)
51 }
52
53 fn find_max_index(&self) -> usize {
54 let mut max = -INFINITY;
55 let mut index: usize = 0;
56
57 for (i, &val) in self.vec.iter().enumerate() {
58 if val > max {
59 max = val;
60 index = i;
61 }
62 }
63
64 index
65 }
66}
67
68impl Next<f64> for HHV {
69 type Output = f64;
70
71 fn next(&mut self, input: f64) -> Self::Output {
72 self.cur_index = (self.cur_index + 1) % (self.n as usize);
73 self.vec[self.cur_index] = input;
74
75 if input > self.vec[self.max_index] {
76 self.max_index = self.cur_index;
77 } else if self.max_index == self.cur_index {
78 self.max_index = self.find_max_index();
79 }
80 self.cached.push(self.vec[self.max_index]);
81 self.cached.remove(0);
82 self.vec[self.max_index]
83 }
84}
85
86impl Update<f64> for HHV {
87 type Output = f64;
88
89 fn update(&mut self, input: f64) -> Self::Output {
90 self.vec[self.cur_index] = input;
91
92 if input > self.vec[self.max_index] {
93 self.max_index = self.cur_index;
94 } else if self.max_index == self.cur_index {
95 self.max_index = self.find_max_index();
96 }
97 self.cached.remove(self.n - 1);
98 self.cached.push(self.vec[self.max_index]);
99
100 self.vec[self.max_index]
101 }
102}
103
104impl<'a, T: High> Next<&'a T> for HHV {
105 type Output = f64;
106
107 fn next(&mut self, input: &'a T) -> Self::Output {
108 self.next(input.high())
109 }
110}
111
112impl Reset for HHV {
113 fn reset(&mut self) {
114 for i in 0..self.n {
115 self.vec[i] = -INFINITY;
116 }
117 }
118}
119
120impl Default for HHV {
121 fn default() -> Self {
122 Self::new(14).unwrap()
123 }
124}
125
126impl fmt::Display for HHV {
127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128 write!(f, "MAX({})", self.n)
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use crate::test_helper::*;
135
136 use super::*;
137
138 macro_rules! test_indicator {
139 ($i:tt) => {
140 #[test]
141 fn test_indicator() {
142 let bar = Bar::new();
143
144 let mut indicator = $i::default();
146
147 let first_output = indicator.next(12.3);
149
150 indicator.next(&bar);
152
153 indicator.reset();
155 assert_eq!(indicator.next(12.3), first_output);
156
157 format!("{}", indicator);
159 }
160 };
161 }
162 test_indicator!(HHV);
163
164 #[test]
165 fn test_new() {
166 assert!(HHV::new(0).is_err());
167 assert!(HHV::new(1).is_ok());
168 }
169
170 #[test]
171 fn test_next() {
172 let mut max = HHV::new(3).unwrap();
173
174 assert_eq!(max.next(4.0), 4.0);
175 assert_eq!(max.next(1.2), 4.0);
176 assert_eq!(max.next(5.0), 5.0);
177 assert_eq!(max.next(3.0), 5.0);
178 assert_eq!(max.next(4.0), 5.0);
179 assert_eq!(max.next(0.0), 4.0);
180 assert_eq!(max.next(-1.0), 4.0);
181 assert_eq!(max.next(-2.0), 0.0);
182 assert_eq!(max.next(-1.5), -1.0);
183 }
184
185 #[test]
186 fn test_update() {
187 let mut max = HHV::new(3).unwrap();
188
189 assert_eq!(max.next(4.0), 4.0);
190 assert_eq!(max.next(1.2), 4.0);
191 assert_eq!(max.next(5.0), 5.0);
192 assert_eq!(max.update(3.0), 4.0);
193 assert_eq!(max.next(3.0), 3.0);
194 }
195
196 #[test]
197 fn test_next_with_bars() {
198 fn bar(high: f64) -> Bar {
199 Bar::new().high(high)
200 }
201
202 let mut max = HHV::new(2).unwrap();
203
204 assert_eq!(max.next(&bar(1.1)), 1.1);
205 assert_eq!(max.next(&bar(4.0)), 4.0);
206 assert_eq!(max.next(&bar(3.5)), 4.0);
207 assert_eq!(max.next(&bar(2.0)), 3.5);
208 }
209
210 #[test]
211 fn test_reset() {
212 let mut max = HHV::new(100).unwrap();
213 assert_eq!(max.next(4.0), 4.0);
214 assert_eq!(max.next(10.0), 10.0);
215 assert_eq!(max.next(4.0), 10.0);
216
217 max.reset();
218 assert_eq!(max.next(4.0), 4.0);
219 }
220
221 #[test]
222 fn test_default() {
223 HHV::default();
224 }
225
226 #[test]
227 fn test_display() {
228 let indicator = HHV::new(7).unwrap();
229 assert_eq!(format!("{}", indicator), "MAX(7)");
230 }
231}