nautechsystems/nautilus_trader · error · anyhow::Error

mass cancel orders failed

Error message

mass cancel orders failed

What it means

Mass cancel (cancel all open orders) for an instrument failed on the OKX REST endpoint — the branch used for spread instruments. http_client.cancel_all_orders returned Err, the failure is logged with a classification, and the task returns 'mass cancel orders failed' with the cause chained.

Source

Thrown at crates/adapters/okx/src/execution.rs:1112

                        if is_rejected {
                            anyhow::bail!("{reason}");
                        }
                    }
                }
            }

            Ok(())
        });
    }

    fn mass_cancel_instrument(&self, instrument_id: InstrumentId) {
        if is_spread_instrument(instrument_id) {
            let http_client = self.http_client.clone();
            self.spawn_task("mass_cancel_orders_http", async move {
                if let Err(e) = http_client.cancel_all_orders(instrument_id).await {
                    log_mass_cancel_failure(classify_okx_http_failure(&e), instrument_id);
                    return Err(anyhow::Error::new(e).context("mass cancel orders failed"));
                }
                Ok(())
            });
            return;
        }

        let ws_private = self.ws_private.clone();

        self.spawn_task("mass_cancel_orders", async move {
            if let Err(e) = ws_private.mass_cancel_orders(instrument_id).await {
                log_mass_cancel_failure(classify_okx_ws_failure(&e), instrument_id);
                return Err(anyhow::Error::new(e).context("mass cancel orders failed"));
            }
            Ok(())
        });
    }

    /// Populates `order_identities` for an order if not already present.

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the classified failure in the log and the chained cause for the OKX error code.
  2. Verify API key trade permission and live/demo domain correctness.
  3. If rate-limited (429), back off and retry the mass cancel, or cancel orders individually.
  4. Confirm network/proxy connectivity to OKX REST and that the instrument id is still valid on OKX.
Defensive patterns

Strategy: retry

Validate before calling

// Check REST reachability and credentials before bulk cancels
// (configuration check) ensure OKX_API_KEY/SECRET/PASSPHRASE set and domain matches env

Try / catch

// Retry mass cancel with backoff, then fall back to per-order HTTP cancels
if let Err(e) = trader.cancel_all_orders(instrument_id) {
    log::warn!("mass cancel failed: {e:?}; falling back to per-order cancels");
    for cid in open_order_ids(instrument_id) { trader.cancel_order(cid); }
}

Prevention

When it happens

Trigger: cancel_all_orders on a spread instrument; http_client.cancel_all_orders(instrument_id) returned Err: HTTP transport failure, non-2xx OKX response, auth failure, or venue-side rejection of the mass-cancel request.

Common situations: Bulk flat before a session close on spread instruments; OKX rate limiting from many mass cancels; API key without trade permission; network outage to OKX REST; instrument id format mismatch after a symbol change.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


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