Skip to content
Advertisement

How to get ALL (or multiple) pair’s historical klines from Binance API in ONE request?

I have a trading bot that trades multiple pairs (30-40). It uses the previous 5m candle for the price input. Therefore, I get 5m history for ALL pairs one by one. Currently, the full cycle takes about 10 minutes, so the 5m candles get updated once in 10m, which is no good.

Any ideas on how to speed things up?

Advertisement

Answer

I think the best option for you will be websocket connection. You cannot recieve kline data once per eg. 5 minutes, but you can recieve every change in candle like you see it in graph. Binance API provide only this, but in compound with websocket connection it will by realy fast, not 10 minutes.

After recieve data you only must to specify when candle was closed, you can do it from timestamps that are in json data (‘t’ and ‘T’). [documentation here]

You must install websockets library.

JavaScript

And here is some sample code how it can work.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement