nautilus_indicators/python/average/
ama.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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::ama::AdaptiveMovingAverage,
24    indicator::{Indicator, MovingAverage},
25};
26
27#[pymethods]
28impl AdaptiveMovingAverage {
29    #[new]
30    #[pyo3(signature = (period_efficiency_ratio, period_fast, period_slow, price_type=None))]
31    #[must_use]
32    pub fn py_new(
33        period_efficiency_ratio: usize,
34        period_fast: usize,
35        period_slow: usize,
36        price_type: Option<PriceType>,
37    ) -> Self {
38        Self::new(
39            period_efficiency_ratio,
40            period_fast,
41            period_slow,
42            price_type,
43        )
44    }
45
46    fn __repr__(&self) -> String {
47        format!(
48            "WeightedMovingAverage({}({},{},{})",
49            self.name(),
50            self.period_efficiency_ratio,
51            self.period_fast,
52            self.period_slow
53        )
54    }
55
56    #[getter]
57    #[pyo3(name = "name")]
58    fn py_name(&self) -> String {
59        self.name()
60    }
61
62    #[getter]
63    #[pyo3(name = "count")]
64    const fn py_count(&self) -> usize {
65        self.count
66    }
67
68    #[getter]
69    #[pyo3(name = "has_inputs")]
70    fn py_has_inputs(&self) -> bool {
71        self.has_inputs()
72    }
73
74    #[getter]
75    #[pyo3(name = "initialized")]
76    const fn py_initialized(&self) -> bool {
77        self.initialized
78    }
79
80    #[pyo3(name = "handle_quote_tick")]
81    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) {
82        self.py_update_raw(quote.extract_price(self.price_type).into());
83    }
84
85    #[pyo3(name = "handle_trade_tick")]
86    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
87        self.update_raw((&trade.price).into());
88    }
89
90    #[pyo3(name = "handle_bar")]
91    fn py_handle_bar(&mut self, bar: &Bar) {
92        self.update_raw((&bar.close).into());
93    }
94
95    #[pyo3(name = "reset")]
96    const fn py_reset(&mut self) {
97        self.reset();
98    }
99
100    #[pyo3(name = "update_raw")]
101    fn py_update_raw(&mut self, value: f64) {
102        self.update_raw(value);
103    }
104}