nautechsystems/nautilus_trader · error
Python on_book_deltas failed: {e}
Error message
Python on_book_deltas failed: {e} What it means
Wraps a Python exception raised inside the strategy's `on_book_deltas` handler. `dispatch_on_book_deltas` invokes the user's Python `on_book_deltas(self, deltas)` for `OrderBookDeltas` updates; any unhandled exception becomes this anyhow error. It means user strategy code failed while processing an L2/L3 book delta batch.
Source
Thrown at crates/trading/src/python/strategy.rs:1160
fn on_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {
self.dispatch_on_quote(*quote)
.map_err(|e| anyhow::anyhow!("Python on_quote failed: {e}"))
}
fn on_trade(&mut self, tick: &TradeTick) -> anyhow::Result<()> {
self.dispatch_on_trade(*tick)
.map_err(|e| anyhow::anyhow!("Python on_trade failed: {e}"))
}
fn on_bar(&mut self, bar: &Bar) -> anyhow::Result<()> {
self.dispatch_on_bar(*bar)
.map_err(|e| anyhow::anyhow!("Python on_bar failed: {e}"))
}
fn on_book_deltas(&mut self, deltas: &OrderBookDeltas) -> anyhow::Result<()> {
self.dispatch_on_book_deltas(deltas)
.map_err(|e| anyhow::anyhow!("Python on_book_deltas failed: {e}"))
}
fn on_book_depth(&mut self, depth: &OrderBookDepth10) -> anyhow::Result<()> {
self.dispatch_on_book_depth(depth)
.map_err(|e| anyhow::anyhow!("Python on_book_depth failed: {e}"))
}
fn on_book(&mut self, order_book: &OrderBook) -> anyhow::Result<()> {
self.dispatch_on_book(order_book)
.map_err(|e| anyhow::anyhow!("Python on_book failed: {e}"))
}
fn on_mark_price(&mut self, mark_price: &MarkPriceUpdate) -> anyhow::Result<()> {
self.dispatch_on_mark_price(*mark_price)
.map_err(|e| anyhow::anyhow!("Python on_mark_price failed: {e}"))
}
fn on_index_price(&mut self, index_price: &IndexPriceUpdate) -> anyhow::Result<()> {View on GitHub (pinned to 18893faf8b)
Solutions
- Read the chained `{e}` traceback to find the failing line in `on_book_deltas`.
- Handle all delta actions (ADD/UPDATE/DELETE/CLEAR) defensively; use `.get()` on local level maps.
- Guard order_id/price being None depending on record type.
- Re-run against a recorded book stream from the failing venue to reproduce.
Example fix
# before
def on_book_deltas(self, deltas):
for d in deltas.deltas:
del self.levels[d.price] # KeyError on DELETE for unknown level
# after
def on_book_deltas(self, deltas):
for d in deltas.deltas:
if d.action == BookAction.CLEAR:
self.levels.clear()
elif d.action == BookAction.DELETE:
self.levels.pop(d.price, None)
else:
self.levels[d.price] = d.size Defensive patterns
Strategy: type-guard
Validate before calling
def handle_delta_safely(levels, d):
if d.action == BookAction.DELETE:
return levels.pop(d.price, None)
if d.action == BookAction.CLEAR:
return levels.clear()
return levels.__setitem__(d.price, d.size) Type guard
def delta_applicable(d):
return d.price is not None and d.action in (BookAction.ADD, BookAction.UPDATE, BookAction.DELETE, BookAction.CLEAR) Try / catch
def on_book_deltas(self, deltas):
try:
self._apply_deltas(deltas)
except Exception as e:
self.log.error(f"on_book_deltas failed for {deltas.instrument_id}: {e}", exc_info=True) Prevention
- Handle ADD, UPDATE, DELETE and CLEAR actions; never assume only adds
- Use dict.pop(price, None) for deletes so unknown levels are tolerated
- Treat order_id as possibly None in market-by-price feeds
- Test snapshot-then-delta sequences including reconnects
When it happens
Trigger: Unhandled Python exception in `on_book_deltas`: assuming every delta has a non-null order_id (market-by-price vs market-by-order), applying deletes to a local dict missing the level, iterating `deltas.deltas` with wrong assumptions about the action enum, or clear/final flag handling bugs.
Common situations: Strategies maintaining a local book snapshot that desyncs after a snapshot/clear delta; venues that send clear actions or deletes for unknown levels; confusing delta handlers with the depth10 handler when switching subscriptions.
Related errors
- Python on_book_depth failed: {e}
- Python on_book failed: {e}
- Python on_degrade failed: {e}
- Python on_time_event failed: {e}
- Python on_data failed: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/644706192b56e92f.
Report an issue: GitHub.