Skip to main content

nautilus_indicators/python/average/
hma.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::hma::HullMovingAverage,
25    indicator::{Indicator, MovingAverage},
26    python::float_precision,
27};
28
29#[pyo3_stub_gen::derive::gen_stub_pymethods]
30#[pymethods]
31impl HullMovingAverage {
32    /// An indicator which calculates a Hull Moving Average (HMA) across a rolling
33    /// window. The HMA, developed by Alan Hull, is an extremely fast and smooth
34    /// moving average.
35    #[new]
36    #[pyo3(signature = (period, price_type=None))]
37    fn py_new(period: usize, price_type: Option<PriceType>) -> PyResult<Self> {
38        Self::new_checked(period, price_type).map_err(to_pyvalue_err)
39    }
40
41    fn __repr__(&self) -> String {
42        format!("HullMovingAverage({})", self.period)
43    }
44
45    #[getter]
46    #[pyo3(name = "name")]
47    fn py_name(&self) -> String {
48        self.name()
49    }
50
51    #[getter]
52    #[pyo3(name = "period")]
53    const fn py_period(&self) -> usize {
54        self.period
55    }
56
57    #[getter]
58    #[pyo3(name = "price_type")]
59    const fn py_price_type(&self) -> PriceType {
60        self.price_type
61    }
62
63    #[getter]
64    #[pyo3(name = "count")]
65    const fn py_count(&self) -> usize {
66        self.count
67    }
68
69    #[getter]
70    #[pyo3(name = "value")]
71    const fn py_value(&self) -> f64 {
72        self.value
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    #[pyo3(name = "handle_quote_tick")]
88    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
89        float_precision::check_quote(quote)?;
90        self.handle_quote(quote).map_err(to_pyvalue_err)
91    }
92
93    #[pyo3(name = "handle_trade_tick")]
94    fn py_handle_trade_tick(&mut self, trade: &TradeTick) -> PyResult<()> {
95        float_precision::check_trade(trade)?;
96        self.handle_trade(trade);
97        Ok(())
98    }
99
100    #[pyo3(name = "handle_bar")]
101    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
102        float_precision::check_bar(bar)?;
103        self.handle_bar(bar);
104        Ok(())
105    }
106
107    #[pyo3(name = "reset")]
108    fn py_reset(&mut self) {
109        self.reset();
110    }
111
112    #[pyo3(name = "update_raw")]
113    fn py_update_raw(&mut self, value: f64) {
114        self.update_raw(value);
115    }
116}