{"record":{"id":"9127efe4d50c44b8","repo":"nautechsystems/nautilus_trader","slug":"order-primary-id-already-being-executed","errorCode":null,"errorMessage":"Order {primary_id} already being executed","messagePattern":"Order (.+?) already being executed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/algorithm/twap.rs","lineNumber":120,"sourceCode":"    fn on_stop(&mut self) -> anyhow::Result<()> {\n        ExecutionAlgorithm::on_stop(self)\n    }\n\n    fn on_resume(&mut self) -> anyhow::Result<()> {\n        ExecutionAlgorithm::on_resume(self)\n    }\n\n    fn on_reset(&mut self) -> anyhow::Result<()> {\n        ExecutionAlgorithm::on_reset(self)\n    }\n}\n\nnautilus_execution_algorithm!(TwapAlgorithm, {\n    fn on_order(&mut self, order: OrderAny) -> anyhow::Result<()> {\n        let primary_id = order.client_order_id();\n\n        if self.scheduled_orders.contains_key(&primary_id) {\n            anyhow::bail!(\"Order {primary_id} already being executed\");\n        }\n\n        log::info!(\"Received order for TWAP execution: {order:?}\");\n\n        // Only market orders supported\n        if order.order_type() != OrderType::Market {\n            let reason = OrderDeniedReason::UnsupportedOrderType {\n                order_type: order.order_type(),\n            }\n            .to_string();\n            return self.deny_order(&order, Ustr::from(&reason));\n        }\n\n        let instrument = {\n            let cache = ExecutionAlgorithmNative::exec_algorithm_core(self).cache_ref();\n            cache.instrument(&order.instrument_id()).cloned()\n        };\n","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/algorithm/twap.rs#L102-L138","documentation":"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.","triggerScenarios":"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.","commonSituations":"Event replay after a crash/restart without deduplication; strategy retry logic re-emitting the order; client_order_id reuse across sessions.","solutions":["Ensure the order is only routed to the exec algorithm once; check algorithm state before resubmitting","Generate a unique client_order_id per submission instead of reusing IDs","Deduplicate order events on replay/reconnect before invoking on_order","Check twap.scheduled_orders.contains_key(&client_order_id) before submitting"],"exampleFix":"// before\nexec_algorithm.submit(order);\n// after\nif !exec_algorithm.is_scheduled(&order.client_order_id()) {\n    exec_algorithm.submit(order);\n}","handlingStrategy":"validation","validationCode":"if exec_algorithm.scheduled_orders().contains_key(&order.client_order_id()) { return; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Submit each order to the exec algorithm exactly once","Deduplicate replayed/reconnected order events","Always generate fresh client_order_ids per submission"],"tags":["rust","twap","duplicate"],"backgroundTag":"file-already-exists","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}