Skip to main content

nautilus_indicators/momentum/
psl.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::fmt::{Debug, Display};
17
18use nautilus_model::data::Bar;
19
20use crate::{
21    average::{MovingAverageFactory, MovingAverageType},
22    indicator::{Indicator, MovingAverage},
23};
24
25#[repr(C)]
26#[derive(Debug)]
27#[cfg_attr(
28    feature = "python",
29    pyo3::pyclass(module = "nautilus_trader.indicators", unsendable)
30)]
31#[cfg_attr(
32    feature = "python",
33    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.indicators")
34)]
35pub struct PsychologicalLine {
36    pub period: usize,
37    pub ma_type: MovingAverageType,
38    pub value: f64,
39    pub initialized: bool,
40    ma: Box<dyn MovingAverage + Send + 'static>,
41    has_inputs: bool,
42    diff: f64,
43    previous_close: f64,
44}
45
46impl Display for PsychologicalLine {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}({},{})", self.name(), self.period, self.ma_type)
49    }
50}
51
52impl Indicator for PsychologicalLine {
53    fn name(&self) -> String {
54        stringify!(PsychologicalLine).to_string()
55    }
56
57    fn has_inputs(&self) -> bool {
58        self.has_inputs
59    }
60
61    fn initialized(&self) -> bool {
62        self.initialized
63    }
64
65    fn handle_bar(&mut self, bar: &Bar) {
66        self.update_raw((&bar.close).into());
67    }
68
69    fn reset(&mut self) {
70        self.ma.reset();
71        self.diff = 0.0;
72        self.previous_close = 0.0;
73        self.value = 0.0;
74        self.has_inputs = false;
75        self.initialized = false;
76    }
77}
78
79impl PsychologicalLine {
80    /// Creates a new [`PsychologicalLine`] instance.
81    #[must_use]
82    pub fn new(period: usize, ma_type: Option<MovingAverageType>) -> Self {
83        Self {
84            period,
85            ma_type: ma_type.unwrap_or(MovingAverageType::Simple),
86            value: 0.0,
87            previous_close: 0.0,
88            ma: MovingAverageFactory::create(ma_type.unwrap_or(MovingAverageType::Simple), period),
89            has_inputs: false,
90            initialized: false,
91            diff: 0.0,
92        }
93    }
94
95    pub fn update_raw(&mut self, close: f64) {
96        if !self.has_inputs {
97            self.previous_close = close;
98        }
99
100        self.diff = close - self.previous_close;
101        if self.diff <= 0.0 {
102            self.ma.update_raw(0.0);
103        } else {
104            self.ma.update_raw(1.0);
105        }
106        self.value = 100.0 * self.ma.value();
107
108        if !self.initialized {
109            self.has_inputs = true;
110
111            if self.ma.initialized() {
112                self.initialized = true;
113            }
114        }
115
116        self.previous_close = close;
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use nautilus_model::data::Bar;
123    use rstest::rstest;
124
125    use crate::{
126        indicator::Indicator,
127        momentum::psl::PsychologicalLine,
128        stubs::{bar_ethusdt_binance_minute_bid, psl_10},
129        testing::assert_approx_equal,
130    };
131
132    #[rstest]
133    fn test_psl_initialized(psl_10: PsychologicalLine) {
134        let display_str = format!("{psl_10}");
135        assert_eq!(display_str, "PsychologicalLine(10,SIMPLE)");
136        assert_eq!(psl_10.period, 10);
137        assert!(!psl_10.initialized);
138        assert!(!psl_10.has_inputs);
139    }
140
141    #[rstest]
142    fn test_value_with_one_input(mut psl_10: PsychologicalLine) {
143        psl_10.update_raw(1.0);
144        assert_eq!(psl_10.value, 0.0);
145    }
146
147    #[rstest]
148    fn test_value_with_three_inputs(mut psl_10: PsychologicalLine) {
149        psl_10.update_raw(1.0);
150        psl_10.update_raw(2.0);
151        psl_10.update_raw(3.0);
152        assert_approx_equal(psl_10.value, 66.6666666667);
153    }
154
155    #[rstest]
156    fn test_value_with_ten_inputs(mut psl_10: PsychologicalLine) {
157        psl_10.update_raw(1.00000);
158        psl_10.update_raw(1.00010);
159        psl_10.update_raw(1.00020);
160        psl_10.update_raw(1.00030);
161        psl_10.update_raw(1.00040);
162        psl_10.update_raw(1.00050);
163        psl_10.update_raw(1.00040);
164        psl_10.update_raw(1.00030);
165        psl_10.update_raw(1.00020);
166        psl_10.update_raw(1.00010);
167        psl_10.update_raw(1.00000);
168        assert_eq!(psl_10.value, 50.0);
169    }
170
171    #[rstest]
172    fn test_initialized_with_required_input(mut psl_10: PsychologicalLine) {
173        for i in 1..10 {
174            psl_10.update_raw(f64::from(i));
175        }
176        assert!(!psl_10.initialized);
177        psl_10.update_raw(10.0);
178        assert!(psl_10.initialized);
179    }
180
181    #[rstest]
182    fn test_handle_bar(mut psl_10: PsychologicalLine, bar_ethusdt_binance_minute_bid: Bar) {
183        psl_10.handle_bar(&bar_ethusdt_binance_minute_bid);
184        assert_eq!(psl_10.value, 0.0);
185        assert!(psl_10.has_inputs);
186        assert!(!psl_10.initialized);
187    }
188
189    #[rstest]
190    fn test_reset(mut psl_10: PsychologicalLine) {
191        psl_10.update_raw(1.0);
192        psl_10.reset();
193        assert_eq!(psl_10.value, 0.0);
194        assert_eq!(psl_10.previous_close, 0.0);
195        assert_eq!(psl_10.diff, 0.0);
196        assert!(!psl_10.has_inputs);
197        assert!(!psl_10.initialized);
198    }
199}