Documentation: https://unicorn-binance-websocket-api.docs.lucit.tech/
Github: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api
PyPI: https://pypi.org/project/unicorn-binance-websocket-api/
UnicornFy: https://github.com/LUCIT-Systems-and-Development/unicorn-fy
Icinga Plugin: https://github.com/LUCIT-Systems-and-Development/check_lucit_collector.py

Installation

You can install the library with `pip install unicorn-binance-websocket-api` from PyPI or you download the latest release or the dev-stage. The easiest way to test the examples is to clone the repository from git with `git clone git@github.com:LUCIT-Systems-and-Development/unicorn-binance-websocket-api.git` to your local system.

How to use the class?

Import the `BinanceWebSocketApiManager` class:

from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager

Additionally we need more imports for this example:

import logging
import time
import os

Configure logging, to get a very verbose mode use DEBUG instead of INFO:

logging.getLogger("unicorn_binance_websocket_api")
logging.basicConfig(level=logging.DEBUG,
                    filename=os.path.basename(__file__) + '.log',
                    format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",
                    style="{")

Create an instance of the `BinanceWebSocketApiManager` to connect to Binance (binance.com):

ubwa = BinanceWebSocketApiManager(exchange="binance.com")

If you would like to use the Binance Jersey API then use:

ubwa = BinanceWebSocketApiManager(exchange="binance.je")

If you would like to use the Binance US API then use:

ubwa = BinanceWebSocketApiManager(exchange="binance.us")

For Binance DEX/chain use:

ubwa = BinanceWebSocketApiManager(exchange="binance.org")
ubwa = BinanceWebSocketApiManager(exchange="binance.org-testnet")

In this tutorial we continue with streams for binance.com (example_binance_dex.py, example_binance_jersey.py, example_binance_us.py):

Now define the channels and markets you wish to stream. In this example we define a multiplex stream with 3 channels and 5 markets, resulting in 15 different streams (in one multistream – only one connection) Unfortunately you can not receive !userData with the other streams combined and must be created as a single websocket stream. Be also aware, that Binance allows up to 1024 subscriptions in one stream, more subscriptions will lead to a crashed stream.

channels = {'trade', 'kline_5m', 'depth5'}
markets = {'bnbbtc', 'ethbtc', 'btcusdt', 'bchabcusdt', 'eosusdt'}

Then create and start the multiplex stream:

multi_stream_id = ubwa.create_stream(channels, markets)

And create a `!userData` stream:

user_data_stream_id = ubwa.create_stream('arr', '!userData', api_key="aaa", api_secret="bbb")

And a `!ticker` and `!miniTicker` stream:

ticker_stream_id = ubwa.create_stream(["arr"], ["!miniTicker"])
miniticker_stream_id = ubwa.create_stream(["arr"], ["!ticker"])

To watch what your streams are doing use:

while True:
    ubwa.print_summary()
    time.sleep(1)
print_summary() Image

Now your streams should be running, and everything happening with the received data is, its stored in the stream_buffer. To get and remove the oldest entry from the stream_buffer stack do:

oldest_stream_data_from_stream_buffer = ubwa.pop_stream_data_from_stream_buffer()

Then you can do with it what every you want. If your database is down for a restart or anything else, you can kick the data back to the stream_buffer with:

ubwa.add_to_stream_buffer(oldest_stream_data_from_stream_buffer)

View this stream_buffer example!

If you would like to unify the received data with UnicornFy, please follow this guide.

Here are some working example files (just clone the repository and run them):
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_binance_dex.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_binance_jersey.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_binance_us.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_binance_futures.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_bookticker.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_demo.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_monitoring.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_multi_stream.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_stream_management.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_stream_management_extended.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_stream_buffer.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_ticker_and_miniticker.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_trade_stream.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_userdata_stream.py
https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_version_of_this_package.py