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

Get coins balances in BTC #78

Closed GeorgeMurAlkh closed 6 years ago

GeorgeMurAlkh commented 6 years ago

Is it possible to get BTC balance of every token? Like in balances of binance site? https://www.binance.com/exchange/private/userAssetTransferBtc I don't want to spam binance API with 'getCurrPrice' requests for each coin.

jaggedsoft commented 6 years ago

Yes good idea, I thought this was already part of the library, I suppose it needs to be integrated

Here is my current approach, kind of messy but it works btcValue is the 'available' btcValue btcTotal includes the amount on orders

global.ticker = {};
binance.prices((error, ticker) => {
    //console.log("prices()", ticker);
    for ( let symbol in ticker ) {
        global.ticker[symbol] = parseFloat(ticker[symbol]);
    }
    binance.balance((error, balances) => {
        let balance = {};
        for ( let asset in balances ) {
            let obj = balances[asset];
            obj.available = parseFloat(obj.available);
            //if ( !obj.available ) continue; // only show items with balances
            obj.onOrder = parseFloat(obj.onOrder);
            obj.btcValue = 0;
            obj.btcTotal = 0;
            if ( asset == 'BTC' ) obj.btcValue = obj.available;
            else if ( asset == 'USDT' ) obj.btcValue = obj.available / global.ticker.BTCUSDT;
            else obj.btcValue = obj.available * global.ticker[asset+'BTC'];
            if ( asset == 'BTC' ) obj.btcTotal = obj.available + obj.onOrder;
            else if ( asset == 'USDT' ) obj.btcTotal = (obj.available + obj.onOrder) / global.ticker.BTCUSDT;
            else obj.btcTotal = (obj.available + obj.onOrder) * global.ticker[asset+'BTC'];
            if ( isNaN(obj.btcValue) ) obj.btcValue = 0;
            if ( isNaN(obj.btcTotal) ) obj.btcTotal = 0;
            balance[asset] = obj;
        }
        //fs.writeFile(global.path+"balance.json", JSON.stringify(balance, null, 4), (err)=>{});
        console.log(balance);
    });
});

image

GeorgeMurAlkh commented 6 years ago

Yes, thanks - I am calculating approximately like you)

Eluvade commented 5 years ago

In case anyone still needs this, here's how I'm doing it:

const equalizeVolume = (rawData, ethBtc, btcUsdt, bnbBtc, xrpBtc, btcUsdc, btcPax, btcUsds, btcTusd) => {
  rawData.forEach(pair => {
    if (pair.symbol.endsWith('ETH')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) * parseFloat(ethBtc.ETHBTC)).toString()
    } else if (pair.symbol.endsWith('USDT')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) / parseFloat(btcUsdt.BTCUSDT)).toString()
    } else if (pair.symbol.endsWith('BNB')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) * parseFloat(bnbBtc.BNBBTC)).toString()
    } else if (pair.symbol.endsWith('XRP')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) * parseFloat(xrpBtc.XRPBTC)).toString()
    } else if (pair.symbol.endsWith('USDC')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) / parseFloat(btcUsdc.BTCUSDC)).toString()
    } else if (pair.symbol.endsWith('PAX')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) / parseFloat(btcPax.BTCPAX)).toString()
    } else if (pair.symbol.endsWith('USDS')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) / parseFloat(btcUsds.BTCUSDS)).toString()
    } else if (pair.symbol.endsWith('TUSD')) {
      pair.quoteVolume = (parseFloat(pair.quoteVolume) /
        parseFloat(btcTusd.BTCTUSD)).toString()
    } else if (!pair.symbol.endsWith('BTC')) {
      pair.quoteVolume = '0'
      console.warn("New market found: " + pair.symbol)
    }
  })
  return rawData
}