Binance - Get current price of selected coins through WebSockets

Viewed 20044

Binance offers Web Socket Streams with several functions such as Aggregate Streams, Trade Streams, Kline/Candlestick Streams, etc. that you can see here https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md

I'm looking to get the current price & last 24h % change of my selected coins and I can't understand how do I manage to get this information. The prices must be in real time, the 24h % change can be called every 60 seconds or something.

I'm currently using CoinCap https://docs.coincap.io/ and it's pretty easy:

  1. To get the 24h % I call the endpoint https://api.coincap.io/v2/assets?ids=bitcoin,ethereum
  2. To get the prices in real time I call the endpoint wss://ws.coincap.io/prices?assets=bitcoin,ethereum

The problem with CoinCap is that I can't filter the prices with the exchange that I want, which in this case is Binance. So I keep getting prices that do not match the Binance.

var socket = new WebSocket('wss://ws.coincap.io/prices?assets=bitcoin,ethereum');
socket.addEventListener('message', function (event)
{
  // parse & show the data
});

For example, the Kline/Candlestick Streams says the following:

The Kline/Candlestick Stream push updates to the current klines/candlestick every second

And returns the following data:

{
  "e": "kline",     // Event type
  "E": 123456789,   // Event time
  "s": "BNBBTC",    // Symbol
  "k": {
    "t": 123400000, // Kline start time
    "T": 123460000, // Kline close time
    "s": "BNBBTC",  // Symbol
    "i": "1m",      // Interval
    "f": 100,       // First trade ID
    "L": 200,       // Last trade ID
    "o": "0.0010",  // Open price
    "c": "0.0020",  // Close price
    "h": "0.0025",  // High price
    "l": "0.0015",  // Low price
    "v": "1000",    // Base asset volume
    "n": 100,       // Number of trades
    "x": false,     // Is this kline closed?
    "q": "1.0000",  // Quote asset volume
    "V": "500",     // Taker buy base asset volume
    "Q": "0.500",   // Taker buy quote asset volume
    "B": "123456"   // Ignore
  }
}

Based on this what is the current price that matches the value seen in the Binance Platform https://www.binance.com/en/markets?

1 Answers
Related