binance-exchange/node-binance-api

Way to get the current price / volume for each coin?

napindc opened this issue · 9 comments

I want to be able to get the current price & volume for each coin, once per minute. I'm using the binance.prices(function(ticker) { to get back the current prices. I can then feed that into something like binance.websockets.candlesticks(['IOTABTC'], "1d", function(candlesticks) {, and I'll get back all of the information I need.

But is there a way for me to disconnect from that websocket after that? In my code I've already stored the price to a database, and a minute later I'll call the same endpoint to repeat the process. But I don't want to kick off a new websocket every minute - I just want to close one after I get the info. from it.

It would be better if there was a non-websocket API to get the price/volume, but I don't see it.

Thank you for your suggestion! I agree we would like 24 hour price statistics for all symbols, so we can easily get daily volume, etc. There is currently no easy way of doing this.

As for closing a websocket connection, yes you can, see: jaggedsoft#13
But a better way of doing what you want (once per minute) would be using the non-websocket chart call:

binance.candlesticks("BNBBTC", "1d", function(ticks, symbol) {
	//console.log("candlesticks()", ticks);
	let last_tick = ticks[ticks.length - 1];
	let [time, open, high, low, close, volume, closeTime, assetVolume, trades, buyBaseVolume, buyAssetVolume, ignored] = last_tick;
	//console.log(last_tick);
	console.log(symbol+" volume: "+volume);
	console.log(symbol+" last price: "+close);
});

BNBBTC volume: 388464.00000000
BNBBTC last price: 0.00023135

I have been asked this question before. I sent it to the Binance CTO. they have been very busy improving their platform
image

image

"But a better way of doing what you want (once per minute) would be using the non-websocket chart call"

I would have to do this one per minute for all ~175 trading pairs though. I'm pretty sure my server would be slowed down by Binance or banned from using their API if I did that.

Perhaps the Binance team could update https://api.binance.com/api/v1/ticker/allPrices to include the 24h volume, in addition to the last price, so it would return:

{ "symbol": "ETHBTC", "price": "0.03796000", "24hVolume" : "123123" },

OR: https://api.binance.com/api/v1/ticker/24hr seems to show both price and volume as well. But for whatever reason - it does not show the coin. If the team could add the coin name to that API, it would solve this.

Yes. https://api.binance.com/api/v1/ticker/24hr appears to be what we want. It is just missing symbol information. I will update you when I know more.

Get 24 hour statistics for all symbols

binance.prevDay(false, function(response) {
	console.log(response);
});

In the newest version on the development branch, you can get this data by passing false as the symbol
https://github.com/jaggedsoft/node-binance-api/

Now available on latest version

Get 24h Price Change Statistics via WebSocket

// For all symbols:
binance.websockets.prevDay(false, function(response) {
  console.log(response);
});

// For a specific symbol:
binance.websockets.prevDay('BNBBTC', function(response) {
  console.log(response);
});
View Response
{ eventType: '24hrTicker',
  eventTime: 1512629577435,
  symbol: 'BNBBTC',
  priceChange: '-0.00002671',
  percentChange: '-12.844',
  averagePrice: '0.00019282',
  prevClose: '0.00020796',
  close: '0.00018125',
  closeQty: '55.00000000',
  bestBid: '0.00018038',
  bestBidQty: '580.00000000',
  bestAsk: '0.00018125',
  bestAskQty: '144.00000000',
  open: '0.00020796',
  high: '0.00021300',
  low: '0.00017555',
  volume: '3731915.00000000',
  quoteVolume: '719.59011818',
  openTime: 1512543177433,
  closeTime: 1512629577433,
  firstTradeId: 2248079,
  lastTradeId: 2284725,
  numTrades: 36647 }

is there a way to get this via non web socket, such as with https://api.binance.com/api/v1/ticker/24hr?

WebSocket is the preferred way to do it, as this does not cost any API requests and you always have the latest data.
Also the only way to do it until the other page is fixed. But, they are aware of the issue and will fix it soon

https://api.binance.com/api/v1/ticker/24hr
Now returning all symbols

binance.prevDay(false, function(response) {
	console.log(response);
});