From d32481a68c9177f802a3f23c9c9723a8d7027621 Mon Sep 17 00:00:00 2001 From: Raanan Date: Sat, 25 Feb 2017 21:19:31 +0100 Subject: [PATCH 1/2] Fetch total account balance and store in JSON Implemented API method return_complete_balances Added a call in bot loop for fetching complete balance Sum coins balance and store in json output --- lendingbot.py | 1 + modules/Data.py | 9 +++++++++ modules/Poloniex.py | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/lendingbot.py b/lendingbot.py index 33142a19..d0eea3e7 100644 --- a/lendingbot.py +++ b/lendingbot.py @@ -67,6 +67,7 @@ while True: try: Data.update_conversion_rates(output_currency, json_output_enabled) + Data.update_total_account_balance() Lending.transfer_balances() Lending.cancel_all() Lending.lend_all() diff --git a/modules/Data.py b/modules/Data.py index db675863..8c691172 100644 --- a/modules/Data.py +++ b/modules/Data.py @@ -132,6 +132,15 @@ def get_lending_currencies(): return currencies +def update_total_account_balance(): + balance_response = api.return_complete_balances('all') + balance = 0 + for cur in balance_response: + balance += float(balance_response[cur]['btcValue']) + # added to BTC as value is BTC + log.updateStatusValue('BTC', 'total_account_balance', balance) + + def truncate(f, n): """Truncates/pads a float f to n decimal places without rounding""" # From https://stackoverflow.com/questions/783897/truncating-floats-in-python diff --git a/modules/Poloniex.py b/modules/Poloniex.py index c4f3544e..dc2d1f13 100644 --- a/modules/Poloniex.py +++ b/modules/Poloniex.py @@ -131,6 +131,16 @@ def transfer_balance(self, currency, amount, from_account, to_account): def return_balances(self): return self.api_query('returnBalances') + + # Returns all of your balances, including available balance, balance on orders, + # and the estimated BTC value of your balance. + # By default, this call is limited to your exchange account; + # set the "account" POST parameter to "all" to include your margin and lending accounts. Sample output: + def return_complete_balances(self, account): + balances = self.api_query('returnCompleteBalances', {"account": account}) + return balances + + def return_available_account_balances(self, account): balances = self.api_query('returnAvailableAccountBalances', {"account": account}) if isinstance(balances, list): # silly api wrapper, empty dict returns a list, which breaks the code later. From ff70c5b6a10756b6aa46db3b7400abc389bf6bc7 Mon Sep 17 00:00:00 2001 From: Raanan Date: Sat, 25 Feb 2017 22:33:24 +0100 Subject: [PATCH 2/2] Added Account Balance --- www/lendingbot.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/www/lendingbot.js b/www/lendingbot.js index cf7f9a95..10164a81 100644 --- a/www/lendingbot.js +++ b/www/lendingbot.js @@ -69,6 +69,7 @@ function updateRawValues(rawData){ table.innerHTML = ""; var currencies = Object.keys(rawData); var totalBTCEarnings = {}; + var totalAccountValueBTC = NaN; for (var keyIndex = 0; keyIndex < currencies.length; ++keyIndex) { var currency = currencies[keyIndex]; @@ -82,6 +83,7 @@ function updateRawValues(rawData){ // no bids for BTC provided by poloniex // this is added so BTC can be handled like other coins for conversions highestBidBTC = 1; + totalAccountValueBTC = parseFloat(rawData[currency]['total_account_balance']); } var couple = rawData[currency]['couple']; @@ -187,19 +189,34 @@ function updateRawValues(rawData){ var thead = table.createTHead(); // show account summary - if (currencies.length > 1 || summaryCoin != earningsOutputCoin) { - earnings = ''; - timespans.forEach(function(timespan) { - earnings += timespan.formatEarnings( summaryCoin, totalBTCEarnings[timespan.name] * summaryCoinRate); - }); + earnings = ''; + timespans.forEach(function(timespan) { + earnings += timespan.formatEarnings( summaryCoin, totalBTCEarnings[timespan.name] * summaryCoinRate); + }); + var row = thead.insertRow(0); + var cell = row.appendChild(document.createElement("th")); + cell.innerHTML = "Account
Estimated
Earnings"; + cell.style.verticalAlign = "text-top"; + cell = row.appendChild(document.createElement("th")); + cell.style.verticalAlign = "text-top"; + cell.setAttribute("colspan", 2); + cell.innerHTML = earnings; + + if( !isNaN(totalAccountValueBTC)) { + // show account total balance var row = thead.insertRow(0); var cell = row.appendChild(document.createElement("th")); - cell.innerHTML = "Account
Estimated
Earnings"; + cell.innerHTML = "Account Balance"; cell.style.verticalAlign = "text-top"; cell = row.appendChild(document.createElement("th")); + cell.style.verticalAlign = "text-top"; cell.setAttribute("colspan", 2); - cell.innerHTML = earnings; + cell.innerHTML = "
"+ prettyFloat(displayUnit.multiplier * totalAccountValueBTC, 2) + " " + displayUnit.name + "
" + if(summaryCoin != 'BTC') { + cell.innerHTML += "
"+ prettyFloat(summaryCoinRate * totalAccountValueBTC, 2) + " " + summaryCoin + "
" + } } + } function handleLocalFile(file) {