nautechsystems/nautilus_trader · error · anyhow::Error
Python on_book_depth failed: {e}
Error message
Python on_book_depth failed: {e} What it means
Wraps a Python exception raised inside the strategy's `on_book_depth` handler. `dispatch_on_book_depth` calls the user's Python `on_book_depth(self, depth)` for `OrderBookDepth10` updates; any unhandled exception becomes this anyhow error. It indicates user code failed while processing fixed-size top-10 book depth.
Source
Thrown at crates/trading/src/python/strategy.rs:1165
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<()> {
self.dispatch_on_index_price(*index_price)
.map_err(|e| anyhow::anyhow!("Python on_index_price failed: {e}"))
}
fn on_funding_rate(&mut self, funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {View on GitHub (pinned to 18893faf8b)
Solutions
- Inspect the chained `{e}` traceback for the failing line in `on_book_depth`.
- Filter out None entries: iterate `[(p, s) for p, s in zip(depth.bids, depth.bid_sizes) if p is not None]`.
- Avoid assuming exactly 10 valid levels; compute on the trimmed set.
- Test against a low-liquidity symbol to cover shallow-book cases.
Example fix
# before
def on_book_depth(self, depth):
total = sum(size for size in depth.bid_sizes) # TypeError on None sizes
# after
def on_book_depth(self, depth):
total = sum(s for p, s in zip(depth.bids, depth.bid_sizes) if p is not None and s is not None) Defensive patterns
Strategy: type-guard
Validate before calling
def valid_depth_rows(prices, sizes):
return [(p, s) for p, s in zip(prices, sizes) if p is not None and s is not None] Type guard
def has_depth(depth):
return any(p is not None for p in depth.bids) and any(p is not None for p in depth.asks) Try / catch
def on_book_depth(self, depth):
try:
self._on_depth(depth)
except Exception as e:
self.log.error(f"on_book_depth failed for {depth.instrument_id}: {e}", exc_info=True) Prevention
- Always filter None-padded slots from the fixed 10-level arrays
- Never assume a full 10 levels per side exists
- Zip bids and asks only after trimming each side independently
- Test on symbols with fewer than 10 resting levels
When it happens
Trigger: Unhandled Python exception in `on_book_depth`: indexing the 10-slot bid/ask arrays beyond non-null entries (trailing None levels), zipping bids/asks of mismatched null-padding, or treating null prices as 0 and hitting downstream arithmetic errors.
Common situations: Thin books with fewer than 10 levels per side producing None-padded arrays; strategies ported from full-book (`on_book_deltas`) code assuming dense arrays; venue adapters that zero-pad vs null-pad inconsistently.
Related errors
- Python on_book_deltas 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/94acc206b2959186.
Report an issue: GitHub.