sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.91k stars 2.19k forks source link

How to use user-data-stream API ? It is not work now. #570

Open tjddnjs124 opened 3 years ago

tjddnjs124 commented 3 years ago

python-binance/binance/websockets.py /

def start_user_socket(self, callback):

Get the user listen key

    user_listen_key = self._client.stream_get_listen_key()
    # and start the socket with this specific key
    return self._start_account_socket('user', user_listen_key, callback)

[My code]

import sys import time from binance.client import Client # Import the Binance Client from binance.websockets import BinanceSocketManager # Import the Binance Socket Manager

li_test1 = [] li_error = []

client = Client(api_key="secret", api_secret = "secret") bm = BinanceSocketManager(client)

def handle_message(msg): print(msg)

conn_key = bm.start_user_socket(handle_message) bm.start()

time.sleep(2) bm.stop_socket(conn_key)


" conn_key = bm.start_user_socket(handle_message) bm.start() "

1) The basic code is not work. --> how? how?

and

i think that the websocket's code is not offer futures-user-data-stream api.

2) How can i take the data that futures-user-data-stream in this codes ?

thanks, teacher.

nikky78 commented 3 years ago

I get the same issue. Nothing is being called back with asyncio extension. Any idea?

oliver-zehentleitner commented 3 years ago

Try it with https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api

jastom1 commented 3 years ago

you can override the function, put this below the declaration:

from binance.client import Client from binance.websockets import * from twisted.internet import reactor import threading

class my_BinanceSocketManager(BinanceSocketManager): def start_user_socket(self, callback): """Start a websocket for user data """

Get the user listen key

    user_listen_key = self._client.stream_get_listen_key()
    # and start the socket with this specific key
    return self._start_account_socket('userData', user_listen_key, callback)
jastom1 commented 3 years ago

and change line: bm = BinanceSocketManager(client) to: bm = my_BinanceSocketManager(client)

ANWB2020 commented 3 years ago

@jastom1

What is the difference to already available "def start_user_socket(self, callback):"?

Please post a very simple example of usage of user_socket.

IDepla commented 3 years ago

import asyncio from binance import AsyncClient, BinanceSocketManager

async def main(): client = await AsyncClient.create( "key", "secret", tld="com", # change to us if you connect to binance.us testnet=True #comment when you don't need anymore ) bm = BinanceSocketManager(client)

start any sockets here, i.e a trade socket

ts = bm.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())
QGB commented 1 year ago

AttributeError: 'BinanceSocketManager' object has no attribute 'start_user_socket'

QGB commented 1 year ago

print(res)

TypeError                                 Traceback (most recent call last)<ipython-input-46-653fd5f88a07> in <module>----> 1 async with ts as tscm:      2     res = await tscm.recv()      3     print(res)      4       5 ~/anaconda3/lib/python3.9/site-packages/binance/streams.py in __aenter__(self)     64      65     async def __aenter__(self):---> 66         await self.connect()     67         return self     68 ~/anaconda3/lib/python3.9/site-packages/binance/streams.py in connect(self)     81      82     async def connect(self):---> 83         await self._before_connect()     84         assert self._path     85         ws_url = self._url + self._prefix + self._path~/anaconda3/lib/python3.9/site-packages/binance/streams.py in _before_connect(self)    244     async def _before_connect(self):
    245         if not self._path:
--> 246             self._path = await self._get_listen_key()
    247 
    248     async def _after_connect(self):

~/anaconda3/lib/python3.9/site-packages/binance/streams.py in _get_listen_key(self)
    257     async def _get_listen_key(self):
    258         if self._keepalive_type == 'user':
--> 259             listen_key = await self._client.stream_get_listen_key()
    260         elif self._keepalive_type == 'margin':  # cross-margin
    261             listen_key = await self._client.margin_stream_get_listen_key()

TypeError: object str can't be used in 'await' expression