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