LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
678 stars 166 forks source link

$all@allTickers not working on binance.com but working on binance.org #178

Closed salian closed 3 years ago

salian commented 3 years ago

When I use BinanceWebSocketApiManager with binance.com exchange and $all@allTickers I get HTTP error 400 Bad Request.

But the same code works fine with binance.org and returns data on websocket.

def run_main_loop(api_key, api_secret):
    binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com")
    binance_websocket_api_manager.create_stream('allTickers', ['$all'])

    while True:
        oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
        if oldest_stream_data_from_stream_buffer:
            print(oldest_stream_data_from_stream_buffer)

The debug log:

BinanceWebSocketApiManager.create_stream(['allTickers'], ['$all'], None, False, False) with stream_id=d39315f7-xxxx-yyyy-86cb-5327cf753148
BinanceWebSocketApiManager._add_stream_to_stream_list(d39315f7-xxxx-yyyy-86cb-5327cf753148, ['allTickers'], ['$all'], None, False, False)
Using selector: KqueueSelector
BinanceWebSocketApiManager.is_stop_request(d39315f7-xxxx-yyyy-86cb-5327cf753148)
BinanceWebSocketApiSocket.start_socket(d39315f7-xxxx-yyyy-86cb-5327cf753148, ['allTickers'], ['$all']) socket_id=399122d6-0000-0000-8590-e2c5f72491dc recent_socket_id=399122d6-0000-0000-8590-e2c5f72491dc
BinanceWebSocketApiManager.is_stop_request(d39315f7-xxxx-yyyy-86cb-5327cf753148)
client - state = CONNECTING
client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x7f9cxxxx60a0>)
client > GET /ws/$all@allTickers HTTP/1.1
client > Headers([('Host', 'stream.binance.com:9443'), ('Upgrade', 'websocket'), ('Connection', 'Upgrade'), ('Sec-WebSocket-Key', 'vT+iYbOxxxxxxxxxozUyRQ=='), ('Sec-WebSocket-Version', '13'), ('Sec-WebSocket-Extensions', 'permessage-deflate; client_max_window_bits'), ('User-Agent', 'unicorn-binance-websocket-api_1.30.0-python_3.8.2')])
client - event = data_received(<173 bytes>)
client < HTTP/1.1 400 Bad Request
client < Headers([('Date', 'Fri, 18 Jun 2021 10:28:27 GMT'), ('Content-Type', 'text/plain'), ('Content-Length', '38'), ('Connection', 'keep-alive')])
client ! failing CONNECTING WebSocket connection with code 1006
client x closing TCP connection
salian commented 3 years ago

Indeed it was my misunderstanding that all Binance endpoints are reasonably alike. $all@allTickers is not available on binance.com exchange. It's only available for dex (binance.org) exchange.

As per https://binance-docs.github.io/apidocs/spot/en/#all-market-tickers-stream the alternative equivalent for websocket stream with every-second updates on 24hr rolling window ticker statistics for all symbols is !ticker@arr

For the sake of anyone who finds this thread, a working example for all tickers would be

from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager

def run_main_loop():
    binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com")
    binance_websocket_api_manager.create_stream('arr', '!ticker')

    while True:
        oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
        if oldest_stream_data_from_stream_buffer:
            print(oldest_stream_data_from_stream_buffer)

if __name__ == "__main__":
    run_main_loop()