From 29fdba77433ac66a60aff99b467451b007400666 Mon Sep 17 00:00:00 2001 From: creslinux Date: Wed, 11 Jul 2018 18:26:27 +0000 Subject: [PATCH] Reduce time taken for def backtest by 60% * on 2 weeks 5minute data Performs bitwise filter on buy/sell rows in the df before passing to the slower itertuples loop the larger the dataset the greater the performance gain --- freqtrade/optimize/backtesting.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 05bcdf4b7..80753809c 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -221,7 +221,21 @@ class Backtesting(object): # Convert from Pandas to list for performance reasons # (Looping Pandas is slow.) - ticker = [x for x in ticker_data.itertuples()] + + #ticker = [x for x in ticker_data.itertuples()] + """ + Use faster numpy to mask out rows we will not use in slower itertuples loop + + Filter rows that are buy or sell to be processed + Also keep the last row from in any state for open trades + """ + mask = (ticker_data['buy'].values != 0) | (ticker_data['sell'].values != 0) + ticker_loop_data=ticker_data[mask] + + end_row = DataFrame(ticker_data[-1:].values, columns=ticker_data.columns) + ticker_loop_data = ticker_loop_data.append(end_row) + + ticker = [x for x in ticker_loop_data.itertuples()] lock_pair_until = None for index, row in enumerate(ticker):