nautechsystems/nautilus_trader · error
Python on_quote failed: {e}
Error message
Python on_quote failed: {e} What it means
Wraps a Python exception raised inside the strategy's `on_quote` handler. The Rust data engine forwards each `QuoteTick` via `dispatch_on_quote`, which calls the user's Python `on_quote(self, quote)`; an unhandled exception there is re-thrown as this anyhow error. It indicates user strategy code failed while processing a top-of-book quote.
Source
Thrown at crates/trading/src/python/strategy.rs:1145
}
fn on_socket_state(&mut self, event: &SocketStateChanged) -> anyhow::Result<()> {
self.dispatch_on_socket_state(event)
.map_err(|e| anyhow::anyhow!("Python on_socket_state failed: {e}"))
}
fn on_instrument(&mut self, instrument: &InstrumentAny) -> anyhow::Result<()> {
Python::attach(|py| {
let py_instrument = instrument_any_to_pyobject(py, instrument.clone())
.map_err(|e| anyhow::anyhow!("Failed to convert InstrumentAny to Python: {e}"))?;
self.dispatch_on_instrument(py_instrument)
.map_err(|e| anyhow::anyhow!("Python on_instrument failed: {e}"))
})
}
fn on_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {
self.dispatch_on_quote(*quote)
.map_err(|e| anyhow::anyhow!("Python on_quote failed: {e}"))
}
fn on_trade(&mut self, tick: &TradeTick) -> anyhow::Result<()> {
self.dispatch_on_trade(*tick)
.map_err(|e| anyhow::anyhow!("Python on_trade failed: {e}"))
}
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<()> {View on GitHub (pinned to 18893faf8b)
Solutions
- Inspect the embedded `{e}` traceback to find the failing line in the Python `on_quote`.
- Guard against missing/zero quote sides before computing (check `quote.bid_size`/`ask_size` or None prices).
- Wrap the handler body in try/except to skip unusable quotes and log them.
- Backtest with a recorded stream of the failing symbol to reproduce the edge case.
Example fix
# before
def on_quote(self, quote):
mid = (quote.bid_price + quote.ask_price) / 2 # TypeError when bid is None
# after
def on_quote(self, quote):
if quote.bid_price is None or quote.ask_price is None:
return
mid = (quote.bid_price + quote.ask_price) / 2 Defensive patterns
Strategy: validation
Validate before calling
def quote_usable(quote):
return (quote.bid_price is not None and quote.ask_price is not None
and quote.bid_price > 0 and quote.ask_price > 0) Type guard
def has_two_sided_book(quote):
return quote.bid_size is not None and quote.ask_size is not None and quote.bid_size > 0 and quote.ask_size > 0 Try / catch
def on_quote(self, quote):
try:
self._on_quote(quote)
except Exception as e:
self.log.error(f"on_quote failed for {quote.instrument_id}: {e}", exc_info=True) Prevention
- Check bid/ask for None (one-sided books) before arithmetic
- Never divide by quote prices without a zero check
- Test against thin/illiquid symbols, not just mocked two-sided quotes
- Keep on_quote fast and stateless where possible to reduce exception surface
When it happens
Trigger: Any unhandled Python exception in `on_quote`: arithmetic on None bid/ask (no best price available), division by a zero quote, assuming `quote.bid_size > 0`, or state-machine asserts that fire on rapid quote updates.
Common situations: Markets with one-sided books producing None bid or ask; illiquid symbols with zero sizes; strategies written against mocked quotes that always had both sides populated; hot-loop bugs exposed by very high quote rates.
Related errors
- Python on_trade failed: {e}
- Python on_quote failed: {e}
- Python on_trade failed: {e}
- Python on_degrade failed: {e}
- Python on_time_event failed: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/58c9660ef2f04a62.
Report an issue: GitHub.