{"record":{"id":"a8d51b588a5b7865","repo":"nautechsystems/nautilus_trader","slug":"invalid-bag-contract-must-have-security-type-spre","errorCode":null,"errorMessage":"Invalid BAG contract: must have security_type=Spread and non-empty combo_legs","messagePattern":"Invalid BAG contract: must have security_type=Spread and non-empty combo_legs","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/interactive_brokers/src/providers/instruments.rs","lineNumber":2323,"sourceCode":"    /// Returns the number of spread instruments loaded (0 or 1).\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if fetching fails.\n    ///\n    /// # Notes\n    ///\n    /// This method now auto-loads all leg instruments from combo_legs and creates\n    /// a proper spread instrument, matching Python's `_load_bag_contract` behavior.\n    pub async fn fetch_bag_contract(\n        &self,\n        client: &ibapi::Client,\n        bag_contract: &Contract,\n    ) -> anyhow::Result<usize> {\n        // Validate BAG contract\n        if bag_contract.security_type != SecurityType::Spread || bag_contract.combo_legs.is_empty()\n        {\n            anyhow::bail!(\n                \"Invalid BAG contract: must have security_type=Spread and non-empty combo_legs\"\n            );\n        }\n\n        tracing::debug!(\n            \"Loading BAG contract with {} legs\",\n            bag_contract.combo_legs.len()\n        );\n\n        // First, load all individual leg instruments and collect their details\n        let mut leg_contract_details = Vec::new();\n        let mut leg_tuples = Vec::new();\n\n        for combo_leg in &bag_contract.combo_legs {\n            // Create a leg contract using information from the combo leg\n            let leg_contract = Contract {\n                contract_id: combo_leg.contract_id,  // Use conId from combo_leg\n                symbol: bag_contract.symbol.clone(), // Use underlying symbol from BAG","sourceCodeStart":2305,"sourceCodeEnd":2341,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/interactive_brokers/src/providers/instruments.rs#L2305-L2341","documentation":"fetch_bag_contract loads a BAG (combo/spread) contract from IB and first validates the input contract. IB represents spreads as security type 'Spread' (BAG) with a non-empty combo_legs list; anything else cannot be decomposed into legs, so the call fails with this validation error.","triggerScenarios":"get_instrument calls fetch_bag_contract with a contract whose security_type is not SecurityType::Spread or whose combo_legs is empty.","commonSituations":"Passing a single-leg contract or a plain future/option to the BAG loading path, or a BAG contract built without legs (missing con_id/ratio/action data).","solutions":["Ensure the contract has security_type = SecurityType::Spread before calling the BAG load path.","Populate combo_legs with at least one leg (con_id, ratio, action).","Check upstream code isn't misclassifying the contract as BAG when it is a single instrument.","Fetch contract details from IB to populate legs automatically rather than constructing by hand."],"exampleFix":"// before: mislabeled BAG\nlet bag = Contract { symbol: \"SPY\".into(), security_type: SecurityType::Stock, ..Default::default() };\nprovider.fetch_bag_contract(&client, &bag).await?;\n// after: proper BAG definition\nlet bag = Contract {\n    symbol: \"SPY,VND\".into(),\n    security_type: SecurityType::Spread,\n    combo_legs: vec![leg_buy, leg_sell],\n    ..Default::default()\n};\nprovider.fetch_bag_contract(&client, &bag).await?;","handlingStrategy":"validation","validationCode":"fn is_valid_bag(contract: &Contract) -> bool {\n    contract.security_type == SecurityType::Spread && !contract.combo_legs.is_empty()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate Spread + non-empty combo_legs before any BAG load call","Build spreads programmatically from IB contract details rather than by hand","Unit-test spread construction helpers"],"tags":["interactive-brokers","bag-contract","validation","spread"],"backgroundTag":"invalid-argument-value","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"}