{"record":{"id":"94acc206b2959186","repo":"nautechsystems/nautilus_trader","slug":"python-on-book-depth-failed-e-94acc2","errorCode":null,"errorMessage":"Python on_book_depth failed: {e}","messagePattern":"Python on_book_depth failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/trading/src/python/strategy.rs","lineNumber":1165,"sourceCode":"\n    fn on_trade(&mut self, tick: &TradeTick) -> anyhow::Result<()> {\n        self.dispatch_on_trade(*tick)\n            .map_err(|e| anyhow::anyhow!(\"Python on_trade failed: {e}\"))\n    }\n\n    fn on_bar(&mut self, bar: &Bar) -> anyhow::Result<()> {\n        self.dispatch_on_bar(*bar)\n            .map_err(|e| anyhow::anyhow!(\"Python on_bar failed: {e}\"))\n    }\n\n    fn on_book_deltas(&mut self, deltas: &OrderBookDeltas) -> anyhow::Result<()> {\n        self.dispatch_on_book_deltas(deltas)\n            .map_err(|e| anyhow::anyhow!(\"Python on_book_deltas failed: {e}\"))\n    }\n\n    fn on_book_depth(&mut self, depth: &OrderBookDepth10) -> anyhow::Result<()> {\n        self.dispatch_on_book_depth(depth)\n            .map_err(|e| anyhow::anyhow!(\"Python on_book_depth failed: {e}\"))\n    }\n\n    fn on_book(&mut self, order_book: &OrderBook) -> anyhow::Result<()> {\n        self.dispatch_on_book(order_book)\n            .map_err(|e| anyhow::anyhow!(\"Python on_book failed: {e}\"))\n    }\n\n    fn on_mark_price(&mut self, mark_price: &MarkPriceUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_mark_price(*mark_price)\n            .map_err(|e| anyhow::anyhow!(\"Python on_mark_price failed: {e}\"))\n    }\n\n    fn on_index_price(&mut self, index_price: &IndexPriceUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_index_price(*index_price)\n            .map_err(|e| anyhow::anyhow!(\"Python on_index_price failed: {e}\"))\n    }\n\n    fn on_funding_rate(&mut self, funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {","sourceCodeStart":1147,"sourceCodeEnd":1183,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/python/strategy.rs#L1147-L1183","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ndef on_book_depth(self, depth):\n    total = sum(size for size in depth.bid_sizes)  # TypeError on None sizes\n\n# after\ndef on_book_depth(self, depth):\n    total = sum(s for p, s in zip(depth.bids, depth.bid_sizes) if p is not None and s is not None)","handlingStrategy":"type-guard","validationCode":"def valid_depth_rows(prices, sizes):\n    return [(p, s) for p, s in zip(prices, sizes) if p is not None and s is not None]","typeGuard":"def has_depth(depth):\n    return any(p is not None for p in depth.bids) and any(p is not None for p in depth.asks)","tryCatchPattern":"def on_book_depth(self, depth):\n    try:\n        self._on_depth(depth)\n    except Exception as e:\n        self.log.error(f\"on_book_depth failed for {depth.instrument_id}: {e}\", exc_info=True)","preventionTips":["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"],"tags":["python","strategy","callback","order-book"],"backgroundTag":"python-strategy-callback-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}