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

BinanceSocketManager fails to work properly with the test network #597

Open naquad opened 3 years ago

naquad commented 3 years ago

The following code is made for python-binance version 0.7.5:

#!/usr/bin/env python3

from pprint import pprint
from binance.client import Client
from binance.websockets import BinanceSocketManager
import logging

logging.basicConfig(level=logging.DEBUG)

WS_URL = "wss://testnet.binance.vision/"
REST_URL = "https://testnet.binance.{}/api"

def event_printer(msg):
    print('<<< socket event >>>', end=' ')
    pprint(msg)

Client.API_URL = REST_URL
BinanceSocketManager.STREAM_URL = WS_URL
client = Client(api_key='', api_secret='', tld='vision')

sockets = BinanceSocketManager(client)
sockets.start_depth_socket('BTCUSDT', event_printer, 5)
sockets.start()
sockets.join()

It behaves like it can't connect, but the same time using other tools to connect to the same URLs work as expected (wscat displays incoming depth events).

oliver-zehentleitner commented 3 years ago

python3 -m pip install unicorn-binance-websocket-api

#!/usr/bin/env python3

from pprint import pprint
from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager
import logging
import time

logging.basicConfig(level=logging.INFO)

def event_printer(msg):
    print('<<< socket event >>>', end='\r\n')
    pprint(msg)

ubwa = BinanceWebSocketApiManager(exchange="binance.com-testnet")
ubwa.create_stream("depth", "BTCUSDT", output="dict")

while True:
    received_data_record = ubwa.pop_stream_data_from_stream_buffer()
    if received_data_record:
        event_printer(received_data_record)
    else:
        time.sleep(0.01)

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

badjano commented 3 years ago

I don´t get why everyone knows the websockets in this repo are broken but does nothing about it... sigh, I guess I should stop complaining and do something myself

kimchirichie commented 3 years ago

I would gladly merge in a fix for websocket. Otherwise, please use oliver-zehentleitner/unicorn-binance-websocket-api for websocket

badjano commented 3 years ago

Would it be acceptable to add unicorn-binance-websocket-api as a dependency for this project? You guys seem to like it a lot

kimchirichie commented 3 years ago

its not a dependency. websocket interface for this library is old, and unmaintained. unicorn is actively maintained.

kimchirichie commented 3 years ago

in fact @oliver-zehentleitner is the maintainer of that library. lol

this repository is fully community driven and i only merge things in here and there. i would recommend unicorn lib for websockets.

badjano commented 3 years ago

I meant adding the unicorn library into python-binance as a dependency for the websocket part

oliver-zehentleitner commented 3 years ago

I don't think that would be practical. Users already using the WS implementation of python-binance would not be able to update python-binance to access updates to the REST methods.

I rather play with the idea to fork python-binance and create a pure REST package. This would have the additional advantage that you could drop the support for <Python_3.5 and remove e.g. Twisted.

badjano commented 3 years ago

I wouldn´t change the architecture for the ws, any code using the broken classes are going to still work, but with unicorn under the hood... is this still impractical?

oliver-zehentleitner commented 3 years ago

maybe then not, but ubwa would need an update to support python-binance structure of callback functions for each stream....

kimchirichie commented 3 years ago

@badjano i dont think thats necessary. i recommend that if you need ws, add ubwa to your project as an additional dependency

taitae25 commented 3 years ago

python3 -m pip install unicorn-binance-websocket-api

#!/usr/bin/env python3

from pprint import pprint
from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager
import logging
import time

logging.basicConfig(level=logging.INFO)

def event_printer(msg):
    print('<<< socket event >>>', end='\r\n')
    pprint(msg)

ubwa = BinanceWebSocketApiManager(exchange="binance.com-testnet")
ubwa.create_stream("depth", "BTCUSDT", output="dict")

while True:
    received_data_record = ubwa.pop_stream_data_from_stream_buffer()
    if received_data_record:
        event_printer(received_data_record)
    else:
        time.sleep(0.01)

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

I tried the code but I get the following message once it enters the loop:

<<< socket event >>> {'id': 1, 'result': None}

Does this mean that there may be something wrong with the actual Testnet ?

Thanks.

oliver-zehentleitner commented 3 years ago

Thats perfect :)

{'id': 1, 'result': None} is the confirmation that Binance accepted the subscriptions: https://binance-docs.github.io/apidocs/spot/en/#live-subscribing-unsubscribing-to-streams

taitae25 commented 3 years ago

Thanks for the quick response!