Binance - 通过 WebSockets 获取所选硬币的当前价格
Binance - Get current price of selected coins through WebSockets
Binance 提供具有聚合流、交易流、Kline/Candlestick 流等多种功能的 Web Socket 流,您可以在此处查看 https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md
我想了解我选择的硬币的当前价格和最近 24 小时的百分比变化,但我不明白如何才能获得这些信息。价格必须是实时的,可以每 60 秒或其他时间调用 24 小时百分比变化。
我目前正在使用 CoinCap https://docs.coincap.io/,它非常简单:
- 为了获得 24h %,我调用了端点
https://api.coincap.io/v2/assets?ids=bitcoin,ethereum
- 为了实时获取价格,我调用端点
wss://ws.coincap.io/prices?assets=bitcoin,ethereum
CoinCap 的问题是我无法使用我想要的交易所过滤价格,在本例中是 Binance。所以我一直收到与币安不匹配的价格。
var socket = new WebSocket('wss://ws.coincap.io/prices?assets=bitcoin,ethereum');
socket.addEventListener('message', function (event)
{
// parse & show the data
});
例如,Kline/Candlestick 流表示如下:
The Kline/Candlestick Stream push updates to the current
klines/candlestick every second
和returns以下数据:
{
"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
}
}
据此,与币安平台中看到的价值相匹配的当前价格是多少 https://www.binance.com/en/markets?
您可以使用 miniTicker。 https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-mini-ticker-stream。最后价格在 c
,24 小时前价格在 o
.
Binance 提供具有聚合流、交易流、Kline/Candlestick 流等多种功能的 Web Socket 流,您可以在此处查看 https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md
我想了解我选择的硬币的当前价格和最近 24 小时的百分比变化,但我不明白如何才能获得这些信息。价格必须是实时的,可以每 60 秒或其他时间调用 24 小时百分比变化。
我目前正在使用 CoinCap https://docs.coincap.io/,它非常简单:
- 为了获得 24h %,我调用了端点
https://api.coincap.io/v2/assets?ids=bitcoin,ethereum
- 为了实时获取价格,我调用端点
wss://ws.coincap.io/prices?assets=bitcoin,ethereum
CoinCap 的问题是我无法使用我想要的交易所过滤价格,在本例中是 Binance。所以我一直收到与币安不匹配的价格。
var socket = new WebSocket('wss://ws.coincap.io/prices?assets=bitcoin,ethereum');
socket.addEventListener('message', function (event)
{
// parse & show the data
});
例如,Kline/Candlestick 流表示如下:
The Kline/Candlestick Stream push updates to the current klines/candlestick every second
和returns以下数据:
{
"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
}
}
据此,与币安平台中看到的价值相匹配的当前价格是多少 https://www.binance.com/en/markets?
您可以使用 miniTicker。 https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-mini-ticker-stream。最后价格在 c
,24 小时前价格在 o
.