From 2d9be6dacee665803b704d7dffd9814396930e4b Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 5 May 2022 19:50:16 +0200 Subject: [PATCH] move open_rate updating to close_bt_order --- freqtrade/optimize/backtesting.py | 6 ++---- freqtrade/persistence/models.py | 6 +++++- tests/optimize/test_backtest_detail.py | 26 ++++++++++++++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 35761c54c..aadda6dbd 100755 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -816,9 +816,7 @@ class Backtesting: # Update trade open_rate on first filled order # this is for cases where adjust_entry_order might have replaced the # initial order from trade opening - if len(trade.select_filled_orders(trade.entry_side)) == 1: - trade.open_rate = order.price - order.close_bt_order(current_time) + order.close_bt_order(current_time, trade) else: trade.open_order_id = str(self.order_id_counter) trade.orders.append(order) @@ -1052,7 +1050,7 @@ class Backtesting: # 3. Process entry orders. order = trade.select_order(trade.entry_side, is_open=True) if order and self._get_order_filled(order.price, row): - order.close_bt_order(current_time) + order.close_bt_order(current_time, trade) trade.open_order_id = None LocalTrade.add_bt_trade(trade) self.wallets.update() diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 299032bb4..c5ea34a30 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -219,11 +219,15 @@ class Order(_DECL_BASE): 'remaining': self.remaining, } - def close_bt_order(self, close_date: datetime): + def close_bt_order(self, close_date: datetime, trade: 'LocalTrade'): self.order_filled_date = close_date self.filled = self.amount self.status = 'closed' self.ft_is_open = False + if (self.ft_order_side == trade.entry_side + and len(trade.select_filled_orders(trade.entry_side)) == 1): + trade.open_rate = self.price + trade.recalc_open_trade_value() @staticmethod def update_orders(orders: List['Order'], order: Dict[str, Any]): diff --git a/tests/optimize/test_backtest_detail.py b/tests/optimize/test_backtest_detail.py index f2e2c89ad..aab864431 100644 --- a/tests/optimize/test_backtest_detail.py +++ b/tests/optimize/test_backtest_detail.py @@ -760,16 +760,29 @@ tc48 = BTContainer(data=[ [0, 5000, 5050, 4950, 5000, 6172, 1, 0], [1, 5000, 5500, 4951, 5000, 6172, 0, 0], # timeout [2, 4900, 5250, 4500, 5100, 6172, 0, 0], # Order readjust - [3, 5100, 5100, 4650, 4750, 6172, 0, 0], + [3, 5100, 5100, 4650, 4750, 6172, 0, 1], [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], - stop_loss=-0.01, roi={"0": 0.10}, profit_perc=0.1, - timeout=1000, - custom_entry_price=4200, - adjust_entry_price=5200, - trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=2, is_short=False)] + stop_loss=-0.01, roi={"0": 0.10}, profit_perc=-0.087, + use_exit_signal=True, timeout=1000, + custom_entry_price=4200, adjust_entry_price=5200, + trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4, is_short=False)] ) +# Test 49: Custom-entry-price short above all candles - readjust order +tc49 = BTContainer(data=[ + # D O H L C V EL XL ES Xs BT + [0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0], + [1, 5000, 5200, 4951, 5000, 6172, 0, 0, 0, 0], # timeout + [2, 4900, 5250, 4900, 5100, 6172, 0, 0, 0, 0], # Order readjust + [3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 1], + [4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0]], + stop_loss=-0.01, roi={"0": 0.10}, profit_perc=0.05, + use_exit_signal=True, timeout=1000, + custom_entry_price=5300, adjust_entry_price=5000, + trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4, is_short=True)] +) + TESTS = [ tc0, tc1, @@ -820,6 +833,7 @@ TESTS = [ tc46, tc47, tc48, + tc49, ]