Skip to main content

nautilus_indicators/python/momentum/
stochastics.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;
18use pyo3::prelude::*;
19
20use crate::{
21    average::MovingAverageType,
22    indicator::Indicator,
23    momentum::stochastics::{Stochastics, StochasticsDMethod},
24    python::float_precision,
25};
26
27#[pymethods]
28#[pyo3_stub_gen::derive::gen_stub_pymethods]
29impl StochasticsDMethod {
30    const fn __hash__(&self) -> isize {
31        *self as isize
32    }
33}
34
35#[pymethods]
36#[pyo3_stub_gen::derive::gen_stub_pymethods]
37impl Stochastics {
38    /// Stochastic oscillator with smoothed K and D outputs.
39    ///
40    /// Defaults to `slowing = 1`, `ma_type = Simple`, and `d_method = MovingAverage`,
41    /// so D is a simple moving average of K. Select `StochasticsDMethod.Ratio`
42    /// for the legacy Nautilus range-weighted D calculation.
43    #[new]
44    #[pyo3(signature = (period_k, period_d, slowing=None, ma_type=None, d_method=None))]
45    pub fn py_new(
46        period_k: usize,
47        period_d: usize,
48        slowing: Option<usize>,
49        ma_type: Option<MovingAverageType>,
50        d_method: Option<StochasticsDMethod>,
51    ) -> PyResult<Self> {
52        Self::new_checked(
53            period_k,
54            period_d,
55            slowing.unwrap_or(Self::DEFAULT_SLOWING),
56            ma_type.unwrap_or(Self::DEFAULT_MA_TYPE),
57            d_method.unwrap_or(Self::DEFAULT_D_METHOD),
58        )
59        .map_err(to_pyvalue_err)
60    }
61
62    fn __repr__(&self) -> String {
63        format!(
64            "Stochastics({},{},{},{:?},{:?})",
65            self.period_k, self.period_d, self.slowing, self.ma_type, self.d_method
66        )
67    }
68
69    #[getter]
70    #[pyo3(name = "name")]
71    fn py_name(&self) -> String {
72        self.name()
73    }
74
75    #[getter]
76    #[pyo3(name = "period_k")]
77    const fn py_period_k(&self) -> usize {
78        self.period_k
79    }
80
81    #[getter]
82    #[pyo3(name = "period_d")]
83    const fn py_period_d(&self) -> usize {
84        self.period_d
85    }
86
87    #[getter]
88    #[pyo3(name = "slowing")]
89    const fn py_slowing(&self) -> usize {
90        self.slowing
91    }
92
93    #[getter]
94    #[pyo3(name = "ma_type")]
95    const fn py_ma_type(&self) -> MovingAverageType {
96        self.ma_type
97    }
98
99    #[getter]
100    #[pyo3(name = "d_method")]
101    const fn py_d_method(&self) -> StochasticsDMethod {
102        self.d_method
103    }
104
105    #[getter]
106    #[pyo3(name = "has_inputs")]
107    fn py_has_inputs(&self) -> bool {
108        self.has_inputs()
109    }
110
111    #[getter]
112    #[pyo3(name = "value_k")]
113    const fn py_value_k(&self) -> f64 {
114        self.value_k
115    }
116
117    #[getter]
118    #[pyo3(name = "value_d")]
119    const fn py_value_d(&self) -> f64 {
120        self.value_d
121    }
122
123    #[getter]
124    #[pyo3(name = "initialized")]
125    const fn py_initialized(&self) -> bool {
126        self.initialized
127    }
128
129    /// Updates the indicator with raw price values.
130    ///
131    /// # Parameters
132    ///
133    /// - `high`: The high price for the period.
134    /// - `low`: The low price for the period.
135    /// - `close`: The close price for the period.
136    #[pyo3(name = "update_raw")]
137    fn py_update_raw(&mut self, high: f64, low: f64, close: f64) {
138        self.update_raw(high, low, close);
139    }
140
141    #[pyo3(name = "handle_bar")]
142    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
143        float_precision::check_bar(bar)?;
144        self.handle_bar(bar);
145        Ok(())
146    }
147
148    #[pyo3(name = "reset")]
149    fn py_reset(&mut self) {
150        self.reset();
151    }
152}