Simplify exit message fiat handling

This commit is contained in:
Matthias
2024-01-06 10:23:21 +01:00
parent 86a9968bdd
commit 934e72656a
2 changed files with 13 additions and 10 deletions

View File

@@ -347,6 +347,7 @@ class Telegram(RPCHandler):
return message return message
def _format_exit_msg(self, msg: Dict[str, Any]) -> str: def _format_exit_msg(self, msg: Dict[str, Any]) -> str:
fiat_currency = msg['fiat_currency']
msg['amount'] = round(msg['amount'], 8) msg['amount'] = round(msg['amount'], 8)
msg['profit_percent'] = round(msg['profit_ratio'] * 100, 2) msg['profit_percent'] = round(msg['profit_ratio'] * 100, 2)
msg['duration'] = msg['close_date'].replace( msg['duration'] = msg['close_date'].replace(
@@ -362,11 +363,11 @@ class Telegram(RPCHandler):
# Check if all exit properties are available. # Check if all exit properties are available.
# This might not be the case if the message origin is triggered by /forceexit # This might not be the case if the message origin is triggered by /forceexit
msg['profit_extra'] = '' msg['profit_extra'] = ''
if (all(prop in msg for prop in ['gain', 'fiat_currency', 'stake_currency'])
and self._rpc._fiat_converter): if self._rpc._fiat_converter and fiat_currency:
msg['profit_fiat'] = self._rpc._fiat_converter.convert_amount( profit_fiat = self._rpc._fiat_converter.convert_amount(
msg['profit_amount'], msg['stake_currency'], msg['fiat_currency']) msg['profit_amount'], msg['stake_currency'], fiat_currency)
msg['profit_extra'] = f" / {msg['profit_fiat']:.3f} {msg['fiat_currency']}" msg['profit_extra'] = f" / {profit_fiat:.3f} {fiat_currency}"
msg['profit_extra'] = ( msg['profit_extra'] = (
f" ({msg['gain']}: {msg['profit_amount']:.8f} {msg['stake_currency']}" f" ({msg['gain']}: {msg['profit_amount']:.8f} {msg['stake_currency']}"
@@ -382,8 +383,8 @@ class Telegram(RPCHandler):
if is_sub_trade or is_final_exit: if is_sub_trade or is_final_exit:
if self._rpc._fiat_converter: if self._rpc._fiat_converter:
cp_fiat = self._rpc._fiat_converter.convert_amount( cp_fiat = self._rpc._fiat_converter.convert_amount(
msg['cumulative_profit'], msg['stake_currency'], msg['fiat_currency']) msg['cumulative_profit'], msg['stake_currency'], fiat_currency)
cp_extra = f" / {cp_fiat:.3f} {msg['fiat_currency']}" cp_extra = f" / {cp_fiat:.3f} {fiat_currency}"
if is_final_exit: if is_final_exit:
profit_prefix = 'Sub ' profit_prefix = 'Sub '
cp_extra = ( cp_extra = (
@@ -421,14 +422,14 @@ class Telegram(RPCHandler):
if is_sub_trade: if is_sub_trade:
if self._rpc._fiat_converter: if self._rpc._fiat_converter:
msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount( msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount(
msg['stake_amount'], msg['stake_currency'], msg['fiat_currency']) msg['stake_amount'], msg['stake_currency'], fiat_currency)
else: else:
msg['stake_amount_fiat'] = 0 msg['stake_amount_fiat'] = 0
rem = round_coin_value(msg['stake_amount'], msg['stake_currency']) rem = round_coin_value(msg['stake_amount'], msg['stake_currency'])
message += f"\n*Remaining:* `({rem}" message += f"\n*Remaining:* `({rem}"
if msg.get('fiat_currency', None): if fiat_currency:
message += f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" message += f", {round_coin_value(msg['stake_amount_fiat'], fiat_currency)}"
message += ")`" message += ")`"
else: else:

View File

@@ -2253,6 +2253,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None:
'profit_amount': -0.05746268, 'profit_amount': -0.05746268,
'profit_ratio': -0.57405275, 'profit_ratio': -0.57405275,
'stake_currency': 'ETH', 'stake_currency': 'ETH',
'fiat_currency': None,
'enter_tag': 'buy_signal1', 'enter_tag': 'buy_signal1',
'exit_reason': ExitType.STOP_LOSS.value, 'exit_reason': ExitType.STOP_LOSS.value,
'open_date': dt_now() - timedelta(days=1, hours=2, minutes=30), 'open_date': dt_now() - timedelta(days=1, hours=2, minutes=30),
@@ -2335,6 +2336,7 @@ def test_send_msg_sell_fill_notification(default_conf, mocker, direction,
'profit_amount': -0.05746268, 'profit_amount': -0.05746268,
'profit_ratio': -0.57405275, 'profit_ratio': -0.57405275,
'stake_currency': 'ETH', 'stake_currency': 'ETH',
'fiat_currency': None,
'enter_tag': enter_signal, 'enter_tag': enter_signal,
'exit_reason': ExitType.STOP_LOSS.value, 'exit_reason': ExitType.STOP_LOSS.value,
'open_date': dt_now() - timedelta(days=1, hours=2, minutes=30), 'open_date': dt_now() - timedelta(days=1, hours=2, minutes=30),