最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Binance - Get current price of selected coins through WebSockets - Stack Overflow

programmeradmin3浏览0评论

Binance offers Web Socket Streams with several functions such as Aggregate Streams, Trade Streams, Kline/Candlestick Streams, etc. that you can see here .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 / and it's pretty easy:

  1. To get the 24h % I call the endpoint ,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 ?

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./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./en/markets?

Share Improve this question asked Feb 13, 2021 at 16:16 LinesofcodeLinesofcode 5,90314 gold badges70 silver badges130 bronze badges 1
  • BTW this github github./binance/binance-spot-api-docs updates irregularly. Better to use this doc page binance-docs.github.io/apidocs/spot/en/#introduction – Mike Malyi Commented Feb 13, 2021 at 16:28
Add a ment  | 

2 Answers 2

Reset to default 9

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.

Don't forget using <symbol>@ticker. The 24h % is already included.

{
  "e": "24hrTicker",  // Event type
  "E": 1672515782136, // Event time
  "s": "BNBBTC",      // Symbol
  "p": "0.0015",      // Price change
  "P": "250.00",      // Price change percent <--- Here
  "w": "0.0018",      // Weighted average price
  "x": "0.0009",      // First trade(F)-1 price (first trade before the 24hr rolling window)
  "c": "0.0025",      // Last price
  "Q": "10",          // Last quantity
  "b": "0.0024",      // Best bid price
  "B": "10",          // Best bid quantity
  "a": "0.0026",      // Best ask price
  "A": "100",         // Best ask quantity
  "o": "0.0010",      // Open price
  "h": "0.0025",      // High price
  "l": "0.0010",      // Low price
  "v": "10000",       // Total traded base asset volume
  "q": "18",          // Total traded quote asset volume
  "O": 0,             // Statistics open time
  "C": 86400000,      // Statistics close time
  "F": 0,             // First trade ID
  "L": 18150,         // Last trade Id
  "n": 18151          // Total number of trades
}

Source: websocket stream

发布评论

评论列表(0)

  1. 暂无评论