nautilus_model/python/orderbook/level.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 pyo3::prelude::*;
17
18use crate::{
19 data::order::BookOrder,
20 orderbook::BookLevel,
21 types::{price::Price, quantity::QuantityRaw},
22};
23
24#[pymethods]
25#[pyo3_stub_gen::derive::gen_stub_pymethods]
26impl BookLevel {
27 fn __repr__(&self) -> String {
28 format!("{self:?}")
29 }
30
31 fn __str__(&self) -> String {
32 // TODO: Return debug string for now
33 format!("{self:?}")
34 }
35
36 #[getter]
37 #[pyo3(name = "price")]
38 fn py_price(&self) -> Price {
39 self.price.value
40 }
41
42 /// Returns the number of orders at this price level.
43 #[pyo3(name = "len")]
44 fn py_len(&self) -> usize {
45 self.len()
46 }
47
48 /// Returns true if this price level has no orders.
49 #[pyo3(name = "is_empty")]
50 fn py_is_empty(&self) -> bool {
51 self.is_empty()
52 }
53
54 /// Returns the total size of all orders at this price level as a float.
55 #[pyo3(name = "size")]
56 fn py_size(&self) -> f64 {
57 self.size()
58 }
59
60 /// Returns the total size of all orders at this price level as raw integer units.
61 #[pyo3(name = "size_raw")]
62 fn py_size_raw(&self) -> QuantityRaw {
63 self.size_raw()
64 }
65
66 /// Returns the total exposure (price * size) of all orders at this price level as a float.
67 #[pyo3(name = "exposure")]
68 fn py_exposure(&self) -> f64 {
69 self.exposure()
70 }
71
72 /// Returns the total exposure (price * size) of all orders at this price level as raw integer units.
73 ///
74 /// Saturates at `QuantityRaw::MAX` if the total exposure would overflow.
75 #[pyo3(name = "exposure_raw")]
76 fn py_exposure_raw(&self) -> QuantityRaw {
77 self.exposure_raw()
78 }
79
80 #[pyo3(name = "first")]
81 fn py_fist(&self) -> Option<BookOrder> {
82 self.first().copied()
83 }
84
85 /// Returns all orders at this price level in FIFO insertion order.
86 #[pyo3(name = "get_orders")]
87 fn py_get_orders(&self) -> Vec<BookOrder> {
88 self.get_orders()
89 }
90}