sammchardy / python-binance

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

ThreadedWebsocketManager() no output #1263

Open neversleep007 opened 1 year ago

neversleep007 commented 1 year ago

I cannot get any output from ThreadedWebsocketManager(). I saw others have similar issue, their solution does not work. I use python 3.8, python-binance==1.0.15 and windows 10. Any solution to solve this? thanks in advanced.

image

oliver-zehentleitner commented 1 year ago

Jupyter notebook is using eventloops and i think your loop collidates with it.

You can try it with UBS, it should work with jupyter:

pip install unicorn-binance-suite

from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager

ubwa = BinanceWebSocketApiManager(exchange="binance.com")
ubwa.create_stream(channels='kline_1h', markets='BTCUSDT')

while True:
    klines = ubwa.pop_stream_data_from_stream_buffer()
    if klines:
        print(klines)
BaraoVlask commented 1 year ago

probably this problem is caused by this error

Task exception was never retrieved
future: <Task finished name='Task-7' coro=<ThreadedApiManager.start_listener() done, defined at /home/gbillig/projetos/binance-bot/.venv/lib/python3.10/site-packages/binance/threaded_stream.py:42> exception=KeyError('e')>
Traceback (most recent call last):
  File "/home/gbillig/projetos/binance-bot/.venv/lib/python3.10/site-packages/binance/threaded_stream.py", line 52, in start_listener
    callback(msg)
  File "/home/gbillig/projetos/binance-bot/monitor.py", line 16, in handle_socket_message
    print(f"message type: {msg['e']}")
KeyError: 'e'

caused by format difference between single and combined socket responses

{'stream': 'bnbbtc@miniTicker', 'data': {'e': '24hrMiniTicker', 'E': 1678219474115, 's': 'BNBBTC', 'c': '0.01300400', 'o': '0.01284500', 'h': '0.01301300', 'l': '0.01273400', 'v': '24221.51500000', 'q': '311.66134858'}}
{'stream': 'bnbbtc@bookTicker', 'data': {'u': 2991543193, 's': 'BNBBTC', 'b': '0.01300300', 'B': '19.12900000', 'a': '0.01300500', 'A': '5.73400000'}}
message type: kline
{'e': 'kline', 'E': 1678219474122, 's': 'BNBBTC', 'k': {'t': 1678219440000, 'T': 1678219499999, 's': 'BNBBTC', 'i': '1m', 'f': 217026179, 'L': 217026199, 'o': '0.01299700', 'c': '0.01300400', 'h': '0.01300400', 'l': '0.01299700', 'v': '9.03500000', 'n': 21, 'x': False, 'q': '0.11743798', 'V': '8.85300000', 'Q': '0.11507235', 'B': '0'}}
{'stream': 'bnbbtc@bookTicker', 'data': {'u': 2991543194, 's': 'BNBBTC', 'b': '0.01300400', 'B': '4.92600000', 'a': '0.01300500', 'A': '5.73400000'}}
{'stream': 'bnbbtc@bookTicker', 'data': {'u': 2991543200, 's': 'BNBBTC', 'b': '0.01300400', 'B': '7.42200000', 'a': '0.01300500', 'A': '5.73400000'}}
{'stream': 'bnbbtc@bookTicker', 'data': {'u': 2991543204, 's': 'BNBBTC', 'b': '0.01300400', 'B': '7.42200000', 'a': '0.01300500', 'A': '4.33400000'}}
{'stream': 'bnbbtc@bookTicker', 'data': {'u': 2991543205, 's': 'BNBBTC', 'b': '0.01300400', 'B': '7.42200000', 'a': '0.01300500', 'A': '0.28000000'}}
message type: depthUpdate

try change this

    def handle_socket_message(msg):
        if 'e' in msg:
            print(f"message type: {msg['e']}")
        print(msg)
BaraoVlask commented 1 year ago

by the way, the comments say it's one or the other


    # multiple sockets can be started
    twm.start_depth_socket(callback=handle_socket_message, symbol=symbol)

    # or a multiplex socket can be started like this
    # see Binance docs for stream names