From cac777cb214499b70f0cc187bc9d4888897fe3cc Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Wed, 4 Oct 2023 13:09:44 -0400 Subject: [PATCH 01/42] add property has_open_sl_orders to trade model --- freqtrade/persistence/trade_model.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 631585127..48fed1782 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -523,6 +523,17 @@ class LocalTrade: ] return len(open_orders_wo_sl) > 0 + @property + def has_open_sl_orders(self) -> int: + """ + True if there are open stoploss orders for this trade + """ + open_sl_orders = [ + o for o in self.orders + if o.ft_order_side in ['stoploss'] and o.ft_is_open + ] + return len(open_sl_orders) > 0 + @property def open_orders_ids(self) -> List[str]: open_orders_ids_wo_sl = [ From 9214af69012a6e73503c174f54ff9534e053447d Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Thu, 5 Oct 2023 22:24:17 -0400 Subject: [PATCH 02/42] update cancel_stoploss_on_exchange to cancel all sl orders of trade --- freqtrade/freqtradebot.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 02d43432d..ebc146ede 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -904,18 +904,18 @@ class FreqtradeBot(LoggingMixin): def cancel_stoploss_on_exchange(self, trade: Trade) -> Trade: # First cancelling stoploss on exchange ... - if trade.stoploss_order_id: - try: - logger.info(f"Canceling stoploss on exchange for {trade}") - co = self.exchange.cancel_stoploss_order_with_result( - trade.stoploss_order_id, trade.pair, trade.amount) - self.update_trade_state(trade, trade.stoploss_order_id, co, stoploss_order=True) - - # Reset stoploss order id. - trade.stoploss_order_id = None - except InvalidOrderException: - logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id} " - f"for pair {trade.pair}") + if trade.has_open_sl_orders: + for o in trade.orders: + if o.ft_order_side == 'stoploss' and o.ft_is_open: + try: + logger.info(f"Canceling stoploss on exchange for {trade} " + f"order: {o.order_id}") + co = self.exchange.cancel_stoploss_order_with_result( + o.order_id, trade.pair, trade.amount) + self.update_trade_state(trade, o.order_id, co, stoploss_order=True) + except InvalidOrderException: + logger.exception(f"Could not cancel stoploss order {o.order_id} " + f"for pair {trade.pair}") return trade def get_valid_enter_price_and_stake( From d5a0759051497c977499c38d747ca953aac1c99f Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 10:29:57 -0400 Subject: [PATCH 03/42] add open_sl_orders helper, use it in cancel_stoploss_on_exchange --- freqtrade/freqtradebot.py | 22 ++++++++++------------ freqtrade/persistence/trade_model.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index ebc146ede..ff3c36bf8 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -904,18 +904,16 @@ class FreqtradeBot(LoggingMixin): def cancel_stoploss_on_exchange(self, trade: Trade) -> Trade: # First cancelling stoploss on exchange ... - if trade.has_open_sl_orders: - for o in trade.orders: - if o.ft_order_side == 'stoploss' and o.ft_is_open: - try: - logger.info(f"Canceling stoploss on exchange for {trade} " - f"order: {o.order_id}") - co = self.exchange.cancel_stoploss_order_with_result( - o.order_id, trade.pair, trade.amount) - self.update_trade_state(trade, o.order_id, co, stoploss_order=True) - except InvalidOrderException: - logger.exception(f"Could not cancel stoploss order {o.order_id} " - f"for pair {trade.pair}") + for oslo in trade.open_sl_orders: + try: + logger.info(f"Canceling stoploss on exchange for {trade} " + f"order: {oslo.order_id}") + co = self.exchange.cancel_stoploss_order_with_result( + oslo.order_id, trade.pair, trade.amount) + self.update_trade_state(trade, oslo.order_id, co, stoploss_order=True) + except InvalidOrderException: + logger.exception(f"Could not cancel stoploss order {oslo.order_id} " + f"for pair {trade.pair}") return trade def get_valid_enter_price_and_stake( diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 48fed1782..e483dcc24 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -523,6 +523,16 @@ class LocalTrade: ] return len(open_orders_wo_sl) > 0 + @property + def open_sl_orders(self) -> List[Order]: + """ + All open stoploss orders for this trade + """ + return [ + o for o in self.orders + if o.ft_order_side in ['stoploss'] and o.ft_is_open + ] + @property def has_open_sl_orders(self) -> int: """ From 2565d509a614b0d0f6730b5d1f819d912c3818f5 Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 10:38:14 -0400 Subject: [PATCH 04/42] remove legacy sl management code from handle_insufficient_funds --- freqtrade/freqtradebot.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index ff3c36bf8..7d78779ff 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -439,10 +439,6 @@ class FreqtradeBot(LoggingMixin): try: fo = self.exchange.fetch_order_or_stoploss_order(order.order_id, order.ft_pair, order.ft_order_side == 'stoploss') - if order.ft_order_side == 'stoploss': - if fo and fo['status'] == 'open': - # Assume this as the open stoploss order - trade.stoploss_order_id = order.order_id if fo: logger.info(f"Found {order} for trade {trade}.") self.update_trade_state(trade, order.order_id, fo, From ea828ccb4a8164f96a0853ae7e9e0b5a06e52adb Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 10:40:23 -0400 Subject: [PATCH 05/42] remove legacy sl management code from create_stoploss_order --- freqtrade/freqtradebot.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 7d78779ff..a8f5665c8 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1189,7 +1189,6 @@ class FreqtradeBot(LoggingMixin): order_obj = Order.parse_from_ccxt_object(stoploss_order, trade.pair, 'stoploss', trade.amount, stop_price) trade.orders.append(order_obj) - trade.stoploss_order_id = str(stoploss_order['id']) trade.stoploss_last_update = datetime.now(timezone.utc) return True except InsufficientFundsError as e: @@ -1198,13 +1197,11 @@ class FreqtradeBot(LoggingMixin): self.handle_insufficient_funds(trade) except InvalidOrderException as e: - trade.stoploss_order_id = None logger.error(f'Unable to place a stoploss order on exchange. {e}') logger.warning('Exiting the trade forcefully') self.emergency_exit(trade, stop_price) except ExchangeError: - trade.stoploss_order_id = None logger.exception('Unable to place a stoploss order on exchange.') return False From 9d82de15d43dcb98e5c6c61912f0709315c874b2 Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 15:56:08 -0400 Subject: [PATCH 06/42] first updated proposition of handle_stoploss_on_exchange, add sl_orders helper --- freqtrade/freqtradebot.py | 100 ++++++++++++++++----------- freqtrade/persistence/trade_model.py | 10 +++ 2 files changed, 70 insertions(+), 40 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index a8f5665c8..3338805f7 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1215,27 +1215,30 @@ class FreqtradeBot(LoggingMixin): """ logger.debug('Handling stoploss on exchange %s ...', trade) - stoploss_order = None - try: - # First we check if there is already a stoploss on exchange - stoploss_order = self.exchange.fetch_stoploss_order( - trade.stoploss_order_id, trade.pair) if trade.stoploss_order_id else None - except InvalidOrderException as exception: - logger.warning('Unable to fetch stoploss order: %s', exception) + stoploss_orders = [] + for slo in trade.sl_orders: + stoploss_order = None + try: + # First we check if there is already a stoploss on exchange + stoploss_order = self.exchange.fetch_stoploss_order( + slo.order_id, trade.pair) if slo.order_id else None + except InvalidOrderException as exception: + logger.warning('Unable to fetch stoploss order: %s', exception) - if stoploss_order: - self.update_trade_state(trade, trade.stoploss_order_id, stoploss_order, - stoploss_order=True) + if stoploss_order: + stoploss_orders.append(stoploss_order) + self.update_trade_state(trade, slo.order_id, stoploss_order, + stoploss_order=True) - # We check if stoploss order is fulfilled - if stoploss_order and stoploss_order['status'] in ('closed', 'triggered'): - trade.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value - self.update_trade_state(trade, trade.stoploss_order_id, stoploss_order, - stoploss_order=True) - self._notify_exit(trade, "stoploss", True) - self.handle_protections(trade.pair, trade.trade_direction) - return True + # We check if stoploss order is fulfilled + if stoploss_order and stoploss_order['status'] in ('closed', 'triggered'): + trade.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value + self.update_trade_state(trade, slo.order_id, stoploss_order, + stoploss_order=True) + self._notify_exit(trade, "stoploss", True) + self.handle_protections(trade.pair, trade.trade_direction) + return True if trade.has_open_orders or not trade.is_open: # Trade has an open Buy or Sell order, Stoploss-handling can't happen in this case @@ -1244,7 +1247,7 @@ class FreqtradeBot(LoggingMixin): return False # If enter order is fulfilled but there is no stoploss, we add a stoploss on exchange - if not stoploss_order: + if len(stoploss_orders) == 0: stop_price = trade.stoploss_or_liquidation if self.edge: stoploss = self.edge.get_stoploss(pair=trade.pair) @@ -1258,27 +1261,7 @@ class FreqtradeBot(LoggingMixin): # in which case the trade will be closed - which we must check below. return False - # If stoploss order is canceled for some reason we add it again - if (trade.is_open - and stoploss_order - and stoploss_order['status'] in ('canceled', 'cancelled')): - if self.create_stoploss_order(trade=trade, stop_price=trade.stoploss_or_liquidation): - return False - else: - logger.warning('Stoploss order was cancelled, but unable to recreate one.') - - # Finally we check if stoploss on exchange should be moved up because of trailing. - # Triggered Orders are now real orders - so don't replace stoploss anymore - if ( - trade.is_open and stoploss_order - and stoploss_order.get('status_stop') != 'triggered' - and (self.config.get('trailing_stop', False) - or self.config.get('use_custom_stoploss', False)) - ): - # if trailing stoploss is enabled we check if stoploss value has changed - # in which case we cancel stoploss order and put another one with new - # value immediately - self.handle_trailing_stoploss_on_exchange(trade, stoploss_order) + self.manage_trade_stoploss_orders(trade, stoploss_orders) return False @@ -1314,6 +1297,43 @@ class FreqtradeBot(LoggingMixin): logger.warning(f"Could not create trailing stoploss order " f"for pair {trade.pair}.") + def manage_trade_stoploss_orders(self, trade, stoploss_orders): + """ + Check to see if stoploss on exchange should be updated + in case of trailing stoploss on exchange + :param trade: Corresponding Trade + :param stoploss_orders: Current on exchange stoploss orders + :return: None + """ + # If all stoploss orderd are canceled for some reason we add it again + canceled_sl_orders = [o for o in stoploss_orders if o.status in ['canceled', 'cancelled']] + if ( + trade.is_open and + len(stoploss_orders) > 0 and + len(stoploss_orders) == len(canceled_sl_orders) + ): + if self.create_stoploss_order(trade=trade, stop_price=trade.stoploss_or_liquidation): + return False + else: + logger.warning('All Stoploss orders are cancelled, but unable to recreate one.') + + active_sl_orders = [o for o in stoploss_orders if o not in canceled_sl_orders] + if len(active_sl_orders) > 0: + last_active_sl_order = active_sl_orders[-1] + # Finally we check if stoploss on exchange should be moved up because of trailing. + # Triggered Orders are now real orders - so don't replace stoploss anymore + if (trade.is_open and + last_active_sl_order.get('status_stop') != 'triggered' and + (self.config.get('trailing_stop', False) or + self.config.get('use_custom_stoploss', False))): + # if trailing stoploss is enabled we check if stoploss value has changed + # in which case we cancel stoploss order and put another one with new + # value immediately + self.handle_trailing_stoploss_on_exchange(trade, last_active_sl_order) + + # TODO cancel remaining_active_sl_orders active_sl_orders[:-1] + return + def manage_open_orders(self) -> None: """ Management of open orders on exchange. Unfilled orders might be cancelled if timeout diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index e483dcc24..ee6531030 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -544,6 +544,16 @@ class LocalTrade: ] return len(open_sl_orders) > 0 + @property + def sl_orders(self) -> List[Order]: + """ + All stoploss orders for this trade + """ + return [ + o for o in self.orders + if o.ft_order_side in ['stoploss'] + ] + @property def open_orders_ids(self) -> List[str]: open_orders_ids_wo_sl = [ From df8f1b93285b33599d1b4c26aeeeb575a2221afd Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 15:58:51 -0400 Subject: [PATCH 07/42] update manage_trade_stoploss_orders description --- freqtrade/freqtradebot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 3338805f7..6f5ba41ae 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1299,8 +1299,7 @@ class FreqtradeBot(LoggingMixin): def manage_trade_stoploss_orders(self, trade, stoploss_orders): """ - Check to see if stoploss on exchange should be updated - in case of trailing stoploss on exchange + Perform required actions acording to existing stoploss orders of trade :param trade: Corresponding Trade :param stoploss_orders: Current on exchange stoploss orders :return: None From 2bb68ca53d23634ba6ff5a8a7005bff6866d85c6 Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 16:08:40 -0400 Subject: [PATCH 08/42] remove stoploss_order_id from LocalTrade class --- freqtrade/persistence/trade_model.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index ee6531030..9c671d4b3 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -366,8 +366,6 @@ class LocalTrade: # percentage value of the initial stop loss initial_stop_loss_pct: Optional[float] = None is_stop_loss_trailing: bool = False - # stoploss order id which is on exchange - stoploss_order_id: Optional[str] = None # last update time of the stoploss order on exchange stoploss_last_update: Optional[datetime] = None # absolute value of the highest reached price From aaa82e1fa9c1f12db70526c463c55b1ac1c2221f Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 16:34:03 -0400 Subject: [PATCH 09/42] remove all occurence of stoploss_order_id in trade_model, update api schemas, update rpc_delete --- freqtrade/persistence/trade_model.py | 6 ------ freqtrade/rpc/api_server/api_schemas.py | 1 - freqtrade/rpc/rpc.py | 17 +++++++++-------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 9c671d4b3..26834ae48 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -636,7 +636,6 @@ class LocalTrade: 'stop_loss_abs': self.stop_loss, 'stop_loss_ratio': self.stop_loss_pct if self.stop_loss_pct else None, 'stop_loss_pct': (self.stop_loss_pct * 100) if self.stop_loss_pct else None, - 'stoploss_order_id': self.stoploss_order_id, 'stoploss_last_update': (self.stoploss_last_update.strftime(DATETIME_PRINT_FORMAT) if self.stoploss_last_update else None), 'stoploss_last_update_timestamp': int(self.stoploss_last_update.replace( @@ -787,7 +786,6 @@ class LocalTrade: logger.info(f'{order.order_type.upper()}_{payment} has been fulfilled for {self}.') elif order.ft_order_side == 'stoploss' and order.status not in ('open', ): - self.stoploss_order_id = None self.close_rate_requested = self.stop_loss self.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value if self.is_open and order.safe_filled > 0: @@ -1378,9 +1376,6 @@ class Trade(ModelBase, LocalTrade): Float(), nullable=True) # type: ignore is_stop_loss_trailing: Mapped[bool] = mapped_column( nullable=False, default=False) # type: ignore - # stoploss order id which is on exchange - stoploss_order_id: Mapped[Optional[str]] = mapped_column( - String(255), nullable=True, index=True) # type: ignore # last update time of the stoploss order on exchange stoploss_last_update: Mapped[Optional[datetime]] = mapped_column(nullable=True) # type: ignore # absolute value of the highest reached price @@ -1805,7 +1800,6 @@ class Trade(ModelBase, LocalTrade): exit_order_status=data["exit_order_status"], stop_loss=data["stop_loss_abs"], stop_loss_pct=data["stop_loss_ratio"], - stoploss_order_id=data["stoploss_order_id"], stoploss_last_update=( datetime.fromtimestamp(data["stoploss_last_update_timestamp"] // 1000, tz=timezone.utc) diff --git a/freqtrade/rpc/api_server/api_schemas.py b/freqtrade/rpc/api_server/api_schemas.py index 97f6251bc..4f154f3a3 100644 --- a/freqtrade/rpc/api_server/api_schemas.py +++ b/freqtrade/rpc/api_server/api_schemas.py @@ -299,7 +299,6 @@ class TradeSchema(BaseModel): stop_loss_abs: Optional[float] = None stop_loss_ratio: Optional[float] = None stop_loss_pct: Optional[float] = None - stoploss_order_id: Optional[str] = None stoploss_last_update: Optional[str] = None stoploss_last_update_timestamp: Optional[int] = None initial_stop_loss_abs: Optional[float] = None diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 0abac3975..3ee4bbc91 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -978,15 +978,16 @@ class RPC: except (ExchangeError): pass - # cancel stoploss on exchange ... + # cancel stoploss on exchange orders ... if (self._freqtrade.strategy.order_types.get('stoploss_on_exchange') - and trade.stoploss_order_id): - try: - self._freqtrade.exchange.cancel_stoploss_order(trade.stoploss_order_id, - trade.pair) - c_count += 1 - except (ExchangeError): - pass + and trade.has_open_sl_orders): + + for oslo in trade.open_sl_orders: + try: + self._freqtrade.exchange.cancel_stoploss_order(oslo.order_id, trade.pair) + c_count += 1 + except (ExchangeError): + pass trade.delete() self._freqtrade.wallets.update() From e8be011e2bdd6c2960fa223520b31d6d45cbbff8 Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Fri, 6 Oct 2023 17:01:12 -0400 Subject: [PATCH 10/42] update manage_trade_stoploss_orders: remove unrelevant TODO --- freqtrade/freqtradebot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 6f5ba41ae..834002495 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1330,7 +1330,6 @@ class FreqtradeBot(LoggingMixin): # value immediately self.handle_trailing_stoploss_on_exchange(trade, last_active_sl_order) - # TODO cancel remaining_active_sl_orders active_sl_orders[:-1] return def manage_open_orders(self) -> None: From c2b32769a19fb9dd04af654570b9ca6e74d4e496 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 28 Nov 2023 06:48:20 +0100 Subject: [PATCH 11/42] Remove further occurance in bot file --- freqtrade/freqtradebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 852e71a6b..69b81a67c 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1080,7 +1080,7 @@ class FreqtradeBot(LoggingMixin): if ( not trade.has_open_orders - and not trade.stoploss_order_id + and not trade.has_open_sl_orders and not self.wallets.check_exit_amount(trade) ): logger.warning( From 41e40e6214093ab89d2cebcbd27a0f5e2fd5eea3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 28 Nov 2023 06:48:27 +0100 Subject: [PATCH 12/42] Update some initial tests --- tests/conftest_trades.py | 1 - tests/conftest_trades_usdt.py | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/conftest_trades.py b/tests/conftest_trades.py index a2276ae16..9ac43d73d 100644 --- a/tests/conftest_trades.py +++ b/tests/conftest_trades.py @@ -266,7 +266,6 @@ def mock_trade_5(fee, is_short: bool): exchange='binance', strategy='SampleStrategy', enter_tag='TEST1', - stoploss_order_id=f'prod_stoploss_{direc(is_short)}_3455', timeframe=5, is_short=is_short, stop_loss_pct=0.10, diff --git a/tests/conftest_trades_usdt.py b/tests/conftest_trades_usdt.py index d73a53605..cf3109090 100644 --- a/tests/conftest_trades_usdt.py +++ b/tests/conftest_trades_usdt.py @@ -282,7 +282,6 @@ def mock_trade_usdt_5(fee, is_short: bool): open_rate=2.0, exchange='binance', strategy='SampleStrategy', - stoploss_order_id=f'prod_stoploss_3455_{direc(is_short)}', timeframe=5, is_short=is_short, ) From 13780d5963ce6ee74268da2076bb78a2788ca678 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 22 Dec 2023 17:22:33 +0100 Subject: [PATCH 13/42] Remove further usage --- freqtrade/persistence/migrations.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index bb6c04922..a5e3c4640 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -91,7 +91,6 @@ def migrate_trades_and_orders_table( is_stop_loss_trailing = get_column_def( cols, 'is_stop_loss_trailing', f'coalesce({stop_loss_pct}, 0.0) <> coalesce({initial_stop_loss_pct}, 0.0)') - stoploss_order_id = get_column_def(cols, 'stoploss_order_id', 'null') stoploss_last_update = get_column_def(cols, 'stoploss_last_update', 'null') max_rate = get_column_def(cols, 'max_rate', '0.0') min_rate = get_column_def(cols, 'min_rate', 'null') @@ -160,7 +159,7 @@ def migrate_trades_and_orders_table( open_rate_requested, close_rate, close_rate_requested, close_profit, stake_amount, amount, amount_requested, open_date, close_date, stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct, - is_stop_loss_trailing, stoploss_order_id, stoploss_last_update, + is_stop_loss_trailing, stoploss_last_update, max_rate, min_rate, exit_reason, exit_order_status, strategy, enter_tag, timeframe, open_trade_value, close_profit_abs, trading_mode, leverage, liquidation_price, is_short, @@ -180,7 +179,7 @@ def migrate_trades_and_orders_table( {initial_stop_loss} initial_stop_loss, {initial_stop_loss_pct} initial_stop_loss_pct, {is_stop_loss_trailing} is_stop_loss_trailing, - {stoploss_order_id} stoploss_order_id, {stoploss_last_update} stoploss_last_update, + {stoploss_last_update} stoploss_last_update, {max_rate} max_rate, {min_rate} min_rate, case when {exit_reason} = 'sell_signal' then 'exit_signal' when {exit_reason} = 'custom_sell' then 'custom_exit' From b33a9059abfa3d5e50957096613ae60c16f5bc12 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 22 Dec 2023 17:35:02 +0100 Subject: [PATCH 14/42] Fix some more tests --- freqtrade/persistence/migrations.py | 24 ++++++++++++------------ tests/persistence/test_persistence.py | 2 -- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index a5e3c4640..fc67448eb 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -280,19 +280,19 @@ def fix_old_dry_orders(engine): # - current Trade is closed # - current Order trade_id not equal to current Trade.id # - current Order not stoploss + # TODO: is this still necessary ? how can this be done now ? + # stmt = update(Order).where( + # Order.ft_is_open.is_(True), + # tuple_(Order.ft_trade_id, Order.order_id).not_in( + # select( + # Trade.id, Trade.stoploss_order_id + # ).where(Trade.stoploss_order_id.is_not(None)) + # ), + # Order.ft_order_side == 'stoploss', + # Order.order_id.like('dry%'), - stmt = update(Order).where( - Order.ft_is_open.is_(True), - tuple_(Order.ft_trade_id, Order.order_id).not_in( - select( - Trade.id, Trade.stoploss_order_id - ).where(Trade.stoploss_order_id.is_not(None)) - ), - Order.ft_order_side == 'stoploss', - Order.order_id.like('dry%'), - - ).values(ft_is_open=False) - connection.execute(stmt) + # ).values(ft_is_open=False) + # connection.execute(stmt) # Close dry-run orders for closed trades. stmt = update(Order).where( diff --git a/tests/persistence/test_persistence.py b/tests/persistence/test_persistence.py index 5829f8b71..95db7bc0f 100644 --- a/tests/persistence/test_persistence.py +++ b/tests/persistence/test_persistence.py @@ -1432,7 +1432,6 @@ def test_to_json(fee): 'stop_loss_abs': None, 'stop_loss_ratio': None, 'stop_loss_pct': None, - 'stoploss_order_id': None, 'stoploss_last_update': None, 'stoploss_last_update_timestamp': None, 'initial_stop_loss_abs': None, @@ -1500,7 +1499,6 @@ def test_to_json(fee): 'stop_loss_abs': None, 'stop_loss_pct': None, 'stop_loss_ratio': None, - 'stoploss_order_id': None, 'stoploss_last_update': None, 'stoploss_last_update_timestamp': None, 'initial_stop_loss_abs': None, From 8234879b583f41a151ba214df12a85d6740253e7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 22 Dec 2023 17:38:14 +0100 Subject: [PATCH 15/42] stoploss_order_id removal tests --- tests/rpc/test_rpc.py | 1 - tests/rpc/test_rpc_apiserver.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 7ea9dae89..ebbc62af6 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -63,7 +63,6 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: 'stop_loss_abs': 9.89e-06, 'stop_loss_pct': -10.0, 'stop_loss_ratio': -0.1, - 'stoploss_order_id': None, 'stoploss_last_update': ANY, 'stoploss_last_update_timestamp': ANY, 'initial_stop_loss_abs': 9.89e-06, diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 17b0399d9..f89b06d39 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1175,7 +1175,6 @@ def test_api_status(botclient, mocker, ticker, fee, markets, is_short, 'stop_loss_abs': ANY, 'stop_loss_pct': ANY, 'stop_loss_ratio': ANY, - 'stoploss_order_id': None, 'stoploss_last_update': ANY, 'stoploss_last_update_timestamp': ANY, 'initial_stop_loss_abs': 0.0, @@ -1379,7 +1378,6 @@ def test_api_force_entry(botclient, mocker, fee, endpoint): 'stop_loss_abs': None, 'stop_loss_pct': None, 'stop_loss_ratio': None, - 'stoploss_order_id': None, 'stoploss_last_update': None, 'stoploss_last_update_timestamp': None, 'initial_stop_loss_abs': None, From 15058d3ce675deac6dd3e6ac7e24bed89643c984 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:16:13 +0100 Subject: [PATCH 16/42] Add type hints to manage_trade_orders, fix content ... --- freqtrade/freqtradebot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 816d35cdc..40cd6cecd 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1299,7 +1299,7 @@ class FreqtradeBot(LoggingMixin): logger.warning(f"Could not create trailing stoploss order " f"for pair {trade.pair}.") - def manage_trade_stoploss_orders(self, trade, stoploss_orders): + def manage_trade_stoploss_orders(self, trade: Trade, stoploss_orders: Dict): """ Perform required actions acording to existing stoploss orders of trade :param trade: Corresponding Trade @@ -1307,7 +1307,7 @@ class FreqtradeBot(LoggingMixin): :return: None """ # If all stoploss orderd are canceled for some reason we add it again - canceled_sl_orders = [o for o in stoploss_orders if o.status in ['canceled', 'cancelled']] + canceled_sl_orders = [o for o in stoploss_orders if o['status'] in ['canceled', 'cancelled']] if ( trade.is_open and len(stoploss_orders) > 0 and From 28e2bfaf1cbe374b04a9088d07ce715801429b06 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:25:35 +0100 Subject: [PATCH 17/42] Fix types of "has" calls --- freqtrade/persistence/trade_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 13578ff6a..a4a785c55 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -512,7 +512,7 @@ class LocalTrade: return [o for o in self.orders if o.ft_is_open and o.ft_order_side != 'stoploss'] @property - def has_open_orders(self) -> int: + def has_open_orders(self) -> bool: """ True if there are open orders for this trade excluding stoploss orders """ @@ -533,7 +533,7 @@ class LocalTrade: ] @property - def has_open_sl_orders(self) -> int: + def has_open_sl_orders(self) -> bool: """ True if there are open stoploss orders for this trade """ From c35b308adabcf7c73a77d23d95df77cdbc79f1a2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:25:39 +0100 Subject: [PATCH 18/42] Fix some tests --- tests/test_freqtradebot.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 06d40dfb0..61c63d064 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1118,12 +1118,11 @@ def test_add_stoploss_on_exchange(mocker, default_conf_usdt, limit_order, is_sho freqtrade.enter_positions() trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short - trade.stoploss_order_id = None trade.is_open = True trades = [trade] freqtrade.exit_positions(trades) - assert trade.stoploss_order_id == '13434334' + assert trade.has_open_sl_orders is True assert stoploss.call_count == 1 assert trade.is_open is True @@ -1535,7 +1534,7 @@ def test_create_stoploss_order_invalid_order( caplog.clear() rpc_mock.reset_mock() freqtrade.create_stoploss_order(trade, 200) - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False assert trade.exit_reason == ExitType.EMERGENCY_EXIT.value assert log_has("Unable to place a stoploss order on exchange. ", caplog) assert log_has("Exiting the trade forcefully", caplog) @@ -1589,14 +1588,13 @@ def test_create_stoploss_order_insufficient_funds( caplog.clear() freqtrade.create_stoploss_order(trade, 200) # stoploss_orderid was empty before - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False assert mock_insuf.call_count == 1 mock_insuf.reset_mock() - trade.stoploss_order_id = 'stoploss_orderid' freqtrade.create_stoploss_order(trade, 200) # No change to stoploss-orderid - assert trade.stoploss_order_id == 'stoploss_orderid' + assert trade.has_open_sl_orders is False assert mock_insuf.call_count == 1 @@ -5679,7 +5677,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap trade = trades[1] reset_open_orders(trade) assert not trade.has_open_orders - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False freqtrade.handle_insufficient_funds(trade) order = trade.orders[0] @@ -5689,7 +5687,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap assert mock_uts.call_count == 0 # No change to orderid - as update_trade_state is mocked assert not trade.has_open_orders - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False caplog.clear() mock_fo.reset_mock() @@ -5700,7 +5698,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap # This part in not relevant anymore # assert not trade.has_open_orders - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False freqtrade.handle_insufficient_funds(trade) order = mock_order_4(is_short=is_short) @@ -5708,8 +5706,8 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap assert mock_fo.call_count == 1 assert mock_uts.call_count == 1 # Found open buy order - assert trade.has_open_orders - assert trade.stoploss_order_id is None + assert trade.has_open_orders is True + assert trade.has_open_sl_orders is False caplog.clear() mock_fo.reset_mock() @@ -5718,7 +5716,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap trade = trades[4] reset_open_orders(trade) assert not trade.has_open_orders - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders freqtrade.handle_insufficient_funds(trade) order = mock_order_5_stoploss(is_short=is_short) @@ -5727,7 +5725,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap assert mock_uts.call_count == 2 # stoploss_order_id is "refound" and added to the trade assert not trade.has_open_orders - assert trade.stoploss_order_id is not None + assert trade.has_open_sl_orders is True caplog.clear() mock_fo.reset_mock() @@ -5738,7 +5736,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap reset_open_orders(trade) # This part in not relevant anymore # assert not trade.has_open_orders - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False freqtrade.handle_insufficient_funds(trade) order = mock_order_6_sell(is_short=is_short) @@ -5747,7 +5745,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap assert mock_uts.call_count == 1 # sell-orderid is "refound" and added to the trade assert trade.open_orders_ids[0] == order['id'] - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False caplog.clear() From 6427144934983ee5c1ba25ac7277c0d6440e236e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:27:32 +0100 Subject: [PATCH 19/42] Fix stoploss test --- tests/test_freqtradebot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 61c63d064..a2337fa2a 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -4130,11 +4130,11 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit( freqtrade.manage_open_orders() trade = Trade.session.scalars(select(Trade)).first() trades = [trade] - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False freqtrade.exit_positions(trades) assert trade - assert trade.stoploss_order_id == '123' + assert trade.has_open_sl_orders is True assert not trade.has_open_orders # Assuming stoploss on exchange is hit @@ -4161,7 +4161,7 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit( mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_executed) freqtrade.exit_positions(trades) - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False assert trade.is_open is False assert trade.exit_reason == ExitType.STOPLOSS_ON_EXCHANGE.value assert rpc_mock.call_count == 4 From c6ffe82a7a72f1293b901d4685685dd0be429cdc Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:29:45 +0100 Subject: [PATCH 20/42] Update more tests --- tests/test_freqtradebot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index a2337fa2a..ca8b6a4b9 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1345,11 +1345,11 @@ def test_handle_stoploss_on_exchange_partial( trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True - trade.stoploss_order_id = None assert freqtrade.handle_stoploss_on_exchange(trade) is False assert stoploss.call_count == 1 - assert trade.stoploss_order_id == "101" + assert trade.has_open_sl_orders is True + assert trade.open_sl_orders[-1].order_id == "101" assert trade.amount == 30 stop_order_dict.update({'id': "102"}) # Stoploss on exchange is cancelled on exchange, but filled partially. @@ -1369,7 +1369,7 @@ def test_handle_stoploss_on_exchange_partial( # Stoploss filled partially ... assert trade.amount == 15 - assert trade.stoploss_order_id == "102" + assert trade.open_sl_orders[-1].order_id == "102" @pytest.mark.parametrize("is_short", [False, True]) From cbfebd397c1149e9aa8543b9e7995788ba866de0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:47:33 +0100 Subject: [PATCH 21/42] Use a trade for test that actually has an open stop order --- tests/persistence/test_trade_fromjson.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/persistence/test_trade_fromjson.py b/tests/persistence/test_trade_fromjson.py index bb5e77f22..302a81c54 100644 --- a/tests/persistence/test_trade_fromjson.py +++ b/tests/persistence/test_trade_fromjson.py @@ -54,7 +54,6 @@ def test_trade_fromjson(): "stop_loss_abs": 0.1981, "stop_loss_ratio": -0.216, "stop_loss_pct": -21.6, - "stoploss_order_id": null, "stoploss_last_update": "2022-10-18 09:13:42", "stoploss_last_update_timestamp": 1666077222000, "initial_stop_loss_abs": 0.1981, From ae3f62cf9be4ae875de3ca48274c2146e60217e8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:50:42 +0100 Subject: [PATCH 22/42] Fix RPC tests --- tests/rpc/test_rpc.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 869150e3d..4d43660e5 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -354,7 +354,6 @@ def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog, is_short): rpc._rpc_delete('200') trades = Trade.session.scalars(select(Trade)).all() - trades[2].stoploss_order_id = '102' trades[2].orders.append( Order( ft_order_side='stoploss', From 1db4732648de9fb269def2a88402f9da3af2fcab Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 09:57:05 +0100 Subject: [PATCH 23/42] Fix some more tests --- tests/test_freqtradebot.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index ca8b6a4b9..3467f037e 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1403,11 +1403,11 @@ def test_handle_stoploss_on_exchange_partial_cancel_here( trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True - trade.stoploss_order_id = None assert freqtrade.handle_stoploss_on_exchange(trade) is False assert stoploss.call_count == 1 - assert trade.stoploss_order_id == "101" + assert trade.has_open_sl_orders is True + assert trade.open_sl_orders[-1].order_id == "101" assert trade.amount == 30 stop_order_dict.update({'id': "102"}) # Stoploss on exchange is open. @@ -1440,7 +1440,8 @@ def test_handle_stoploss_on_exchange_partial_cancel_here( # Canceled Stoploss filled partially ... assert log_has_re('Cancelling current stoploss on exchange.*', caplog) - assert trade.stoploss_order_id == "102" + assert trade.has_open_sl_orders is True + assert trade.open_sl_orders[-1].order_id == "102" assert trade.amount == 15 @@ -4027,7 +4028,17 @@ def test_execute_trade_exit_sloe_cancel_exception( PairLock.session = MagicMock() freqtrade.config['dry_run'] = False - trade.stoploss_order_id = "abcd" + trade.orders.append( + Order( + ft_order_side='stoploss', + ft_pair=trade.pair, + ft_is_open=True, + ft_amount=trade.amount, + ft_price=trade.stop_loss, + order_id='abcd', + status='open', + ) + ) freqtrade.execute_trade_exit(trade=trade, limit=1234, exit_check=ExitCheckTuple(exit_type=ExitType.STOP_LOSS)) From 33bd433c2299cd98a9c7dead01a47c317e555d4b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 10:31:03 +0100 Subject: [PATCH 24/42] Don't run against all orders, only consider open sl orders. --- freqtrade/freqtradebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 40cd6cecd..fec2ed6a4 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1219,7 +1219,7 @@ class FreqtradeBot(LoggingMixin): logger.debug('Handling stoploss on exchange %s ...', trade) stoploss_orders = [] - for slo in trade.sl_orders: + for slo in trade.open_sl_orders: stoploss_order = None try: # First we check if there is already a stoploss on exchange From 600e311b3ee116555d62fef0d6ab1dd4cfe58e0b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 10:35:30 +0100 Subject: [PATCH 25/42] Fix test test_handle_stoploss_on_exchange_custom_stop --- tests/test_freqtradebot.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 3467f037e..95635edff 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1866,6 +1866,7 @@ def test_handle_stoploss_on_exchange_custom_stop( exit_order, ]), get_fee=fee, + is_cancel_order_result_suitable=MagicMock(return_value=True), ) mocker.patch.multiple( EXMS, @@ -1896,7 +1897,6 @@ def test_handle_stoploss_on_exchange_custom_stop( trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True - trade.stoploss_order_id = '100' trade.stoploss_last_update = dt_now() - timedelta(minutes=601) trade.orders.append( Order( @@ -1908,8 +1908,8 @@ def test_handle_stoploss_on_exchange_custom_stop( order_id='100', ) ) - - stoploss_order_hanging = MagicMock(return_value={ + Trade.commit() + slo = { 'id': '100', 'status': 'open', 'type': 'stop_loss_limit', @@ -1918,9 +1918,17 @@ def test_handle_stoploss_on_exchange_custom_stop( 'info': { 'stopPrice': '2.0805' } - }) + } + slo_canceled = deepcopy(slo) + slo_canceled.update({'status': 'canceled'}) - mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hanging) + def fetch_stoploss_order_mock(order_id, *args, **kwargs): + x = deepcopy(slo) + x['id'] = order_id + return x + + mocker.patch(f'{EXMS}.fetch_stoploss_order', MagicMock(fetch_stoploss_order_mock)) + mocker.patch(f'{EXMS}.cancel_stoploss_order', return_value=slo_canceled) assert freqtrade.handle_trade(trade) is False assert freqtrade.handle_stoploss_on_exchange(trade) is False @@ -1939,7 +1947,6 @@ def test_handle_stoploss_on_exchange_custom_stop( stoploss_order_mock = MagicMock(return_value={'id': 'so1', 'status': 'open'}) mocker.patch(f'{EXMS}.cancel_stoploss_order', cancel_order_mock) mocker.patch(f'{EXMS}.create_stoploss', stoploss_order_mock) - trade.stoploss_order_id = '100' # stoploss should not be updated as the interval is 60 seconds assert freqtrade.handle_trade(trade) is False @@ -1953,10 +1960,12 @@ def test_handle_stoploss_on_exchange_custom_stop( # setting stoploss_on_exchange_interval to 0 seconds freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 0 + cancel_order_mock.assert_not_called() + stoploss_order_mock.assert_not_called() assert freqtrade.handle_stoploss_on_exchange(trade) is False - cancel_order_mock.assert_called_once_with('100', 'ETH/USDT') + cancel_order_mock.assert_called_once_with('13434334', 'ETH/USDT') # Long uses modified ask - offset, short modified bid + offset stoploss_order_mock.assert_called_once_with( amount=pytest.approx(trade.amount), From 68f9402384aad8db16418abd5c32c0e364f5eb26 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 10:44:21 +0100 Subject: [PATCH 26/42] Fix further test --- tests/test_freqtradebot.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 95635edff..a37d88fd6 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1759,9 +1759,6 @@ def test_handle_stoploss_on_exchange_trailing_error( {'id': exit_order['id']}, ]), get_fee=fee, - ) - mocker.patch.multiple( - EXMS, create_stoploss=stoploss, stoploss_adjust=MagicMock(return_value=True), ) @@ -1783,10 +1780,8 @@ def test_handle_stoploss_on_exchange_trailing_error( trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True - trade.stoploss_order_id = "abcd" trade.stop_loss = 0.2 trade.stoploss_last_update = (dt_now() - timedelta(minutes=601)).replace(tzinfo=None) - trade.is_short = is_short stoploss_order_hanging = { 'id': "abcd", @@ -1798,6 +1793,16 @@ def test_handle_stoploss_on_exchange_trailing_error( 'stopPrice': '0.1' } } + trade.orders.append( + Order( + ft_order_side='stoploss', + ft_pair=trade.pair, + ft_is_open=True, + ft_amount=trade.amount, + ft_price=3, + order_id='abcd', + ) + ) mocker.patch(f'{EXMS}.cancel_stoploss_order', side_effect=InvalidOrderException()) mocker.patch(f'{EXMS}.fetch_stoploss_order', @@ -1807,6 +1812,8 @@ def test_handle_stoploss_on_exchange_trailing_error( # Still try to create order assert stoploss.call_count == 1 + # TODO: Is this actually correct ? This will create a new order every time, + assert len(trade.open_sl_orders) == 2 # Fail creating stoploss order trade.stoploss_last_update = dt_now() - timedelta(minutes=601) @@ -1814,7 +1821,7 @@ def test_handle_stoploss_on_exchange_trailing_error( cancel_mock = mocker.patch(f'{EXMS}.cancel_stoploss_order') mocker.patch(f'{EXMS}.create_stoploss', side_effect=ExchangeError()) freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging) - assert cancel_mock.call_count == 1 + assert cancel_mock.call_count == 2 assert log_has_re(r"Could not create trailing stoploss order for pair ETH/USDT\..*", caplog) From a39b329e3baa84dbfe27658d087ccbefb7d2500e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 23 Dec 2023 15:30:24 +0100 Subject: [PATCH 27/42] Fix line-length --- freqtrade/freqtradebot.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index fec2ed6a4..e414f9e82 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1307,12 +1307,13 @@ class FreqtradeBot(LoggingMixin): :return: None """ # If all stoploss orderd are canceled for some reason we add it again - canceled_sl_orders = [o for o in stoploss_orders if o['status'] in ['canceled', 'cancelled']] + canceled_sl_orders = [o for o in stoploss_orders + if o['status'] in ('canceled', 'cancelled')] if ( - trade.is_open and - len(stoploss_orders) > 0 and - len(stoploss_orders) == len(canceled_sl_orders) - ): + trade.is_open and + len(stoploss_orders) > 0 and + len(stoploss_orders) == len(canceled_sl_orders) + ): if self.create_stoploss_order(trade=trade, stop_price=trade.stoploss_or_liquidation): return False else: From f0073078e9a4950f84e78cc02d03b6b124055d79 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 2 Jan 2024 11:27:42 +0100 Subject: [PATCH 28/42] Fix stop order test --- tests/test_freqtradebot.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index a37d88fd6..bafb13a9f 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1468,7 +1468,7 @@ def test_handle_sle_cancel_cant_recreate(mocker, default_conf_usdt, fee, caplog, ) mocker.patch.multiple( EXMS, - fetch_stoploss_order=MagicMock(return_value={'status': 'canceled', 'id': 100}), + fetch_stoploss_order=MagicMock(return_value={'status': 'canceled', 'id': '100'}), create_stoploss=MagicMock(side_effect=ExchangeError()), ) freqtrade = FreqtradeBot(default_conf_usdt) @@ -1478,7 +1478,6 @@ def test_handle_sle_cancel_cant_recreate(mocker, default_conf_usdt, fee, caplog, trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short trade.is_open = True - trade.stoploss_order_id = "100" trade.orders.append( Order( ft_order_side='stoploss', @@ -1493,8 +1492,8 @@ def test_handle_sle_cancel_cant_recreate(mocker, default_conf_usdt, fee, caplog, assert trade assert freqtrade.handle_stoploss_on_exchange(trade) is False - assert log_has_re(r'Stoploss order was cancelled, but unable to recreate one.*', caplog) - assert trade.stoploss_order_id is None + assert log_has_re(r'All Stoploss orders are cancelled, but unable to recreate one\.', caplog) + assert trade.has_open_sl_orders is False assert trade.is_open is True From 501e256c587456d7c19eb822288a08a1969617d7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 2 Jan 2024 16:25:42 +0100 Subject: [PATCH 29/42] Fix further stoploss test --- tests/test_freqtradebot.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 703c06118..763ca8b48 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1656,7 +1656,7 @@ def test_handle_stoploss_on_exchange_trailing( trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True - trade.stoploss_order_id = '100' + assert trade.has_open_sl_orders is False trade.stoploss_last_update = dt_now() - timedelta(minutes=20) trade.orders.append( Order( @@ -1669,24 +1669,31 @@ def test_handle_stoploss_on_exchange_trailing( ) ) - stoploss_order_hanging = MagicMock(return_value={ + stoploss_order_hanging = { 'id': '100', 'status': 'open', 'type': 'stop_loss_limit', 'price': hang_price, 'average': 2, + 'fee': {}, + 'amount': 0, 'info': { 'stopPrice': stop_price[0] } - }) + } + stoploss_order_cancel = deepcopy(stoploss_order_hanging) + stoploss_order_cancel['status'] = 'canceled' - mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hanging) + mocker.patch(f'{EXMS}.fetch_stoploss_order', return_value=stoploss_order_hanging) + mocker.patch(f'{EXMS}.cancel_stoploss_order', return_value=stoploss_order_cancel) # stoploss initially at 5% assert freqtrade.handle_trade(trade) is False assert freqtrade.handle_stoploss_on_exchange(trade) is False - assert trade.stoploss_order_id == '13434334' + assert len(trade.open_sl_orders) == 1 + + assert trade.open_sl_orders[-1].order_id == '13434334' # price jumped 2x mocker.patch( @@ -1698,14 +1705,17 @@ def test_handle_stoploss_on_exchange_trailing( }) ) - cancel_order_mock = MagicMock() + cancel_order_mock = MagicMock(return_value={ + 'id': '13434334', 'status': 'canceled', 'fee': {}, 'amount': trade.amount}) stoploss_order_mock = MagicMock(return_value={'id': 'so1', 'status': 'open'}) + mocker.patch(f'{EXMS}.fetch_stoploss_order') mocker.patch(f'{EXMS}.cancel_stoploss_order', cancel_order_mock) mocker.patch(f'{EXMS}.create_stoploss', stoploss_order_mock) # stoploss should not be updated as the interval is 60 seconds assert freqtrade.handle_trade(trade) is False assert freqtrade.handle_stoploss_on_exchange(trade) is False + assert len(trade.open_sl_orders) == 1 cancel_order_mock.assert_not_called() stoploss_order_mock.assert_not_called() @@ -1736,8 +1746,14 @@ def test_handle_stoploss_on_exchange_trailing( 'last': bid[1], }) ) + mocker.patch(f'{EXMS}.cancel_stoploss_order_with_result', + return_value={'id': 'so1', 'status': 'canceled'}) + assert len(trade.open_sl_orders) == 1 + assert trade.open_sl_orders[-1].order_id == 'so1' + assert freqtrade.handle_trade(trade) is True - assert trade.stoploss_order_id is None + assert trade.is_open is False + assert trade.has_open_sl_orders is False @pytest.mark.parametrize("is_short", [False, True]) From 206809d2e7a8e9876bec38c6aeefd9bf02d3d3c8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 2 Jan 2024 17:05:21 +0100 Subject: [PATCH 30/42] Update emergency sell test --- tests/test_freqtradebot.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index d07c1db23..33b0828a9 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1279,7 +1279,7 @@ def test_handle_stoploss_on_exchange_emergency(mocker, default_conf_usdt, fee, i trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short assert trade.is_open - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False # emergency exit triggered # Trailing stop should not act anymore @@ -1294,7 +1294,6 @@ def test_handle_stoploss_on_exchange_emergency(mocker, default_conf_usdt, fee, i 'remaining': enter_order['amount'], 'info': {'stopPrice': 22}, }]) - trade.stoploss_order_id = "107" trade.stoploss_last_update = dt_now() - timedelta(hours=1) trade.stop_loss = 24 trade.exit_reason = None @@ -1311,14 +1310,14 @@ def test_handle_stoploss_on_exchange_emergency(mocker, default_conf_usdt, fee, i ) freqtrade.config['trailing_stop'] = True stoploss = MagicMock(side_effect=InvalidOrderException()) - + assert trade.has_open_sl_orders is True Trade.commit() mocker.patch(f'{EXMS}.cancel_stoploss_order_with_result', side_effect=InvalidOrderException()) mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_cancelled) mocker.patch(f'{EXMS}.create_stoploss', stoploss) assert freqtrade.handle_stoploss_on_exchange(trade) is False - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False assert trade.is_open is False assert trade.exit_reason == str(ExitType.EMERGENCY_EXIT) From e199083287f9311d11400699b549620e613af6f2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 2 Jan 2024 17:10:33 +0100 Subject: [PATCH 31/42] Fix test ... --- tests/test_freqtradebot.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 33b0828a9..6738fc5ae 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1161,11 +1161,11 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ trade = Trade.session.scalars(select(Trade)).first() assert trade.is_short == is_short assert trade.is_open - assert trade.stoploss_order_id is None + assert trade.has_open_sl_orders is False assert freqtrade.handle_stoploss_on_exchange(trade) is False assert stoploss.call_count == 1 - assert trade.stoploss_order_id == "13434334" + assert trade.open_sl_orders[-1].order_id == "13434334" # Second case: when stoploss is set but it is not yet hit # should do nothing and return false @@ -1176,7 +1176,8 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ assert freqtrade.handle_stoploss_on_exchange(trade) is False hanging_stoploss_order.assert_called_once_with('13434334', trade.pair) - assert trade.stoploss_order_id == "13434334" + assert len(trade.open_sl_orders) == 1 + assert trade.open_sl_orders[-1].order_id == "13434334" # Third case: when stoploss was set but it was canceled for some reason # should set a stoploss immediately and return False @@ -1192,7 +1193,8 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ assert freqtrade.handle_stoploss_on_exchange(trade) is False assert stoploss.call_count == 1 - assert trade.stoploss_order_id == "103_1" + assert len(trade.open_sl_orders) == 1 + assert trade.open_sl_orders[-1].order_id == "103_1" assert trade.amount == amount_before # Fourth case: when stoploss is set and it is hit @@ -1218,7 +1220,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hit) assert freqtrade.handle_stoploss_on_exchange(trade) is True assert log_has_re(r'STOP_LOSS_LIMIT is hit for Trade\(id=1, .*\)\.', caplog) - assert trade.stoploss_order_id is None + assert len(trade.open_sl_orders) == 0 assert trade.is_open is False caplog.clear() @@ -1226,26 +1228,27 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ trade.is_open = True freqtrade.handle_stoploss_on_exchange(trade) assert log_has('Unable to place a stoploss order on exchange.', caplog) - assert trade.stoploss_order_id is None + assert len(trade.open_sl_orders) == 0 # Fifth case: fetch_order returns InvalidOrder # It should try to add stoploss order stop_order_dict.update({'id': "105"}) - trade.stoploss_order_id = "105" stoploss.reset_mock() mocker.patch(f'{EXMS}.fetch_stoploss_order', side_effect=InvalidOrderException()) mocker.patch(f'{EXMS}.create_stoploss', stoploss) freqtrade.handle_stoploss_on_exchange(trade) + assert len(trade.open_sl_orders) == 1 assert stoploss.call_count == 1 # Sixth case: Closed Trade # Should not create new order - trade.stoploss_order_id = None trade.is_open = False + trade.open_sl_orders[-1].ft_is_open = False stoploss.reset_mock() mocker.patch(f'{EXMS}.fetch_order') mocker.patch(f'{EXMS}.create_stoploss', stoploss) assert freqtrade.handle_stoploss_on_exchange(trade) is False + assert trade.has_open_sl_orders is False assert stoploss.call_count == 0 From 14660f54f8fca7767155746ab3de05bca99ca9fd Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 2 Jan 2024 19:22:16 +0100 Subject: [PATCH 32/42] Remove duplicate call to update_trade_state --- freqtrade/freqtradebot.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 0b30d89a4..3b01f7756 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1229,8 +1229,6 @@ class FreqtradeBot(LoggingMixin): # We check if stoploss order is fulfilled if stoploss_order and stoploss_order['status'] in ('closed', 'triggered'): trade.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value - self.update_trade_state(trade, slo.order_id, stoploss_order, - stoploss_order=True) self._notify_exit(trade, "stoploss", True) self.handle_protections(trade.pair, trade.trade_direction) return True From dc9c4da95e8f38fb8a249cea6be068b6bf68eb2c Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 2 Jan 2024 19:22:46 +0100 Subject: [PATCH 33/42] Improve integration test stability --- tests/test_integration.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_integration.py b/tests/test_integration.py index 2e7f38fc8..ffb955f11 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -49,7 +49,7 @@ def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee, stoploss_order_closed['filled'] = stoploss_order_closed['amount'] # Sell first trade based on stoploss, keep 2nd and 3rd trade open - stop_orders = [stoploss_order_closed, stoploss_order_open, stoploss_order_open] + stop_orders = [stoploss_order_closed, stoploss_order_open.copy(), stoploss_order_open.copy()] stoploss_order_mock = MagicMock( side_effect=stop_orders) # Sell 3rd trade (not called for the first trade) @@ -100,9 +100,10 @@ def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee, stop_order = stop_orders[idx] stop_order['id'] = f"stop{idx}" oobj = Order.parse_from_ccxt_object(stop_order, trade.pair, 'stoploss') + oobj.ft_is_open = True trade.orders.append(oobj) - trade.stoploss_order_id = f"stop{idx}" + assert len(trade.open_sl_orders) == 1 n = freqtrade.exit_positions(trades) assert n == 2 @@ -113,6 +114,7 @@ def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee, # Only order for 3rd trade needs to be cancelled assert cancel_order_mock.call_count == 1 + assert stoploss_order_mock.call_count == 3 # Wallets must be updated between stoploss cancellation and selling, and will be updated again # during update_trade_state assert wallets_mock.call_count == 4 From 9f3c6f2dcc2fc4982823a6b581b52c3bc1ae9c38 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 06:48:32 +0100 Subject: [PATCH 34/42] Fix some tests and comments --- tests/test_freqtradebot.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 6738fc5ae..57cd1a2f5 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1198,8 +1198,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ assert trade.amount == amount_before # Fourth case: when stoploss is set and it is hit - # should unset stoploss_order_id and return true - # as a trade actually happened + # should return true as a trade actually happened caplog.clear() stop_order_dict.update({'id': "103_1"}) @@ -1871,7 +1870,6 @@ def test_stoploss_on_exchange_price_rounding( price_to_precision=price_mock, ) freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) - open_trade_usdt.stoploss_order_id = '13434334' open_trade_usdt.stop_loss = 222.55 freqtrade.handle_trailing_stoploss_on_exchange(open_trade_usdt, {}) @@ -2078,7 +2076,7 @@ def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, limit_orde freqtrade.enter_positions() trade = Trade.session.scalars(select(Trade)).first() trade.is_open = True - trade.stoploss_order_id = '100' + trade.stoploss_last_update = dt_now() trade.orders.append( Order( @@ -4194,8 +4192,7 @@ def test_may_execute_trade_exit_after_stoploss_on_exchange_hit( assert not trade.has_open_orders # Assuming stoploss on exchange is hit - # stoploss_order_id should become None - # and trade should be sold at the price of stoploss + # trade should be sold at the price of stoploss, with exit_reaeon STOPLOSS_ON_EXCHANGE stoploss_executed = MagicMock(return_value={ "id": "123", "timestamp": 1542707426845, @@ -5721,7 +5718,6 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap def reset_open_orders(trade): - trade.stoploss_order_id = None trade.is_short = is_short create_mock_trades(fee, is_short=is_short) @@ -5779,7 +5775,7 @@ def test_handle_insufficient_funds(mocker, default_conf_usdt, fee, is_short, cap assert log_has_re(r"Trying to refind Order\(.*", caplog) assert mock_fo.call_count == 1 assert mock_uts.call_count == 2 - # stoploss_order_id is "refound" and added to the trade + # stoploss order is "refound" and added to the trade assert not trade.has_open_orders assert trade.has_open_sl_orders is True From 59b34865740cdeb170971034c58cf1f8c7914022 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 06:49:49 +0100 Subject: [PATCH 35/42] Update migrations --- freqtrade/persistence/migrations.py | 21 +++++++++------------ tests/persistence/test_migrations.py | 6 +++--- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index fc67448eb..2970da918 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -276,23 +276,20 @@ def fix_old_dry_orders(engine): with engine.begin() as connection: # Update current dry-run Orders where + # - stoploss order is Open (will be replaced eventually) + # 2nd query: # - current Order is open # - current Trade is closed # - current Order trade_id not equal to current Trade.id # - current Order not stoploss - # TODO: is this still necessary ? how can this be done now ? - # stmt = update(Order).where( - # Order.ft_is_open.is_(True), - # tuple_(Order.ft_trade_id, Order.order_id).not_in( - # select( - # Trade.id, Trade.stoploss_order_id - # ).where(Trade.stoploss_order_id.is_not(None)) - # ), - # Order.ft_order_side == 'stoploss', - # Order.order_id.like('dry%'), - # ).values(ft_is_open=False) - # connection.execute(stmt) + stmt = update(Order).where( + Order.ft_is_open.is_(True), + Order.ft_order_side == 'stoploss', + Order.order_id.like('dry%'), + + ).values(ft_is_open=False) + connection.execute(stmt) # Close dry-run orders for closed trades. stmt = update(Order).where( diff --git a/tests/persistence/test_migrations.py b/tests/persistence/test_migrations.py index f2bb0b2f1..6ef098cb3 100644 --- a/tests/persistence/test_migrations.py +++ b/tests/persistence/test_migrations.py @@ -74,7 +74,7 @@ def test_init_dryrun_db(default_conf, tmpdir): assert Path(filename).is_file() -def test_migrate_new(mocker, default_conf, fee, caplog): +def test_migrate(mocker, default_conf, fee, caplog): """ Test Database migration (starting with new pairformat) """ @@ -277,7 +277,6 @@ def test_migrate_new(mocker, default_conf, fee, caplog): assert trade.exit_reason is None assert trade.strategy is None assert trade.timeframe == '5m' - assert trade.stoploss_order_id == 'dry_stop_order_id222' assert trade.stoploss_last_update is None assert log_has("trying trades_bak1", caplog) assert log_has("trying trades_bak2", caplog) @@ -294,9 +293,10 @@ def test_migrate_new(mocker, default_conf, fee, caplog): assert orders[0].order_id == 'dry_buy_order' assert orders[0].ft_order_side == 'buy' + # All dry-run stoploss orders will be closed assert orders[-1].order_id == 'dry_stop_order_id222' assert orders[-1].ft_order_side == 'stoploss' - assert orders[-1].ft_is_open is True + assert orders[-1].ft_is_open is False assert orders[1].order_id == 'dry_buy_order22' assert orders[1].ft_order_side == 'buy' From b9a43b8e248a53dd28a26b9eeedcfced61f8bf92 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 07:12:49 +0100 Subject: [PATCH 36/42] Don't store 'stoploss_last_updated' explicitly it can easily be derived from the very last stoploss order. --- freqtrade/freqtradebot.py | 1 - freqtrade/persistence/migrations.py | 4 +--- freqtrade/persistence/trade_model.py | 20 ++++++-------------- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 3b01f7756..26631eb30 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1184,7 +1184,6 @@ class FreqtradeBot(LoggingMixin): order_obj = Order.parse_from_ccxt_object(stoploss_order, trade.pair, 'stoploss', trade.amount, stop_price) trade.orders.append(order_obj) - trade.stoploss_last_update = datetime.now(timezone.utc) return True except InsufficientFundsError as e: logger.warning(f"Unable to place stoploss order {e}.") diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index 2970da918..eb55cf455 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -91,7 +91,6 @@ def migrate_trades_and_orders_table( is_stop_loss_trailing = get_column_def( cols, 'is_stop_loss_trailing', f'coalesce({stop_loss_pct}, 0.0) <> coalesce({initial_stop_loss_pct}, 0.0)') - stoploss_last_update = get_column_def(cols, 'stoploss_last_update', 'null') max_rate = get_column_def(cols, 'max_rate', '0.0') min_rate = get_column_def(cols, 'min_rate', 'null') exit_reason = get_column_def(cols, 'sell_reason', get_column_def(cols, 'exit_reason', 'null')) @@ -159,7 +158,7 @@ def migrate_trades_and_orders_table( open_rate_requested, close_rate, close_rate_requested, close_profit, stake_amount, amount, amount_requested, open_date, close_date, stop_loss, stop_loss_pct, initial_stop_loss, initial_stop_loss_pct, - is_stop_loss_trailing, stoploss_last_update, + is_stop_loss_trailing, max_rate, min_rate, exit_reason, exit_order_status, strategy, enter_tag, timeframe, open_trade_value, close_profit_abs, trading_mode, leverage, liquidation_price, is_short, @@ -179,7 +178,6 @@ def migrate_trades_and_orders_table( {initial_stop_loss} initial_stop_loss, {initial_stop_loss_pct} initial_stop_loss_pct, {is_stop_loss_trailing} is_stop_loss_trailing, - {stoploss_last_update} stoploss_last_update, {max_rate} max_rate, {min_rate} min_rate, case when {exit_reason} = 'sell_signal' then 'exit_signal' when {exit_reason} = 'custom_sell' then 'custom_exit' diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 7d88294b0..9db13dabc 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -364,8 +364,6 @@ class LocalTrade: # percentage value of the initial stop loss initial_stop_loss_pct: Optional[float] = None is_stop_loss_trailing: bool = False - # last update time of the stoploss order on exchange - stoploss_last_update: Optional[datetime] = None # absolute value of the highest reached price max_rate: Optional[float] = None # Lowest price reached @@ -455,8 +453,8 @@ class LocalTrade: @property def stoploss_last_update_utc(self): - if self.stoploss_last_update: - return self.stoploss_last_update.replace(tzinfo=timezone.utc) + if self.has_open_sl_orders: + return max(o.order_date_utc for o in self.open_sl_orders) return None @property @@ -638,10 +636,10 @@ class LocalTrade: 'stop_loss_abs': self.stop_loss, 'stop_loss_ratio': self.stop_loss_pct if self.stop_loss_pct else None, 'stop_loss_pct': (self.stop_loss_pct * 100) if self.stop_loss_pct else None, - 'stoploss_last_update': (self.stoploss_last_update.strftime(DATETIME_PRINT_FORMAT) - if self.stoploss_last_update else None), - 'stoploss_last_update_timestamp': int(self.stoploss_last_update.replace( - tzinfo=timezone.utc).timestamp() * 1000) if self.stoploss_last_update else None, + 'stoploss_last_update': (self.stoploss_last_update_utc.strftime(DATETIME_PRINT_FORMAT) + if self.stoploss_last_update_utc else None), + 'stoploss_last_update_timestamp': int(self.stoploss_last_update_utc.timestamp() * 1000 + ) if self.stoploss_last_update_utc else None, 'initial_stop_loss_abs': self.initial_stop_loss, 'initial_stop_loss_ratio': (self.initial_stop_loss_pct if self.initial_stop_loss_pct else None), @@ -1378,10 +1376,6 @@ class LocalTrade: exit_order_status=data["exit_order_status"], stop_loss=data["stop_loss_abs"], stop_loss_pct=data["stop_loss_ratio"], - stoploss_last_update=( - datetime.fromtimestamp(data["stoploss_last_update_timestamp"] // 1000, - tz=timezone.utc) - if data["stoploss_last_update_timestamp"] else None), initial_stop_loss=data["initial_stop_loss_abs"], initial_stop_loss_pct=data["initial_stop_loss_ratio"], min_rate=data["min_rate"], @@ -1487,8 +1481,6 @@ class Trade(ModelBase, LocalTrade): Float(), nullable=True) # type: ignore is_stop_loss_trailing: Mapped[bool] = mapped_column( nullable=False, default=False) # type: ignore - # last update time of the stoploss order on exchange - stoploss_last_update: Mapped[Optional[datetime]] = mapped_column(nullable=True) # type: ignore # absolute value of the highest reached price max_rate: Mapped[Optional[float]] = mapped_column( Float(), nullable=True, default=0.0) # type: ignore From acbea4e26ffe9e6e7ed927ca243a9537a65b8dfe Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 18:15:21 +0100 Subject: [PATCH 37/42] Fix some tests after update_stoploss_date removal --- tests/test_freqtradebot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 57cd1a2f5..2c14fdca1 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1675,6 +1675,7 @@ def test_handle_stoploss_on_exchange_trailing( ft_amount=trade.amount, ft_price=trade.stop_loss, order_id='100', + order_date=dt_now() - timedelta(minutes=20), ) ) @@ -1767,8 +1768,9 @@ def test_handle_stoploss_on_exchange_trailing( @pytest.mark.parametrize("is_short", [False, True]) def test_handle_stoploss_on_exchange_trailing_error( - mocker, default_conf_usdt, fee, caplog, limit_order, is_short + mocker, default_conf_usdt, fee, caplog, limit_order, is_short, time_machine ) -> None: + time_machine.move_to(dt_now() - timedelta(minutes=601)) enter_order = limit_order[entry_side(is_short)] exit_order = limit_order[exit_side(is_short)] # When trailing stoploss is set @@ -1809,7 +1811,6 @@ def test_handle_stoploss_on_exchange_trailing_error( trade.is_short = is_short trade.is_open = True trade.stop_loss = 0.2 - trade.stoploss_last_update = (dt_now() - timedelta(minutes=601)).replace(tzinfo=None) stoploss_order_hanging = { 'id': "abcd", @@ -1829,12 +1830,14 @@ def test_handle_stoploss_on_exchange_trailing_error( ft_amount=trade.amount, ft_price=3, order_id='abcd', + order_date=dt_now(), ) ) mocker.patch(f'{EXMS}.cancel_stoploss_order', side_effect=InvalidOrderException()) mocker.patch(f'{EXMS}.fetch_stoploss_order', return_value=stoploss_order_hanging) + time_machine.shift(timedelta(minutes=50)) freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging) assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/USDT.*", caplog) @@ -1844,10 +1847,10 @@ def test_handle_stoploss_on_exchange_trailing_error( assert len(trade.open_sl_orders) == 2 # Fail creating stoploss order - trade.stoploss_last_update = dt_now() - timedelta(minutes=601) caplog.clear() cancel_mock = mocker.patch(f'{EXMS}.cancel_stoploss_order') mocker.patch(f'{EXMS}.create_stoploss', side_effect=ExchangeError()) + time_machine.shift(timedelta(minutes=50)) freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging) assert cancel_mock.call_count == 2 assert log_has_re(r"Could not create trailing stoploss order for pair ETH/USDT\..*", caplog) From 88ba82d4fd4ee74bf86bdc599d7dceb5b1c58b02 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 18:17:31 +0100 Subject: [PATCH 38/42] Fix more tests --- tests/test_freqtradebot.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 2c14fdca1..2d962ae1b 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1383,8 +1383,9 @@ def test_handle_stoploss_on_exchange_partial( @pytest.mark.parametrize("is_short", [False, True]) def test_handle_stoploss_on_exchange_partial_cancel_here( - mocker, default_conf_usdt, fee, is_short, limit_order, caplog) -> None: + mocker, default_conf_usdt, fee, is_short, limit_order, caplog, time_machine) -> None: stop_order_dict = {'id': "101", "status": "open"} + time_machine.move_to(dt_now()) default_conf_usdt['trailing_stop'] = True stoploss = MagicMock(return_value=stop_order_dict) enter_order = limit_order[entry_side(is_short)] @@ -1443,7 +1444,7 @@ def test_handle_stoploss_on_exchange_partial_cancel_here( }) mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hit) mocker.patch(f'{EXMS}.cancel_stoploss_order_with_result', stoploss_order_cancel) - trade.stoploss_last_update = dt_now() - timedelta(minutes=10) + time_machine.shift(timedelta(minutes=15)) assert freqtrade.handle_stoploss_on_exchange(trade) is False # Canceled Stoploss filled partially ... @@ -1934,7 +1935,6 @@ def test_handle_stoploss_on_exchange_custom_stop( trade = Trade.session.scalars(select(Trade)).first() trade.is_short = is_short trade.is_open = True - trade.stoploss_last_update = dt_now() - timedelta(minutes=601) trade.orders.append( Order( ft_order_side='stoploss', @@ -1942,6 +1942,7 @@ def test_handle_stoploss_on_exchange_custom_stop( ft_is_open=True, ft_amount=trade.amount, ft_price=trade.stop_loss, + order_date=dt_now() - timedelta(minutes=601), order_id='100', ) ) From 6eaf42fe33fb1106c0241ca86cd6a1d3092cda4f Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 18:17:53 +0100 Subject: [PATCH 39/42] Default order_date to dt_now if it's not set via ccxt and wasn't previously set. --- freqtrade/persistence/trade_model.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 9db13dabc..1484c006f 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -170,6 +170,8 @@ class Order(ModelBase): order_date = safe_value_fallback(order, 'timestamp') if order_date: self.order_date = datetime.fromtimestamp(order_date / 1000, tz=timezone.utc) + elif not self.order_date: + self.order_date = dt_now() self.ft_is_open = True if self.status in NON_OPEN_EXCHANGE_STATES: From 58058f0197ee331832ca1c1889eae2aba718b2ab Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 18:20:23 +0100 Subject: [PATCH 40/42] Fix migration test --- tests/persistence/test_migrations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/persistence/test_migrations.py b/tests/persistence/test_migrations.py index 6ef098cb3..a6a107a5e 100644 --- a/tests/persistence/test_migrations.py +++ b/tests/persistence/test_migrations.py @@ -277,7 +277,6 @@ def test_migrate(mocker, default_conf, fee, caplog): assert trade.exit_reason is None assert trade.strategy is None assert trade.timeframe == '5m' - assert trade.stoploss_last_update is None assert log_has("trying trades_bak1", caplog) assert log_has("trying trades_bak2", caplog) assert log_has("Running database migration for trades - backup: trades_bak2, orders_bak0", From 3ab226a0965fdaf7428a75e5d6b5245bc812272c Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 18:24:03 +0100 Subject: [PATCH 41/42] Remove unused import --- freqtrade/persistence/migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/persistence/migrations.py b/freqtrade/persistence/migrations.py index eb55cf455..cf2e06f71 100644 --- a/freqtrade/persistence/migrations.py +++ b/freqtrade/persistence/migrations.py @@ -1,7 +1,7 @@ import logging from typing import List, Optional -from sqlalchemy import inspect, select, text, tuple_, update +from sqlalchemy import inspect, select, text, update from freqtrade.exceptions import OperationalException from freqtrade.persistence.trade_model import Order, Trade From e76888882dadae9286ed1dcc64830b487c9fd5ca Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 19 Jan 2024 18:59:52 +0100 Subject: [PATCH 42/42] Fix typehint --- freqtrade/freqtradebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 26631eb30..73fa9fa68 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1289,7 +1289,7 @@ class FreqtradeBot(LoggingMixin): logger.warning(f"Could not create trailing stoploss order " f"for pair {trade.pair}.") - def manage_trade_stoploss_orders(self, trade: Trade, stoploss_orders: Dict): + def manage_trade_stoploss_orders(self, trade: Trade, stoploss_orders: List[Dict]): """ Perform required actions acording to existing stoploss orders of trade :param trade: Corresponding Trade