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

binance.depth error #8

Closed malas closed 6 years ago

malas commented 6 years ago

When i call binance.depth i sometime get an error: Uncaught TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined

how can i handle such situation and check the cause of error?

Example call:


  binance.depth("REQETH", function(depth) {
    console.log("market depth", depth);
  });
jaggedsoft commented 6 years ago

Thank you for your report! I suspect what is happening here is that your recvWindow is timing out. It defaults to 5 seconds. Please make sure you are on the latest version of node-binance-api by going to your project directory and issuing an npm update

Then please test adding recvWindow to your "options" call like this:

binance.options({
'APIKEY': 'xxx',
'APISECRET': 'xxx',
'recvWindow': 60000
});

If this works, please let me know. If not, please try editing node-binance-api.js search for depth: replace the function and tell me if this works:

        depth: function(symbol, callback) {
            publicRequest(base+"v1/depth", {symbol:symbol, recvWindow:60000}, function(data) {
                return callback(depthData(data));
            });
        },
malas commented 6 years ago

thanks for quick response.

i will be able to do the tests only on Monday. however from what i managed to test out is that on the depth function there is no data received so calling any property on the data raises my earlier posted error.

by the way, the error strikes a lot faster than 5 seconds and it only occurs on REQ/ETH.

jaggedsoft commented 6 years ago

I had reports of another person having the same problem but it was due to their system time by being off. For windows: image

On linux: ntpdate -s time.nist.gov

Please let me know either way. I am happy to help any way that I can and I will be making an update to catch errors like this and print out error messages.

Another option is using WebSockets for realtime data updates:

binance.websockets.depthCache(["REQETH"], function(symbol, depth) {
    console.log(symbol+" depth cache update");
    console.log("ask: " + binance.min(depth.asks));
    console.log("bid: " + binance.max(depth.bids));
    //console.log(depth); // print the entire depth cache
});

image Example with sorting: (like picture)

binance.websockets.depthCache(["REQETH"], function(symbol, depth) {
    let max = 9; // Only show the 9 best bids / asks (optional)
    let baseVolume = true; // Show results in BTC/ETH/USDT value (optional)
    let bids = binance.sortBids(depth.bids, max, baseVolume);
    let asks = binance.sortAsks(depth.asks, max, baseVolume);
    console.log("asks", binance.reverse(asks));
    console.log("bids", bids);
    let askvol = binance.sum(Object.values(asks));
    let bidvol = binance.sum(Object.values(bids));
    let ratio = binance.percent(bidvol, bidvol + askvol);
    console.log("ask: " + binance.first(asks)+"\tvolume: "+askvol.toFixed(3));
    console.log("bid: " + binance.first(bids)+"\tvolume: "+bidvol.toFixed(3));
    console.log("buy ratio: " + ratio.toFixed(2) + "%");
});