SkillAgentSearch skills...

Unicorn Binance Websocket Api

A Python SDK to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, com-vanilla-options+testnet, com-portfolio_margin, us, tr) in a simple, fast, flexible, robust and fully-featured way.

Install / Use

npx skills add oliver-zehentleitner/unicorn-binance-websocket-api

Installs into whichever agent you are using.

README

GitHub Release GitHub Downloads PyPi Release PyPi Downloads Conda-Forge Version Conda-Forge Downloads License Supported Python Version Code style: black PyPI - Status codecov CodeQL Unit Tests Build and Publish GH+PyPi Conda-Forge Build Read the Docs Read How To`s Github Telegram Reddit Keep the Why

UBS-Banner

UNICORN Binance WebSocket API

Description | Installation | Documentation | Examples | Change Log | Wiki | Social | Notifications | Bugs | Contributing | Disclaimer

A Python SDK to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, com-vanilla-options+testnet, com-portfolio_margin, us, tr) in a simple, fast, flexible, robust and fully-featured way.

Part of 'UNICORN Binance Suite'.

Receive Data from Binance WebSockets

Create a multiplex websocket connection to Binance with a stream_buffer with just 3 lines of code

from unicorn_binance_websocket_api import BinanceWebSocketApiManager

ubwa = BinanceWebSocketApiManager(exchange="binance.com")
ubwa.create_stream(channels=['trade', 'kline_1m'], markets=['btcusdt', 'bnbbtc', 'ethbtc'])

And 4 more lines to print out the data

while True:
    oldest_data_from_stream_buffer = ubwa.pop_stream_data_from_stream_buffer()
    if oldest_data_from_stream_buffer:
        print(oldest_data_from_stream_buffer)

Or with a callback function just do

from unicorn_binance_websocket_api import BinanceWebSocketApiManager

def process_new_receives(stream_data):
    print(str(stream_data))

ubwa = BinanceWebSocketApiManager(exchange="binance.com")
ubwa.create_stream(channels=['trade', 'kline_1m'], 
                   markets=['btcusdt', 'bnbbtc', 'ethbtc'], 
                   process_stream_data=process_new_receives)

Or with an async callback function just do

from unicorn_binance_websocket_api import BinanceWebSocketApiManager
import asyncio

async def process_new_receives(stream_data):
    print(stream_data)
    await asyncio.sleep(1)

ubwa = BinanceWebSocketApiManager()
ubwa.create_stream(channels=['trade', 'kline_1m'],
                   markets=['btcusdt', 'bnbbtc', 'ethbtc'],
                   process_stream_data_async=process_new_receives)

Or await the stream data in an asyncio coroutine

All the methods of data collection presented have their own advantages and disadvantages. However, this is the generally recommended method for processing data from streams.

from unicorn_binance_websocket_api import BinanceWebSocketApiManager
import asyncio

async def main():
    async def process_asyncio_queue(stream_id=None):
        print(f"Start processing the data from stream '{ubwa.get_stream_label(stream_id)}':")
        while ubwa.is_stop_request(stream_id) is False:
            data = await ubwa.get_stream_data_from_asyncio_queue(stream_id)
            print(data)
            ubwa.asyncio_queue_task_done(stream_id)
    ubwa.create_stream(channels=['trade'],
                       markets=['ethbtc', 'btcusdt'],
                       stream_label="TRADES",
                       process_asyncio_queue=process_asyncio_queue)
    while not ubwa.is_manager_stopping():
        await asyncio.sleep(1)

with BinanceWebSocketApiManager(exchange='binance.com') as ubwa:
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\r\nGracefully stopping ...")
    except Exception as e:
        print(f"\r\nERROR: {e}\r\nGracefully stopping ...")

Basically that's it, but there are more options.

Receive private UserData Streams

Create a private !userData stream to receive account updates like order fills, balance changes and position updates in real time. A valid api_key and api_secret is required.

Set the credentials globally on the manager

All streams created on this manager inherit the credentials:

from unicorn_binance_websocket_api import BinanceWebSocketApiManager

async def process_userdata(stream_data):
    print(stream_data)

ubwa = BinanceWebSocketApiManager(exchange="binance.com",
                                  api_key="YOUR_BINANCE_API_KEY",
                                  api_secret="YOUR_BINANCE_API_SECRET")
ubwa.create_stream(channels='arr',
                   markets='!userData',
                   process_stream_data_async=process_userdata)

Or pass the credentials per stream

Useful when running multiple !userData streams with different API keys on the same manager:

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

ubwa.create_stream(channels='arr',
                   markets='!userData',
                   api_key="API_KEY_ACCOUNT_A",
                   api_secret="API_SECRET_ACCOUNT_A",
                   stream_label="ACCOUNT_A",
                   process_stream_data_async=process_userdata)

ubwa.create_stream(channels='arr',
                   markets='!userData',
                   api_key="API_KEY_ACCOUNT_B",
                   api_secret="API_SECRET_ACCOUNT_B",
                   stream_label="ACCOUNT_B",
                   process_stream_data_async=process_userdata)

Per-stream credentials override the manager defaults. Isolated Margin additionally requires the symbols parameter:

ubwa_im = BinanceWebSocketApiManager(exchange="binance.com-isolated_margin")
ubwa_im.create_stream(channels='arr',
                      markets='!userData',
                      symbols='btcusdt',
                      api_key="YOUR_BINANCE_API_KEY",
                      api_secret="YOUR_BINANCE_API_SECRET",

Related Skills

View on GitHub
GitHub Stars736
CategoryDevelopment
Updated4d ago
Forks169

Languages

Python

Security Score

100/100

Audited on Aug 4, 2026

No findings