{"record":{"id":"29985f6e2062b358","repo":"nautechsystems/nautilus_trader","slug":"cannot-extract-market-id-from-instrument-id","errorCode":null,"errorMessage":"Cannot extract market ID from {instrument_id}","messagePattern":"Cannot extract market ID from (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/betfair/src/common/parse.rs","lineNumber":493,"sourceCode":"    )\n    .with_info(info))\n}\n\n/// Extracts the Betfair market ID from a Nautilus instrument ID.\n///\n/// Instrument IDs follow the format `{market_id}-{selection_id}.BETFAIR`\n/// or `{market_id}-{selection_id}-{handicap}.BETFAIR`.\n///\n/// # Errors\n///\n/// Returns an error if the symbol does not contain a hyphen separator.\npub fn extract_market_id(instrument_id: &InstrumentId) -> anyhow::Result<String> {\n    let symbol = instrument_id.symbol.as_str();\n    let parts: Vec<&str> = symbol.splitn(3, '-').collect();\n    if parts.len() >= 2 {\n        Ok(parts[0].to_string())\n    } else {\n        anyhow::bail!(\"Cannot extract market ID from {instrument_id}\")\n    }\n}\n\n/// Extracts the selection ID and handicap from a Nautilus instrument ID.\n///\n/// # Errors\n///\n/// Returns an error if the symbol cannot be parsed into the expected format.\npub fn extract_selection_id(\n    instrument_id: &InstrumentId,\n) -> anyhow::Result<(SelectionId, Decimal)> {\n    let symbol = instrument_id.symbol.as_str();\n    let parts: Vec<&str> = symbol.splitn(3, '-').collect();\n    if parts.len() < 2 {\n        anyhow::bail!(\"Cannot extract selection ID from {instrument_id}\");\n    }\n\n    let selection_id: SelectionId = parts[1]","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/betfair/src/common/parse.rs#L475-L511","documentation":"extract_market_id parses a Betfair instrument ID whose symbol is expected in the form <marketId>-<selectionId>[-<handicap>]. If the symbol cannot be split into at least two '-'-separated parts, no market ID can be extracted and this error is raised.","triggerScenarios":"Calling extract_market_id (directly or via subscribe_book_deltas, submit_order, cancel_order, modify_order) with an InstrumentId whose symbol contains no '-' separator, e.g. 'BETFAIR' instead of '1.23456789-12345'.","commonSituations":"Hand-constructing instrument IDs without the market-selection format; using instrument IDs from another adapter; stripping the suffix when normalizing symbols.","solutions":["Build the instrument ID as '<marketId>-<selectionId>' (optionally '-<handicap>') per Betfair conventions","Validate the symbol format with extract_market_id/extract_selection_id before use","Regenerate the instrument ID from the Betfair catalog rather than constructing it manually"],"exampleFix":"// before\nlet instrument_id = InstrumentId::from(\"BETFAIR.WIN\");\n// after\nlet instrument_id = InstrumentId::from(\"BETFAIR.1.23456789-12345\");","handlingStrategy":"validation","validationCode":"fn valid_betfair_symbol(symbol: &str) -> bool {\n    let mut it = symbol.splitn(2, '-');\n    it.next().map_or(false, |m| !m.is_empty()) && it.next().map_or(false, |s| s.parse::<u64>().is_ok())\n}","typeGuard":"fn as_betfair_instrument_id(id: &InstrumentId) -> Option<String> {\n    extract_market_id(id).ok()\n}","tryCatchPattern":"match extract_market_id(&instrument_id) {\n    Ok(market_id) => subscribe(market_id).await,\n    Err(e) => { log::error!(\"bad Betfair instrument id: {e}\"); Err(e) }\n}","preventionTips":["Create Betfair instrument IDs only via the adapter's catalog/symbol conventions","Validate instrument IDs at config load, before runtime","Never strip the '-<selectionId>' suffix from Betfair symbols"],"tags":["betfair","instrument-id","parsing"],"backgroundTag":"invalid-identifier-format","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}