{"record":{"id":"d72cb5ec07cbd7a7","repo":"nautechsystems/nautilus_trader","slug":"python-on-historical-trades-failed-e-d72cb5","errorCode":null,"errorMessage":"Python on_historical_trades failed: {e}","messagePattern":"Python on_historical_trades failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/trading/src/python/strategy.rs","lineNumber":1239,"sourceCode":"\n    fn on_historical_book_deltas(&mut self, deltas: &[OrderBookDelta]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_book_deltas(deltas.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_book_deltas failed: {e}\"))\n    }\n\n    fn on_historical_book_depth(&mut self, depths: &[OrderBookDepth10]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_book_depth(depths.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_book_depth failed: {e}\"))\n    }\n\n    fn on_historical_quotes(&mut self, quotes: &[QuoteTick]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_quotes(quotes.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_quotes failed: {e}\"))\n    }\n\n    fn on_historical_trades(&mut self, trades: &[TradeTick]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_trades(trades.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_trades failed: {e}\"))\n    }\n\n    fn on_historical_funding_rates(\n        &mut self,\n        funding_rates: &[FundingRateUpdate],\n    ) -> anyhow::Result<()> {\n        self.dispatch_on_historical_funding_rates(funding_rates.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_funding_rates failed: {e}\"))\n    }\n\n    fn on_historical_bars(&mut self, bars: &[Bar]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_bars(bars.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_bars failed: {e}\"))\n    }\n\n    fn on_historical_mark_prices(&mut self, mark_prices: &[MarkPriceUpdate]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_mark_prices(mark_prices.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_mark_prices failed: {e}\"))","sourceCodeStart":1221,"sourceCodeEnd":1257,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/python/strategy.rs#L1221-L1257","documentation":"Raised when the strategy's Python `on_historical_trades` handler throws while processing historical TradeTicks. The Rust bridge clones the trade slice and dispatches it through PyO3; any resulting error is wrapped in this anyhow message. The exception originates in the user's Python handler.","triggerScenarios":"Historical trade data is replayed (backtest or history request) and `on_historical_trades(self, trades)` raises — e.g. VWAP math with empty lists, side parsing errors, or wrong payload attribute access.","commonSituations":"Tick-based backtests loading trade history; handlers computing VWAP/footprint metrics hitting zero-quantity division; signature or payload-shape drift after upgrades.","solutions":["Read the chained Python traceback in `e` and fix `on_historical_trades`","Guard empty trade lists and zero sizes before computing aggregates","Confirm the signature `on_historical_trades(self, trades: list[TradeTick])`","Test with an empty-trade replay to catch degenerate cases early"],"exampleFix":"// before (Python)\ndef on_historical_trades(self, trades):\n    vwap = sum(t.price * t.size for t in trades) / sum(t.size for t in trades)  # ZeroDivisionError\n// after\ndef on_historical_trades(self, trades):\n    total = sum(t.size for t in trades)\n    if total == 0:\n        return\n    vwap = sum(t.price * t.size for t in trades) / total","handlingStrategy":"validation","validationCode":"def trades_ok(trades):\n    return bool(trades) and sum(t.size for t in trades) > 0","typeGuard":"def trades_usable(trades):\n    return trades is not None and len(trades) > 0 and all(t.size and t.size > 0 for t in trades)","tryCatchPattern":"try:\n    strategy.on_historical_trades(trades)\nexcept Exception as e:\n    strategy.log.error(f\"on_historical_trades failed: {e}\", exc_info=True)","preventionTips":["Guard zero total size before VWAP/notional division","Handle empty trade history gracefully during warm-up","Validate trade side/agggressor fields before branch logic","Run a minimal trade replay test before large backtests"],"tags":["rust","python","nautilustrader","callback","historical-data"],"backgroundTag":"python-callback-exception","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"}