nautechsystems/nautilus_trader · error

Order {primary_id} already being executed

Error message

Order {primary_id} already being executed

What it means

TwapAlgorithm::on_order rejects an order whose client_order_id is already present in scheduled_orders, since each primary order can be scheduled for TWAP execution only once. A duplicate on_order call for the same order would double-count executions.

Source

Thrown at crates/trading/src/algorithm/twap.rs:120

    fn on_stop(&mut self) -> anyhow::Result<()> {
        ExecutionAlgorithm::on_stop(self)
    }

    fn on_resume(&mut self) -> anyhow::Result<()> {
        ExecutionAlgorithm::on_resume(self)
    }

    fn on_reset(&mut self) -> anyhow::Result<()> {
        ExecutionAlgorithm::on_reset(self)
    }
}

nautilus_execution_algorithm!(TwapAlgorithm, {
    fn on_order(&mut self, order: OrderAny) -> anyhow::Result<()> {
        let primary_id = order.client_order_id();

        if self.scheduled_orders.contains_key(&primary_id) {
            anyhow::bail!("Order {primary_id} already being executed");
        }

        log::info!("Received order for TWAP execution: {order:?}");

        // Only market orders supported
        if order.order_type() != OrderType::Market {
            let reason = OrderDeniedReason::UnsupportedOrderType {
                order_type: order.order_type(),
            }
            .to_string();
            return self.deny_order(&order, Ustr::from(&reason));
        }

        let instrument = {
            let cache = ExecutionAlgorithmNative::exec_algorithm_core(self).cache_ref();
            cache.instrument(&order.instrument_id()).cloned()
        };

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Ensure the order is only routed to the exec algorithm once; check algorithm state before resubmitting
  2. Generate a unique client_order_id per submission instead of reusing IDs
  3. Deduplicate order events on replay/reconnect before invoking on_order
  4. Check twap.scheduled_orders.contains_key(&client_order_id) before submitting

Example fix

// before
exec_algorithm.submit(order);
// after
if !exec_algorithm.is_scheduled(&order.client_order_id()) {
    exec_algorithm.submit(order);
}
Defensive patterns

Strategy: validation

Validate before calling

if exec_algorithm.scheduled_orders().contains_key(&order.client_order_id()) { return; }

Prevention

When it happens

Trigger: Submitting the same order to the TWAP algorithm twice, re-processing an order event (e.g. after a reconnect/replay), or reusing a client_order_id for a new submission routed to the same algorithm.

Common situations: Event replay after a crash/restart without deduplication; strategy retry logic re-emitting the order; client_order_id reuse across sessions.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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