Change formatting slightly

This commit is contained in:
Matthias 2021-12-30 17:18:46 +01:00
parent fa12098bff
commit 45ac3b3562
3 changed files with 118 additions and 121 deletions

View File

@ -224,9 +224,9 @@ class Telegram(RPCHandler):
is_fill = msg['type'] in [RPCMessageType.BUY_FILL, RPCMessageType.SHORT_FILL] is_fill = msg['type'] in [RPCMessageType.BUY_FILL, RPCMessageType.SHORT_FILL]
emoji = '\N{CHECK MARK}' if is_fill else '\N{LARGE BLUE CIRCLE}' emoji = '\N{CHECK MARK}' if is_fill else '\N{LARGE BLUE CIRCLE}'
enter_side = {'enter': 'Long', 'entered': 'Longed'} if msg['type'] \ enter_side = ({'enter': 'Long', 'entered': 'Longed'} if msg['type']
in [RPCMessageType.BUY_FILL, RPCMessageType.BUY] \ in [RPCMessageType.BUY_FILL, RPCMessageType.BUY]
else {'enter': 'Short', 'entered': 'Shorted'} else {'enter': 'Short', 'entered': 'Shorted'})
message = ( message = (
f"{emoji} *{msg['exchange']}:*" f"{emoji} *{msg['exchange']}:*"
f" {enter_side['entered'] if is_fill else enter_side['enter']} {msg['pair']}" f" {enter_side['entered'] if is_fill else enter_side['enter']} {msg['pair']}"
@ -259,6 +259,8 @@ class Telegram(RPCHandler):
msg['enter_tag'] = msg['enter_tag'] if "enter_tag" in msg.keys() else None msg['enter_tag'] = msg['enter_tag'] if "enter_tag" in msg.keys() else None
msg['emoji'] = self._get_sell_emoji(msg) 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 "")
# Check if all sell properties are available. # Check if all sell properties are available.
# This might not be the case if the message origin is triggered by /forcesell # This might not be the case if the message origin is triggered by /forcesell
@ -272,20 +274,19 @@ class Telegram(RPCHandler):
else: else:
msg['profit_extra'] = '' msg['profit_extra'] = ''
is_fill = msg['type'] == RPCMessageType.SELL_FILL is_fill = msg['type'] == RPCMessageType.SELL_FILL
message = "".join([ message = (
f"{msg['emoji']} *{msg['exchange']}:* ", f"{msg['emoji']} *{msg['exchange']}:* "
f"{'Exited' if is_fill else 'Exiting'} {msg['pair']} (#{msg['trade_id']})\n", f"{'Exited' if is_fill else 'Exiting'} {msg['pair']} (#{msg['trade_id']})\n"
f"*{'Profit' if is_fill else 'Unrealized Profit'}:* ", f"*{'Profit' if is_fill else 'Unrealized Profit'}:* "
f"`{msg['profit_ratio']:.2%}{msg['profit_extra']}`\n", f"`{msg['profit_ratio']:.2%}{msg['profit_extra']}`\n"
f"*Enter Tag:* `{msg['enter_tag']}`\n", f"*Enter Tag:* `{msg['enter_tag']}`\n"
f"*Exit Reason:* `{msg['sell_reason']}`\n", f"*Exit Reason:* `{msg['sell_reason']}`\n"
f"*Duration:* `{msg['duration']} ({msg['duration_min']:.1f} min)`\n", f"*Duration:* `{msg['duration']} ({msg['duration_min']:.1f} min)`\n"
f"*Direction:* `{msg['direction']}`\n", f"*Direction:* `{msg['direction']}`\n"
f"*Leverage:* `{msg['leverage']:.1f}`\n" if f"{msg['leverage_text']}"
msg.get('leverage', None) is not None else "", f"*Amount:* `{msg['amount']:.8f}`\n"
f"*Amount:* `{msg['amount']:.8f}`\n",
f"*Open Rate:* `{msg['open_rate']:.8f}`\n" f"*Open Rate:* `{msg['open_rate']:.8f}`\n"
]) )
if msg['type'] == RPCMessageType.SELL: if msg['type'] == RPCMessageType.SELL:
message += (f"*Current Rate:* `{msg['current_rate']:.8f}`\n" message += (f"*Current Rate:* `{msg['current_rate']:.8f}`\n"
f"*Close Rate:* `{msg['limit']:.8f}`") f"*Close Rate:* `{msg['limit']:.8f}`")

View File

@ -1651,10 +1651,9 @@ def test_show_config_handle(default_conf, update, mocker) -> None:
assert '*Initial Stoploss:* `-0.1`' in msg_mock.call_args_list[0][0][0] assert '*Initial Stoploss:* `-0.1`' in msg_mock.call_args_list[0][0][0]
@pytest.mark.parametrize( @pytest.mark.parametrize('message_type,enter,enter_signal,leverage', [
'message_type,enter,enter_signal,leverage', (RPCMessageType.BUY, 'Long', 'long_signal_01', None),
[(RPCMessageType.BUY, 'Long', 'long_signal_01', None), (RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)])
(RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)])
def test_send_msg_buy_notification(default_conf, mocker, caplog, message_type, def test_send_msg_buy_notification(default_conf, mocker, caplog, message_type,
enter, enter_signal, leverage) -> None: enter, enter_signal, leverage) -> None:
@ -1704,10 +1703,9 @@ def test_send_msg_buy_notification(default_conf, mocker, caplog, message_type,
msg_mock.call_args_list[0][1]['disable_notification'] is True msg_mock.call_args_list[0][1]['disable_notification'] is True
@pytest.mark.parametrize( @pytest.mark.parametrize('message_type,enter,enter_signal', [
'message_type,enter,enter_signal', (RPCMessageType.BUY_CANCEL, 'Long', 'long_signal_01'),
[(RPCMessageType.BUY_CANCEL, 'Long', 'long_signal_01'), (RPCMessageType.SHORT_CANCEL, 'Short', 'short_signal_01')])
(RPCMessageType.SHORT_CANCEL, 'Short', 'short_signal_01')])
def test_send_msg_buy_cancel_notification(default_conf, mocker, message_type, def test_send_msg_buy_cancel_notification(default_conf, mocker, message_type,
enter, enter_signal) -> None: enter, enter_signal) -> None:
@ -1754,10 +1752,9 @@ def test_send_msg_protection_notification(default_conf, mocker, time_machine) ->
"*All pairs* will be locked until `2021-09-01 06:45:00`.") "*All pairs* will be locked until `2021-09-01 06:45:00`.")
@pytest.mark.parametrize( @pytest.mark.parametrize('message_type,entered,enter_signal,leverage', [
'message_type,entered,enter_signal,leverage', (RPCMessageType.BUY_FILL, 'Longed', 'long_signal_01', None),
[(RPCMessageType.BUY_FILL, 'Longed', 'long_signal_01', None), (RPCMessageType.SHORT_FILL, 'Shorted', 'short_signal_01', 2.0)])
(RPCMessageType.SHORT_FILL, 'Shorted', 'short_signal_01', 2.0)])
def test_send_msg_buy_fill_notification(default_conf, mocker, message_type, entered, def test_send_msg_buy_fill_notification(default_conf, mocker, message_type, entered,
enter_signal, leverage) -> None: enter_signal, leverage) -> None:
@ -1779,15 +1776,15 @@ def test_send_msg_buy_fill_notification(default_conf, mocker, message_type, ente
'amount': 1333.3333333333335, 'amount': 1333.3333333333335,
'open_date': arrow.utcnow().shift(hours=-1) 'open_date': arrow.utcnow().shift(hours=-1)
}) })
message = [f'\N{CHECK MARK} *Binance:* {entered} ETH/BTC (#1)\n', leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else ''
f'*Enter Tag:* `{enter_signal}`\n', assert msg_mock.call_args[0][0] == (
f'*Leverage:* `{leverage}`\n' if leverage else '', f'\N{CHECK MARK} *Binance:* {entered} ETH/BTC (#1)\n'
'*Amount:* `1333.33333333`\n', f'*Enter Tag:* `{enter_signal}`\n'
'*Open Rate:* `0.00001099`\n', f"{leverage_text}"
'*Total:* `(0.00100000 BTC, 12.345 USD)`'] '*Amount:* `1333.33333333`\n'
# raise ValueError(msg_mock.call_args[0][0]) '*Open Rate:* `0.00001099`\n'
assert msg_mock.call_args[0][0] \ '*Total:* `(0.00100000 BTC, 12.345 USD)`'
== "".join(message) )
def test_send_msg_sell_notification(default_conf, mocker) -> None: def test_send_msg_sell_notification(default_conf, mocker) -> None:
@ -1818,19 +1815,19 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None:
'open_date': arrow.utcnow().shift(hours=-1), 'open_date': arrow.utcnow().shift(hours=-1),
'close_date': arrow.utcnow(), 'close_date': arrow.utcnow(),
}) })
assert msg_mock.call_args[0][0] \ assert msg_mock.call_args[0][0] == (
== ('\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n' '\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
'*Unrealized Profit:* `-57.41% (loss: -0.05746268 ETH / -24.812 USD)`\n' '*Unrealized Profit:* `-57.41% (loss: -0.05746268 ETH / -24.812 USD)`\n'
'*Enter Tag:* `buy_signal1`\n' '*Enter Tag:* `buy_signal1`\n'
'*Exit Reason:* `stop_loss`\n' '*Exit Reason:* `stop_loss`\n'
'*Duration:* `1:00:00 (60.0 min)`\n' '*Duration:* `1:00:00 (60.0 min)`\n'
'*Direction:* `Long`\n' '*Direction:* `Long`\n'
'*Leverage:* `1.0`\n' '*Leverage:* `1.0`\n'
'*Amount:* `1333.33333333`\n' '*Amount:* `1333.33333333`\n'
'*Open Rate:* `0.00007500`\n' '*Open Rate:* `0.00007500`\n'
'*Current Rate:* `0.00003201`\n' '*Current Rate:* `0.00003201`\n'
'*Close Rate:* `0.00003201`' '*Close Rate:* `0.00003201`'
) )
msg_mock.reset_mock() msg_mock.reset_mock()
telegram.send_msg({ telegram.send_msg({
@ -1853,18 +1850,18 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None:
'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30), 'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30),
'close_date': arrow.utcnow(), 'close_date': arrow.utcnow(),
}) })
assert msg_mock.call_args[0][0] \ assert msg_mock.call_args[0][0] == (
== ('\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n' '\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
'*Unrealized Profit:* `-57.41%`\n' '*Unrealized Profit:* `-57.41%`\n'
'*Enter Tag:* `buy_signal1`\n' '*Enter Tag:* `buy_signal1`\n'
'*Exit Reason:* `stop_loss`\n' '*Exit Reason:* `stop_loss`\n'
'*Duration:* `1 day, 2:30:00 (1590.0 min)`\n' '*Duration:* `1 day, 2:30:00 (1590.0 min)`\n'
'*Direction:* `Long`\n' '*Direction:* `Long`\n'
'*Amount:* `1333.33333333`\n' '*Amount:* `1333.33333333`\n'
'*Open Rate:* `0.00007500`\n' '*Open Rate:* `0.00007500`\n'
'*Current Rate:* `0.00003201`\n' '*Current Rate:* `0.00003201`\n'
'*Close Rate:* `0.00003201`' '*Close Rate:* `0.00003201`'
) )
# Reset singleton function to avoid random breaks # Reset singleton function to avoid random breaks
telegram._rpc._fiat_converter.convert_amount = old_convamount telegram._rpc._fiat_converter.convert_amount = old_convamount
@ -1882,9 +1879,9 @@ def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None:
'pair': 'KEY/ETH', 'pair': 'KEY/ETH',
'reason': 'Cancelled on exchange' 'reason': 'Cancelled on exchange'
}) })
assert msg_mock.call_args[0][0] \ assert msg_mock.call_args[0][0] == (
== ('\N{WARNING SIGN} *Binance:* Cancelling exit Order for KEY/ETH (#1).' '\N{WARNING SIGN} *Binance:* Cancelling exit Order for KEY/ETH (#1).'
' Reason: Cancelled on exchange.') ' Reason: Cancelled on exchange.')
msg_mock.reset_mock() msg_mock.reset_mock()
telegram.send_msg({ telegram.send_msg({
@ -1894,17 +1891,15 @@ def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None:
'pair': 'KEY/ETH', 'pair': 'KEY/ETH',
'reason': 'timeout' 'reason': 'timeout'
}) })
assert msg_mock.call_args[0][0] \ assert msg_mock.call_args[0][0] == (
== ('\N{WARNING SIGN} *Binance:* Cancelling exit Order for KEY/ETH (#1).' '\N{WARNING SIGN} *Binance:* Cancelling exit Order for KEY/ETH (#1). Reason: timeout.')
' Reason: timeout.')
# Reset singleton function to avoid random breaks # Reset singleton function to avoid random breaks
telegram._rpc._fiat_converter.convert_amount = old_convamount telegram._rpc._fiat_converter.convert_amount = old_convamount
@pytest.mark.parametrize( @pytest.mark.parametrize('direction,enter_signal,leverage', [
'direction,enter_signal,leverage', ('Long', 'long_signal_01', None),
[('Long', 'long_signal_01', None), ('Short', 'short_signal_01', 2.0)])
('Short', 'short_signal_01', 2.0)])
def test_send_msg_sell_fill_notification(default_conf, mocker, direction, def test_send_msg_sell_fill_notification(default_conf, mocker, direction,
enter_signal, leverage) -> None: enter_signal, leverage) -> None:
@ -1932,18 +1927,20 @@ def test_send_msg_sell_fill_notification(default_conf, mocker, direction,
'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30), 'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30),
'close_date': arrow.utcnow(), 'close_date': arrow.utcnow(),
}) })
message = ['\N{WARNING SIGN} *Binance:* Exited KEY/ETH (#1)\n',
'*Profit:* `-57.41%`\n', leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else ''
f'*Enter Tag:* `{enter_signal}`\n', assert msg_mock.call_args[0][0] == (
'*Exit Reason:* `stop_loss`\n', '\N{WARNING SIGN} *Binance:* Exited KEY/ETH (#1)\n'
'*Duration:* `1 day, 2:30:00 (1590.0 min)`\n', '*Profit:* `-57.41%`\n'
f"*Direction:* `{direction}`\n", f'*Enter Tag:* `{enter_signal}`\n'
f"*Leverage:* `{leverage:.1f}`\n" if leverage is not None else "", '*Exit Reason:* `stop_loss`\n'
'*Amount:* `1333.33333333`\n', '*Duration:* `1 day, 2:30:00 (1590.0 min)`\n'
'*Open Rate:* `0.00007500`\n', f"*Direction:* `{direction}`\n"
'*Close Rate:* `0.00003201`'] f"{leverage_text}"
assert msg_mock.call_args[0][0] \ '*Amount:* `1333.33333333`\n'
== "".join(message) '*Open Rate:* `0.00007500`\n'
'*Close Rate:* `0.00003201`'
)
def test_send_msg_status_notification(default_conf, mocker) -> None: def test_send_msg_status_notification(default_conf, mocker) -> None:
@ -1982,12 +1979,11 @@ def test_send_msg_unknown_type(default_conf, mocker) -> None:
}) })
@pytest.mark.parametrize( @pytest.mark.parametrize('message_type,enter,enter_signal,leverage', [
'message_type,enter,enter_signal,leverage', (RPCMessageType.BUY, 'Long', 'long_signal_01', None),
[(RPCMessageType.BUY, 'Long', 'long_signal_01', None), (RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)])
(RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)]) def test_send_msg_buy_notification_no_fiat(
def test_send_msg_buy_notification_no_fiat(default_conf, mocker, message_type, default_conf, mocker, message_type, enter, enter_signal, leverage) -> None:
enter, enter_signal, leverage) -> None:
del default_conf['fiat_display_currency'] del default_conf['fiat_display_currency']
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
@ -2009,22 +2005,23 @@ def test_send_msg_buy_notification_no_fiat(default_conf, mocker, message_type,
'open_date': arrow.utcnow().shift(hours=-1) 'open_date': arrow.utcnow().shift(hours=-1)
}) })
message = [f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n', leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else ''
f'*Enter Tag:* `{enter_signal}`\n', assert msg_mock.call_args[0][0] == (
f'*Leverage:* `{leverage}`\n' if leverage else '', f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n'
'*Amount:* `1333.33333333`\n', f'*Enter Tag:* `{enter_signal}`\n'
'*Open Rate:* `0.00001099`\n', f'{leverage_text}'
'*Current Rate:* `0.00001099`\n', '*Amount:* `1333.33333333`\n'
'*Total:* `(0.00100000 BTC)`'] '*Open Rate:* `0.00001099`\n'
assert msg_mock.call_args[0][0] == "".join(message) '*Current Rate:* `0.00001099`\n'
'*Total:* `(0.00100000 BTC)`'
)
@pytest.mark.parametrize( @pytest.mark.parametrize('direction,enter_signal,leverage', [
'direction,enter_signal,leverage', ('Long', 'long_signal_01', None),
[('Long', 'long_signal_01', None), ('Short', 'short_signal_01', 2.0)])
('Short', 'short_signal_01', 2.0)]) def test_send_msg_sell_notification_no_fiat(
def test_send_msg_sell_notification_no_fiat(default_conf, mocker, direction, default_conf, mocker, direction, enter_signal, leverage) -> None:
enter_signal, leverage) -> None:
del default_conf['fiat_display_currency'] del default_conf['fiat_display_currency']
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
@ -2051,19 +2048,20 @@ def test_send_msg_sell_notification_no_fiat(default_conf, mocker, direction,
'close_date': arrow.utcnow(), 'close_date': arrow.utcnow(),
}) })
message = ['\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n', leverage_text = f'*Leverage:* `{leverage}`\n' if leverage else ''
'*Unrealized Profit:* `-57.41%`\n', assert msg_mock.call_args[0][0] == (
f'*Enter Tag:* `{enter_signal}`\n', '\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
'*Exit Reason:* `stop_loss`\n', '*Unrealized Profit:* `-57.41%`\n'
'*Duration:* `2:35:03 (155.1 min)`\n', f'*Enter Tag:* `{enter_signal}`\n'
f'*Direction:* `{direction}`\n', '*Exit Reason:* `stop_loss`\n'
f'*Leverage:* `{leverage}`\n' if leverage else '', '*Duration:* `2:35:03 (155.1 min)`\n'
'*Amount:* `1333.33333333`\n', f'*Direction:* `{direction}`\n'
'*Open Rate:* `0.00007500`\n', f'{leverage_text}'
'*Current Rate:* `0.00003201`\n', '*Amount:* `1333.33333333`\n'
'*Close Rate:* `0.00003201`', '*Open Rate:* `0.00007500`\n'
] '*Current Rate:* `0.00003201`\n'
assert msg_mock.call_args[0][0] == "".join(message) '*Close Rate:* `0.00003201`'
)
@pytest.mark.parametrize('msg,expected', [ @pytest.mark.parametrize('msg,expected', [

View File

@ -2949,15 +2949,13 @@ def test_execute_trade_exit_down(default_conf_usdt, ticker_usdt, fee, ticker_usd
@pytest.mark.parametrize( @pytest.mark.parametrize(
"is_short,amount,open_rate,current_rate,limit,profit_amount," "is_short,amount,open_rate,current_rate,limit,profit_amount,profit_ratio,profit_or_loss", [
"profit_ratio,profit_or_loss", [
(False, 30, 2.0, 2.3, 2.25, 7.18125, 0.11938903, 'profit'), (False, 30, 2.0, 2.3, 2.25, 7.18125, 0.11938903, 'profit'),
(True, 29.70297029, 2.02, 2.2, 2.25, -7.14876237, -0.11944465, 'loss'), # TODO-lev (True, 29.70297029, 2.02, 2.2, 2.25, -7.14876237, -0.11944465, 'loss'), # TODO-lev
]) ])
def test_execute_trade_exit_custom_exit_price( def test_execute_trade_exit_custom_exit_price(
default_conf_usdt, ticker_usdt, fee, ticker_usdt_sell_up, is_short, amount, open_rate, default_conf_usdt, ticker_usdt, fee, ticker_usdt_sell_up, is_short, amount, open_rate,
current_rate, limit, profit_amount, profit_ratio, profit_or_loss, mocker current_rate, limit, profit_amount, profit_ratio, profit_or_loss, mocker) -> None:
) -> None:
rpc_mock = patch_RPCManager(mocker) rpc_mock = patch_RPCManager(mocker)
patch_exchange(mocker) patch_exchange(mocker)
mocker.patch.multiple( mocker.patch.multiple(