Skip to content

Converting Depth Data to Array

Jon Eyrick edited this page Nov 11, 2017 · 2 revisions

Often times depth data is required to be in array format. For example, when drawing charts: https://bfxdata.com/mobileSite/mobileGraphMarketDepthBTCUSD.php Data format: https://bfxdata.com/json/marketDepthBidsBTCUSD.json

For this reason I have added an array function to convert an object into an array:

binance.depth("BNBBTC", function(depth, symbol) {
	console.log(symbol+" bids", binance.array(depth.bids));
	console.log(symbol+" asks", binance.array(depth.asks));
});

Market Depth Chart If you want to draw an actual depth chart you need to use cumulative values. For this there are some built in functions:

binance.depth("BTCUSDT", function(depth, symbol) {
	//console.log(symbol+" market depth", 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)
	};
	fs.writeFile("json/depth/"+symbol+".json", JSON.stringify(output), function(err){});
});
Clone this wiki locally