{"record":{"id":"a098e99b4b242cc9","repo":"nautechsystems/nautilus_trader","slug":"invalid-position-idx-idx-expected-0-1-or-2","errorCode":null,"errorMessage":"invalid 'position_idx': {idx}, expected 0, 1, or 2","messagePattern":"invalid 'position_idx': (.+?), expected 0, 1, or 2","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/bybit/src/common/parse.rs","lineNumber":1920,"sourceCode":"        }\n    }\n\n    if let Some(value) = params.get(\"smp_type\") {\n        let smp_type = value.as_str().ok_or_else(|| {\n            anyhow::anyhow!(\"invalid type for 'smp_type': {value}, expected string\")\n        })?;\n        result.smp_type = Some(parse_smp_type(smp_type)?);\n    }\n\n    if let Some(value) = params.get(\"position_idx\") {\n        let idx = value.as_i64().ok_or_else(|| {\n            anyhow::anyhow!(\"invalid type for 'position_idx': {value}, expected integer\")\n        })?;\n        result.position_idx = Some(match idx {\n            0 => BybitPositionIdx::OneWay,\n            1 => BybitPositionIdx::BuyHedge,\n            2 => BybitPositionIdx::SellHedge,\n            _ => anyhow::bail!(\"invalid 'position_idx': {idx}, expected 0, 1, or 2\"),\n        });\n    }\n\n    let has_bbo_side_type = params.get(\"bbo_side_type\").is_some();\n    let has_bbo_level = params.get(\"bbo_level\").is_some();\n\n    if has_bbo_side_type != has_bbo_level {\n        anyhow::bail!(\"'bbo_side_type' and 'bbo_level' must be provided together\");\n    }\n\n    if let Some(value) = params.get(\"bbo_side_type\") {\n        let side_type = value.as_str().ok_or_else(|| {\n            anyhow::anyhow!(\"invalid type for 'bbo_side_type': {value}, expected string\")\n        })?;\n        result.bbo_side_type = Some(parse_bbo_side_type(side_type)?);\n    }\n\n    if let Some(value) = params.get(\"bbo_level\") {","sourceCodeStart":1902,"sourceCodeEnd":1938,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/bybit/src/common/parse.rs#L1902-L1938","documentation":"The 'position_idx' field parsed from TP/SL update params did not map to 0, 1, or 2, which are the only values Bybit uses for one-way (0) or hedge-mode (1 buy, 2 sell) position indexing; any other integer is rejected by parse_bybit_tp_sl_params.","triggerScenarios":"Passing 'position_idx' with a value like 3, -1, or 99 in the TP/SL params.","commonSituations":"Loop-generated position indices; hardcoded index copied from another exchange's convention; off-by-one where modes were assumed 1-based.","solutions":["Use only 0, 1, or 2 for 'position_idx'","Map the desired hedge mode to the correct Bybit index (OneWay=0, BuyHedge=1, SellHedge=2)","Omit 'position_idx' if the account uses one-way mode and no override is needed"],"exampleFix":"// before\nparams.set(\"position_idx\", 3);\n// after\nparams.set(\"position_idx\", 1); // BuyHedge","handlingStrategy":"validation","validationCode":"fn validate_position_idx(params: &Params) -> Result<(), String> {\n    if let Some(v) = params.get(\"position_idx\") {\n        match v.as_i64() {\n            Some(i) if (0..=2).contains(&i) => Ok(()),\n            other => Err(format!(\"position_idx must be 0, 1, or 2, got {other:?}\")),\n        }\n    } else { Ok(()) }\n}","typeGuard":"fn is_valid_position_idx(v: &Value) -> bool {\n    matches!(v.as_i64(), Some(0 | 1 | 2))\n}","tryCatchPattern":"match parse_bybit_tp_sl_params(&params) {\n    Ok(p) => submit(p),\n    Err(e) if e.to_string().contains(\"invalid 'position_idx'\") => {\n        log::warn(\"position_idx must be 0 (OneWay), 1 (BuyHedge), or 2 (SellHedge)\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Use the BybitPositionIdx enum instead of raw integers where possible","Remember Bybit indices are 0-based: 0=OneWay, 1=BuyHedge, 2=SellHedge","Clamp/map any exchange-agnostic mode value to a valid Bybit index"],"tags":["bybit","enum-value","position-mode","params-validation"],"backgroundTag":"invalid-enum-value","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"}