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

Websocket for marketdepth #216

Closed t-westwood closed 6 years ago

t-westwood commented 6 years ago

Hi JaggedSoft,

I was wondering if there was a way to websocket all currencies for the market depth and save the results to file?

Cheers bro

t-westwood commented 6 years ago

the files would be individual like writing to file for candlestick data etc

t-westwood commented 6 years ago

something like the code below, but to filter through all symbols? (and to save them to individual files)

binance.websockets.depthCache(["BTCUSDT"], function(symbol, depth) { let max = 10; // Only show the 10 best bids / asks (optional) let bids = binance.sortBids(depth.bids, max); let asks = binance.sortAsks(depth.asks, max); console.log(binance.reverse(asks)); // print asks console.log("ask: ", binance.first(asks)); console.log("bid: ", binance.first(bids)); console.log(bids); // print bids });

jaggedsoft commented 6 years ago

Yes you could do something like this

binance.prevDay(false, (error, prevDay) => {
    let markets = [];
    for ( let obj of prevDay ) {
        markets.push(obj.symbol);
        console.log(obj.symbol+" volume:"+obj.volume+" change: "+obj.priceChangePercent+"%");
    }

    // if there are problems here then some delay needs to be added before opening too many of these at once
    binance.websockets.depthCache(markets, function(symbol, depth) {
        let maximum = Infinity;
        let baseValue = "cumulative"; // for charts
        let bids = binance.sortBids(depth.bids, maximum, baseValue);
        let asks = binance.sortAsks(depth.asks, maximum, baseValue);
        console.log(symbol+" bids", bids);
        console.log(symbol+" asks", asks);
        let output = {
            bids: binance.array(bids),
            asks: binance.array(asks).reverse()
        };
        fs.writeFile("json/depth/"+symbol+".json", JSON.stringify(output, null, 4), function(err){});
    });
});
t-westwood commented 6 years ago

Cheers bro!

jaggedsoft commented 6 years ago

No problem, just a note: "cumulative" is for drawing depth charts. You can remove baseValue entirely if that's not what you want.

https://github.com/jaggedsoft/node-binance-api/wiki/Converting-Depth-Data-to-Array

t-westwood commented 6 years ago

When running the code above, you was right, there was DEPTH CACHE OUT OF SYNC !!' ] errors popping up, but i changed the maximum's to 15 and no problems ;)

And to your above reply, i was only after the 15 bid/asks either side of the current price. I will have a play around with it.

Thanks again!

t-westwood commented 6 years ago

Hi dude,

When I use the code you gave me, after running it the once, I seem to be getting some kind of IP ban (i think (when i go to binance.com on my phone or via app, no data available, and when I try to run the websocket again i get the error below))

Should I add some kind of delay after a full cycle (when all currencies have been checked)? I'm only running this one websocket. To be honest i'm not too sure how the request's work with a websocket.

TypeError: prevDay is not iterable at binance.prevDay (C:\Users\user\Documents\bot\testDepth.js:6:19) at C:\Users\user\Documents\bot\node_modules\node-binance-api\node-binance-api.js:696:49 at Request._callback (C:\Users\user\Documents\bot\node_modules\node-binance-api\node-binance-api.js:63:24) at Request.self.callback (C:\Users\user\Documents\bot\node_modules\request\request.js:186:22) at emitTwo (events.js:126:13) at Request.emit (events.js:214:7) at Request. (C:\Users\user\Documents\bot\node_modules\request\request.js:1163:10) at emitOne (events.js:116:13) at Request.emit (events.js:211:7) at IncomingMessage. (C:\Users\user\Documents\bot\node_modules\request\request.js:1085:12)

t-westwood commented 6 years ago

Maybe I should have the websocket search /BTC symbols only, i'm guessing the requests then would be halved? Thanks JS