nautechsystems/nautilus_trader · error

Python on_mark_price failed: {e}

Error message

Python on_mark_price failed: {e}

What it means

This error wraps any Python exception raised inside the actor's `on_mark_price` callback when a `MarkPriceUpdate` is dispatched to the Python subclass. The Rust `PyDataActor` calls the Python method via `call_method1` and converts the `PyErr` into an anyhow error with this message. The real failure is in user Python code; the wrapped traceback after `{e}` points to it.

Source

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

    fn on_book_deltas(&mut self, deltas: &OrderBookDeltas) -> anyhow::Result<()> {
        self.dispatch_on_book_deltas(deltas.clone())
            .map_err(|e| anyhow::anyhow!("Python on_book_deltas failed: {e}"))
    }

    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<()> {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the appended Python traceback after 'Python on_mark_price failed:' to find the raising line in your handler.
  2. Guard against None/NaN mark prices before arithmetic (check `math.isnan` / None).
  3. Verify attribute names against the MarkPriceUpdate API for your installed nautilus_trader version.
  4. Add unit tests that feed synthetic MarkPriceUpdate messages to the actor callback.

Example fix

// before
def on_mark_price(self, mark_price):
    spread = mark_price.mark_price - self.mid

// after
def on_mark_price(self, mark_price):
    mp = mark_price.mark_price
    if mp is None or mp != mp:  # None or NaN
        return
    spread = mp - self.mid
Defensive patterns

Strategy: type-guard

Validate before calling

import math
if mark_price.mark_price is None or math.isnan(mark_price.mark_price):
    return

Type guard

def has_valid_mark_price(update):
    mp = getattr(update, "mark_price", None)
    return isinstance(mp, (int, float)) and not math.isnan(mp)

Try / catch

def on_mark_price(self, mark_price):
    try:
        self._update_mark(mark_price)
    except Exception as e:
        self.log.error(f"on_mark_price failed: {e}", exc_info=True)

Prevention

When it happens

Trigger: Any exception raised by the user's `on_mark_price(self, mark_price)` override — e.g. arithmetic on a None/NaN mark price, keying a dict by an attribute that doesn't exist on the converted MarkPriceUpdate, or a raising conversion of MarkPriceUpdate into a Python object.

Common situations: Derivatives strategies computing funding/PnL from mark prices that hit a NaN first tick; renaming mark price fields after a nautilus_trader upgrade so the handler references stale attributes; subscribing to mark price without realizing the callback runs on every update.

Related errors


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