Skip to main content

nautilus_indicators/python/momentum/
macd.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::{
18    data::{Bar, QuoteTick, TradeTick},
19    enums::PriceType,
20};
21use pyo3::prelude::*;
22
23use crate::{
24    average::MovingAverageType,
25    indicator::{Indicator, MovingAverage},
26    momentum::macd::MovingAverageConvergenceDivergence,
27    python::float_precision,
28};
29
30#[pymethods]
31#[pyo3_stub_gen::derive::gen_stub_pymethods]
32impl MovingAverageConvergenceDivergence {
33    /// Moving average convergence/divergence, signal, and histogram.
34    #[new]
35    #[pyo3(signature = (fast_period, slow_period, signal_period=None, ma_type=None, price_type=None))]
36    pub fn py_new(
37        fast_period: usize,
38        slow_period: usize,
39        signal_period: Option<usize>,
40        ma_type: Option<MovingAverageType>,
41        price_type: Option<PriceType>,
42    ) -> PyResult<Self> {
43        Self::new_checked(fast_period, slow_period, signal_period, ma_type, price_type)
44            .map_err(to_pyvalue_err)
45    }
46
47    fn __repr__(&self) -> String {
48        format!(
49            "MovingAverageConvergenceDivergence({},{},{:?})",
50            self.fast_period, self.slow_period, self.price_type
51        )
52    }
53
54    #[getter]
55    #[pyo3(name = "ma_type")]
56    const fn py_ma_type(&self) -> MovingAverageType {
57        self.ma_type
58    }
59
60    #[getter]
61    #[pyo3(name = "price_type")]
62    const fn py_price_type(&self) -> PriceType {
63        self.price_type
64    }
65
66    #[getter]
67    #[pyo3(name = "name")]
68    fn py_name(&self) -> String {
69        self.name()
70    }
71
72    #[getter]
73    #[pyo3(name = "fast_period")]
74    const fn py_fast_period(&self) -> usize {
75        self.fast_period
76    }
77
78    #[getter]
79    #[pyo3(name = "slow_period")]
80    const fn py_slow_period(&self) -> usize {
81        self.slow_period
82    }
83
84    #[getter]
85    #[pyo3(name = "signal_period")]
86    const fn py_signal_period(&self) -> usize {
87        self.signal_period
88    }
89
90    #[getter]
91    #[pyo3(name = "signal")]
92    const fn py_signal(&self) -> f64 {
93        self.signal
94    }
95
96    #[getter]
97    #[pyo3(name = "histogram")]
98    const fn py_histogram(&self) -> f64 {
99        self.histogram
100    }
101
102    #[getter]
103    #[pyo3(name = "count")]
104    fn py_count(&self) -> usize {
105        self.count()
106    }
107
108    #[getter]
109    #[pyo3(name = "has_inputs")]
110    fn py_has_inputs(&self) -> bool {
111        self.has_inputs()
112    }
113
114    #[getter]
115    #[pyo3(name = "initialized")]
116    const fn py_initialized(&self) -> bool {
117        self.initialized
118    }
119
120    #[getter]
121    #[pyo3(name = "value")]
122    const fn py_value(&self) -> f64 {
123        self.value
124    }
125
126    #[pyo3(name = "handle_quote_tick")]
127    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
128        float_precision::check_quote(quote)?;
129        self.handle_quote(quote).map_err(to_pyvalue_err)
130    }
131
132    #[pyo3(name = "handle_trade_tick")]
133    fn py_handle_trade_tick(&mut self, trade: &TradeTick) -> PyResult<()> {
134        float_precision::check_trade(trade)?;
135        self.handle_trade(trade);
136        Ok(())
137    }
138
139    #[pyo3(name = "handle_bar")]
140    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
141        float_precision::check_bar(bar)?;
142        self.handle_bar(bar);
143        Ok(())
144    }
145
146    #[pyo3(name = "reset")]
147    fn py_reset(&mut self) {
148        self.reset();
149    }
150
151    #[pyo3(name = "update_raw")]
152    fn py_update_raw(&mut self, close: f64) {
153        self.update_raw(close);
154    }
155}