Skip to main content

nautilus_indicators/python/average/
lr.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_model::data::Bar;
17use pyo3::prelude::*;
18
19use crate::{average::lr::LinearRegression, indicator::Indicator};
20
21#[pymethods]
22#[pyo3_stub_gen::derive::gen_stub_pymethods]
23impl LinearRegression {
24    /// Creates a new `LinearRegression` instance.
25    #[new]
26    #[must_use]
27    pub fn py_new(period: usize) -> Self {
28        Self::new(period)
29    }
30
31    fn __repr__(&self) -> String {
32        format!("LinearRegression({})", self.period)
33    }
34
35    #[getter]
36    #[pyo3(name = "name")]
37    fn py_name(&self) -> String {
38        self.name()
39    }
40
41    #[getter]
42    #[pyo3(name = "period")]
43    const fn py_period(&self) -> usize {
44        self.period
45    }
46
47    #[getter]
48    #[pyo3(name = "slope")]
49    const fn py_slope(&self) -> f64 {
50        self.slope
51    }
52
53    #[getter]
54    #[pyo3(name = "intercept")]
55    const fn py_intercept(&self) -> f64 {
56        self.intercept
57    }
58
59    #[getter]
60    #[pyo3(name = "degree")]
61    const fn py_degree(&self) -> f64 {
62        self.degree
63    }
64
65    #[getter]
66    #[pyo3(name = "cfo")]
67    const fn py_cfo(&self) -> f64 {
68        self.cfo
69    }
70
71    #[getter]
72    #[pyo3(name = "r2")]
73    const fn py_r2(&self) -> f64 {
74        self.r2
75    }
76
77    #[getter]
78    #[pyo3(name = "has_inputs")]
79    fn py_has_inputs(&self) -> bool {
80        self.has_inputs()
81    }
82
83    #[getter]
84    #[pyo3(name = "value")]
85    const fn py_value(&self) -> f64 {
86        self.value
87    }
88
89    #[getter]
90    #[pyo3(name = "initialized")]
91    const fn py_initialized(&self) -> bool {
92        self.initialized
93    }
94
95    /// Updates the linear regression with a new data point.
96    #[pyo3(name = "update_raw")]
97    fn py_update_raw(&mut self, close: f64) {
98        self.update_raw(close);
99    }
100
101    #[pyo3(name = "handle_bar")]
102    fn py_handle_bar(&mut self, bar: &Bar) {
103        self.handle_bar(bar);
104    }
105
106    #[pyo3(name = "reset")]
107    fn py_reset(&mut self) {
108        self.reset();
109    }
110}