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

Shouldn't binance.depth return data as array? #249

Closed bengrunfeld closed 6 years ago

bengrunfeld commented 6 years ago

Title

Short Description:

Rather, depth data should be returned as an array. E.g.

{
  bids: [
    [0.00022997, 49.00000000],
    [0.00022867, 11.00000000],
    [0.00022865, 1149.00000000],
    [0.00022810, 20.00000000]
], asks: [
]

Platform:

node version:

code

binance.depth("BNBBTC", (error, depth, symbol) => {
  console.log(symbol+" market depth", depth);
});

Thank you, supercoder!! ;)

jaggedsoft commented 6 years ago

I had found that managing the depth cache as an object was way more efficient in terms of speed. However if you prefer the array format you can still request it as such:

binance.websockets.depthCache(['BTCUSDT'], (symbol, depth) => {
    const limit = 3;
    let bids = binance.sortBids(depth.bids, limit);
    let asks = binance.sortAsks(depth.asks, limit);

    //console.log("best bid: "+binance.first(bids));
    //console.log("best ask: "+binance.first(asks));
    let output = {bids:binance.array(bids), asks:binance.array(asks)};
    console.log(symbol, output);

    // save information to json file
    //file.writeFile("json/depth/"+symbol+".json", JSON.stringify(output, null, 4), (err)=>{});
});