Failed to get the CM Future account position via websocket

I was following Binance API Documentation to get the positions of CM future account via websocket. However, I got an error saying the <listenKey>@position is an invalid string. Could anyone help me with it?

This is my code:

cm_future_client = CMFutures(
        key=<MyKey>,
        secret=<MySecret>,
    )
    key = cm_future_client.new_listen_key()['listenKey']
    client = CMFuturesWebsocketClient(on_message=on_message)
    client.user_data(f"{key}@position")

This is the error message:

{"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":1697786807841}

你用的是哪个库?
如果是 https://github.com/binance/binance-futures-connector-python, 可以直接
client.subscribe(f"{key}.position")

1 Like

user_data() 这个方法是用来接受 user data, 比如订单信息,账户信息, 只能传listen key

我使用你说的这个库的, 我用了subscribe, 但还是没用, 这是我的代码:

from binance.cm_futures import CMFutures
from binance.websocket.cm_futures.websocket_client import CMFuturesWebsocketClient

cm_future_client = CMFutures(
        key="mykey",
        secret="mysecret",
    )
    key = cm_future_client.new_listen_key()['listenKey']
    client = CMFuturesWebsocketClient(on_message=on_message)
    client.subscribe(f"{key}@position")
    # {key}.position 也不行 
    # client.subscribe(f"{key}.position")

错误代码是

{"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":1698119535725}

请求用户数据 并使用 send 方法 发送请求内容

from binance.websocket.um_futures.websocket_client import UMFuturesWebsocketClient
from binance.um_futures import UMFutures
API_KEY = 'xx'
API_SECRET = 'xx'

uClient = UMFutures(key=API_KEY, secret=API_SECRET)
def message_handler(_, message):
    print(json.loads(message)

listenKey = uClient.new_listen_key()['listenKey']
ws_client = UMFuturesWebsocketClient(on_message=message_handler)
ws_client.send({'method': 'REQUEST', 'params': [f'{listenKey}@position'], 'id': 1}, )
time.sleep(3)
ws_client.send({'method': 'REQUEST', 'params': [f'{listenKey}@position'], 'id': 2},)
ws_client.stop()
sys.exit()