nautechsystems/nautilus_trader · error

Python on_time_event failed: {e}

Error message

Python on_time_event failed: {e}

What it means

The strategy's Python-level `on_time_event` (timer/alarm) callback raised an exception. Before dispatch, `route_time_event(self, event)` routes the event, then `dispatch_on_time_event` invokes the Python method; any exception is wrapped at strategy.rs:1107 as `Python on_time_event failed: {e}`. Because timers fire on a schedule, this error repeats every interval until fixed.

Source

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

    fn on_fault(&mut self) -> anyhow::Result<()> {
        self.dispatch_on_fault()
            .map_err(|e| anyhow::anyhow!("Python on_fault failed: {e}"))
    }

    fn on_save(&self) -> anyhow::Result<IndexMap<String, Vec<u8>>> {
        self.dispatch_on_save()
            .map_err(|e| anyhow::anyhow!("Python on_save failed: {e}"))
    }

    fn on_load(&mut self, state: IndexMap<String, Vec<u8>>) -> anyhow::Result<()> {
        self.dispatch_on_load(&state)
            .map_err(|e| anyhow::anyhow!("Python on_load failed: {e}"))
    }

    fn on_time_event(&mut self, event: &TimeEvent) -> anyhow::Result<()> {
        route_time_event(self, event);
        self.dispatch_on_time_event(event)
            .map_err(|e| anyhow::anyhow!("Python on_time_event failed: {e}"))
    }

    #[allow(unused_variables)]
    fn on_data(&mut self, data: &CustomData) -> anyhow::Result<()> {
        Python::attach(|py| {
            let py_data: Py<PyAny> = Py::new(py, data.clone())?.into_any();
            self.dispatch_on_data(py_data)
                .map_err(|e| anyhow::anyhow!("Python on_data failed: {e}"))
        })
    }

    fn on_signal(&mut self, signal: &Signal) -> anyhow::Result<()> {
        self.dispatch_on_signal(signal)
            .map_err(|e| anyhow::anyhow!("Python on_signal failed: {e}"))
    }

    fn on_queue_state(&mut self, event: &QueueStateChanged) -> anyhow::Result<()> {
        self.dispatch_on_queue_state(event)

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect `{e}` for the Python exception and fix the handler body; note the TimeEvent `name` to identify which timer fired.
  2. Add early-return guards for not-yet-available state (e.g. `if self.last_price is None: return`).
  3. Wrap periodic timer logic in try/except with logging so one bad tick does not kill the strategy loop.
  4. Check `route_time_event` wiring: ensure each registered timer name maps to the intended handler.

Example fix

// before
def on_time_event(self, event):
    ret = (self.last_close - self.ema) / self.ema  # ZeroDivisionError on first ticks

// after
def on_time_event(self, event):
    if self.ema is None or self.ema == 0:
        return
    ret = (self.last_close - self.ema) / self.ema
Defensive patterns

Strategy: try-catch

Validate before calling

def safe_timer(handler):
    def wrapper(event):
        try:
            handler(event)
        except Exception as e:
            log.error(f"timer {getattr(event, 'name', '?')} failed: {e}")
    return wrapper
# register: clock.set_timer("tick", interval, callback=safe_timer(self.on_time_event))

Type guard

def is_warm(self, *attrs):
    return all(getattr(self, a, None) is not None for a in attrs)

Try / catch

def on_time_event(self, event):
    try:
        self._periodic_update()
    except Exception as e:
        self.log.error(f"timer {event.name} handler failed: {e}")

Prevention

When it happens

Trigger: A timer registered via `clock.set_timer(...)` (or an interval/time handler) fires and the Python handler raises — e.g. division by zero in a periodic indicator update, calling a market-data method with no data yet, or an exception in code the timer lambda captured.

Common situations: Timers that fire before warm-up data exists; timer callbacks using `self.last_price` that is still None; exceptions inside repeated timers that spam logs and may halt the strategy; timer name/wiring mistakes where the handler for a different timer raises.

Related errors


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