{"record":{"id":"58c9660ef2f04a62","repo":"nautechsystems/nautilus_trader","slug":"python-on-quote-failed-e-58c966","errorCode":null,"errorMessage":"Python on_quote failed: {e}","messagePattern":"Python on_quote failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/python/strategy.rs","lineNumber":1145,"sourceCode":"    }\n\n    fn on_socket_state(&mut self, event: &SocketStateChanged) -> anyhow::Result<()> {\n        self.dispatch_on_socket_state(event)\n            .map_err(|e| anyhow::anyhow!(\"Python on_socket_state failed: {e}\"))\n    }\n\n    fn on_instrument(&mut self, instrument: &InstrumentAny) -> anyhow::Result<()> {\n        Python::attach(|py| {\n            let py_instrument = instrument_any_to_pyobject(py, instrument.clone())\n                .map_err(|e| anyhow::anyhow!(\"Failed to convert InstrumentAny to Python: {e}\"))?;\n            self.dispatch_on_instrument(py_instrument)\n                .map_err(|e| anyhow::anyhow!(\"Python on_instrument failed: {e}\"))\n        })\n    }\n\n    fn on_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {\n        self.dispatch_on_quote(*quote)\n            .map_err(|e| anyhow::anyhow!(\"Python on_quote failed: {e}\"))\n    }\n\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<()> {","sourceCodeStart":1127,"sourceCodeEnd":1163,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/python/strategy.rs#L1127-L1163","documentation":"Wraps a Python exception raised inside the strategy's `on_quote` handler. The Rust data engine forwards each `QuoteTick` via `dispatch_on_quote`, which calls the user's Python `on_quote(self, quote)`; an unhandled exception there is re-thrown as this anyhow error. It indicates user strategy code failed while processing a top-of-book quote.","triggerScenarios":"Any unhandled Python exception in `on_quote`: arithmetic on None bid/ask (no best price available), division by a zero quote, assuming `quote.bid_size > 0`, or state-machine asserts that fire on rapid quote updates.","commonSituations":"Markets with one-sided books producing None bid or ask; illiquid symbols with zero sizes; strategies written against mocked quotes that always had both sides populated; hot-loop bugs exposed by very high quote rates.","solutions":["Inspect the embedded `{e}` traceback to find the failing line in the Python `on_quote`.","Guard against missing/zero quote sides before computing (check `quote.bid_size`/`ask_size` or None prices).","Wrap the handler body in try/except to skip unusable quotes and log them.","Backtest with a recorded stream of the failing symbol to reproduce the edge case."],"exampleFix":"# before\ndef on_quote(self, quote):\n    mid = (quote.bid_price + quote.ask_price) / 2  # TypeError when bid is None\n\n# after\ndef on_quote(self, quote):\n    if quote.bid_price is None or quote.ask_price is None:\n        return\n    mid = (quote.bid_price + quote.ask_price) / 2","handlingStrategy":"validation","validationCode":"def quote_usable(quote):\n    return (quote.bid_price is not None and quote.ask_price is not None\n            and quote.bid_price > 0 and quote.ask_price > 0)","typeGuard":"def has_two_sided_book(quote):\n    return quote.bid_size is not None and quote.ask_size is not None and quote.bid_size > 0 and quote.ask_size > 0","tryCatchPattern":"def on_quote(self, quote):\n    try:\n        self._on_quote(quote)\n    except Exception as e:\n        self.log.error(f\"on_quote failed for {quote.instrument_id}: {e}\", exc_info=True)","preventionTips":["Check bid/ask for None (one-sided books) before arithmetic","Never divide by quote prices without a zero check","Test against thin/illiquid symbols, not just mocked two-sided quotes","Keep on_quote fast and stateless where possible to reduce exception surface"],"tags":["python","strategy","callback","market-data"],"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"}