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::{
21    indicator::Indicator, python::float_precision, ratio::spread_analyzer::SpreadAnalyzer,
22};
23
24#[pyo3_stub_gen::derive::gen_stub_pymethods]
25#[pymethods]
26impl SpreadAnalyzer {
27    /// Calculates the current bid-ask spread and its average across a rolling window.
28    ///
29    /// A zero capacity is accepted.
30    #[new]
31    fn py_new(instrument_id: InstrumentId, capacity: usize) -> PyResult<Self> {
32        Self::new_checked(capacity, instrument_id).map_err(to_pyvalue_err)
33    }
34
35    fn __repr__(&self) -> String {
36        format!("SpreadAnalyzer({})", self.capacity)
37    }
38
39    #[getter]
40    #[pyo3(name = "name")]
41    fn py_name(&self) -> String {
42        self.name()
43    }
44
45    #[getter]
46    #[pyo3(name = "capacity")]
47    const fn py_capacity(&self) -> usize {
48        self.capacity
49    }
50
51    #[getter]
52    #[pyo3(name = "instrument_id")]
53    const fn py_instrument_id(&self) -> InstrumentId {
54        self.instrument_id
55    }
56
57    #[getter]
58    #[pyo3(name = "current")]
59    const fn py_current(&self) -> f64 {
60        self.current
61    }
62
63    #[getter]
64    #[pyo3(name = "average")]
65    const fn py_average(&self) -> f64 {
66        self.average
67    }
68
69    #[getter]
70    #[pyo3(name = "initialized")]
71    const fn py_initialized(&self) -> bool {
72        self.initialized
73    }
74
75    #[getter]
76    #[pyo3(name = "has_inputs")]
77    fn py_has_inputs(&self) -> bool {
78        self.has_inputs()
79    }
80
81    #[pyo3(name = "handle_quote_tick")]
82    fn py_handle_quote_tick(&mut self, quote: &QuoteTick) -> PyResult<()> {
83        // The analyzer ignores quotes for other instruments without converting them
84        if quote.instrument_id == self.instrument_id {
85            float_precision::check_quote(quote)?;
86        }
87
88        self.handle_quote(quote).map_err(to_pyvalue_err)
89    }
90
91    #[pyo3(name = "reset")]
92    fn py_reset(&mut self) {
93        self.reset();
94    }
95}