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};
28
29#[pymethods]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl MovingAverageConvergenceDivergence {
32    /// Creates a new `MovingAverageConvergenceDivergence` instance.
33    #[new]
34    #[pyo3(signature = (fast_period, slow_period, ma_type=None, price_type=None))]
35    #[must_use]
36    pub fn py_new(
37        fast_period: usize,
38        slow_period: usize,
39        ma_type: Option<MovingAverageType>,
40        price_type: Option<PriceType>,
41    ) -> Self {
42        Self::new(fast_period, slow_period, ma_type, price_type)
43    }
44
45    fn __repr__(&self) -> String {
46        format!(
47            "MovingAverageConvergenceDivergence({},{},{:?})",
48            self.fast_period, self.slow_period, self.price_type
49        )
50    }
51
52    #[getter]
53    #[pyo3(name = "name")]
54    fn py_name(&self) -> String {
55        self.name()
56    }
57
58    #[getter]
59    #[pyo3(name = "fast_period")]
60    const fn py_fast_period(&self) -> usize {
61        self.fast_period
62    }
63
64    #[getter]
65    #[pyo3(name = "slow_period")]
66    const fn py_slow_period(&self) -> usize {
67        self.slow_period
68    }
69
70    #[getter]
71    #[pyo3(name = "count")]
72    fn py_count(&self) -> usize {
73        self.count()
74    }
75
76    #[getter]
77    #[pyo3(name = "has_inputs")]
78    fn py_has_inputs(&self) -> bool {
79        self.has_inputs()
80    }
81
82    #[getter]
83    #[pyo3(name = "initialized")]
84    const fn py_initialized(&self) -> bool {
85        self.initialized
86    }
87
88    #[getter]
89    #[pyo3(name = "value")]
90    const fn py_value(&self) -> f64 {
91        self.value
92    }
93
94    #[pyo3(name = "handle_quote_tick")]
95    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
96        self.handle_quote(quote).map_err(to_pyvalue_err)
97    }
98
99    #[pyo3(name = "handle_trade_tick")]
100    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
101        self.handle_trade(trade);
102    }
103
104    #[pyo3(name = "handle_bar")]
105    fn py_handle_bar(&mut self, bar: &Bar) {
106        self.handle_bar(bar);
107    }
108
109    #[pyo3(name = "reset")]
110    fn py_reset(&mut self) {
111        self.reset();
112    }
113
114    #[pyo3(name = "update_raw")]
115    fn py_update_raw(&mut self, close: f64) {
116        self.update_raw(close);
117    }
118}