{"record":{"id":"2f5ce0feae28cc11","repo":"nautechsystems/nautilus_trader","slug":"python-on-book-failed-e-2f5ce0","errorCode":null,"errorMessage":"Python on_book failed: {e}","messagePattern":"Python on_book failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/trading/src/python/strategy.rs","lineNumber":1170,"sourceCode":"\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<()> {\n        self.dispatch_on_funding_rate(*funding_rate)\n            .map_err(|e| anyhow::anyhow!(\"Python on_funding_rate failed: {e}\"))\n    }\n\n    fn on_instrument_status(&mut self, data: &InstrumentStatus) -> anyhow::Result<()> {","sourceCodeStart":1152,"sourceCodeEnd":1188,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/python/strategy.rs#L1152-L1188","documentation":"Wraps a Python exception raised inside the strategy's `on_book` handler. `dispatch_on_book` calls the user's Python `on_book(self, order_book)` with the full `OrderBook` state object; any unhandled exception becomes this anyhow error. It means user strategy code failed while processing the complete book snapshot.","triggerScenarios":"Unhandled Python exception in `on_book`: calling `order_book.best_bid_price()` on an empty book (returns None and is then used arithmetically), iterating empty bids/asks, or misusing book methods (e.g. `book.update_count`, imprecision flags) across adapter versions.","commonSituations":"Book accessed before any deltas arrive (empty book right after subscribe); venue outages or session resets producing empty books; strategies computing spreads without checking both sides exist.","solutions":["Read the chained `{e}` traceback to find the failing line in `on_book`.","Check `order_book.bid_count() > 0 and order_book.ask_count() > 0` (or None best prices) before computing.","Wrap book-derived computations in try/except and skip empty states.","Reproduce with a recorded data session that includes the empty-book interval."],"exampleFix":"# before\ndef on_book(self, order_book):\n    spread = order_book.best_ask_price() - order_book.best_bid_price()  # None - None\n\n# after\ndef on_book(self, order_book):\n    if order_book.bid_count() == 0 or order_book.ask_count() == 0:\n        return\n    spread = order_book.best_ask_price() - order_book.best_bid_price()","handlingStrategy":"validation","validationCode":"def book_two_sided(book):\n    return book.bid_count() > 0 and book.ask_count() > 0","typeGuard":"def best_prices_available(book):\n    bb, ba = book.best_bid_price(), book.best_ask_price()\n    return bb is not None and ba is not None","tryCatchPattern":"def on_book(self, order_book):\n    try:\n        self._on_book(order_book)\n    except Exception as e:\n        self.log.error(f\"on_book failed for {order_book.instrument_id}: {e}\", exc_info=True)","preventionTips":["Check book emptiness (bid/ask count) before reading best prices","Expect empty books right after subscribe and after venue resets","Compute spreads only when both sides exist","Record and replay book sessions to cover outage intervals"],"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-14T05:17:10.506Z"}