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_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::ama::AdaptiveMovingAverage,
25    indicator::{Indicator, MovingAverage},
26    python::float_precision,
27};
28
29#[pyo3_stub_gen::derive::gen_stub_pymethods]
30#[pymethods]
31impl AdaptiveMovingAverage {
32    /// An indicator which calculates an adaptive moving average (AMA) across a
33    /// rolling window. Developed by Perry Kaufman, the AMA is a moving average
34    /// designed to account for market noise and volatility. The AMA will closely
35    /// follow prices when the price swings are relatively small and the noise is
36    /// low. The AMA will increase lag when the price swings increase.
37    #[new]
38    #[pyo3(signature = (period_efficiency_ratio, period_fast, period_slow, price_type=None))]
39    pub fn py_new(
40        period_efficiency_ratio: usize,
41        period_fast: usize,
42        period_slow: usize,
43        price_type: Option<PriceType>,
44    ) -> PyResult<Self> {
45        Self::new_checked(
46            period_efficiency_ratio,
47            period_fast,
48            period_slow,
49            price_type,
50        )
51        .map_err(to_pyvalue_err)
52    }
53
54    fn __repr__(&self) -> String {
55        format!(
56            "WeightedMovingAverage({}({},{},{})",
57            self.name(),
58            self.period_efficiency_ratio,
59            self.period_fast,
60            self.period_slow
61        )
62    }
63
64    #[getter]
65    #[pyo3(name = "name")]
66    fn py_name(&self) -> String {
67        self.name()
68    }
69
70    #[getter]
71    #[pyo3(name = "period_efficiency_ratio")]
72    const fn py_period_efficiency_ratio(&self) -> usize {
73        self.period_efficiency_ratio
74    }
75
76    #[getter]
77    #[pyo3(name = "period_fast")]
78    const fn py_period_fast(&self) -> usize {
79        self.period_fast
80    }
81
82    #[getter]
83    #[pyo3(name = "period_slow")]
84    const fn py_period_slow(&self) -> usize {
85        self.period_slow
86    }
87
88    #[getter]
89    #[pyo3(name = "alpha_fast")]
90    const fn py_alpha_fast(&self) -> f64 {
91        self.alpha_fast()
92    }
93
94    #[getter]
95    #[pyo3(name = "alpha_slow")]
96    const fn py_alpha_slow(&self) -> f64 {
97        self.alpha_slow()
98    }
99
100    #[getter]
101    #[pyo3(name = "alpha_diff")]
102    fn py_alpha_diff(&self) -> f64 {
103        self.alpha_diff()
104    }
105
106    #[getter]
107    #[pyo3(name = "price_type")]
108    const fn py_price_type(&self) -> PriceType {
109        self.price_type
110    }
111
112    #[getter]
113    #[pyo3(name = "value")]
114    const fn py_value(&self) -> f64 {
115        self.value
116    }
117
118    #[getter]
119    #[pyo3(name = "count")]
120    const fn py_count(&self) -> usize {
121        self.count
122    }
123
124    #[getter]
125    #[pyo3(name = "has_inputs")]
126    fn py_has_inputs(&self) -> bool {
127        self.has_inputs()
128    }
129
130    #[getter]
131    #[pyo3(name = "initialized")]
132    const fn py_initialized(&self) -> bool {
133        self.initialized
134    }
135
136    #[pyo3(name = "handle_quote_tick")]
137    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
138        float_precision::check_quote(quote)?;
139        self.handle_quote(quote).map_err(to_pyvalue_err)
140    }
141
142    #[pyo3(name = "handle_trade_tick")]
143    fn py_handle_trade_tick(&mut self, trade: &TradeTick) -> PyResult<()> {
144        float_precision::check_trade(trade)?;
145        self.handle_trade(trade);
146        Ok(())
147    }
148
149    #[pyo3(name = "handle_bar")]
150    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
151        float_precision::check_bar(bar)?;
152        self.handle_bar(bar);
153        Ok(())
154    }
155
156    #[pyo3(name = "reset")]
157    fn py_reset(&mut self) {
158        self.reset();
159    }
160
161    #[pyo3(name = "update_raw")]
162    fn py_update_raw(&mut self, value: f64) {
163        self.update_raw(value);
164    }
165}