Closed Tribic closed 5 years ago
It is easier to just grab all the data at once. If you only need the best bid and ask, the bookTickers
function will return the best bid/ask for every symbol.
Also, you can subscribe to a WebSocket so you always have up to date data streamed to your program.
// This is a regular web request, returning bestBid, bestAsk, for all symbols
binance.bookTickers((error, ticker) => {
console.log("bookTickers()", ticker);
console.log("Price of BNB: ", ticker.BNBBTC);
});
// This is a websocket, binance will send you constant updates
binance.websockets.prevDay(false, (error, response) => {
console.log(response);
});
Having said that, I will provide a complete example of how I do it
global.bookTicker = {}; // Keep bookTicker object always updated, so you can access it anywhere in your program
binance.websockets.prevDay(false, (error, response) => {
let symbol = response.symbol;
global.bookTicker[symbol] = response;
//console.log(response);
});
// this is just to show you the data we have. you can use it anywhere, for any symbol
setInterval(() => {
// All of these variables are available: bestBid bestAsk bestBidQty bestAskQty open high low volume quoteVolume priceChange percentChange averagePrice prevClose close closeQty openTime closeTime firstTradeId lastTradeId numTrades
console.log("BTC bid: "+global.bookTicker.BTCUSDT.bestBid+" \t best ask: "+global.bookTicker.BTCUSDT.bestAsk);
}, 3000);
Hi, thanks for your answer :) but as i need bids and asks as arrays, it´s actually not what i thought of, but u gave me the hint of setInterval i switched my loop to setinterval - not the way it meant be by coding nodejs and your api, i think, but it works! :)
A much more elegant way? Is there a way to call the binance.depth function from outside, giving it the symbol and getting back an object, lets say myObj, so that i can work with it?
Like:
z=0;
max=20;
MySymbolArray= ["VETBTC", "BNBBTC", "XVGBTC"];
Do {
myObj = binance.depth(MySymbolArray[z]); // <------------------
let bids = binance.sortBids(myObj.bids, max);
let asks = binance.sortAsks(myObj.asks, max);
//make sure it´s done till here before going on somehow!!! // <------------------
//Converting into Arrays
askQuantity = myObj.values(asks);
askPrice = myObj.keys(asks);
bidQuantity = myObj.values(bids);
bidPrice = myObj.keys(bids);
//Calculating and Output
//...
z++;
} //forever
The best way to do this is to subscribe with websockets depthCache. Then you will always have an up to date order book. You will get a notification every time there is an update.
Example:
const Binance = require('node-binance-api');
const binance = new Binance();
binance.websockets.depthCache(["BTCUSDT"], 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) + "%");
});
Handling Code for working with an array of symbols Hi there, i´m pretty new to node js, but i managed to connect from windows10 to binance an get nearly exactly what i wanted to. Thanks to this great API! My Strategy is based on bids and asks, so i´m using the binance.depth function and added some calculation and concole.log code to it.
Now the problem: when i put a loop around binace.depth the results are sometimes wrong, mainly because of the wrong order.
Result:
or
How can it be achieved, that the following loop awaits the finish of the one before? i tried async await, but it didnt work because of binance.depth function?! As i said, im pretty new to node js and async code :(
thanks in advance!