nautechsystems/nautilus_trader · error
Python on_trade failed: {e}
Error message
Python on_trade failed: {e} What it means
Wraps any failure raised when the Rust core dispatches a TradeTick into the Python actor's `on_trade` handler. The Python `on_trade` implementation raised (or dispatch into Python failed), and Rust re-raises with the 'Python on_trade failed' prefix.
Source
Thrown at crates/common/src/python/actor.rs:1108
}
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.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<()> {View on GitHub (pinned to 18893faf8b)
Solutions
- Inspect the wrapped `{e}` traceback and fix the exception inside `on_trade`
- Verify the override signature `on_trade(self, tick)` taking a TradeTick
- Initialize all handler state in on_start before subscribing to trade ticks
- Add defensive checks for None/empty aggressor side or price fields
Example fix
// before
def on_trade(self, tick):
self.vwap.update(tick.price, None) # TypeError on None size
// after
def on_trade(self, tick):
if tick.size is None or tick.size.as_double() <= 0:
return
self.vwap.update(tick.price, tick.size) Defensive patterns
Strategy: try-catch
Validate before calling
def _valid_trade(tick) -> bool:
return tick.price is not None and tick.size is not None and tick.size > 0 Try / catch
try:
self.dispatch_on_trade(tick)
except Exception as e:
self.log.error(f'Python on_trade failed: {e}', exc_info=True) Prevention
- Check size/price validity before aggregation
- Guard order submissions triggered from on_trade (rate/duplicate checks)
- Initialize trade-aggregation state in on_start
When it happens
Trigger: A trade tick arrives after subscribe_trade_ticks and the Python `on_trade(self, tick)` override raises an exception.
Common situations: Trade aggregation logic with uninitialized state, wrong handler signature, or exceptions from order submission triggered on every trade in fast markets.
Related errors
- Python on_quote failed: {e}
- Python on_order failed: {e}
- Python on_order_list failed: {e}
- Python on_signal failed: {e}
- Python on_queue_state failed: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/f7879e9614d9dc1d.
Report an issue: GitHub.