diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index fea7a56ef..c9c128af5 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -1181,7 +1181,7 @@ class Backtesting: self.wallets.update() # 4. Create exit orders (if any) - if not trade.has_open_orders: + if trade.has_open_position: self._check_trade_exit(trade, row, current_time) # Place exit order if necessary # 5. Process exit orders. diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index defeb0e3f..e49eea974 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -544,6 +544,25 @@ class LocalTrade: ] return len(open_orders_wo_sl) > 0 + @property + def has_open_position(self) -> bool: + """ + True if there is an open position for this trade + """ + entry_orders = [ + o for o in self.orders + if o.ft_order_side == self.entry_side + ] + entry_orders_filled_qty = sum(eo.filled for eo in entry_orders) + + exit_orders = [ + o for o in self.orders + if o.ft_order_side == self.exit_side + ] + exit_orders_filled_qty = sum(eo.filled for eo in exit_orders) + + return (entry_orders_filled_qty - exit_orders_filled_qty) > 0 + @property def open_sl_orders(self) -> List[Order]: """