From 66edbcd3d5facf31788319b874ba195ab92b27ab Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 May 2022 20:08:34 +0200 Subject: [PATCH] Fix slight backtesting bug in edge-case scenarios --- freqtrade/optimize/backtesting.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index fa32666cc..bfb4031cc 100755 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -895,26 +895,30 @@ class Backtesting: self.protections.stop_per_pair(pair, current_time, side) self.protections.global_stop(current_time, side) - def manage_open_orders(self, trade: LocalTrade, current_time, row: Tuple) -> bool: + def manage_open_orders(self, trade: LocalTrade, current_time: datetime, row: Tuple) -> bool: """ Check if any open order needs to be cancelled or replaced. Returns True if the trade should be deleted. """ for order in [o for o in trade.orders if o.ft_is_open]: - if self.check_order_cancel(trade, order, current_time): + oc = self.check_order_cancel(trade, order, current_time) + if oc: # delete trade due to order timeout return True - elif self.check_order_replace(trade, order, current_time, row): + elif oc is None and self.check_order_replace(trade, order, current_time, row): # delete trade due to user request self.canceled_trade_entries += 1 return True # default maintain trade return False - def check_order_cancel(self, trade: LocalTrade, order: Order, current_time) -> bool: + def check_order_cancel( + self, trade: LocalTrade, order: Order, current_time: datetime) -> Optional[bool]: """ Check if current analyzed order has to be canceled. - Returns True if the trade should be Deleted (initial order was canceled). + Returns True if the trade should be Deleted (initial order was canceled), + False if it's Canceled + None if the order is still active. """ timedout = self.strategy.ft_check_timed_out( trade, # type: ignore[arg-type] @@ -928,12 +932,13 @@ class Backtesting: else: # Close additional entry order del trade.orders[trade.orders.index(order)] + return False if order.side == trade.exit_side: self.timedout_exit_orders += 1 # Close exit order and retry exiting on next signal. del trade.orders[trade.orders.index(order)] - - return False + return False + return None def check_order_replace(self, trade: LocalTrade, order: Order, current_time, row: Tuple) -> bool: