Skip to main content

nautilus_indicators/python/book/
imbalance.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_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    /// Creates a new `BookImbalanceRatio` instance.
25    #[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    // The ratio only converts sizes once both sides are present
91    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}