'Binance futures ACCOUNT_UPDATE stream trade via websocket problem?

I am getting no response from the Binance api when executing the following code, is there anything that I could have missed here?

import json
import websocket


socket='wss://fstream.binance.com/ws'


def on_open(ws):
    print("opened")
    subscribe_message = {
        "method": "SUBSCRIBE",
        "params":
        [
         "ACCOUNT_UPDATE"
         ],
         "id": 1
         }

    ws.send(json.dumps(subscribe_message))

def on_message(ws, message):
    print("received a message")
    print(json.loads(message))     

def on_close(ws):
    print("closed connection")        

ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
ws.run_forever()


Solution 1:[1]

I have found the issue. The thing is that you should request the listen_key from different http servers for the spot and and for futures account. For spot:

def get_listen_key_by_REST(binance_api_key):
  url = ' https://api.binance.com/api/v1/listenKey'
  response = requests.post(url, headers={'X-MBX-APIKEY': binance_api_key})
  json = response.json()
  return json['listenKey']

Than you should subscribe to:

wss://stream.binance.com:9443/stream?streams=<listen_key>/btcbusd@bookTicker/...

For futures :

def get_listen_key_by_REST(binance_api_key):
  url = ' https://fapi.binance.com/fapi/v1/listenKey'
  response = requests.post(url, headers={'X-MBX-APIKEY': binance_api_key})
  json = response.json()
  return json['listenKey']

Than you should subscribe to:

wss://fstream.binance.com/stream?streams=<listen_key>/btcbusd@aggTrade/...

The listen keys that those 2 servers will give you are one and the same, but if you request it from the wrong server, the subscription will not work.

Solution 2:[2]

use this base_url work for me

wss://stream.binancefuture.com/ws/<listen_key>

Solution 3:[3]

I have the same problem. Obviously Ali Karimi code is not going to work because he have not inserted the listen key.

However, I have tried:

socket3=f"wss://fstream-auth.binance.com/ws/L4ToFWpcpnvlGnEZhEbSQi2Gg8oLmQXSvpc2AArMybGW2ZeZkBspYjnQiOSq"

or
socket5=f"wss://stream.binancefuture.com/ws/L4ToFWpcpnvlGnEZhEbSQi2Gg8oLmQXSvpc2AArMybGW2ZeZkBspYjnQiOSq"

but with no effect .... I have tried with and without the subscribe message :

    subscribe_message = {
    "method": "SUBSCRIBE",
    "params":
    [
     "ACCOUNT_UPDATE"
     ],
     "id": 1
     }

again achieving nothing. I really want to see my futures account with the default python lib. If you just want to see your futures account binance lib is working for me:

import asyncio
from binance.client import AsyncClient
from binance.streams import BinanceSocketManager
import api_store
async def main():
  client = await AsyncClient.create(api_store.api_key_binance, 
  api_store.api_secret_binance)
  bm = BinanceSocketManager(client)
  # start any sockets here, i.e a trade socket
  #ts = bm.user_socket()
  ts = bm.futures_user_socket()#-?? ?????????? ????? ??? ?? ???? ???????

  # then start receiving messages
  async with ts as tscm:
      while True:
          res = await tscm.recv()
          print(res)
  await client.close_connection()
if __name__ == "__main__":
  loop = asyncio.get_event_loop()
  loop.run_until_complete(main())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefan2
Solution 2 mehdi salimi
Solution 3