File tree Expand file tree Collapse file tree 6 files changed +110
-10
lines changed
Expand file tree Collapse file tree 6 files changed +110
-10
lines changed Original file line number Diff line number Diff line change 11"""HyperLiquid MCP 常量定义"""
22
33# OCO 订单分组类型
4- OCO_GROUP_NEW_POSITION = "normalTpSl" # 新仓位的括号订单
5- OCO_GROUP_EXISTING_POSITION = "positionTpSl" # 现有仓位的止盈止损
4+ # 注意: 必须与 HyperLiquid SDK 中的 Grouping 类型定义一致
5+ # SDK 定义: Literal["na"], Literal["normalTpsl"], Literal["positionTpsl"]
6+ OCO_GROUP_NEW_POSITION = "normalTpsl" # 新仓位的括号订单 (小写 s)
7+ OCO_GROUP_EXISTING_POSITION = "positionTpsl" # 现有仓位的止盈止损 (小写 s)
68
79# 订单类型常量
810ORDER_TYPE_LIMIT_GTC = {"limit" : {"tif" : "Gtc" }}
Original file line number Diff line number Diff line change @@ -810,12 +810,12 @@ async def set_position_tpsl(
810810 # Add stop loss order if specified
811811 if sl_px is not None :
812812 # For SL orders, use market order for fast execution
813- # No limit_px needed when isMarket=True
813+ # When isMarket=True, limit_px should be 0 or the trigger price
814814 sl_order = {
815815 "coin" : coin ,
816816 "is_buy" : not is_long ,
817817 "sz" : float (position_size ),
818- "limit_px" : float (sl_px ), # Use trigger price as limit_px
818+ "limit_px" : float (sl_px ), # For market orders, use trigger price
819819 "order_type" : {
820820 "trigger" : {
821821 "triggerPx" : float (sl_px ),
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ """
3+ 调试止盈止损订单的 JSON 格式
4+ """
5+
6+ import json
7+ import os
8+ import sys
9+
10+ # Add parent directory to path
11+ sys .path .insert (0 , os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
12+
13+ from hyperliquid .utils .signing import order_type_to_wire
14+
15+ # 模拟一个止盈订单
16+ tp_order = {
17+ "coin" : "BTC" ,
18+ "is_buy" : False , # 平多仓
19+ "sz" : 0.001 ,
20+ "limit_px" : 95000.0 ,
21+ "order_type" : {
22+ "trigger" : {
23+ "triggerPx" : 95000.0 ,
24+ "isMarket" : False ,
25+ "tpsl" : "tp" ,
26+ }
27+ },
28+ "reduce_only" : True ,
29+ }
30+
31+ # 模拟一个止损订单
32+ sl_order = {
33+ "coin" : "BTC" ,
34+ "is_buy" : False , # 平多仓
35+ "sz" : 0.001 ,
36+ "limit_px" : 90000.0 ,
37+ "order_type" : {
38+ "trigger" : {
39+ "triggerPx" : 90000.0 ,
40+ "isMarket" : True ,
41+ "tpsl" : "sl" ,
42+ }
43+ },
44+ "reduce_only" : True ,
45+ }
46+
47+ print ("=" * 60 )
48+ print ("止盈订单 (TP Order):" )
49+ print ("=" * 60 )
50+ print (json .dumps (tp_order , indent = 2 ))
51+
52+ print ("\n " + "=" * 60 )
53+ print ("止损订单 (SL Order):" )
54+ print ("=" * 60 )
55+ print (json .dumps (sl_order , indent = 2 ))
56+
57+ # 测试 order_type 转换
58+ print ("\n " + "=" * 60 )
59+ print ("TP Order Type Wire:" )
60+ print ("=" * 60 )
61+ tp_wire_type = order_type_to_wire (tp_order ["order_type" ])
62+ print (json .dumps (tp_wire_type , indent = 2 ))
63+
64+ print ("\n " + "=" * 60 )
65+ print ("SL Order Type Wire:" )
66+ print ("=" * 60 )
67+ sl_wire_type = order_type_to_wire (sl_order ["order_type" ])
68+ print (json .dumps (sl_wire_type , indent = 2 ))
69+
70+ print ("\n ✅ 订单类型格式验证通过!" )
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ """测试 OCO 分组常量修复"""
3+
4+ from services .constants import OCO_GROUP_EXISTING_POSITION , OCO_GROUP_NEW_POSITION
5+
6+ print ("=" * 60 )
7+ print ("OCO 分组常量验证" )
8+ print ("=" * 60 )
9+
10+ print (f"\n OCO_GROUP_NEW_POSITION = { repr (OCO_GROUP_NEW_POSITION )} " )
11+ print (f"OCO_GROUP_EXISTING_POSITION = { repr (OCO_GROUP_EXISTING_POSITION )} " )
12+
13+ # 验证与 SDK 定义一致
14+ assert OCO_GROUP_NEW_POSITION == "normalTpsl" , f"错误: { OCO_GROUP_NEW_POSITION } "
15+ assert (
16+ OCO_GROUP_EXISTING_POSITION == "positionTpsl"
17+ ), f"错误: { OCO_GROUP_EXISTING_POSITION } "
18+
19+ print ("\n ✅ 所有常量值正确!" )
20+ print ("✅ 与 HyperLiquid SDK Grouping 类型定义一致 (小写 's')" )
21+ print ("\n 修复说明:" )
22+ print ("- 之前: normalTpSl, positionTpSl (大写 S - 错误)" )
23+ print ("- 现在: normalTpsl, positionTpsl (小写 s - 正确)" )
24+ print ("\n 这个修复解决了 '422 Failed to deserialize the JSON body' 错误" )
Original file line number Diff line number Diff line change @@ -10,8 +10,11 @@ def check_constants():
1010 OCO_GROUP_NEW_POSITION ,
1111 )
1212
13- assert OCO_GROUP_NEW_POSITION == "normalTpSl" , "新仓位分组常量错误"
14- assert OCO_GROUP_EXISTING_POSITION == "positionTpSl" , "现有仓位分组常量错误"
13+ # 必须与 HyperLiquid SDK 中的 Grouping 类型定义完全一致(小写 's')
14+ assert OCO_GROUP_NEW_POSITION == "normalTpsl" , "新仓位分组常量错误(应为小写s)"
15+ assert (
16+ OCO_GROUP_EXISTING_POSITION == "positionTpsl"
17+ ), "现有仓位分组常量错误(应为小写s)"
1518 assert ADDRESS_PREFIX_LEN == 6 , "地址前缀长度常量错误"
1619 print ("✅ 常量检查通过" )
1720
Original file line number Diff line number Diff line change 1212)
1313
1414
15- def test_oco_group_constants ():
16- """测试 OCO 分组常量值"""
17- assert OCO_GROUP_NEW_POSITION == "normalTpSl"
18- assert OCO_GROUP_EXISTING_POSITION == "positionTpSl"
15+ def test_oco_grouping_constants ():
16+ """测试 OCO 分组常量与 SDK 定义一致"""
17+ # 必须与 HyperLiquid SDK 中的 Grouping 类型定义完全一致
18+ assert OCO_GROUP_NEW_POSITION == "normalTpsl" # 注意小写 's'
19+ assert OCO_GROUP_EXISTING_POSITION == "positionTpsl" # 注意小写 's'
1920
2021
2122def test_order_type_constants ():
You can’t perform that action at this time.
0 commit comments