nautilus_indicators/python/ratio/
spread_analyzer.rs1use 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 #[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 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}