Skip to main content

Binance Websocket

[NOT RELEASED] This is very much a work in progress, despite being on pypi. Most things might be wrongly documented; API will change

Features

  • Reconnect with incremental backoff
  • Respond to ping
  • request/response factories e.g. add_order_factory make websocket events feel like calling an API
  • ... but provides more info than a simple request/response; for instance, add_order goes through each stage submitted->pending->open or canceled, emitting a notification at each stage

Installing

pip install bittrade-binance-websocket or poetry add bittrade-binance-websocket

General considerations

Observables/Reactivex

The whole library is build with Reactivex.

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 the examples folder or in the Getting Started below

Concurrency

Internally the library uses threads. For your main program you don't have to worry about threads; you can block the main thread.

Getting started

Connect to the public feeds

from bittrade_huobi_websocket import public_websocket_connection, subscribe_ticker
from bittrade_huobi_websocket.operators import keep_messages_only, filter_new_socket_only

# Prepare connection - note, this is a ConnectableObservable, so it will only trigger connection when we call its ``connect`` method
socket_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")

Logging

We 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 the DEBUG level: bittrade_huobi_websocket.raw_socket.sent and bittrade_huobi_websocket.raw_socket.received

To view a full, timestamped history of the socket exchanges use

handler = 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 feeds

Similar to bittrade-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.

See examples/sign.py for 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

*_http methods

REST functions over http should be suffixed with _http e.g. get_book_http. They should return an Observable containing the full json body; this is easily achieved via prepare_request and send_request.

Where possible models should be defined to describe the raw result and parsed result if available/useful.

Reactive operators may be provided for parsing, but they should never be included in the raw functionality of the *_http function, only optional.

Any operator that maps to CCXT types should be suffixed with _ccxt e.g. parse_book_ccxt.

Metadata

Release files for bittrade-binance-websocket 0.4.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bittrade-binance-websocket 0.4.8
File Size Uploaded
bittrade_binance_websocket-0.4.8.tar.gz 33.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bittrade-binance-websocket 0.4.8
File Interpreter ABI Platform
bittrade_binance_websocket-0.4.8-py3-none-any.whl Python 3 none any Details

Total release size: 90.2 kB

Release files / bittrade_binance_websocket-0.4.8.tar.gz

Download URL bittrade_binance_websocket-0.4.8.tar.gz
Size 33.1 kB
Tags Source
SHA-256 checksum
How to use checksums
82835b79cb30f7f5a5be83c98ab666059d7327cd25e7412176d31d294517226f
BLAKE2b-256 checksum
How to use checksums
20c1404c31f897fbe00e9992d18a568141c4dbdb2ff3a3b75e943e6f630493e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.5 Darwin/22.4.0

Release files / bittrade_binance_websocket-0.4.8-py3-none-any.whl

Download URL bittrade_binance_websocket-0.4.8-py3-none-any.whl
Size 57.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cab431ea4b6be78a0ca3e7e515b0c6350c45de904bd0263040de06a7f5c94da
BLAKE2b-256 checksum
How to use checksums
7f50002541f1ed6a9ab2d95148651efdb66856a6cf41ba5eff963596b48a7cfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.5 Darwin/22.4.0

Release history Release notifications | RSS feed

This release

0.4.8 This release

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page