{"record":{"id":"f7879e9614d9dc1d","repo":"nautechsystems/nautilus_trader","slug":"python-on-trade-failed-e","errorCode":null,"errorMessage":"Python on_trade failed: {e}","messagePattern":"Python on_trade failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1108,"sourceCode":"    }\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.clone())\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<()> {","sourceCodeStart":1090,"sourceCodeEnd":1126,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1090-L1126","documentation":"Wraps any failure raised when the Rust core dispatches a TradeTick into the Python actor's `on_trade` handler. The Python `on_trade` implementation raised (or dispatch into Python failed), and Rust re-raises with the 'Python on_trade failed' prefix.","triggerScenarios":"A trade tick arrives after subscribe_trade_ticks and the Python `on_trade(self, tick)` override raises an exception.","commonSituations":"Trade aggregation logic with uninitialized state, wrong handler signature, or exceptions from order submission triggered on every trade in fast markets.","solutions":["Inspect the wrapped `{e}` traceback and fix the exception inside `on_trade`","Verify the override signature `on_trade(self, tick)` taking a TradeTick","Initialize all handler state in on_start before subscribing to trade ticks","Add defensive checks for None/empty aggressor side or price fields"],"exampleFix":"// before\ndef on_trade(self, tick):\n    self.vwap.update(tick.price, None)  # TypeError on None size\n// after\ndef on_trade(self, tick):\n    if tick.size is None or tick.size.as_double() <= 0:\n        return\n    self.vwap.update(tick.price, tick.size)","handlingStrategy":"try-catch","validationCode":"def _valid_trade(tick) -> bool:\n    return tick.price is not None and tick.size is not None and tick.size > 0","typeGuard":null,"tryCatchPattern":"try:\n    self.dispatch_on_trade(tick)\nexcept Exception as e:\n    self.log.error(f'Python on_trade failed: {e}', exc_info=True)","preventionTips":["Check size/price validity before aggregation","Guard order submissions triggered from on_trade (rate/duplicate checks)","Initialize trade-aggregation state in on_start"],"tags":["python","actor","callback","market-data"],"backgroundTag":"python-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"}