From f175f4841847b7705f54382858fe6a923dae47fa Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 15 May 2018 00:31:56 +0300 Subject: [PATCH 1/2] Fix get balance functionality --- freqtrade/rpc/rpc.py | 37 +++++++++--------- freqtrade/tests/rpc/test_rpc.py | 36 ++++++++--------- freqtrade/tests/rpc/test_rpc_telegram.py | 49 ++++++++++-------------- 3 files changed, 55 insertions(+), 67 deletions(-) diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 05bdafd08..dd3eed001 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -245,35 +245,34 @@ class RPC(object): """ :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'] + for coin, balance in exchange.get_balances().items(): + if not balance['total']: + continue + + rate = None if coin == 'BTC': - currency["Rate"] = 1.0 + rate = 1.0 else: if coin == 'USDT': - currency["Rate"] = 1.0 / exchange.get_ticker('BTC/USDT', False)['bid'] + rate = 1.0 / exchange.get_ticker('BTC/USDT', False)['bid'] else: - currency["Rate"] = exchange.get_ticker(coin + '/BTC', False)['bid'] - currency['BTC'] = currency["Rate"] * currency["Balance"] - total = total + currency['BTC'] + rate = exchange.get_ticker(coin + '/BTC', False)['bid'] + est_btc: float = rate * balance['total'] + total = total + est_btc output.append( { - 'currency': currency['Currency'], - 'available': currency['Available'], - 'balance': currency['Balance'], - 'pending': currency['Pending'], - 'est_btc': currency['BTC'] + 'currency': coin, + 'available': balance['free'], + 'balance': balance['total'], + 'pending': balance['used'], + 'est_btc': est_btc } ) + if total == 0.0: + return True, '`All balances are zero.`' + fiat = self.freqtrade.fiat_converter symbol = fiat_display_currency value = fiat.convert_amount(total, 'BTC', symbol) diff --git a/freqtrade/tests/rpc/test_rpc.py b/freqtrade/tests/rpc/test_rpc.py index f600af5dc..1d1b3b39c 100644 --- a/freqtrade/tests/rpc/test_rpc.py +++ b/freqtrade/tests/rpc/test_rpc.py @@ -288,22 +288,18 @@ def test_rpc_balance_handle(default_conf, mocker): """ Test rpc_balance() method """ - mock_balance = [ - { - 'Currency': 'BTC', - 'Balance': 10.0, - 'Available': 12.0, - 'Pending': 0.0, - 'CryptoAddress': 'XXXX', + mock_balance = { + 'BTC': { + 'free': 10.0, + 'total': 12.0, + 'used': 2.0, }, - { - 'Currency': 'ETH', - 'Balance': 0.0, - 'Available': 0.0, - 'Pending': 0.0, - 'CryptoAddress': 'XXXX', + 'ETH': { + 'free': 0.0, + 'total': 0.0, + 'used': 0.0, } - ] + } patch_get_signal(mocker, (True, False)) mocker.patch.multiple( @@ -324,15 +320,15 @@ def test_rpc_balance_handle(default_conf, mocker): (error, res) = rpc.rpc_balance(default_conf['fiat_display_currency']) assert not error (trade, x, y, z) = res - assert prec_satoshi(x, 10) - assert prec_satoshi(z, 150000) + assert prec_satoshi(x, 12) + assert prec_satoshi(z, 180000) assert 'USD' in y assert len(trade) == 1 assert 'BTC' in trade[0]['currency'] - assert prec_satoshi(trade[0]['available'], 12) - assert prec_satoshi(trade[0]['balance'], 10) - assert prec_satoshi(trade[0]['pending'], 0) - assert prec_satoshi(trade[0]['est_btc'], 10) + assert prec_satoshi(trade[0]['available'], 10) + assert prec_satoshi(trade[0]['balance'], 12) + assert prec_satoshi(trade[0]['pending'], 2) + assert prec_satoshi(trade[0]['est_btc'], 12) def test_rpc_start(mocker, default_conf) -> None: diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index 38e9c8b8a..27637c013 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -554,36 +554,29 @@ def test_telegram_balance_handle(default_conf, update, mocker) -> None: """ Test _balance() method """ - mock_balance = [ - { - 'Currency': 'BTC', - 'Balance': 10.0, - 'Available': 12.0, - 'Pending': 0.0, - 'CryptoAddress': 'XXXX', + + mock_balance = { + 'BTC': { + 'total': 12.0, + 'free': 12.0, + 'used': 0.0 }, - { - 'Currency': 'ETH', - 'Balance': 0.0, - 'Available': 0.0, - 'Pending': 0.0, - 'CryptoAddress': 'XXXX', + 'ETH': { + 'total': 0.0, + 'free': 0.0, + 'used': 0.0 }, - { - 'Currency': 'USDT', - 'Balance': 10000.0, - 'Available': 0.0, - 'Pending': 0.0, - 'CryptoAddress': 'XXXX', + 'USDT': { + 'total': 10000.0, + 'free': 10000.0, + 'used': 0.0 }, - { - 'Currency': 'LTC', - 'Balance': 10.0, - 'Available': 10.0, - 'Pending': 0.0, - 'CryptoAddress': 'XXXX', + 'LTC': { + 'total': 10.0, + 'free': 10.0, + 'used': 0.0 } - ] + } def mock_ticker(symbol, refresh): """ @@ -626,7 +619,7 @@ def test_telegram_balance_handle(default_conf, update, mocker) -> None: assert '*Currency*: USDT' in result assert 'Balance' in result assert 'Est. BTC' in result - assert '*BTC*: 12.00000000' in result + assert '*BTC*: 14.00000000' in result def test_zero_balance_handle(default_conf, update, mocker) -> None: @@ -636,7 +629,7 @@ def test_zero_balance_handle(default_conf, update, mocker) -> None: patch_get_signal(mocker, (True, False)) patch_coinmarketcap(mocker, value={'price_usd': 15000.0}) mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock()) - mocker.patch('freqtrade.freqtradebot.exchange.get_balances', return_value=[]) + mocker.patch('freqtrade.freqtradebot.exchange.get_balances', return_value={}) msg_mock = MagicMock() mocker.patch.multiple( From d112d90e8ef9235219711d1d84b28ac1c405735d Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 15 May 2018 13:37:34 +0300 Subject: [PATCH 2/2] Make telegram message beautiful --- freqtrade/rpc/telegram.py | 18 ++++++++---------- freqtrade/tests/rpc/test_rpc_telegram.py | 12 ++++++------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 086c408ac..c640fc77b 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -264,17 +264,15 @@ class Telegram(RPC): (currencys, total, symbol, value) = result output = '' for currency in currencys: - output += """*Currency*: {currency} - *Available*: {available} - *Balance*: {balance} - *Pending*: {pending} - *Est. BTC*: {est_btc: .8f} - """.format(**currency) + output += "*{currency}:*\n" \ + "\t`Available: {available: .8f}`\n" \ + "\t`Balance: {balance: .8f}`\n" \ + "\t`Pending: {pending: .8f}`\n" \ + "\t`Est. BTC: {est_btc: .8f}`\n".format(**currency) - output += """*Estimated Value*: - *BTC*: {0: .8f} - *{1}*: {2: .2f} - """.format(total, symbol, value) + output += "\n*Estimated Value*:\n" \ + "\t`BTC: {0: .8f}`\n" \ + "\t`{1}: {2: .2f}`\n".format(total, symbol, value) self.send_msg(output) @authorized_only diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index 27637c013..28fdc7902 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -614,12 +614,12 @@ def test_telegram_balance_handle(default_conf, update, mocker) -> None: telegram._balance(bot=MagicMock(), update=update) result = msg_mock.call_args_list[0][0][0] assert msg_mock.call_count == 1 - assert '*Currency*: BTC' in result - assert '*Currency*: ETH' not in result - assert '*Currency*: USDT' in result - assert 'Balance' in result - assert 'Est. BTC' in result - assert '*BTC*: 14.00000000' in result + assert '*BTC:*' in result + assert '*ETH:*' not in result + assert '*USDT:*' in result + assert 'Balance:' in result + assert 'Est. BTC:' in result + assert 'BTC: 14.00000000' in result def test_zero_balance_handle(default_conf, update, mocker) -> None: