Skip to main content

nautilus_indicators/python/average/
ama.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::ama::AdaptiveMovingAverage,
24    indicator::{Indicator, MovingAverage},
25};
26
27#[pymethods]
28#[pyo3_stub_gen::derive::gen_stub_pymethods]
29impl AdaptiveMovingAverage {
30    /// An indicator which calculates an adaptive moving average (AMA) across a
31    /// rolling window. Developed by Perry Kaufman, the AMA is a moving average
32    /// designed to account for market noise and volatility. The AMA will closely
33    /// follow prices when the price swings are relatively small and the noise is
34    /// low. The AMA will increase lag when the price swings increase.
35    #[new]
36    #[pyo3(signature = (period_efficiency_ratio, period_fast, period_slow, price_type=None))]
37    #[must_use]
38    pub fn py_new(
39        period_efficiency_ratio: usize,
40        period_fast: usize,
41        period_slow: usize,
42        price_type: Option<PriceType>,
43    ) -> Self {
44        Self::new(
45            period_efficiency_ratio,
46            period_fast,
47            period_slow,
48            price_type,
49        )
50    }
51
52    fn __repr__(&self) -> String {
53        format!(
54            "WeightedMovingAverage({}({},{},{})",
55            self.name(),
56            self.period_efficiency_ratio,
57            self.period_fast,
58            self.period_slow
59        )
60    }
61
62    #[getter]
63    #[pyo3(name = "name")]
64    fn py_name(&self) -> String {
65        self.name()
66    }
67
68    #[getter]
69    #[pyo3(name = "count")]
70    const fn py_count(&self) -> usize {
71        self.count
72    }
73
74    #[getter]
75    #[pyo3(name = "has_inputs")]
76    fn py_has_inputs(&self) -> bool {
77        self.has_inputs()
78    }
79
80    #[getter]
81    #[pyo3(name = "initialized")]
82    const fn py_initialized(&self) -> bool {
83        self.initialized
84    }
85
86    #[pyo3(name = "handle_quote_tick")]
87    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) {
88        self.py_update_raw(quote.extract_price(self.price_type).into());
89    }
90
91    #[pyo3(name = "handle_trade_tick")]
92    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
93        self.update_raw((&trade.price).into());
94    }
95
96    #[pyo3(name = "handle_bar")]
97    fn py_handle_bar(&mut self, bar: &Bar) {
98        self.update_raw((&bar.close).into());
99    }
100
101    #[pyo3(name = "reset")]
102    const fn py_reset(&mut self) {
103        self.reset();
104    }
105
106    #[pyo3(name = "update_raw")]
107    fn py_update_raw(&mut self, value: f64) {
108        self.update_raw(value);
109    }
110}