{"record":{"id":"47dd217ece6cbbfd","repo":"nautechsystems/nautilus_trader","slug":"no-valid-legs-loaded-for-bag-contract","errorCode":null,"errorMessage":"No valid legs loaded for BAG contract","messagePattern":"No valid legs loaded for BAG contract","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/interactive_brokers/src/providers/instruments.rs","lineNumber":2450,"sourceCode":"\n            // Get the contract details for this leg (should be cached now)\n            let leg_details_clone = self\n                .contract_details\n                .get(&leg_instrument_id)\n                .map(|entry| entry.value().clone())\n                .ok_or_else(|| {\n                    anyhow::anyhow!(\n                        \"Contract details not found for leg {} after loading\",\n                        leg_instrument_id\n                    )\n                })?;\n\n            leg_contract_details.push((leg_details_clone, ratio));\n            leg_tuples.push((leg_instrument_id, ratio));\n        }\n\n        if leg_tuples.is_empty() {\n            anyhow::bail!(\"No valid legs loaded for BAG contract\");\n        }\n\n        // Create spread instrument ID from leg tuples\n        let spread_instrument_id = create_spread_instrument_id(&leg_tuples)\n            .context(\"Failed to create spread instrument ID from leg tuples\")?;\n\n        // Fetch BAG contract details (for storing the mapping)\n        let bag_details_vec = client\n            .contract_details(bag_contract)\n            .await\n            .context(\"Failed to fetch BAG contract details from IB\")?;\n\n        if bag_details_vec.is_empty() {\n            tracing::warn!(\"No contract details returned for BAG contract\");\n\n            if bag_contract.contract_id != 0 && self.instruments.contains_key(&spread_instrument_id)\n            {\n                self.contract_id_to_instrument_id","sourceCodeStart":2432,"sourceCodeEnd":2468,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/interactive_brokers/src/providers/instruments.rs#L2432-L2468","documentation":"After iterating the BAG contract's combo legs, fetch_bag_contract requires at least one leg to have resolved successfully (each leg's own contract details must be fetched and parsed into an instrument ID). If every leg failed to load, leg_tuples is empty and there is no basis to construct the spread, so the call fails.","triggerScenarios":"All individual legs of a BAG contract failed during leg loading — e.g., each leg's con_id lookup returned nothing or leg instrument IDs could not be resolved — leaving leg_tuples empty.","commonSituations":"Expired or invalid leg con_ids, network/permissions failures silently skipping legs, or legs whose symbols/exchanges don't resolve on IB.","solutions":["Inspect debug logs for per-leg failures; fix the underlying leg resolution errors (bad con_ids, missing permissions).","Validate each leg's con_id is live on IB (check in TWS).","Re-run after confirming IB Gateway connectivity and market-data permissions for each leg's exchange.","If the spread is stale (legs expired), redefine the spread with current leg contracts."],"exampleFix":"// before: spread with expired leg con_ids\nlet legs = vec![ComboLeg { con_id: 1, ratio: 1, action: \"BUY\".into(), ..Default::default() }];\n// after: valid, current leg con_ids\nlet legs = vec![\n    ComboLeg { con_id: 552824014, ratio: 1, action: \"BUY\".into(), ..Default::default() },\n    ComboLeg { con_id: 552824046, ratio: 1, action: \"SELL\".into(), ..Default::default() },\n];","handlingStrategy":"validation","validationCode":"fn legs_look_valid(contract: &Contract) -> bool {\n    contract.combo_legs.iter().all(|l| l.con_id != 0 && l.ratio > 0)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check leg con_ids are live in TWS before building spreads","Review debug logs for per-leg failures — a single silent leg failure can empty the whole set","Replace legs when contracts expire"],"tags":["interactive-brokers","bag-contract","spread","leg-resolution"],"backgroundTag":"empty-result-set","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"}