Skip to main content

nautilus_indicators/python/volatility/
kp.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::data::Bar;
18use pyo3::prelude::*;
19
20use crate::{
21    average::MovingAverageType, indicator::Indicator, python::float_precision,
22    volatility::kp::KeltnerPosition,
23};
24
25#[pymethods]
26#[pyo3_stub_gen::derive::gen_stub_pymethods]
27impl KeltnerPosition {
28    /// Creates a new `KeltnerPosition` instance.
29    #[new]
30    #[pyo3(signature = (period, k_multiplier, ma_type=None, ma_type_atr=None, use_previous=None, atr_floor=None))]
31    pub fn py_new(
32        period: usize,
33        k_multiplier: f64,
34        ma_type: Option<MovingAverageType>,
35        ma_type_atr: Option<MovingAverageType>,
36        use_previous: Option<bool>,
37        atr_floor: Option<f64>,
38    ) -> PyResult<Self> {
39        Self::new_checked(
40            period,
41            k_multiplier,
42            ma_type,
43            ma_type_atr,
44            use_previous,
45            atr_floor,
46        )
47        .map_err(to_pyvalue_err)
48    }
49
50    fn __repr__(&self) -> String {
51        format!("KeltnerPosition({})", self.period)
52    }
53
54    #[getter]
55    #[pyo3(name = "name")]
56    fn py_name(&self) -> String {
57        self.name()
58    }
59
60    #[getter]
61    #[pyo3(name = "period")]
62    const fn py_period(&self) -> usize {
63        self.period
64    }
65
66    #[getter]
67    #[pyo3(name = "k_multiplier")]
68    const fn py_k_multiplier(&self) -> f64 {
69        self.k_multiplier
70    }
71
72    #[getter]
73    #[pyo3(name = "use_previous")]
74    const fn py_use_previous(&self) -> bool {
75        self.use_previous
76    }
77
78    #[getter]
79    #[pyo3(name = "atr_floor")]
80    const fn py_atr_floor(&self) -> f64 {
81        self.atr_floor
82    }
83
84    #[getter]
85    #[pyo3(name = "has_inputs")]
86    fn py_has_inputs(&self) -> bool {
87        self.has_inputs()
88    }
89
90    #[getter]
91    #[pyo3(name = "value")]
92    const fn py_value(&self) -> f64 {
93        self.value
94    }
95
96    #[getter]
97    #[pyo3(name = "initialized")]
98    const fn py_initialized(&self) -> bool {
99        self.initialized
100    }
101
102    #[pyo3(name = "update_raw")]
103    fn py_update_raw(&mut self, high: f64, low: f64, close: f64) {
104        self.update_raw(high, low, close);
105    }
106
107    #[pyo3(name = "handle_bar")]
108    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
109        float_precision::check_bar(bar)?;
110        self.handle_bar(bar);
111        Ok(())
112    }
113
114    #[pyo3(name = "reset")]
115    fn py_reset(&mut self) {
116        self.reset();
117    }
118}