Skip to content

Commit a66d5bf

Browse files
committed
Optimize own order books auditing
1 parent 12f6733 commit a66d5bf

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

crates/model/src/python/orderbook/own.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ impl OwnOrderBook {
227227
self.is_order_in_book(client_order_id)
228228
}
229229

230+
#[pyo3(name = "orders_to_list")]
231+
fn py_orders_to_list(&self) -> Vec<OwnBookOrder> {
232+
let total_orders = self.bids.cache.len() + self.asks.cache.len();
233+
let mut all_orders = Vec::with_capacity(total_orders);
234+
235+
all_orders.extend(
236+
self.bids()
237+
.flat_map(|level| level.orders.values().cloned())
238+
.chain(self.asks().flat_map(|level| level.orders.values().cloned())),
239+
);
240+
241+
all_orders
242+
}
243+
230244
#[pyo3(name = "bids_to_list")]
231245
fn py_bids_to_list(&self) -> Vec<OwnBookOrder> {
232246
self.bids()

nautilus_trader/cache/cache.pyx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,16 +4523,18 @@ cdef class Cache(CacheFacade):
45234523
Logs all failures as errors.
45244524
45254525
"""
4526-
cdef Order order
4527-
cdef ClientOrderId client_order_id
4528-
45294526
self._log.debug("Starting own book audit", LogColor.MAGENTA)
45304527
cdef double start_us = time.time() * 1_000_000
45314528

4529+
cdef:
4530+
list[object] own_book_orders
4531+
ClientOrderId client_order_id
4532+
Order order
4533+
45324534
for own_book in self._own_order_books.values():
45334535
self._log.debug(f"Auditing {own_book} ", LogColor.MAGENTA)
45344536

4535-
own_book_orders = own_book.bids_to_list() + own_book.asks_to_list()
4537+
own_book_orders = own_book.orders_to_list()
45364538
if own_book_orders:
45374539
self._log.debug(
45384540
f"Auditing {len(own_book_orders)} own book orders", LogColor.MAGENTA,
@@ -4546,11 +4548,10 @@ cdef class Cache(CacheFacade):
45464548
f"Audit error - {client_order_id!r} in own book was not in cache",
45474549
)
45484550
continue
4549-
order_status_pyo3 = order_status_to_pyo3(<OrderStatus>order._fsm.state)
4550-
if order_status_pyo3 != own_book_order.status:
4551+
if own_book_order.status.value != <OrderStatus>order._fsm.state:
45514552
self._log.error(
45524553
f"Audit error - {client_order_id!r} own book status {own_book_order.status} "
4553-
f"did not match cached order status {order_status_pyo3}"
4554+
f"did not match cached order status {order.status_string_c()}"
45544555
)
45554556
own_book_order = order.to_own_book_order()
45564557
own_book.update(own_book_order)

nautilus_trader/core/nautilus_pyo3.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,7 @@ class OwnOrderBook:
32593259
def bid_client_order_ids(self) -> list[ClientOrderId]: ...
32603260
def ask_client_order_ids(self) -> list[ClientOrderId]: ...
32613261
def is_order_in_book(self, client_order_id: ClientOrderId) -> bool:...
3262+
def orders_to_list(self) -> list[OwnBookOrder]: ...
32623263
def bids_to_list(self) -> list[OwnBookOrder]: ...
32633264
def asks_to_list(self) -> list[OwnBookOrder]: ...
32643265
def bids_to_dict(

0 commit comments

Comments
 (0)