ux_indicators/indicators/
tr.rs

1#![allow(dead_code)]
2
3use std::fmt;
4
5use crate::helpers::max3;
6use crate::{Close, High, Low, Next, Reset};
7
8use crate::{Factory};
9use crate::indicators::SimpleMovingAverage;
10
11pub struct TrFactory {
12}
13
14impl TrFactory {
15    pub fn new() -> Self {
16        Self{}
17    }
18}
19
20impl Factory for TrFactory {
21    fn create() -> Box<dyn Next<f64, Output = Box<[f64]>>> {
22        Box::new(SimpleMovingAverage::default())
23    }
24}
25
26/// The range of a day's trading is simply _high_ - _low_.
27/// The true range extends it to yesterday's closing price if it was outside of today's range.
28///
29/// The true range is the largest of one the following:
30///
31/// * Most recent period's high minus the most recent period's low
32/// * Absolute value of the most recent period's high minus the previous close
33/// * Absolute value of the most recent period's low minus the previous close
34///
35/// # Formula
36///
37/// TR = max[(high - low), abs(high - close<sub>prev</sub>), abs(low - close<sub>prev</sub>)]
38///
39/// # Example
40///
41/// ```
42/// extern crate core;
43/// #[macro_use] extern crate assert_approx_eq;
44///
45/// use core::{Next, DataItem};
46/// use core::indicators::TrueRange;
47///
48/// fn main() {
49///     let data = vec![
50///         // open, high, low, close, tr
51///         (9.7   , 10.0, 9.0, 9.5  , 1.0),  // tr = high - low = 10.0 - 9.0 = 1.0
52///         (9.9   , 10.4, 9.8, 10.2 , 0.9),  // tr = high - prev_close = 10.4 - 9.5 = 0.9
53///         (10.1  , 10.7, 9.4, 9.7  , 1.3),  // tr = high - low = 10.7 - 9.4 = 1.3
54///         (9.1   , 9.2 , 8.1, 8.4  , 1.6),  // tr = prev_close - low = 9.7 - 8.1 = 1.6
55///     ];
56///     let mut indicator = TrueRange::new();
57///
58///     for (open, high, low, close, tr) in data {
59///         let di = DataItem::builder()
60///             .high(high)
61///             .low(low)
62///             .close(close)
63///             .open(open)
64///             .volume(1000.0)
65///             .build().unwrap();
66///         assert_approx_eq!(indicator.next(&di), tr);
67///     }
68/// }
69/// ```
70
71#[derive(Debug, Clone)]
72pub struct TrueRange {
73    prev_close: Option<f64>,
74}
75
76impl TrueRange {
77    pub fn new() -> Self {
78        Self { prev_close: None }
79    }
80}
81
82impl Default for TrueRange {
83    fn default() -> Self {
84        Self::new()
85    }
86}
87
88impl fmt::Display for TrueRange {
89    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90        write!(f, "TRUE_RANGE()")
91    }
92}
93
94impl Next<f64> for TrueRange {
95    type Output = f64;
96
97    fn next(&mut self, input: f64) -> Self::Output {
98        let distance = match self.prev_close {
99            Some(prev) => (input - prev).abs(),
100            None => 0.0,
101        };
102        self.prev_close = Some(input);
103        distance
104    }
105}
106
107impl<'a, T: High + Low + Close> Next<&'a T> for TrueRange {
108    type Output = f64;
109
110    fn next(&mut self, bar: &'a T) -> Self::Output {
111        let max_dist = match self.prev_close {
112            Some(prev_close) => {
113                let dist1 = bar.high() - bar.low();
114                let dist2 = (bar.high() - prev_close).abs();
115                let dist3 = (bar.low() - prev_close).abs();
116                max3(dist1, dist2, dist3)
117            }
118            None => bar.high() - bar.low(),
119        };
120        self.prev_close = Some(bar.close());
121        max_dist
122    }
123}
124
125impl Reset for TrueRange {
126    fn reset(&mut self) {
127        self.prev_close = None;
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use super::*;
134    use crate::test_helper::*;
135
136    // test_indicator!(TrueRange);
137
138    #[test]
139    fn test_next_f64() {
140        let mut tr = TrueRange::new();
141        assert_eq!(round(tr.next(2.5)), 0.0);
142        assert_eq!(round(tr.next(3.6)), 1.1);
143        assert_eq!(round(tr.next(3.3)), 0.3);
144    }
145
146    #[test]
147    fn test_next_bar() {
148        let mut tr = TrueRange::new();
149
150        let bar1 = Bar::new().high(10).low(7.5).close(9);
151        let bar2 = Bar::new().high(11).low(9).close(9.5);
152        let bar3 = Bar::new().high(9).low(5).close(8);
153
154        assert_eq!(tr.next(&bar1), 2.5);
155        assert_eq!(tr.next(&bar2), 2.0);
156        assert_eq!(tr.next(&bar3), 4.5);
157    }
158
159    #[test]
160    fn test_reset() {
161        let mut tr = TrueRange::new();
162
163        let bar1 = Bar::new().high(10).low(7.5).close(9);
164        let bar2 = Bar::new().high(11).low(9).close(9.5);
165
166        tr.next(&bar1);
167        tr.next(&bar2);
168
169        tr.reset();
170        let bar3 = Bar::new().high(60).low(15).close(51);
171        assert_eq!(tr.next(&bar3), 45.0);
172    }
173
174    #[test]
175    fn test_default() {
176        TrueRange::default();
177    }
178
179    #[test]
180    fn test_display() {
181        let indicator = TrueRange::new();
182        assert_eq!(format!("{}", indicator), "TRUE_RANGE()");
183    }
184}