nautechsystems/nautilus_trader · error

Python on_historical_trades failed: {e}

Error message

Python on_historical_trades failed: {e}

What it means

Raised by `DataActor::on_historical_trades` (crates/common/src/python/actor.rs:1233) when dispatching a slice of `TradeTick` historical data to the Python actor's `on_historical_trades` callback fails. The dispatch copies the trades and calls the Python method through pyo3; exceptions raised in the Python handler, an absent callback method, or trade-to-Python conversion failures are wrapped as `Python on_historical_trades failed: {e}`.

Source

Thrown at crates/common/src/python/actor.rs:1233

    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 wrapped `{e}` cause to get the original Python exception and fix the `on_historical_trades` handler.
  2. Ensure the Python actor implements `on_historical_trades(self, trades)`.
  3. Handle empty trade lists and validate price/size fields before aggregation math.
  4. Confirm TradeTick field names against your installed nautilus version.

Example fix

# before
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_size = sum(t.size for t in trades)
    if not trades or total_size == 0:
        return
    vwap = sum(t.price * t.size for t in trades) / total_size
Defensive patterns

Strategy: try-catch

Validate before calling

assert callable(getattr(actor, 'on_historical_trades', None)), "actor must implement on_historical_trades"

Type guard

def has_trades_handler(obj):
        return callable(getattr(obj, 'on_historical_trades', None))

Try / catch

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

Prevention

When it happens

Trigger: Fires when historical trade ticks are delivered (e.g. result of `request_trades`) and the Python `on_historical_trades` callback raises an exception, the Python object lacks a callable `on_historical_trades`, or the TradeTick list fails conversion.

Common situations: Handler analysis code (VWAP, aggregation) raising on empty input or bad price/size fields; requesting historical trades without implementing the handler; confusion between live on_trade_tick and historical variant; schema changes after upgrades.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


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