diff --git a/freqtrade/rpc/__init__.py b/freqtrade/rpc/__init__.py index 500024ef8..e8c5e7170 100644 --- a/freqtrade/rpc/__init__.py +++ b/freqtrade/rpc/__init__.py @@ -295,3 +295,35 @@ def rpc_trade_statistics(stake_currency, fiat_display_currency) -> None: best_rate=round(bp_rate * 100, 2), ) return markdown_msg + +def rpc_balance(fiat_display_currency): + """ + :return: current account balance per crypto + """ + balances = [ + c for c in exchange.get_balances() + if c['Balance'] or c['Available'] or c['Pending'] + ] + if not balances: + return (True, '`All balances are zero.`') + + output = [] + total = 0.0 + for currency in balances: + coin = currency['Currency'] + if coin == 'BTC': + currency["Rate"] = 1.0 + else: + currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid'] + currency['BTC'] = currency["Rate"] * currency["Balance"] + total = total + currency['BTC'] + output.append({'currency': currency['Currency'], + 'available': currency['Available'], + 'balance': currency['Balance'], + 'pending': currency['Pending'], + 'est_btc': currency['BTC'] + }) + fiat = CryptoToFiatConverter() + symbol = fiat_display_currency + value = fiat.convert_amount(total, 'BTC', symbol) + return (output, total, symbol, value) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 61280aea2..76985111d 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -10,7 +10,8 @@ from telegram.ext import CommandHandler, Updater from freqtrade.rpc.__init__ import (rpc_status_table, rpc_trade_status, rpc_daily_profit, - rpc_trade_statistics + rpc_trade_statistics, + rpc_balance ) from freqtrade import __version__, exchange @@ -243,23 +244,14 @@ def _balance(bot: Bot, update: Update) -> None: Handler for /balance Returns current account balance per crypto """ - output = '' - balances = [ - c for c in exchange.get_balances() - if c['Balance'] or c['Available'] or c['Pending'] - ] - if not balances: - output = '`All balances are zero.`' + (error, result) = rpc_balance(_CONF['fiat_display_currency']) + if error: + send_msg('`All balances are zero.`') + return - total = 0.0 - for currency in balances: - coin = currency['Currency'] - if coin == 'BTC': - currency["Rate"] = 1.0 - else: - currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid'] - currency['BTC'] = currency["Rate"] * currency["Balance"] - total = total + currency['BTC'] + (currencys, total, symbol, value) = result + output = '' + for currency in currencys: output += """*Currency*: {Currency} *Available*: {Available} *Balance*: {Balance} @@ -268,10 +260,6 @@ def _balance(bot: Bot, update: Update) -> None: """.format(**currency) - symbol = _CONF['fiat_display_currency'] - value = _FIAT_CONVERT.convert_amount( - total, 'BTC', symbol - ) output += """*Estimated Value*: *BTC*: {0: .8f} *{1}*: {2: .2f} diff --git a/freqtrade/tests/rpc/test_rpc.py b/freqtrade/tests/rpc/test_rpc.py index d4424d120..f1fbba042 100644 --- a/freqtrade/tests/rpc/test_rpc.py +++ b/freqtrade/tests/rpc/test_rpc.py @@ -155,7 +155,6 @@ def test_rpc_trade_statistics( default_conf, update, ticker, ticker_sell_up, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) - msg_mock = MagicMock() mocker.patch('freqtrade.main.rpc.send_msg', MagicMock()) mocker.patch.multiple('freqtrade.rpc.telegram', _CONF=default_conf,