From 45d12dbc83fd6a24f1d7b28bcf6bf5022053d454 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 7 Dec 2019 15:28:56 +0100 Subject: [PATCH] Avoid a few calculations during backtesting --- 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 6e7d36368..7b5bd34b4 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -262,13 +262,22 @@ class Backtesting: return ticker def _get_close_rate(self, sell_row, trade: Trade, sell, trade_dur) -> float: + """ + Get close rate for backtesting result + """ # Special handling if high or low hit STOP_LOSS or ROI if sell.sell_type in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS): # Set close_rate to stoploss - closerate = trade.stop_loss + return trade.stop_loss elif sell.sell_type == (SellType.ROI): roi_entry, roi = self.strategy.min_roi_reached_entry(trade_dur) if roi is not None: + if roi == -1 and roi_entry % self.timeframe_mins == 0: + # When forceselling with ROI=-1, the roi-entry will always be "on the entry". + # If that entry is a multiple of the timeframe (so on open) + # - we'll use open instead of close + return sell_row.open + # - (Expected abs profit + open_rate + open_fee) / (fee_close -1) closerate = - (trade.open_rate * roi + trade.open_rate * (1 + trade.fee_open)) / (trade.fee_close - 1) @@ -276,19 +285,13 @@ class Backtesting: # Use the maximum between closerate and low as we # cannot sell outside of a candle. # Applies when a new ROI setting comes in place and the whole candle is above that. - closerate = max(closerate, sell_row.low) - if roi == -1 and roi_entry % self.timeframe_mins == 0: - # When forceselling with ROI=-1, the roi-entry will always be "on the entry". - # If that entry is a multiple of the timeframe (so on open) - # - we'll use open instead of close - closerate = max(closerate, sell_row.open) + return max(closerate, sell_row.low) else: # This should not be reached... - closerate = sell_row.open + return sell_row.open else: - closerate = sell_row.open - return closerate + return sell_row.open def _get_sell_trade_entry( self, pair: str, buy_row: DataFrame,