Kraken交易所WebSocket AddOrder接口无法接收订单响应问题求助
Kraken WebSocket addOrder 响应接收问题解决
问题场景
通过WebSocket发送订单后无法正确接收响应,执行代码时收到错误:
{"errorMessage":"Public market data subscriptions are unavailable on this endpoint. Try ws.kraken.com","event":"subscriptionStatus","status":"error","subscription":{"name":"addOrder","token":"X"}}
错误原因
代码中错误地对addOrder执行了订阅操作,但Kraken的ws-auth.kraken.com交易端点不需要订阅addOrder事件——该接口仅用于发送交易请求并直接接收响应,订阅操作是针对公共市场数据(如行情、成交)的,这类操作需要使用ws.kraken.com端点。
解决方案
- 移除不必要的
subscribe请求,直接在WebSocket连接后发送addOrder事件请求 - 发送请求后直接接收响应,解析响应字段判断订单状态
修正后的代码
import asyncio import websockets import json async def test(): async for ws in websockets.connect("wss://ws-auth.kraken.com/"): try: token = generate_token() # 确保生成符合Kraken要求的授权令牌 volume = 0.0001 req_id = '12345' # 直接构造并发送addOrder请求,无需提前订阅 order_request = { 'event': 'addOrder', 'token': token, 'reqid': req_id, 'ordertype': 'market', 'type': 'buy', 'pair': "XBT/USD", 'volume': volume, 'userref': req_id, 'validate': 1 # 测试用,仅验证不实际下单;正式环境可移除 } await ws.send(json.dumps(order_request)) # 接收并解析订单响应 order_response = await ws.recv() response_data = json.loads(order_response) if response_data.get('event') == 'addOrderStatus': if response_data['status'] == 'success': print(f"订单提交成功,订单ID: {response_data['txid']}") else: print(f"订单提交失败,错误信息: {response_data['errorMessage']}") except websockets.ConnectionClosed: continue except Exception as e: print(f"请求异常: {str(e)}") # 替换为你的令牌生成逻辑(基于Kraken API密钥和签名) def generate_token(): return "your_valid_authentication_token" asyncio.run(test())
响应说明
发送addOrder请求后,会收到addOrderStatus事件响应:
- 成功时:
status为success,包含txid字段(订单ID) - 失败时:
status为error,包含errorMessage字段(错误详情)
内容的提问来源于stack exchange,提问作者PecPeter
相关产品推荐
相关产品推荐

