Search code examples
javascriptnode.jsbinancecryptocurrency

Getting average volume for multiple coins within certain interval using sockets or REST


I am trying to get an average volume within certain interval of time using Binance API. For example, I want to have an average volume where time range would be defined like this (pseudo code):

startTime = now() - 24h - 20 min
endTime = now() - 20 min

And I have to do this for 200 trading pairs (eg all USDT related trading pairs).

I know I can make one API call per symbol (https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md#klinecandlestick-data) to get this data using REST, like this:

  async function getKline(marketSymbol, timeInterval, limit) {
    var url = 'https://api.binance.com/api/v3/klines?symbol=' + marketSymbol
                   + '&interval=' + timeInterval
                   + '&startTime' + startTime
                   + '&endTime' + endTime
                   + '&limit=' + limit;
    
    const response = await fetch(url);
    const data = await response.json();
    
    return data;
  }

But cause I have to do this every minute, it would be 200 API calls per minute. And I am already using sockets to fetch candlestick data every two seconds for 200 pairs.

So I guess using REST api/v3/klines is not performant or a way to go here. I mean I know this request has a request weight that equals to 1, and I would be probably fine with restrictions and limits, but, is there some other way to go to get average volume within certain interval for multiple coins like in one single request, or in any way other than making that much API calls every minute?


Solution

  • Since you are already using socket for 200 pairs you don't need any other API call. This is the socket response for klines:

    {
      "e": "kline",     // Event type
      "E": 123456789,   // Event time
      "s": "BTCUSDT",    // Symbol
      "k": {
        "t": 123400000, // Kline start time
        "T": 123460000, // Kline close time
        "s": "BTCUSDT",  // 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
      }
    }
    

    so you have all data that you need. You can save all candlestick from the websocket and calculate the mean of volumes. In this way if you have to calculate volume mean in 24h you need to wait 24h data collection, otherwise you can create a first configuration method that is called for first that cal all 200 symbols historical data with weight calculation. If 150 calls is bigger then the maximum weight you have to wait a minutes for other 50 calls.

    Unfortunatly binance doesn't support multi symbols calls