Binance Socket — Stream data without API

Matjaž Hozjan
2 min readSep 16, 2021

--

Let’s make socket to Binance!

Before using sockets my way to go where API’s. They are fine and useful for getting history data or making trades. For on the fly data sockets are much faster with almost no limitations on the brokers side.

I made example code on 1m kline chart data. as result is as:

{'stream': 'btcusdt@kline_1m', 'data': {'e': 'kline', 'E': 1631789078466, 's': 'BTCUSDT', 'k': {'t': 1631789040000, 'T': 1631789099999, 's': 'BTCUSDT', 'i': '1m', 'f': 1056251946, 'L': 1056252405, 'o': '47860.71000000', 'c': '47880.05000000', 'h': '47892.91000000', 'l': '47857.75000000', 'v': '13.96884000', 'n': 460, 'x': False, 'q': '668701.59914730', 'V': '9.60299000', 'Q': '459733.48923420', 'B': '0'}}}
import json
from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager

# channels to record
channels = ['kline_1m']
exchangeName = "binance"
# Pairs you wish to monitor
market = ["ETHUSDT", "BTCUSDT"]

For more Websocket market streams option, please visit the official Binance web page. Choose the right stream for you and place it to “channels =”

At “market = ” place symbols that you are wish to monitor. Suggeston is to not place them all at one socket stream, unless you have some crazy hardware setup. It will generate lag and errors in no time, if there is to many markets to monitor. Just divide tickers based on some segregation and run multiple times preferable from different hardware to get everything down. My list has like 100 pairs and is maintainable just fine. Those pairs are stored in DB that why am calling them before socket stream open. That list in DB is manageable based on other parameters so that I have an open socket just for those pairs that are in my focus just now.

Connect to Binance with Unicorn:

# Connection class for Binance
class login:
def socket(self, exchangeName, channels, market):
binance_websocket_api_manager = BinanceWebSocketApiManager(exchange=exchangeName + ".com")
multi_stream_id = binance_websocket_api_manager.create_stream(channels, market)
return binance_websocket_api_manager

binance_websocket_api_manager = login().socket(exchangeName, channels, market)

Is it possible to complicate connection to web socket and make a lot of code to avoid errors or you use it as I did and just print those errors (us traceback or something), make investigations and if they are dealbreaker for your code make some edit.

while True:
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer:
sckt = json.loads(oldest_stream_data_from_stream_buffer)
try:
print(sckt)
#save here to DB or use data as you wish
pass

except:
pass

When I was recording order book for each second to make some ML predictions based on it. Than DB was filling up in Tb’s by hours so I changed that to some other logic and reduced data a lot. Am saying all these to warn you, that socket can produce a lot of data that you maybe don’t need so just focus on those that you want and make best from it. For everything that you missed just call it via API and download it when you need it.

More at TokensQuant.com

--

--

Matjaž Hozjan
Matjaž Hozjan

Written by Matjaž Hozjan

FX, stock trader and full time DataScientist at SportRadar AG

No responses yet