nautechsystems/nautilus_trader · error · anyhow::Error

Python on_book failed: {e}

Error message

Python on_book failed: {e}

What it means

Wraps a Python exception raised inside the strategy's `on_book` handler. `dispatch_on_book` calls the user's Python `on_book(self, order_book)` with the full `OrderBook` state object; any unhandled exception becomes this anyhow error. It means user strategy code failed while processing the complete book snapshot.

Source

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

    fn on_bar(&mut self, bar: &Bar) -> anyhow::Result<()> {
        self.dispatch_on_bar(*bar)
            .map_err(|e| anyhow::anyhow!("Python on_bar failed: {e}"))
    }

    fn on_book_deltas(&mut self, deltas: &OrderBookDeltas) -> anyhow::Result<()> {
        self.dispatch_on_book_deltas(deltas)
            .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<()> {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the chained `{e}` traceback to find the failing line in `on_book`.
  2. Check `order_book.bid_count() > 0 and order_book.ask_count() > 0` (or None best prices) before computing.
  3. Wrap book-derived computations in try/except and skip empty states.
  4. Reproduce with a recorded data session that includes the empty-book interval.

Example fix

# before
def on_book(self, order_book):
    spread = order_book.best_ask_price() - order_book.best_bid_price()  # None - None

# after
def on_book(self, order_book):
    if order_book.bid_count() == 0 or order_book.ask_count() == 0:
        return
    spread = order_book.best_ask_price() - order_book.best_bid_price()
Defensive patterns

Strategy: validation

Validate before calling

def book_two_sided(book):
    return book.bid_count() > 0 and book.ask_count() > 0

Type guard

def best_prices_available(book):
    bb, ba = book.best_bid_price(), book.best_ask_price()
    return bb is not None and ba is not None

Try / catch

def on_book(self, order_book):
    try:
        self._on_book(order_book)
    except Exception as e:
        self.log.error(f"on_book failed for {order_book.instrument_id}: {e}", exc_info=True)

Prevention

When it happens

Trigger: Unhandled Python exception in `on_book`: calling `order_book.best_bid_price()` on an empty book (returns None and is then used arithmetically), iterating empty bids/asks, or misusing book methods (e.g. `book.update_count`, imprecision flags) across adapter versions.

Common situations: Book accessed before any deltas arrive (empty book right after subscribe); venue outages or session resets producing empty books; strategies computing spreads without checking both sides exist.

Related errors


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