{"record":{"id":"b95c494ce3b10913","repo":"nautechsystems/nautilus_trader","slug":"cannot-generate-operation-event-for-account","errorCode":null,"errorMessage":"Cannot generate {operation} event for {}: account_id is not set","messagePattern":"Cannot generate (.+?) event for (.+?): account_id is not set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/algorithm/mod.rs","lineNumber":1541,"sourceCode":"fn publish_order_initialized(order: &OrderAny) {\n    let event = OrderEventAny::Initialized(order.init_event().clone());\n    publish_order_event(&event);\n}\n\nfn publish_order_event(event: &OrderEventAny) {\n    let topic = format!(\"events.order.{}\", event.strategy_id());\n    msgbus::publish_order_event(topic.into(), event);\n}\n\nfn registered_trader_id(core: &ExecutionAlgorithmCore) -> anyhow::Result<TraderId> {\n    DataActorNative::core(core)\n        .trader_id()\n        .ok_or_else(|| anyhow::anyhow!(\"ExecutionAlgorithm not registered: trader_id is not set\"))\n}\n\nfn required_account_id(order: &OrderAny, operation: &str) -> anyhow::Result<AccountId> {\n    order.account_id().ok_or_else(|| {\n        anyhow::anyhow!(\n            \"Cannot generate {operation} event for {}: account_id is not set\",\n            order.client_order_id()\n        )\n    })\n}\n\n#[cfg(test)]\nmod tests {\n    use std::{cell::RefCell, rc::Rc};\n\n    use nautilus_common::{\n        actor::DataActor,\n        cache::Cache,\n        clock::TestClock,\n        component::Component,\n        enums::ComponentTrigger,\n        msgbus::{\n            self, TypedHandler,","sourceCodeStart":1523,"sourceCodeEnd":1559,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/algorithm/mod.rs#L1523-L1559","documentation":"Execution algorithms require every order they act on to carry an AccountId, because generated modify/cancel order events must be attributed to the account that owns the order. `required_account_id` extracts the order's account_id and throws this anyhow error when it is None (the order was constructed without an account). This guards downstream event generation (e.g. OrderModify/OrderCancel events) from being emitted for an order the execution layer cannot attribute.","triggerScenarios":"Calling `modify_order` or `cancel_order` on an ExecutionAlgorithm with an `OrderAny` whose account_id was never set (e.g. built via OrderAny builders without `account_id(...)`, or deserialized from fixtures lacking the field).","commonSituations":"Test fixtures or manually constructed orders missing account_id; orders restored from persistence/migrations created before account_id was mandatory; wiring an algorithm against orders from another subsystem that defers account assignment until routing.","solutions":["Set the account_id on the OrderAny before passing it to modify_order/cancel_order (or in tests, use a constructor/factory that assigns it).","Check where the order originated (builder, snapshot, fixture) and ensure account_id is populated at creation time.","If this surfaces in a test like test_required_account_id_errors_when_missing_for_algorithm_event, this error is expected behavior asserting the guard; keep it and fix the fixture instead.","If orders legitimately lack account_id until routing, resolve/assign it earlier in the pipeline so the algorithm always receives fully attributed orders."],"exampleFix":"// before\nlet order = OrderAny::builder(...).build(); // no account_id\nalgo.cancel_order(order)?;\n\n// after\nlet order = OrderAny::builder(...)\n    .account_id(account_id) // e.g. AccountId::from(\"SIM-001\")\n    .build();\nalgo.cancel_order(order)?;","handlingStrategy":"validation","validationCode":"if order.account_id().is_none() {\n    return Err(anyhow::anyhow!(\n        \"order {} has no account_id; cannot generate modify/cancel event\",\n        order.client_order_id()\n    ));\n}","typeGuard":"fn has_account_id(order: &OrderAny) -> bool {\n    order.account_id().is_some()\n}","tryCatchPattern":null,"preventionTips":["Always construct OrderAny through factories/builders that require account_id.","Validate orders at pipeline entry (account_id present) before handing them to execution algorithms.","Keep test fixtures aligned with production constructors so orders are never built without account_id."],"tags":["rust","execution-algorithm","missing-field","account-id","order-events"],"backgroundTag":"missing-required-argument","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}