buy/sell fill notification code consolidation

This commit is contained in:
ethan 2021-11-13 10:23:47 -05:00
parent a177e58dc4
commit 7412b7ba51

View File

@ -220,53 +220,48 @@ class Telegram(RPCHandler):
msg['stake_amount_fiat'] = 0 msg['stake_amount_fiat'] = 0
content = [] content = []
content.append( # Buy Order Fill
f"\N{LARGE BLUE CIRCLE} *{msg['exchange']}:* Buying {msg['pair']}" if msg['type'] == RPCMessageType.BUY_FILL:
f" (#{msg['trade_id']})\n"
)
if msg.get('buy_tag', None):
content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n")
content.append(f"*Amount:* `{msg['amount']:.8f}`\n")
content.append(f"*Open Rate:* `{msg['limit']:.8f}`\n")
content.append(f"*Current Rate:* `{msg['current_rate']:.8f}`\n")
content.append(
f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}"
)
if msg.get('fiat_currency', None):
content.append( content.append(
f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" f"\N{CHECK MARK} *{msg['exchange']}:* Bought {msg['pair']}"
f" (#{msg['trade_id']})\n"
) )
if msg.get('buy_tag', None):
content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n")
content.append(f"*Amount:* `{msg['amount']:.8f}`\n")
content.append(f"*Open Rate:* `{msg['open_rate']:.8f}`\n")
content.append(
f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}"
)
if msg.get('fiat_currency', None):
content.append(
f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}"
)
message = ''.join(content) message = ''.join(content)
message += ")`" message += ")`"
return message # Buy Order Ask
def _format_buy_fill_msg(self, msg: Dict[str, Any]) -> str:
if self._rpc._fiat_converter:
msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount(
msg['stake_amount'], msg['stake_currency'], msg['fiat_currency'])
else: else:
msg['stake_amount_fiat'] = 0
content = []
content.append(
f"\N{CHECK MARK} *{msg['exchange']}:* Bought {msg['pair']}"
f" (#{msg['trade_id']})\n"
)
if msg.get('buy_tag', None):
content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n")
content.append(f"*Amount:* `{msg['amount']:.8f}`\n")
content.append(f"*Open Rate:* `{msg['open_rate']:.8f}`\n")
content.append(
f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}"
)
if msg.get('fiat_currency', None):
content.append( content.append(
f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" f"\N{LARGE BLUE CIRCLE} *{msg['exchange']}:* Buying {msg['pair']}"
f" (#{msg['trade_id']})\n"
) )
if msg.get('buy_tag', None):
content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n")
content.append(f"*Amount:* `{msg['amount']:.8f}`\n")
content.append(f"*Open Rate:* `{msg['limit']:.8f}`\n")
content.append(f"*Current Rate:* `{msg['current_rate']:.8f}`\n")
content.append(
f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}"
)
if msg.get('fiat_currency', None):
content.append(
f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}"
)
message = ''.join(content)
message += ")`"
message = ''.join(content)
message += ")`"
return message return message
def _format_sell_msg(self, msg: Dict[str, Any]) -> str: def _format_sell_msg(self, msg: Dict[str, Any]) -> str:
@ -290,71 +285,42 @@ class Telegram(RPCHandler):
else: else:
msg['profit_extra'] = '' msg['profit_extra'] = ''
message = ("{emoji} *{exchange}:* Selling {pair} (#{trade_id})\n" # Sell Order Fill
"*Unrealized Profit:* `{profit_ratio:.2%}{profit_extra}`\n" if msg['type'] == RPCMessageType.SELL_FILL:
"*Buy Tag:* `{buy_tag}`\n" message = ("{emoji} *{exchange}:* Sold {pair} (#{trade_id})\n"
"*Sell Reason:* `{sell_reason}`\n" "*Profit:* `{profit_ratio:.2%}{profit_extra}`\n"
"*Duration:* `{duration} ({duration_min:.1f} min)`\n" "*Buy Tag:* `{buy_tag}`\n"
"*Amount:* `{amount:.8f}`\n" "*Sell Reason:* `{sell_reason}`\n"
"*Open Rate:* `{open_rate:.8f}`\n" "*Duration:* `{duration} ({duration_min:.1f} min)`\n"
"*Current Rate:* `{current_rate:.8f}`\n" "*Amount:* `{amount:.8f}`\n"
"*Close Rate:* `{limit:.8f}`").format(**msg) "*Close Rate:* `{close_rate:.8f}`").format(**msg)
return message # Sell Order Ask
def _format_sell_fill_msg(self, msg: Dict[str, Any]) -> str:
import pprint
pprint.pprint(msg)
msg['amount'] = round(msg['amount'], 8)
msg['profit_percent'] = round(msg['profit_ratio'] * 100, 2)
msg['duration'] = msg['close_date'].replace(
microsecond=0) - msg['open_date'].replace(microsecond=0)
msg['duration_min'] = msg['duration'].total_seconds() / 60
msg['buy_tag'] = msg['buy_tag'] if "buy_tag" in msg.keys() else None
msg['emoji'] = self._get_sell_emoji(msg)
# Check if all sell properties are available.
# This might not be the case if the message origin is triggered by /forcesell
if (all(prop in msg for prop in ['gain', 'fiat_currency', 'stake_currency'])
and self._rpc._fiat_converter):
msg['profit_fiat'] = self._rpc._fiat_converter.convert_amount(
msg['profit_amount'], msg['stake_currency'], msg['fiat_currency'])
msg['profit_extra'] = (' ({gain}: {profit_amount:.8f} {stake_currency}'
' / {profit_fiat:.3f} {fiat_currency})').format(**msg)
else: else:
msg['profit_extra'] = '' message = ("{emoji} *{exchange}:* Selling {pair} (#{trade_id})\n"
"*Unrealized Profit:* `{profit_ratio:.2%}{profit_extra}`\n"
message = ("{emoji} *{exchange}:* Sold {pair} (#{trade_id})\n" "*Buy Tag:* `{buy_tag}`\n"
"*Profit:* `{profit_ratio:.2%}{profit_extra}`\n" "*Sell Reason:* `{sell_reason}`\n"
"*Buy Tag:* `{buy_tag}`\n" "*Duration:* `{duration} ({duration_min:.1f} min)`\n"
"*Sell Reason:* `{sell_reason}`\n" "*Amount:* `{amount:.8f}`\n"
"*Duration:* `{duration} ({duration_min:.1f} min)`\n" "*Open Rate:* `{open_rate:.8f}`\n"
"*Amount:* `{amount:.8f}`\n" "*Current Rate:* `{current_rate:.8f}`\n"
"*Close Rate:* `{close_rate:.8f}`").format(**msg) "*Close Rate:* `{limit:.8f}`").format(**msg)
return message return message
def compose_message(self, msg: Dict[str, Any], msg_type: RPCMessageType) -> str: def compose_message(self, msg: Dict[str, Any], msg_type: RPCMessageType) -> str:
if msg_type in [RPCMessageType.BUY, RPCMessageType.BUY_FILL]:
if msg_type == RPCMessageType.BUY:
message = self._format_buy_msg(msg) message = self._format_buy_msg(msg)
elif msg_type in [RPCMessageType.SELL, RPCMessageType.SELL_FILL]:
message = self._format_sell_msg(msg)
elif msg_type in (RPCMessageType.BUY_CANCEL, RPCMessageType.SELL_CANCEL): elif msg_type in (RPCMessageType.BUY_CANCEL, RPCMessageType.SELL_CANCEL):
msg['message_side'] = 'buy' if msg_type == RPCMessageType.BUY_CANCEL else 'sell' msg['message_side'] = 'buy' if msg_type == RPCMessageType.BUY_CANCEL else 'sell'
message = ("\N{WARNING SIGN} *{exchange}:* " message = ("\N{WARNING SIGN} *{exchange}:* "
"Cancelling open {message_side} Order for {pair} (#{trade_id}). " "Cancelling open {message_side} Order for {pair} (#{trade_id}). "
"Reason: {reason}.".format(**msg)) "Reason: {reason}.".format(**msg))
elif msg_type == RPCMessageType.BUY_FILL:
message = self._format_buy_fill_msg(msg)
elif msg_type == RPCMessageType.SELL_FILL:
message = self._format_sell_fill_msg(msg)
elif msg_type == RPCMessageType.SELL:
message = self._format_sell_msg(msg)
elif msg_type == RPCMessageType.PROTECTION_TRIGGER: elif msg_type == RPCMessageType.PROTECTION_TRIGGER:
message = ( message = (
"*Protection* triggered due to {reason}. " "*Protection* triggered due to {reason}. "