fix: 修复止盈止损订单的422反序列化错误 #18
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🐛 问题描述
用户在设置现有仓位的止盈止损时,遇到 HyperLiquid API 返回 422 反序列化错误:
{ "success": false, "error": "(422, None, 'Failed to deserialize the JSON body into the target type', ...)" }请求示例:
{ "coin": "BTC", "position_size": 0.009, "stop_loss_price": 114000, "take_profit_price": 120000 }🔍 根本原因
订单结构中存在矛盾配置:
isMarket: true(市价单)limit_px(使用激进价格计算)✅ 修复方案
1. 止盈订单 (Take Profit): 使用限价单
2. 止损订单 (Stop Loss): 使用市价单
3. 移除激进价格计算
_slippage_price()计算激进价格limit_px🧪 测试覆盖
新增单元测试 (8个)
test_take_profit_order_structure- 止盈订单结构验证test_stop_loss_order_structure- 止损订单结构验证test_combined_tpsl_order_structure- 同时设置止盈止损test_short_position_tpsl_direction- 空头仓位方向测试test_tpsl_without_position_fails- 无仓位时失败测试test_tpsl_price_validation- 价格验证test_auto_detect_position_size- 自动检测仓位大小test_limit_px_equals_trigger_px- limit_px 等于 trigger_px 验证更新集成测试 (2个)
test_bracket_order_uses_correct_grouping- Bracket订单分组验证test_set_position_tpsl_uses_correct_grouping- 止盈止损分组验证测试结果
所有测试全部通过 ✅
📝 变更文件
services/hyperliquid_services.py- 修复set_position_tpsl()方法tests/unit/test_tpsl_orders.py- 新增单元测试tests/integration/test_oco_grouping.py- 更新集成测试🔄 影响范围
set_position_tpsl()方法positionTpSl)📋 Checklist
🎯 相关Issue
修复用户报告的止盈止损设置失败问题(422错误)