Skip to main content

nautilus_indicators/python/ratio/
spread_analyzer.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::QuoteTick, identifiers::InstrumentId};
18use pyo3::prelude::*;
19
20use crate::{indicator::Indicator, ratio::spread_analyzer::SpreadAnalyzer};
21
22#[pyo3_stub_gen::derive::gen_stub_pymethods]
23#[pymethods]
24impl SpreadAnalyzer {
25    /// An indicator which calculates the efficiency ratio across a rolling window.
26    ///
27    /// The Kaufman Efficiency measures the ratio of the relative market speed in
28    /// relation to the volatility, this could be thought of as a proxy for noise.
29    #[new]
30    fn py_new(instrument_id: InstrumentId, capacity: usize) -> Self {
31        Self::new(capacity, instrument_id)
32    }
33
34    fn __repr__(&self) -> String {
35        format!("SpreadAnalyzer({})", self.capacity)
36    }
37
38    #[getter]
39    #[pyo3(name = "name")]
40    fn py_name(&self) -> String {
41        self.name()
42    }
43
44    #[getter]
45    #[pyo3(name = "capacity")]
46    const fn py_capacity(&self) -> usize {
47        self.capacity
48    }
49
50    #[getter]
51    #[pyo3(name = "instrument_id")]
52    const fn py_instrument_id(&self) -> InstrumentId {
53        self.instrument_id
54    }
55
56    #[getter]
57    #[pyo3(name = "current")]
58    const fn py_current(&self) -> f64 {
59        self.current
60    }
61
62    #[getter]
63    #[pyo3(name = "average")]
64    const fn py_average(&self) -> f64 {
65        self.average
66    }
67
68    #[getter]
69    #[pyo3(name = "initialized")]
70    const fn py_initialized(&self) -> bool {
71        self.initialized
72    }
73
74    #[getter]
75    #[pyo3(name = "has_inputs")]
76    fn py_has_inputs(&self) -> bool {
77        self.has_inputs()
78    }
79
80    #[pyo3(name = "handle_quote_tick")]
81    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
82        self.handle_quote(quote).map_err(to_pyvalue_err)
83    }
84
85    #[pyo3(name = "reset")]
86    fn py_reset(&mut self) {
87        self.reset();
88    }
89}