nautilus_indicators/python/book/
imbalance.rs1use nautilus_model::{orderbook::OrderBook, types::Quantity};
17use pyo3::prelude::*;
18
19use crate::{book::imbalance::BookImbalanceRatio, indicator::Indicator, python::float_precision};
20
21#[pymethods]
22#[pyo3_stub_gen::derive::gen_stub_pymethods]
23impl BookImbalanceRatio {
24 #[new]
26 const fn py_new() -> Self {
27 Self::new()
28 }
29
30 fn __repr__(&self) -> String {
31 self.to_string()
32 }
33
34 #[getter]
35 #[pyo3(name = "name")]
36 fn py_name(&self) -> String {
37 self.name()
38 }
39
40 #[getter]
41 #[pyo3(name = "count")]
42 const fn py_count(&self) -> usize {
43 self.count
44 }
45
46 #[getter]
47 #[pyo3(name = "value")]
48 const fn py_value(&self) -> f64 {
49 self.value
50 }
51
52 #[getter]
53 #[pyo3(name = "has_inputs")]
54 fn py_has_inputs(&self) -> bool {
55 self.has_inputs()
56 }
57
58 #[getter]
59 #[pyo3(name = "initialized")]
60 const fn py_initialized(&self) -> bool {
61 self.initialized
62 }
63
64 #[pyo3(name = "handle_book")]
65 fn py_handle_book(&mut self, book: &OrderBook) -> PyResult<()> {
66 check_sizes(book.best_bid_size(), book.best_ask_size())?;
67 self.handle_book(book);
68 Ok(())
69 }
70
71 #[pyo3(name = "update")]
72 #[pyo3(signature = (best_bid=None, best_ask=None))]
73 fn py_update(
74 &mut self,
75 best_bid: Option<Quantity>,
76 best_ask: Option<Quantity>,
77 ) -> PyResult<()> {
78 check_sizes(best_bid, best_ask)?;
79 self.update(best_bid, best_ask);
80 Ok(())
81 }
82
83 #[pyo3(name = "reset")]
84 fn py_reset(&mut self) {
85 self.reset();
86 }
87}
88
89fn check_sizes(best_bid: Option<Quantity>, best_ask: Option<Quantity>) -> PyResult<()> {
90 if let (Some(best_bid), Some(best_ask)) = (best_bid, best_ask) {
92 float_precision::check(best_bid.precision)?;
93 float_precision::check(best_ask.precision)?;
94 }
95
96 Ok(())
97}