Icinga Plugin:


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` to your local system.

How to use the class?

Import the `BinanceWebSocketApiManager` class:

from unicorn_binance_websocket_api.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:

                    filename=os.path.basename(__file__) + '.log',       
                    format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",       

Create an instance of the `BinanceWebSocketApiManager` to connect to Binance (

binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="")

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

binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="")

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

binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="")

For Binance DEX/chain use:

binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="")
binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="")

In this tutorial we continue with streams for (,,

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 = binance_websocket_api_manager.create_stream(channels, markets)

And create a `!userData` stream:

binance_websocket_api_manager.set_private_api_config(binance_api_key, binance_api_secret)
user_data_stream_id = binance_websocket_api_manager.create_stream('arr', '!userData')

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

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

To watch what your streams are doing use:

while True:

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 = binance_websocket_api_manager.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:


View this stream_buffer example!

An other way to process the received data is, to provide an own process_streams class (use this template to start) when creating the manager instance, so instead of writing the raw_stream_data to the stream_buffer, each receive will directly trigger the function/method you provided:

from unicorn_binance_websocket_api_process_streams import BinanceWebSocketApiProcessStreams
binance_websocket_api_manager = BinanceWebSocketApiManager(BinanceWebSocketApiProcessStreams.process_stream_data)

The process_streams template is only printing the data, so you have to extend the file `` and replace “print” with your business logic.

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):

Leave a comment

Your email address will not be published. Required fields are marked *