Skip to main content

nautilus_indicators/python/volatility/
atr.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, QuoteTick, TradeTick};
18use pyo3::prelude::*;
19
20use crate::{
21    average::MovingAverageType, indicator::Indicator, python::float_precision,
22    volatility::atr::AverageTrueRange,
23};
24
25#[pymethods]
26#[pyo3_stub_gen::derive::gen_stub_pymethods]
27impl AverageTrueRange {
28    /// An indicator which calculates an Average True Range (ATR) across a rolling window.
29    #[new]
30    #[pyo3(signature = (period, ma_type=None, use_previous=None, value_floor=None))]
31    pub fn py_new(
32        period: usize,
33        ma_type: Option<MovingAverageType>,
34        use_previous: Option<bool>,
35        value_floor: Option<f64>,
36    ) -> PyResult<Self> {
37        Self::new_checked(period, ma_type, use_previous, value_floor).map_err(to_pyvalue_err)
38    }
39
40    fn __repr__(&self) -> String {
41        format!(
42            "AverageTrueRange({},{},{},{})",
43            self.period, self.ma_type, self.use_previous, self.value_floor,
44        )
45    }
46
47    #[getter]
48    #[pyo3(name = "ma_type")]
49    const fn py_ma_type(&self) -> MovingAverageType {
50        self.ma_type
51    }
52
53    #[getter]
54    #[pyo3(name = "use_previous")]
55    const fn py_use_previous(&self) -> bool {
56        self.use_previous
57    }
58
59    #[getter]
60    #[pyo3(name = "value_floor")]
61    const fn py_value_floor(&self) -> f64 {
62        self.value_floor
63    }
64
65    #[getter]
66    #[pyo3(name = "name")]
67    fn py_name(&self) -> String {
68        self.name()
69    }
70
71    #[getter]
72    #[pyo3(name = "period")]
73    const fn py_period(&self) -> usize {
74        self.period
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 = "count")]
85    const fn py_count(&self) -> usize {
86        self.count
87    }
88
89    #[getter]
90    #[pyo3(name = "value")]
91    const fn py_value(&self) -> f64 {
92        self.value
93    }
94
95    #[getter]
96    #[pyo3(name = "initialized")]
97    const fn py_initialized(&self) -> bool {
98        self.initialized
99    }
100
101    #[pyo3(name = "update_raw")]
102    fn py_update_raw(&mut self, high: f64, low: f64, close: f64) {
103        self.update_raw(high, low, close);
104    }
105
106    #[pyo3(name = "handle_quote_tick")]
107    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
108        self.handle_quote(quote).map_err(to_pyvalue_err)
109    }
110
111    #[pyo3(name = "handle_trade_tick")]
112    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
113        self.handle_trade(trade);
114    }
115
116    #[pyo3(name = "handle_bar")]
117    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
118        float_precision::check_bar(bar)?;
119        self.handle_bar(bar);
120        Ok(())
121    }
122
123    #[pyo3(name = "reset")]
124    fn py_reset(&mut self) {
125        self.reset();
126    }
127}