Package: bittrade-binance-websocket
Binance Websocket[NOT RELEASED] This is very much a work in progress, despite being on pypi.
Most things might be wrongly documented; APIwillchangeFeaturesReconnect with incremental backoffRespond to pingrequest/response factories e.g.add_order_factorymake websocket events feel like calling an API... but provides more info than a simple request/response;
for instance,add_ordergoes through each stage submitted->pending->open or canceled,
emitting a notification at each stageInstallingpip install bittrade-binance-websocketorpoetry add bittrade-binance-websocketGeneral considerationsObservables/ReactivexThe whole library is build withReactivex.Though Observables seem complicated at first, they are the best way to handle - and (synchronously) test - complex situations that arise over time, like an invalid sequence of messages or socket disconnection and backoff reconnects.For simple use cases, they are also rather easy to use as shown in theexamplesfolder or in the Getting Started belowConcurrencyInternally the library uses threads.
For your main program you don't have to worry about threads; you can block the main thread.Getting startedConnect to the public feedsfrombittrade_huobi_websocketimportpublic_websocket_connection,subscribe_tickerfrombittrade_huobi_websocket.operatorsimportkeep_messages_only,filter_new_socket_only# Prepare connection - note, this is a ConnectableObservable, so it will only trigger connection when we call its ``connect`` methodsocket_connection=public_websocket_connection()# Prepare a feed with only "real" messages, dropping things like status update, heartbeat, etc…messages=socket_connection.pipe(keep_messages_only(),)socket_connection.pipe(filter_new_socket_only(),subscribe_ticker('USDT/USD',messages)).subscribe(print,print,print# you can do anything with the messages; here we simply print them out)socket_connection.connect()(This script is complete, it should run "as is")LoggingWe use Python's standard logging.
You can modify what logs you see as follows:logging.getLogger('bittrade_huobi_websocket').addHandler(logging.StreamHandler())In addition, two special logger logs every message sent/received from the socket (except heartbeat) at theDEBUGlevel:bittrade_huobi_websocket.raw_socket.sentandbittrade_huobi_websocket.raw_socket.receivedTo view a full, timestamped history of the socket exchanges usehandler = FileHandler("logs/raw_socket.log")
handler.setLevel(DEBUG)
logger = logging.getLogger("bittrade_huobi_websocket.raw_socket.sent")
formatter = logging.Formatter("%(asctime)s.%(msecs)03d <== %(message)s", datefmt="%H:%M:%S")
handler.setFormatter(formatter)
logger.addHandler(handler)
handler = FileHandler("logs/raw_socket.log")
handler.setLevel(DEBUG)
logger = logging.getLogger("bittrade_huobi_websocket.raw_socket.received")
formatter = logging.Formatter("%(asctime)s.%(msecs)03d --> %(message)s", datefmt="%H:%M:%S")
handler.setFormatter(formatter)
logger.addHandler(handler)Private feedsSimilar tobittrade-kraken-rest, this library attempts to get as little access to sensitive information as possible.This means that you'll need to implement the signature token yourself. The library never has access to your API secret.Seeexamples/sign.pyfor an example of implementation but it is generally as simple as:authenticated_sockets=connection.pipe(filter_new_socket_only(),operators.map(add_token),operators.share(),)Development guidelines*_httpmethodsREST functions over http should be suffixed with_httpe.g.get_book_http.
They should return an Observable containing thefulljson body; this is easily achieved viaprepare_requestandsend_request.Where possible models should be defined to describe therawresult andparsed resultif available/useful.Reactive operators may be provided for parsing, but they should never be included in therawfunctionality of the*_httpfunction, only optional.Any operator that maps to CCXT types should be suffixed with_ccxte.g.parse_book_ccxt.