Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binance - Get current price of selected coins through WebSockets

Tags:

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?

like image 997
Linesofcode Avatar asked Feb 13 '21 16:02

Linesofcode


People also ask

What is Websocket Binance?

Messages are received as dictionary objects relating to the message formats defined in the Binance WebSocket API documentation. Websockets are setup to reconnect with a maximum of 5 retries with an exponential backoff strategy.

Is Binance API data free?

Are there examples of a free Binance API? Yes, CoinRanking and CoinGecko are both free Binance APIs.

Does Binance provide API?

Binance APIs. Unlimited Opportunities with One Key. We offer access to Spot, Margin, Futures and Options API trading for over 300 Digital and Fiat currencies. API trading provides a testing environment, API documentation, and Sample Code in 6 languages.

How do I get live data from Binance?

We will use python and websocket client to connect to Binance Websocket streams. The same applies for other crypto exchanges, just change the endpoint and the message payload values. Then we will get the websocket endpoint from the docs and we will retrieve data for the executed trades of a certain symbol.


1 Answers

You can use miniTicker. https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-mini-ticker-stream. The last price would be in c, 24h ago the price would be in o.

like image 196
Mike Malyi Avatar answered Nov 12 '22 00:11

Mike Malyi