From 8943d42509eb33c0c6fa91ac385eaeb0ed445b86 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 4 Mar 2022 19:42:29 +0100 Subject: [PATCH] Update telegram notifications to properly detect shorts --- freqtrade/persistence/models.py | 5 +++-- freqtrade/rpc/rpc.py | 2 +- freqtrade/rpc/telegram.py | 4 ++-- tests/rpc/test_rpc.py | 5 +++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 3e6232568..36340033c 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -197,7 +197,7 @@ class Order(_DECL_BASE): self.order_filled_date = datetime.now(timezone.utc) self.order_update_date = datetime.now(timezone.utc) - def to_json(self) -> Dict[str, Any]: + def to_json(self, entry_side: str) -> Dict[str, Any]: return { 'pair': self.ft_pair, 'order_id': self.order_id, @@ -219,6 +219,7 @@ class Order(_DECL_BASE): tzinfo=timezone.utc).timestamp() * 1000) if self.order_filled_date else None, 'order_type': self.order_type, 'price': self.price, + 'ft_is_entry': self.ft_order_side == entry_side, 'remaining': self.remaining, } @@ -458,7 +459,7 @@ class LocalTrade(): def to_json(self) -> Dict[str, Any]: filled_orders = self.select_filled_orders() - orders = [order.to_json() for order in filled_orders] + orders = [order.to_json(self.enter_side) for order in filled_orders] return { 'trade_id': self.id, diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 0badfd458..a77f6f69c 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -171,7 +171,7 @@ class RPC: # calculate profit and send message to user if trade.is_open: try: - closing_side = "buy" if trade.is_short else "sell" + closing_side = trade.exit_side current_rate = self._freqtrade.exchange.get_rate( trade.pair, refresh=False, side=closing_side) except (ExchangeError, PricingError): diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 1c06c56fc..90a602ef8 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -398,7 +398,7 @@ class Telegram(RPCHandler): first_avg = filled_orders[0]["safe_price"] for x, order in enumerate(filled_orders): - if order['ft_order_side'] != 'buy': + if not order['ft_is_entry']: continue cur_entry_datetime = arrow.get(order["order_filled_date"]) cur_entry_amount = order["amount"] @@ -465,7 +465,7 @@ class Telegram(RPCHandler): messages = [] for r in results: r['open_date_hum'] = arrow.get(r['open_date']).humanize() - r['num_entries'] = len([o for o in r['orders'] if o['ft_order_side'] == 'buy']) + r['num_entries'] = len([o for o in r['orders'] if o['ft_is_entry']]) r['sell_reason'] = r.get('sell_reason', "") lines = [ "*Trade ID:* `{trade_id}`" + diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 5fe3cae42..ce881bcf1 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -122,7 +122,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: 'order_date': ANY, 'order_timestamp': ANY, 'order_filled_date': ANY, 'order_filled_timestamp': ANY, 'order_type': 'limit', 'price': 1.098e-05, 'is_open': False, 'pair': 'ETH/BTC', 'order_id': ANY, - 'remaining': ANY, 'status': ANY + 'remaining': ANY, 'status': ANY, 'ft_is_entry': True, }], } @@ -204,7 +204,8 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: 'order_date': ANY, 'order_timestamp': ANY, 'order_filled_date': ANY, 'order_filled_timestamp': ANY, 'order_type': 'limit', 'price': 1.098e-05, 'is_open': False, 'pair': 'ETH/BTC', 'order_id': ANY, - 'remaining': ANY, 'status': ANY}], + 'remaining': ANY, 'status': ANY, 'ft_is_entry': True, + }], }