nautechsystems/nautilus_trader · error · anyhow::Error

Python on_historical_trades failed: {e}

Error message

Python on_historical_trades failed: {e}

What it means

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.

Source

Thrown at crates/trading/src/python/strategy.rs:1239

    fn on_historical_book_deltas(&mut self, deltas: &[OrderBookDelta]) -> anyhow::Result<()> {
        self.dispatch_on_historical_book_deltas(deltas.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_book_deltas failed: {e}"))
    }

    fn on_historical_book_depth(&mut self, depths: &[OrderBookDepth10]) -> anyhow::Result<()> {
        self.dispatch_on_historical_book_depth(depths.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_book_depth failed: {e}"))
    }

    fn on_historical_quotes(&mut self, quotes: &[QuoteTick]) -> anyhow::Result<()> {
        self.dispatch_on_historical_quotes(quotes.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_quotes failed: {e}"))
    }

    fn on_historical_trades(&mut self, trades: &[TradeTick]) -> anyhow::Result<()> {
        self.dispatch_on_historical_trades(trades.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_trades failed: {e}"))
    }

    fn on_historical_funding_rates(
        &mut self,
        funding_rates: &[FundingRateUpdate],
    ) -> anyhow::Result<()> {
        self.dispatch_on_historical_funding_rates(funding_rates.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_funding_rates failed: {e}"))
    }

    fn on_historical_bars(&mut self, bars: &[Bar]) -> anyhow::Result<()> {
        self.dispatch_on_historical_bars(bars.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_bars failed: {e}"))
    }

    fn on_historical_mark_prices(&mut self, mark_prices: &[MarkPriceUpdate]) -> anyhow::Result<()> {
        self.dispatch_on_historical_mark_prices(mark_prices.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_mark_prices failed: {e}"))

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the chained Python traceback in `e` and fix `on_historical_trades`
  2. Guard empty trade lists and zero sizes before computing aggregates
  3. Confirm the signature `on_historical_trades(self, trades: list[TradeTick])`
  4. Test with an empty-trade replay to catch degenerate cases early

Example fix

// before (Python)
def on_historical_trades(self, trades):
    vwap = sum(t.price * t.size for t in trades) / sum(t.size for t in trades)  # ZeroDivisionError
// after
def on_historical_trades(self, trades):
    total = sum(t.size for t in trades)
    if total == 0:
        return
    vwap = sum(t.price * t.size for t in trades) / total
Defensive patterns

Strategy: validation

Validate before calling

def trades_ok(trades):
    return bool(trades) and sum(t.size for t in trades) > 0

Type guard

def trades_usable(trades):
    return trades is not None and len(trades) > 0 and all(t.size and t.size > 0 for t in trades)

Try / catch

try:
    strategy.on_historical_trades(trades)
except Exception as e:
    strategy.log.error(f"on_historical_trades failed: {e}", exc_info=True)

Prevention

When it happens

Trigger: 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.

Common situations: Tick-based backtests loading trade history; handlers computing VWAP/footprint metrics hitting zero-quantity division; signature or payload-shape drift after upgrades.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/d72cb5ec07cbd7a7. Report an issue: GitHub.