Skip to main content

nautilus_indicators/momentum/
pressure.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    volatility::atr::AverageTrueRange,
24};
25
26#[repr(C)]
27#[derive(Debug)]
28#[cfg_attr(
29    feature = "python",
30    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.indicators", unsendable)
31)]
32#[cfg_attr(
33    feature = "python",
34    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.indicators")
35)]
36pub struct Pressure {
37    pub period: usize,
38    pub ma_type: MovingAverageType,
39    pub atr_floor: f64,
40    pub value: f64,
41    pub value_cumulative: f64,
42    pub initialized: bool,
43    atr: AverageTrueRange,
44    average_volume: Box<dyn MovingAverage + Send + 'static>,
45    has_inputs: bool,
46}
47
48impl Display for Pressure {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "{}({},{})", self.name(), self.period, self.ma_type,)
51    }
52}
53
54impl Indicator for Pressure {
55    fn name(&self) -> String {
56        stringify!(Pressure).to_string()
57    }
58
59    fn has_inputs(&self) -> bool {
60        self.has_inputs
61    }
62
63    fn initialized(&self) -> bool {
64        self.initialized
65    }
66
67    fn handle_bar(&mut self, bar: &Bar) {
68        self.update_raw(
69            (&bar.high).into(),
70            (&bar.low).into(),
71            (&bar.close).into(),
72            (&bar.volume).into(),
73        );
74    }
75
76    fn reset(&mut self) {
77        self.atr.reset();
78        self.average_volume.reset();
79        self.value = 0.0;
80        self.value_cumulative = 0.0;
81        self.has_inputs = false;
82        self.initialized = false;
83    }
84}
85
86impl Pressure {
87    /// Creates a new [`Pressure`] instance.
88    ///
89    /// # Panics
90    ///
91    /// Panics if `period` is not positive (> 0).
92    #[must_use]
93    pub fn new(period: usize, ma_type: Option<MovingAverageType>, atr_floor: Option<f64>) -> Self {
94        assert!(period > 0, "Pressure: period must be > 0");
95        let ma_type = ma_type.unwrap_or(MovingAverageType::Exponential);
96        Self {
97            period,
98            ma_type,
99            atr_floor: atr_floor.unwrap_or(0.0),
100            value: 0.0,
101            value_cumulative: 0.0,
102            atr: AverageTrueRange::new(period, Some(ma_type), Some(false), atr_floor),
103            average_volume: MovingAverageFactory::create(ma_type, period),
104            has_inputs: false,
105            initialized: false,
106        }
107    }
108
109    pub fn update_raw(&mut self, high: f64, low: f64, close: f64, volume: f64) {
110        self.atr.update_raw(high, low, close);
111        self.average_volume.update_raw(volume);
112
113        self.has_inputs = true;
114
115        let avg_vol = self.average_volume.value();
116        if avg_vol == 0.0 {
117            self.value = 0.0;
118            return;
119        }
120
121        let atr_val = if self.atr.value > 0.0 {
122            self.atr.value
123        } else {
124            (high - low).abs().max(self.atr_floor)
125        };
126
127        if atr_val == 0.0 {
128            self.value = 0.0;
129            return;
130        }
131
132        let relative_volume = volume / avg_vol;
133        let buy_pressure = ((close - low) / atr_val) * relative_volume;
134        let sell_pressure = ((high - close) / atr_val) * relative_volume;
135
136        self.value = buy_pressure - sell_pressure;
137        self.value_cumulative += self.value;
138
139        if self.atr.initialized && self.average_volume.initialized() && !self.initialized {
140            self.initialized = true;
141        }
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use rstest::rstest;
148
149    use super::*;
150    use crate::stubs::{bar_ethusdt_binance_minute_bid, pressure_10};
151
152    #[rstest]
153    fn test_name_returns_expected_string(pressure_10: Pressure) {
154        assert_eq!(pressure_10.name(), "Pressure");
155    }
156
157    #[rstest]
158    fn test_str_repr_returns_expected_string() {
159        let pressure = Pressure::new(10, Some(MovingAverageType::Exponential), None);
160        assert_eq!(format!("{pressure}"), "Pressure(10,EXPONENTIAL)");
161    }
162
163    #[rstest]
164    fn test_period_returns_expected_value(pressure_10: Pressure) {
165        assert_eq!(pressure_10.period, 10);
166    }
167
168    #[rstest]
169    fn test_initialized_without_inputs_returns_false(pressure_10: Pressure) {
170        assert!(!pressure_10.initialized());
171    }
172
173    #[rstest]
174    fn test_value_with_all_higher_inputs_returns_expected_value() {
175        let mut pressure = Pressure::new(10, Some(MovingAverageType::Exponential), None);
176
177        let high_values = [
178            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
179        ];
180        let low_values = [
181            0.9, 1.9, 2.9, 3.9, 4.9, 5.9, 6.9, 7.9, 8.9, 9.9, 10.1, 10.2, 10.3, 11.1, 11.4,
182        ];
183        let close_values = [
184            1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, 10.1, 11.1, 12.1, 13.1, 14.1, 15.1,
185        ];
186        let volume_values = [
187            100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1100.0, 1200.0,
188            1300.0, 1400.0, 1500.0,
189        ];
190
191        let mut expected_cumulative = 0.0;
192        let mut expected_last = 0.0;
193
194        for i in 0..15 {
195            pressure.update_raw(
196                high_values[i],
197                low_values[i],
198                close_values[i],
199                volume_values[i],
200            );
201
202            let atr_val = if pressure.atr.value > 0.0 {
203                pressure.atr.value
204            } else {
205                (high_values[i] - low_values[i])
206                    .abs()
207                    .max(pressure.atr_floor)
208            };
209            let avg_vol = pressure.average_volume.value();
210            if avg_vol != 0.0 && atr_val != 0.0 {
211                let relative_volume = volume_values[i] / avg_vol;
212                let buy_pressure = ((close_values[i] - low_values[i]) / atr_val) * relative_volume;
213                let sell_pressure =
214                    ((high_values[i] - close_values[i]) / atr_val) * relative_volume;
215                let bar_value = buy_pressure - sell_pressure;
216                expected_cumulative += bar_value;
217                expected_last = bar_value;
218            }
219        }
220
221        assert!(pressure.initialized());
222        assert!((pressure.value - expected_last).abs() < 1e-6);
223        assert!((pressure.value_cumulative - expected_cumulative).abs() < 1e-6);
224    }
225
226    #[rstest]
227    fn test_handle_bar(mut pressure_10: Pressure, bar_ethusdt_binance_minute_bid: Bar) {
228        pressure_10.handle_bar(&bar_ethusdt_binance_minute_bid);
229        assert_eq!(pressure_10.value, -0.018_181_818_181_818_132);
230        assert_eq!(pressure_10.value_cumulative, -0.018_181_818_181_818_132);
231        assert!(pressure_10.has_inputs);
232        assert!(!pressure_10.initialized);
233    }
234
235    #[rstest]
236    fn test_reset_successfully_returns_indicator_to_fresh_state(mut pressure_10: Pressure) {
237        pressure_10.update_raw(1.00020, 1.00050, 1.00070, 100.0);
238        pressure_10.update_raw(1.00030, 1.00060, 1.00080, 200.0);
239        pressure_10.update_raw(1.00070, 1.00080, 1.00090, 300.0);
240
241        pressure_10.reset();
242
243        assert!(!pressure_10.initialized());
244        assert_eq!(pressure_10.value, 0.0);
245        assert_eq!(pressure_10.value_cumulative, 0.0);
246        assert!(!pressure_10.has_inputs);
247    }
248
249    #[rstest]
250    fn test_ma_type_default_and_override() {
251        let pressure_default = Pressure::new(10, None, None);
252        assert_eq!(pressure_default.ma_type, MovingAverageType::Exponential);
253
254        let pressure_simple = Pressure::new(10, Some(MovingAverageType::Simple), None);
255        assert_eq!(pressure_simple.ma_type, MovingAverageType::Simple);
256    }
257
258    #[rstest]
259    fn test_initialized_after_enough_inputs() {
260        let mut pressure = Pressure::new(3, Some(MovingAverageType::Exponential), None);
261        for _ in 0..3 {
262            pressure.update_raw(1.3, 1.0, 1.1, 100.0);
263        }
264        assert!(pressure.initialized());
265    }
266
267    #[rstest]
268    fn test_atr_floor_applied_to_zero_range() {
269        let mut pressure = Pressure::new(1, Some(MovingAverageType::Simple), Some(0.5));
270        pressure.update_raw(1.5, 1.0, 1.2, 100.0);
271        assert!((pressure.value + 0.2).abs() < 1e-6);
272        assert!(!pressure.value_cumulative.is_nan());
273    }
274}