nautechsystems/nautilus_trader · error
Python on_bar failed: {e}
Error message
Python on_bar failed: {e} What it means
Wraps any failure raised when the Rust core dispatches a Bar into the Python actor's `on_bar` handler. The Python `on_bar` implementation raised (or the dispatch failed), and Rust re-raises it prefixed with 'Python on_bar failed'.
Source
Thrown at crates/common/src/python/actor.rs:1113
.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<()> {
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<()> {View on GitHub (pinned to 18893faf8b)
Solutions
- Read the chained `{e}` traceback and fix the exception in `on_bar`
- Confirm the signature `on_bar(self, bar)` and filter by `bar.bar_type` before strategy-specific logic
- Handle bars for unexpected instruments/bar specs by early-returning rather than raising
- Ensure indicators are registered/initialized before the first bar arrives
Example fix
// before
def on_bar(self, bar):
self.fast.update_bar(bar); self.slow.update_bar(bar) # slow may be None
// after
def on_bar(self, bar):
if self.slow is None:
return
self.fast.update_bar(bar); self.slow.update_bar(bar) Defensive patterns
Strategy: try-catch
Validate before calling
def _check_on_bar(self, bar):
try:
self.on_bar(bar)
except Exception as e:
self.log.error(f'on_bar failed: {e!r}') Try / catch
try:
self.dispatch_on_bar(bar)
except Exception as e:
self.log.error(f'Python on_bar failed: {e}', exc_info=True) Prevention
- Filter bars by bar_type before strategy logic
- Update all indicators for every bar type subscribed
- Backtest with session gaps and partial data
When it happens
Trigger: A bar closes (subscribe_bars active) and the Python `on_bar(self, bar)` override raises an exception.
Common situations: Strategy signal logic in `on_bar` accessing indicators updated only for some bar types, wrong handler arity, or state machine bugs when a bar arrives out of expected sequence (gaps, session boundaries).
Related errors
- Python on_order failed: {e}
- Python on_order_list failed: {e}
- Python on_signal failed: {e}
- Python on_queue_state failed: {e}
- Python on_socket_state failed: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/8ba6792267abae6e.
Report an issue: GitHub.