|
| 1 | +global.ticker = {}; |
| 2 | +global.balance = {}; |
| 3 | +global.minimums = {}; |
| 4 | +// Get exchangeInfo on startup |
| 5 | +//minNotional = minimum order value (price * quantity) |
| 6 | +binance.exchangeInfo((error, data) => { |
| 7 | + if ( error ) console.error(error); |
| 8 | + let minimums = {}; |
| 9 | + for ( let obj of data.symbols ) { |
| 10 | + let filters = {status: obj.status}; |
| 11 | + for ( let filter of obj.filters ) { |
| 12 | + if ( filter.filterType == "MIN_NOTIONAL" ) { |
| 13 | + filters.minNotional = filter.minNotional; |
| 14 | + } else if ( filter.filterType == "PRICE_FILTER" ) { |
| 15 | + filters.minPrice = filter.minPrice; |
| 16 | + filters.maxPrice = filter.maxPrice; |
| 17 | + filters.tickSize = filter.tickSize; |
| 18 | + } else if ( filter.filterType == "LOT_SIZE" ) { |
| 19 | + filters.stepSize = filter.stepSize; |
| 20 | + filters.minQty = filter.minQty; |
| 21 | + filters.maxQty = filter.maxQty; |
| 22 | + } |
| 23 | + } |
| 24 | + //filters.baseAssetPrecision = obj.baseAssetPrecision; |
| 25 | + //filters.quoteAssetPrecision = obj.quoteAssetPrecision; |
| 26 | + filters.orderTypes = obj.orderTypes; |
| 27 | + filters.icebergAllowed = obj.icebergAllowed; |
| 28 | + minimums[obj.symbol] = filters; |
| 29 | + } |
| 30 | + //console.log(minimums); |
| 31 | + global.minimums = minimums; |
| 32 | + //fs.writeFile("json/minimums.json", JSON.stringify(minimums, null, 4), (err)=>{}); |
| 33 | + |
| 34 | + // Get ticker prices |
| 35 | + binance.prices((error, ticker) => { |
| 36 | + if ( error ) console.error(error); |
| 37 | + for ( let symbol in ticker ) { |
| 38 | + global.ticker[symbol] = parseFloat(ticker[symbol]); |
| 39 | + } |
| 40 | + // Get balance on a timer every 5 seconds |
| 41 | + setInterval(function(){ balance(); }, 5000); |
| 42 | + balance(); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +// Get your balances |
| 47 | +function balance() { |
| 48 | + binance.balance((error, balances) => { |
| 49 | + if ( error ) console.error(error); |
| 50 | + let btc = 0.00; |
| 51 | + for ( let asset in balances ) { |
| 52 | + let obj = balances[asset]; |
| 53 | + obj.available = parseFloat(obj.available); |
| 54 | + //if ( !obj.available ) continue; |
| 55 | + obj.onOrder = parseFloat(obj.onOrder); |
| 56 | + obj.btcValue = 0; |
| 57 | + obj.btcTotal = 0; |
| 58 | + if ( asset == 'BTC' ) obj.btcValue = obj.available; |
| 59 | + else if ( asset == 'USDT' ) obj.btcValue = obj.available / global.ticker.BTCUSDT; |
| 60 | + else obj.btcValue = obj.available * global.ticker[asset+'BTC']; |
| 61 | + if ( asset == 'BTC' ) obj.btcTotal = obj.available + obj.onOrder; |
| 62 | + else if ( asset == 'USDT' ) obj.btcTotal = (obj.available + obj.onOrder) / global.ticker.BTCUSDT; |
| 63 | + else obj.btcTotal = (obj.available + obj.onOrder) * global.ticker[asset+'BTC']; |
| 64 | + if ( isNaN(obj.btcValue) ) obj.btcValue = 0; |
| 65 | + if ( isNaN(obj.btcTotal) ) obj.btcTotal = 0; |
| 66 | + btc+= obj.btcTotal; |
| 67 | + global.balance[asset] = obj; |
| 68 | + } |
| 69 | + //fs.writeFile("json/balance.json", JSON.stringify(global.balance, null, 4), (err)=>{}); |
| 70 | + }); |
| 71 | +} |
0 commit comments