From 7ac44380f7ec67e86b2db2c06aa10a8746bb9b8e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 30 Jan 2022 15:44:13 +0100 Subject: [PATCH] Extract backtest order closing to models class --- freqtrade/optimize/backtesting.py | 14 +++----------- freqtrade/persistence/models.py | 6 ++++++ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index ff139c674..220d7557e 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -591,9 +591,7 @@ class Backtesting: cost=stake_amount + trade.fee_open, ) if pos_adjust and self._get_order_filled(order.price, row): - order.filled = order.amount - order.status = 'closed' - order.ft_is_open = False + order.close_bt_order(current_time) else: trade.open_order_id = self.order_id_counter trade.orders.append(order) @@ -718,11 +716,8 @@ class Backtesting: # 2. Process buy orders. order = trade.select_order('buy', is_open=True) if order and self._get_order_filled(order.price, row): - order.order_filled_date = row[DATE_IDX] + order.close_bt_order(current_time) trade.open_order_id = None - order.filled = order.amount - order.status = 'closed' - order.ft_is_open = False LocalTrade.add_bt_trade(trade) # 3. Create sell orders (if any) @@ -733,10 +728,7 @@ class Backtesting: order = trade.select_order('sell', is_open=True) if order and self._get_order_filled(order.price, row): trade.open_order_id = None - order.order_filled_date = trade.close_date = row[DATE_IDX] - order.filled = order.amount - order.status = 'closed' - order.ft_is_open = False + trade.close_date = current_time trade.close(order.price, show_msg=False) # logger.debug(f"{pair} - Backtesting sell {trade}") diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index ee7ad3fdd..6dd4ceaad 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -188,6 +188,12 @@ class Order(_DECL_BASE): 'status': self.status, } + def close_bt_order(self, close_date: datetime): + self.order_filled_date = close_date + self.filled = self.amount + self.status = 'closed' + self.ft_is_open = False + @staticmethod def update_orders(orders: List['Order'], order: Dict[str, Any]): """