nautilus_indicators/python/average/
wma.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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::wma::WeightedMovingAverage,
25    indicator::{Indicator, MovingAverage},
26};
27
28#[pymethods]
29impl WeightedMovingAverage {
30    /// Creates a new [`WeightedMovingAverage`] instance.
31    ///
32    /// # Errors
33    ///
34    /// Returns a `PyErr` if `period` does not equal the length of `weights`.
35    #[new]
36    #[pyo3(signature = (period, weights, price_type=None))]
37    pub fn py_new(
38        period: usize,
39        weights: Vec<f64>,
40        price_type: Option<PriceType>,
41    ) -> PyResult<Self> {
42        Self::new_checked(period, weights, price_type).map_err(to_pyvalue_err)
43    }
44
45    fn __repr__(&self) -> String {
46        format!("WeightedMovingAverage({},{:?})", self.period, self.weights)
47    }
48
49    #[getter]
50    #[pyo3(name = "name")]
51    fn py_name(&self) -> String {
52        self.name()
53    }
54
55    #[getter]
56    #[pyo3(name = "period")]
57    const fn py_period(&self) -> usize {
58        self.period
59    }
60
61    #[getter]
62    #[pyo3(name = "count")]
63    fn py_count(&self) -> usize {
64        self.count()
65    }
66
67    #[getter]
68    #[pyo3(name = "has_inputs")]
69    fn py_has_inputs(&self) -> bool {
70        self.has_inputs()
71    }
72
73    #[getter]
74    #[pyo3(name = "initialized")]
75    const fn py_initialized(&self) -> bool {
76        self.initialized
77    }
78
79    #[pyo3(name = "handle_quote_tick")]
80    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) {
81        self.py_update_raw(quote.extract_price(self.price_type).into());
82    }
83
84    #[pyo3(name = "handle_trade_tick")]
85    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
86        self.update_raw((&trade.price).into());
87    }
88
89    #[pyo3(name = "handle_bar")]
90    fn py_handle_bar(&mut self, bar: &Bar) {
91        self.update_raw((&bar.close).into());
92    }
93
94    #[pyo3(name = "reset")]
95    fn py_reset(&mut self) {
96        self.reset();
97    }
98
99    #[pyo3(name = "update_raw")]
100    fn py_update_raw(&mut self, value: f64) {
101        self.update_raw(value);
102    }
103}