Skip to main content

nautilus_indicators/python/momentum/
roc.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 nautilus_core::python::to_pyvalue_err;
17use nautilus_model::data::{Bar, QuoteTick, TradeTick};
18use pyo3::prelude::*;
19
20use crate::{
21    indicator::Indicator,
22    momentum::roc::{RateOfChange, RateOfChangeMode},
23    python::float_precision,
24};
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods]
28impl RateOfChangeMode {
29    const fn __hash__(&self) -> isize {
30        *self as isize
31    }
32}
33
34#[pymethods]
35#[pyo3_stub_gen::derive::gen_stub_pymethods]
36impl RateOfChange {
37    /// Rate of change with configurable output units.
38    #[new]
39    #[pyo3(signature = (period, use_log=None, mode=None))]
40    pub fn py_new(
41        period: usize,
42        use_log: Option<bool>,
43        mode: Option<RateOfChangeMode>,
44    ) -> PyResult<Self> {
45        let mode = mode.unwrap_or(if use_log.unwrap_or(false) {
46            RateOfChangeMode::Log
47        } else {
48            RateOfChangeMode::Percentage
49        });
50        Self::new_checked(period, mode).map_err(to_pyvalue_err)
51    }
52
53    fn __repr__(&self) -> String {
54        format!("RateOfChange({},{})", self.period, self.use_log)
55    }
56
57    #[getter]
58    #[pyo3(name = "name")]
59    fn py_name(&self) -> String {
60        self.name()
61    }
62
63    #[getter]
64    #[pyo3(name = "period")]
65    const fn py_period(&self) -> usize {
66        self.period
67    }
68
69    #[getter]
70    #[pyo3(name = "use_log")]
71    const fn py_use_log(&self) -> bool {
72        self.use_log
73    }
74
75    #[getter]
76    #[pyo3(name = "mode")]
77    const fn py_mode(&self) -> RateOfChangeMode {
78        self.mode
79    }
80
81    #[getter]
82    #[pyo3(name = "has_inputs")]
83    fn py_has_inputs(&self) -> bool {
84        self.has_inputs()
85    }
86
87    #[getter]
88    #[pyo3(name = "value")]
89    const fn py_value(&self) -> f64 {
90        self.value
91    }
92
93    #[getter]
94    #[pyo3(name = "initialized")]
95    const fn py_initialized(&self) -> bool {
96        self.initialized
97    }
98
99    #[pyo3(name = "update_raw")]
100    fn py_update_raw(&mut self, price: f64) {
101        self.update_raw(price);
102    }
103
104    #[pyo3(name = "handle_quote_tick")]
105    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
106        self.handle_quote(quote).map_err(to_pyvalue_err)
107    }
108
109    #[pyo3(name = "handle_trade_tick")]
110    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
111        self.handle_trade(trade);
112    }
113
114    #[pyo3(name = "handle_bar")]
115    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
116        float_precision::check_bar(bar)?;
117        self.handle_bar(bar);
118        Ok(())
119    }
120
121    #[pyo3(name = "reset")]
122    fn py_reset(&mut self) {
123        self.reset();
124    }
125}