Skip to main content

nautilus_indicators/python/average/
zscore.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::{average::zscore::ZScore, indicator::Indicator};
24
25#[pyo3_stub_gen::derive::gen_stub_pymethods]
26#[pymethods]
27impl ZScore {
28    /// Creates a new `ZScore` instance.
29    ///
30    /// Computes `(x - mean) / std` using sample standard deviation. The window
31    /// expands until `period` observations, then rolls at that length. With one
32    /// observation or a finite constant window, `mean` matches the input exactly,
33    /// while `std` and `value` are 0. Other zero `std` values produce `value` 0;
34    /// non-finite `std` values produce `value` `NaN`. `price_type` affects only
35    /// quote handling.
36    #[new]
37    #[pyo3(signature = (period, price_type=None))]
38    fn py_new(period: i64, price_type: Option<PriceType>) -> PyResult<Self> {
39        if period < 0 {
40            return Err(to_pyvalue_err("`period` must be at least 2"));
41        }
42
43        let period = usize::try_from(period).map_err(to_pyvalue_err)?;
44        Self::new_checked(period, price_type).map_err(to_pyvalue_err)
45    }
46
47    fn __repr__(&self) -> String {
48        format!("ZScore({})", self.period)
49    }
50
51    #[getter]
52    #[pyo3(name = "name")]
53    fn py_name(&self) -> String {
54        self.name()
55    }
56
57    #[getter]
58    #[pyo3(name = "period")]
59    const fn py_period(&self) -> usize {
60        self.period
61    }
62
63    #[getter]
64    #[pyo3(name = "price_type")]
65    const fn py_price_type(&self) -> PriceType {
66        self.price_type
67    }
68
69    #[getter]
70    #[pyo3(name = "count")]
71    const fn py_count(&self) -> usize {
72        self.count
73    }
74
75    #[getter]
76    #[pyo3(name = "value")]
77    const fn py_value(&self) -> f64 {
78        self.value
79    }
80
81    #[getter]
82    #[pyo3(name = "mean")]
83    const fn py_mean(&self) -> f64 {
84        self.mean
85    }
86
87    #[getter]
88    #[pyo3(name = "std")]
89    const fn py_std(&self) -> f64 {
90        self.std
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    #[pyo3(name = "handle_quote_tick")]
106    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
107        self.handle_quote(quote).map_err(to_pyvalue_err)
108    }
109
110    #[pyo3(name = "handle_trade_tick")]
111    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
112        self.handle_trade(trade);
113    }
114
115    #[pyo3(name = "handle_bar")]
116    fn py_handle_bar(&mut self, bar: &Bar) {
117        self.handle_bar(bar);
118    }
119
120    #[pyo3(name = "reset")]
121    fn py_reset(&mut self) {
122        self.reset();
123    }
124
125    /// Updates the indicator with a raw observation.
126    #[pyo3(name = "update_raw")]
127    fn py_update_raw(&mut self, value: f64) {
128        self.update_raw(value);
129    }
130}