nautechsystems/nautilus_trader · error

Python on_instrument_status failed: {e}

Error message

Python on_instrument_status failed: {e}

What it means

This error wraps any Python exception raised inside the actor's `on_instrument_status` callback when an `InstrumentStatus` event (e.g. trading halt/resume) is dispatched to the Python subclass. The Rust `PyDataActor` invokes the Python method via `call_method1`; a resulting `PyErr` is converted to an anyhow error with this message. The exception itself is raised in user Python code or during Rust-to-Python conversion of the event.

Source

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

    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<()> {
        self.dispatch_on_option_greeks(*greeks)
            .map_err(|e| anyhow::anyhow!("Python on_option_greeks failed: {e}"))
    }

    fn on_option_chain(&mut self, slice: &OptionChainSlice) -> anyhow::Result<()> {
        self.dispatch_on_option_chain(slice.clone())
            .map_err(|e| anyhow::anyhow!("Python on_option_chain failed: {e}"))
    }

    #[cfg(feature = "defi")]

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the appended Python traceback after 'Python on_instrument_status failed:' to locate the raising line.
  2. Handle unknown status values with a default branch instead of assuming a closed enum.
  3. Match the InstrumentStatus API (event, status action) for your installed nautilus_trader version.
  4. Log the raw event before processing so unexpected statuses are visible in production.

Example fix

// before
def on_instrument_status(self, data):
    if data.status == InstrumentStatusAction.TRADING_HALT:
        self.cancel_all()

// after
def on_instrument_status(self, data):
    action = getattr(data, "status", None)
    if action == InstrumentStatusAction.TRADING_HALT:
        self.cancel_all()
    else:
        self.log.warning(f"Unhandled instrument status: {action}")
Defensive patterns

Strategy: validation

Validate before calling

action = getattr(data, "status", None)
known = {s for s in InstrumentStatusAction}
if action not in known:
    self.log.warning(f"unknown status {action!r}"); return

Type guard

def is_known_status(data):
    action = getattr(data, "status", None)
    try:
        return action in set(InstrumentStatusAction)
    except TypeError:
        return False

Try / catch

def on_instrument_status(self, data):
    try:
        self._handle_status(data)
    except Exception as e:
        self.log.error(f"on_instrument_status failed: {e}", exc_info=True)

Prevention

When it happens

Trigger: Any exception raised by the user's `on_instrument_status(self, data)` override — e.g. comparing data.status against a non-existent enum member, mutating a dict during iteration, or a helper expecting a different status-event type.

Common situations: Strategies reacting to exchange trading halts/maintenance windows; venue-specific status events arriving with statuses the handler's enum mapping lacks; brokers pushing unexpected instrument status during volatile sessions.

Related errors


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