jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.58k stars 767 forks source link

Websockets depth cache not working for a lot of symbols #307

Closed MauriPastorini closed 5 years ago

MauriPastorini commented 5 years ago

Title

code

binance.websockets.depthCache(
      symbols,
      (symbol, depth) => {
        console.log("Depth cache: ", depth);
        let bids = binance.sortBids(depth.bids);
        let asks = binance.sortAsks(depth.asks);

Symbols is the array with all the symbols ['ETHBTC',.....] Result

node-binance-api\node-binance-api.js:1771
                            if (err) throw err;
                                     ^

Error: ETIMEDOUT
    at Timeout._onTimeout (G:\....\node_modules\request\request.js:849:19)
    at ontimeout (timers.js:475:11)
    at tryOnTimeout (timers.js:310:5)
    at Timer.listOnTimeout (timers.js:270:5)

thank you

jaggedsoft commented 5 years ago

You are likely doing calls other than depthCache to cause this. The ETIMEDOUT error typically triggers when you send too many requests to Binance in a short amount of time and get temporarily banned by their loadbalancer for a short time. If you are using the depth function then it downloads a complete snapshot of the order book, so if that is the case you will need to delay your calls to depth by 350ms each.

MauriPastorini commented 5 years ago

Hi jaggedsoft, thank you for your response. But I am not doing too many request, I am running only the script below and throws me that error. Also, I tried opening websockets manually for all the symbols as I tried with this library, and I could connect successfully, but again, not with the library

Here is the code:

const binance = require("node-binance-api")();

console.log("Starting");
binance.exchangeInfo(function(error, data) {
  if (error) {
    console.error("Error getting exchange information of Binance: ", error);
    return;
  }
  let symbols = [];
  for (let obj of data.symbols) {
    symbols.push(obj.symbol);
  }
  binance.websockets.depthCache(
    symbols,
    (symbol, depth) => {
      console.log("Depth cache: ", depth);
    },
    5
  );
});

Error:

node-binance-api\node-binance-api.js:1771
                            if (err) throw err;
                                     ^

Error: ETIMEDOUT
    at Timeout._onTimeout (G:\....\node_modules\request\request.js:849:19)
    at ontimeout (timers.js:475:11)
    at tryOnTimeout (timers.js:310:5)
    at Timer.listOnTimeout (timers.js:270:5)
jaggedsoft commented 5 years ago

Odd, that code is working fine for myself and countless others. I couldn't even begin to think why this would happen. Check out this code which connects to the websocket endpoints without using this library: https://github.com/jaggedsoft/node-binance-api/blob/master/examples/experiments.js

MauriPastorini commented 5 years ago

Yes this is very odd. I tried that example and it is working great and opening the websockets manually works great too.

ghost commented 4 years ago

Same thing happens to me with around 200 symbols. Works fine with 10-20 @jaggedsoft