nautechsystems/nautilus_trader · error
Python on_bar failed: {e}
Error message
Python on_bar failed: {e} What it means
Wraps a Python exception raised inside the strategy's `on_bar` handler. `dispatch_on_bar` calls the user's Python `on_bar(self, bar)` for each `Bar` delivered by the Rust core; an unhandled exception is re-thrown as this anyhow error. It indicates user strategy code failed while processing an aggregated bar.
Source
Thrown at crates/trading/src/python/strategy.rs:1155
.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<()> {
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
- Inspect the chained `{e}` traceback for the failing line in the Python `on_bar`.
- Validate indicator warm-up length before indexing rolling buffers.
- Wrap indicator updates in try/except during the first N bars.
- Run a backtest over the same bar aggregation to reproduce offline.
Example fix
# before
def on_bar(self, bar):
self.closes.append(bar.close)
avg = sum(self.closes[-self.window:]) / self.window # short-buffer math errors early
# after
def on_bar(self, bar):
self.closes.append(bar.close)
if len(self.closes) < self.window:
return
avg = sum(self.closes[-self.window:]) / self.window Defensive patterns
Strategy: validation
Validate before calling
# before using rolling state
def enough_history(closes, window):
return len(closes) >= window Type guard
def is_valid_bar(bar):
return bar.open is not None and bar.high >= bar.low and bar.close is not None Try / catch
def on_bar(self, bar):
try:
self._on_bar(bar)
except Exception as e:
self.log.error(f"on_bar failed for {bar.bar_type}: {e}", exc_info=True) Prevention
- Wait for indicator warm-up before reading rolling buffers
- Validate bar integrity (high >= low, nonzero prices) before use
- Pin and re-verify Price/Quantity conversion helpers after upgrades
- Backtest with the exact bar aggregation used live
When it happens
Trigger: Unhandled Python exception in `on_bar`: indicator update calls (e.g. feeding an uninitialized indicator), division by a zero bar close, lookups into deques/arrays shorter than expected on early bars, or `bar.close.as_double()` misuse after API changes.
Common situations: Indicator warm-up windows longer than the available history causing index errors; strategies assuming bars never have zero volume; version upgrades where Price/Quantity conversion helpers changed (e.g. `.as_double()` vs `float()`).
Related errors
- Python on_historical_bars failed: {e}
- Python on_bar failed: {e}
- Python on_degrade failed: {e}
- Python on_time_event failed: {e}
- Python on_data failed: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/84960ba42ad4ab7e.
Report an issue: GitHub.