From 9bf86bbe273668d9a8a6f72303a4533bd090534b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 30 Jan 2022 20:00:11 +0100 Subject: [PATCH] Extract backtesting row validation to separate function --- freqtrade/optimize/backtesting.py | 32 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 945022b66..4da8390af 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -658,6 +658,22 @@ class Backtesting: return False + def validate_row( + self, data: Dict, pair: str, row_index: int, current_time: datetime) -> Optional[Tuple]: + try: + # Row is treated as "current incomplete candle". + # Buy / sell signals are shifted by 1 to compensate for this. + row = data[pair][row_index] + except IndexError: + # missing Data for one pair at the end. + # Warnings for this are shown during data loading + return None + + # Waits until the time-counter reaches the start of the data for this pair. + if row[DATE_IDX] > current_time: + return None + return row + def backtest(self, processed: Dict, start_date: datetime, end_date: datetime, max_open_trades: int = 0, position_stacking: bool = False, @@ -701,17 +717,8 @@ class Backtesting: self.check_abort() for i, pair in enumerate(data): row_index = indexes[pair] - try: - # Row is treated as "current incomplete candle". - # Buy / sell signals are shifted by 1 to compensate for this. - row = data[pair][row_index] - except IndexError: - # missing Data for one pair at the end. - # Warnings for this are shown during data loading - continue - - # Waits until the time-counter reaches the start of the data for this pair. - if row[DATE_IDX] > current_time: + row = self.validate_row(data, pair, row_index, current_time) + if not row: continue row_index += 1 @@ -769,8 +776,7 @@ class Backtesting: self.run_protections(enable_protections, pair, current_time) # 5. Cancel expired buy/sell orders. - canceled = self.check_order_cancel(trade, current_time) - if canceled: + if self.check_order_cancel(trade, current_time): # Close trade due to buy timeout expiration. open_trade_count -= 1 open_trades[pair].remove(trade)