From bb51da82978efe7592ed7a14619ab74a91eef4df Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 27 Nov 2020 17:38:15 +0100 Subject: [PATCH] Fix slow backtest due to protections --- freqtrade/optimize/backtesting.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index e3f5e7671..2684a249c 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -173,6 +173,17 @@ class Backtesting: return data, timerange + def prepare_backtest(self, enable_protections): + """ + Backtesting setup method - called once for every call to "backtest()". + """ + PairLocks.use_db = False + Trade.use_db = False + if enable_protections: + # Reset persisted data - used for protections only + PairLocks.reset_locks() + Trade.reset_trades() + def _get_ohlcv_as_lists(self, processed: Dict[str, DataFrame]) -> Dict[str, Tuple]: """ Helper function to convert a processed dataframes into lists for performance reasons. @@ -328,13 +339,7 @@ class Backtesting: f"max_open_trades: {max_open_trades}, position_stacking: {position_stacking}" ) trades = [] - PairLocks.use_db = False - Trade.use_db = False - if enable_protections: - # Reset persisted data - used for protections only - - PairLocks.reset_locks() - Trade.reset_trades() + self.prepare_backtest(enable_protections) # Use dict of lists with data for performance # (looping lists is a lot faster than pandas DataFrames) @@ -351,9 +356,6 @@ class Backtesting: while tmp <= end_date: open_trade_count_start = open_trade_count - if enable_protections: - self.protections.global_stop(tmp) - for i, pair in enumerate(data): if pair not in indexes: indexes[pair] = 0 @@ -410,6 +412,7 @@ class Backtesting: trades.append(trade_entry) if enable_protections: self.protections.stop_per_pair(pair, row[DATE_IDX]) + self.protections.global_stop(tmp) # Move time one configured time_interval ahead. tmp += timedelta(minutes=self.timeframe_min)