Skip to main content

nautilus_indicators/python/average/
vidya.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::{MovingAverageType, vidya::VariableIndexDynamicAverage},
25    indicator::{Indicator, MovingAverage},
26    python::float_precision,
27};
28
29#[pyo3_stub_gen::derive::gen_stub_pymethods]
30#[pymethods]
31impl VariableIndexDynamicAverage {
32    /// Variable index dynamic average.
33    #[new]
34    #[pyo3(signature = (period, price_type=None, cmo_ma_type=None, cmo_period=None))]
35    pub fn py_new(
36        period: usize,
37        price_type: Option<PriceType>,
38        cmo_ma_type: Option<MovingAverageType>,
39        cmo_period: Option<usize>,
40    ) -> PyResult<Self> {
41        Self::new_checked(
42            period,
43            cmo_period.unwrap_or(period),
44            price_type,
45            cmo_ma_type,
46        )
47        .map_err(to_pyvalue_err)
48    }
49
50    fn __repr__(&self) -> String {
51        format!(
52            "VariableIndexDynamicAverage({},{:?})",
53            self.period, self.price_type
54        )
55    }
56
57    #[getter]
58    #[pyo3(name = "name")]
59    fn py_name(&self) -> String {
60        self.name()
61    }
62
63    #[getter]
64    #[pyo3(name = "period")]
65    const fn py_period(&self) -> usize {
66        self.period
67    }
68
69    #[getter]
70    #[pyo3(name = "cmo_period")]
71    const fn py_cmo_period(&self) -> usize {
72        self.cmo_period
73    }
74
75    #[getter]
76    #[pyo3(name = "price_type")]
77    const fn py_price_type(&self) -> PriceType {
78        self.price_type
79    }
80
81    #[getter]
82    #[pyo3(name = "count")]
83    fn py_count(&self) -> usize {
84        self.count()
85    }
86
87    #[getter]
88    #[pyo3(name = "alpha")]
89    const fn py_alpha(&self) -> f64 {
90        self.alpha
91    }
92
93    #[getter]
94    #[pyo3(name = "has_inputs")]
95    fn py_has_inputs(&self) -> bool {
96        self.has_inputs()
97    }
98
99    #[getter]
100    #[pyo3(name = "initialized")]
101    const fn py_initialized(&self) -> bool {
102        self.initialized
103    }
104
105    #[getter]
106    #[pyo3(name = "cmo_pct")]
107    const fn py_cmo_pct(&self) -> f64 {
108        self.cmo_pct
109    }
110
111    #[getter]
112    #[pyo3(name = "value")]
113    const fn py_value(&self) -> f64 {
114        self.value
115    }
116
117    #[pyo3(name = "handle_quote_tick")]
118    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
119        float_precision::check_quote(quote)?;
120        self.handle_quote(quote).map_err(to_pyvalue_err)
121    }
122
123    #[pyo3(name = "handle_trade_tick")]
124    fn py_handle_trade_tick(&mut self, trade: &TradeTick) -> PyResult<()> {
125        float_precision::check_trade(trade)?;
126        self.handle_trade(trade);
127        Ok(())
128    }
129
130    #[pyo3(name = "handle_bar")]
131    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
132        float_precision::check_bar(bar)?;
133        self.handle_bar(bar);
134        Ok(())
135    }
136
137    #[pyo3(name = "reset")]
138    fn py_reset(&mut self) {
139        self.reset();
140    }
141
142    #[pyo3(name = "update_raw")]
143    fn py_update_raw(&mut self, value: f64) {
144        self.update_raw(value);
145    }
146}