Skip to main content

nautilus_indicators/python/volatility/
rvi.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::rvi::RelativeVolatilityIndex,
23};
24
25#[pymethods]
26#[pyo3_stub_gen::derive::gen_stub_pymethods]
27impl RelativeVolatilityIndex {
28    /// An indicator which calculates a Relative Volatility Index (RVI) across a rolling window.
29    #[new]
30    #[pyo3(signature = (period, scalar=None, ma_type=None))]
31    pub fn py_new(
32        period: usize,
33        scalar: Option<f64>,
34        ma_type: Option<MovingAverageType>,
35    ) -> PyResult<Self> {
36        Self::new_checked(period, scalar, ma_type).map_err(to_pyvalue_err)
37    }
38
39    fn __repr__(&self) -> String {
40        format!(
41            "RelativeVolatilityIndex({},{},{})",
42            self.period, self.scalar, self.ma_type,
43        )
44    }
45
46    #[getter]
47    #[pyo3(name = "ma_type")]
48    const fn py_ma_type(&self) -> MovingAverageType {
49        self.ma_type
50    }
51
52    #[getter]
53    #[pyo3(name = "name")]
54    fn py_name(&self) -> String {
55        self.name()
56    }
57
58    #[getter]
59    #[pyo3(name = "period")]
60    const fn py_period(&self) -> usize {
61        self.period
62    }
63
64    #[getter]
65    #[pyo3(name = "scalar")]
66    const fn py_scalar(&self) -> f64 {
67        self.scalar
68    }
69
70    #[getter]
71    #[pyo3(name = "has_inputs")]
72    fn py_has_inputs(&self) -> bool {
73        self.has_inputs()
74    }
75
76    #[getter]
77    #[pyo3(name = "value")]
78    const fn py_value(&self) -> f64 {
79        self.value
80    }
81
82    #[getter]
83    #[pyo3(name = "initialized")]
84    const fn py_initialized(&self) -> bool {
85        self.initialized
86    }
87
88    #[pyo3(name = "update_raw")]
89    fn py_update_raw(&mut self, close: f64) {
90        self.update_raw(close);
91    }
92
93    #[pyo3(name = "handle_quote_tick")]
94    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
95        self.handle_quote(quote).map_err(to_pyvalue_err)
96    }
97
98    #[pyo3(name = "handle_trade_tick")]
99    fn py_handle_trade_tick(&mut self, trade: &TradeTick) {
100        self.handle_trade(trade);
101    }
102
103    #[pyo3(name = "handle_bar")]
104    fn py_handle_bar(&mut self, bar: &Bar) -> PyResult<()> {
105        float_precision::check_bar(bar)?;
106        self.handle_bar(bar);
107        Ok(())
108    }
109
110    #[pyo3(name = "reset")]
111    fn py_reset(&mut self) {
112        self.reset();
113    }
114}