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.core.nautilus_pyo3.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    };
130
131    #[rstest]
132    fn test_psl_initialized(psl_10: PsychologicalLine) {
133        let display_str = format!("{psl_10}");
134        assert_eq!(display_str, "PsychologicalLine(10,SIMPLE)");
135        assert_eq!(psl_10.period, 10);
136        assert!(!psl_10.initialized);
137        assert!(!psl_10.has_inputs);
138    }
139
140    #[rstest]
141    fn test_value_with_one_input(mut psl_10: PsychologicalLine) {
142        psl_10.update_raw(1.0);
143        assert_eq!(psl_10.value, 0.0);
144    }
145
146    #[rstest]
147    fn test_value_with_three_inputs(mut psl_10: PsychologicalLine) {
148        psl_10.update_raw(1.0);
149        psl_10.update_raw(2.0);
150        psl_10.update_raw(3.0);
151        assert_eq!(psl_10.value, 66.666_666_666_666_66);
152    }
153
154    #[rstest]
155    fn test_value_with_ten_inputs(mut psl_10: PsychologicalLine) {
156        psl_10.update_raw(1.00000);
157        psl_10.update_raw(1.00010);
158        psl_10.update_raw(1.00020);
159        psl_10.update_raw(1.00030);
160        psl_10.update_raw(1.00040);
161        psl_10.update_raw(1.00050);
162        psl_10.update_raw(1.00040);
163        psl_10.update_raw(1.00030);
164        psl_10.update_raw(1.00020);
165        psl_10.update_raw(1.00010);
166        psl_10.update_raw(1.00000);
167        assert_eq!(psl_10.value, 50.0);
168    }
169
170    #[rstest]
171    fn test_initialized_with_required_input(mut psl_10: PsychologicalLine) {
172        for i in 1..10 {
173            psl_10.update_raw(f64::from(i));
174        }
175        assert!(!psl_10.initialized);
176        psl_10.update_raw(10.0);
177        assert!(psl_10.initialized);
178    }
179
180    #[rstest]
181    fn test_handle_bar(mut psl_10: PsychologicalLine, bar_ethusdt_binance_minute_bid: Bar) {
182        psl_10.handle_bar(&bar_ethusdt_binance_minute_bid);
183        assert_eq!(psl_10.value, 0.0);
184        assert!(psl_10.has_inputs);
185        assert!(!psl_10.initialized);
186    }
187
188    #[rstest]
189    fn test_reset(mut psl_10: PsychologicalLine) {
190        psl_10.update_raw(1.0);
191        psl_10.reset();
192        assert_eq!(psl_10.value, 0.0);
193        assert_eq!(psl_10.previous_close, 0.0);
194        assert_eq!(psl_10.diff, 0.0);
195        assert!(!psl_10.has_inputs);
196        assert!(!psl_10.initialized);
197    }
198}