From 3d67e0893ad21fd9845c6aae930e176c60676dbb Mon Sep 17 00:00:00 2001 From: Axel-CH Date: Sun, 14 Apr 2024 00:16:55 -0400 Subject: [PATCH] edit backtest_loop to check exit if trade has open position --- freqtrade/optimize/backtesting.py | 2 +- freqtrade/persistence/trade_model.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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]: """