From 2395988bf8ff959ea7e6bb07925ad66ed8606ddb Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 30 Dec 2021 17:32:36 +0100 Subject: [PATCH] Leverage defaults to 1.0, which should not be shown. --- freqtrade/rpc/telegram.py | 5 ++-- tests/rpc/test_rpc_telegram.py | 47 +++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 5f1b1fabd..047b5fa75 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -233,8 +233,9 @@ class Telegram(RPCHandler): f" (#{msg['trade_id']})\n" ) message += f"*Enter Tag:* `{msg['enter_tag']}`\n" if msg.get('enter_tag', None) else "" - message += f"*Leverage:* `{msg['leverage']}`\n" if msg.get('leverage', None) else "" message += f"*Amount:* `{msg['amount']:.8f}`\n" + if msg.get('leverage') and msg.get('leverage', 1.0) != 1.0: + message += f"*Leverage:* `{msg['leverage']}`\n" if msg['type'] in [RPCMessageType.BUY_FILL, RPCMessageType.SHORT_FILL]: message += f"*Open Rate:* `{msg['open_rate']:.8f}`\n" @@ -260,7 +261,7 @@ class Telegram(RPCHandler): msg['enter_tag'] = msg['enter_tag'] if "enter_tag" in msg.keys() else None msg['emoji'] = self._get_sell_emoji(msg) msg['leverage_text'] = (f"*Leverage:* `{msg['leverage']:.1f}`\n" - if msg.get('leverage', None) is not None else "") + if msg.get('leverage', None) and msg.get('leverage', 1.0) != 1.0 else "") # Check if all sell properties are available. # This might not be the case if the message origin is triggered by /forcesell diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index ac74f1e88..9cfa39615 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -1653,6 +1653,8 @@ def test_show_config_handle(default_conf, update, mocker) -> None: @pytest.mark.parametrize('message_type,enter,enter_signal,leverage', [ (RPCMessageType.BUY, 'Long', 'long_signal_01', None), + (RPCMessageType.BUY, 'Long', 'long_signal_01', 1.0), + (RPCMessageType.BUY, 'Long', 'long_signal_01', 5.0), (RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)]) def test_send_msg_buy_notification(default_conf, mocker, caplog, message_type, enter, enter_signal, leverage) -> None: @@ -1677,15 +1679,17 @@ def test_send_msg_buy_notification(default_conf, mocker, caplog, message_type, telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) telegram.send_msg(msg) - message = [f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n', - f'*Enter Tag:* `{enter_signal}`\n', - f'*Leverage:* `{leverage}`\n' if leverage else '', - '*Amount:* `1333.33333333`\n', - '*Open Rate:* `0.00001099`\n', - '*Current Rate:* `0.00001099`\n', - '*Total:* `(0.00100000 BTC, 12.345 USD)`'] + leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else '' - assert msg_mock.call_args[0][0] == "".join(message) + assert msg_mock.call_args[0][0] == ( + f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n' + f'*Enter Tag:* `{enter_signal}`\n' + '*Amount:* `1333.33333333`\n' + f'{leverage_text}' + '*Open Rate:* `0.00001099`\n' + '*Current Rate:* `0.00001099`\n' + '*Total:* `(0.00100000 BTC, 12.345 USD)`' + ) freqtradebot.config['telegram']['notification_settings'] = {'buy': 'off'} caplog.clear() @@ -1753,8 +1757,10 @@ def test_send_msg_protection_notification(default_conf, mocker, time_machine) -> @pytest.mark.parametrize('message_type,entered,enter_signal,leverage', [ - (RPCMessageType.BUY_FILL, 'Longed', 'long_signal_01', None), - (RPCMessageType.SHORT_FILL, 'Shorted', 'short_signal_01', 2.0)]) + (RPCMessageType.BUY_FILL, 'Longed', 'long_signal_01', 1.0), + (RPCMessageType.BUY_FILL, 'Longed', 'long_signal_02', 2.0), + (RPCMessageType.SHORT_FILL, 'Shorted', 'short_signal_01', 2.0), + ]) def test_send_msg_buy_fill_notification(default_conf, mocker, message_type, entered, enter_signal, leverage) -> None: @@ -1776,12 +1782,12 @@ def test_send_msg_buy_fill_notification(default_conf, mocker, message_type, ente 'amount': 1333.3333333333335, 'open_date': arrow.utcnow().shift(hours=-1) }) - leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else '' + leverage_text = f'*Leverage:* `{leverage}`\n' if leverage != 1.0 else '' assert msg_mock.call_args[0][0] == ( f'\N{CHECK MARK} *Binance:* {entered} ETH/BTC (#1)\n' f'*Enter Tag:* `{enter_signal}`\n' - f"{leverage_text}" '*Amount:* `1333.33333333`\n' + f"{leverage_text}" '*Open Rate:* `0.00001099`\n' '*Total:* `(0.00100000 BTC, 12.345 USD)`' ) @@ -1822,7 +1828,6 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: '*Exit Reason:* `stop_loss`\n' '*Duration:* `1:00:00 (60.0 min)`\n' '*Direction:* `Long`\n' - '*Leverage:* `1.0`\n' '*Amount:* `1333.33333333`\n' '*Open Rate:* `0.00007500`\n' '*Current Rate:* `0.00003201`\n' @@ -1899,6 +1904,8 @@ def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None: @pytest.mark.parametrize('direction,enter_signal,leverage', [ ('Long', 'long_signal_01', None), + ('Long', 'long_signal_01', 1.0), + ('Long', 'long_signal_01', 5.0), ('Short', 'short_signal_01', 2.0)]) def test_send_msg_sell_fill_notification(default_conf, mocker, direction, enter_signal, leverage) -> None: @@ -1928,7 +1935,7 @@ def test_send_msg_sell_fill_notification(default_conf, mocker, direction, 'close_date': arrow.utcnow(), }) - leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else '' + leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else '' assert msg_mock.call_args[0][0] == ( '\N{WARNING SIGN} *Binance:* Exited KEY/ETH (#1)\n' '*Profit:* `-57.41%`\n' @@ -1981,6 +1988,7 @@ def test_send_msg_unknown_type(default_conf, mocker) -> None: @pytest.mark.parametrize('message_type,enter,enter_signal,leverage', [ (RPCMessageType.BUY, 'Long', 'long_signal_01', None), + (RPCMessageType.BUY, 'Long', 'long_signal_01', 2.0), (RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)]) def test_send_msg_buy_notification_no_fiat( default_conf, mocker, message_type, enter, enter_signal, leverage) -> None: @@ -2005,12 +2013,12 @@ def test_send_msg_buy_notification_no_fiat( 'open_date': arrow.utcnow().shift(hours=-1) }) - leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else '' + leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else '' assert msg_mock.call_args[0][0] == ( f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n' f'*Enter Tag:* `{enter_signal}`\n' - f'{leverage_text}' '*Amount:* `1333.33333333`\n' + f'{leverage_text}' '*Open Rate:* `0.00001099`\n' '*Current Rate:* `0.00001099`\n' '*Total:* `(0.00100000 BTC)`' @@ -2019,7 +2027,10 @@ def test_send_msg_buy_notification_no_fiat( @pytest.mark.parametrize('direction,enter_signal,leverage', [ ('Long', 'long_signal_01', None), - ('Short', 'short_signal_01', 2.0)]) + ('Long', 'long_signal_01', 1.0), + ('Long', 'long_signal_01', 5.0), + ('Short', 'short_signal_01', 2.0), + ]) def test_send_msg_sell_notification_no_fiat( default_conf, mocker, direction, enter_signal, leverage) -> None: del default_conf['fiat_display_currency'] @@ -2048,7 +2059,7 @@ def test_send_msg_sell_notification_no_fiat( 'close_date': arrow.utcnow(), }) - leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else '' + leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else '' assert msg_mock.call_args[0][0] == ( '\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n' '*Unrealized Profit:* `-57.41%`\n'