nautechsystems/nautilus_trader · error

Python on_index_price failed: {e}

Error message

Python on_index_price failed: {e}

What it means

This error wraps any Python exception raised inside the actor's `on_index_price` callback when an `IndexPriceUpdate` is delivered to the Python subclass. The Rust `PyDataActor` invokes the Python method via `call_method1`; a `PyErr` is converted to an anyhow error with this message. The actual exception origin is in user Python handler code (or the Rust-to-Python conversion), not the dispatch itself.

Source

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

    fn on_book_depth(&mut self, depth: &OrderBookDepth10) -> anyhow::Result<()> {
        self.dispatch_on_book_depth(depth)
            .map_err(|e| anyhow::anyhow!("Python on_book_depth failed: {e}"))
    }

    fn on_book(&mut self, order_book: &OrderBook) -> anyhow::Result<()> {
        self.dispatch_on_book(order_book)
            .map_err(|e| anyhow::anyhow!("Python on_book failed: {e}"))
    }

    fn on_mark_price(&mut self, mark_price: &MarkPriceUpdate) -> anyhow::Result<()> {
        self.dispatch_on_mark_price(*mark_price)
            .map_err(|e| anyhow::anyhow!("Python on_mark_price failed: {e}"))
    }

    fn on_index_price(&mut self, index_price: &IndexPriceUpdate) -> anyhow::Result<()> {
        self.dispatch_on_index_price(*index_price)
            .map_err(|e| anyhow::anyhow!("Python on_index_price failed: {e}"))
    }

    fn on_funding_rate(&mut self, funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {
        self.dispatch_on_funding_rate(*funding_rate)
            .map_err(|e| anyhow::anyhow!("Python on_funding_rate failed: {e}"))
    }

    fn on_instrument_status(&mut self, data: &InstrumentStatus) -> anyhow::Result<()> {
        self.dispatch_on_instrument_status(*data)
            .map_err(|e| anyhow::anyhow!("Python on_instrument_status failed: {e}"))
    }

    fn on_instrument_close(&mut self, update: &InstrumentClose) -> anyhow::Result<()> {
        self.dispatch_on_instrument_close(*update)
            .map_err(|e| anyhow::anyhow!("Python on_instrument_close failed: {e}"))
    }

    fn on_option_greeks(&mut self, greeks: &OptionGreeks) -> anyhow::Result<()> {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect the Python traceback appended after 'Python on_index_price failed:' to locate the raising line.
  2. Validate index_price fields (None/NaN) before computing anything in the handler.
  3. Confirm the callback signature matches on_index_price(self, index_price) for the installed version.
  4. Filter subscriptions so only expected instruments reach this handler.

Example fix

// before
def on_index_price(self, index_price):
    self.last_index[index_price.instrument_id.value] = index_price.price

// after
def on_index_price(self, index_price):
    if index_price.price is None:
        return
    self.last_index[str(index_price.instrument_id)] = index_price.price
Defensive patterns

Strategy: type-guard

Validate before calling

if index_price.price is None or index_price.price != index_price.price:
    return  # None or NaN

Type guard

def is_usable_index_price(update):
    px = getattr(update, "price", None)
    return isinstance(px, (int, float)) and px == px and px != 0

Try / catch

def on_index_price(self, index_price):
    try:
        self._on_index(index_price)
    except Exception as e:
        self.log.error(f"on_index_price failed: {e}", exc_info=True)

Prevention

When it happens

Trigger: Any exception raised by the user's `on_index_price(self, index_price)` override — e.g. referencing index_price.price when the conversion produced a different attribute, dict lookups on an unseen instrument_id, or helper functions that raise on the first index-price tick.

Common situations: Index-arbitrage strategies consuming index prices whose handler assumes an attribute layout from an older nautilus_trader version; strategies receiving index prices for instruments they didn't expect due to misconfigured subscriptions.

Related errors


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