Add parameter to enable protections for backtesting

This commit is contained in:
Matthias 2020-11-23 20:29:29 +01:00
parent 32189d27c8
commit e2d15f4082
5 changed files with 25 additions and 5 deletions

View File

@ -20,11 +20,13 @@ ARGS_COMMON_OPTIMIZE = ["timeframe", "timerange", "dataformat_ohlcv",
"max_open_trades", "stake_amount", "fee"]
ARGS_BACKTEST = ARGS_COMMON_OPTIMIZE + ["position_stacking", "use_max_market_positions",
"enable_protections",
"strategy_list", "export", "exportfilename"]
ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path",
"position_stacking", "epochs", "spaces",
"use_max_market_positions", "print_all",
"position_stacking", "use_max_market_positions",
"enable_protections",
"epochs", "spaces", "print_all",
"print_colorized", "print_json", "hyperopt_jobs",
"hyperopt_random_state", "hyperopt_min_trades",
"hyperopt_loss"]

View File

@ -144,6 +144,14 @@ AVAILABLE_CLI_OPTIONS = {
action='store_false',
default=True,
),
"enable_protections": Arg(
'--enable-protections', '--enableprotections',
help='Enable protections for backtesting.'
'Will slow backtesting down by a considerable amount, but will include '
'configured protections',
action='store_true',
default=False,
),
"strategy_list": Arg(
'--strategy-list',
help='Provide a space-separated list of strategies to backtest. '

View File

@ -211,6 +211,9 @@ class Configuration:
self._args_to_config(config, argname='position_stacking',
logstring='Parameter --enable-position-stacking detected ...')
self._args_to_config(
config, argname='enable_protections',
logstring='Parameter --enable-protections detected, enabling Protections. ...')
# Setting max_open_trades to infinite if -1
if config.get('max_open_trades') == -1:
config['max_open_trades'] = float('inf')

View File

@ -297,7 +297,8 @@ class Backtesting:
def backtest(self, processed: Dict, stake_amount: float,
start_date: datetime, end_date: datetime,
max_open_trades: int = 0, position_stacking: bool = False) -> DataFrame:
max_open_trades: int = 0, position_stacking: bool = False,
enable_protections: bool = False) -> DataFrame:
"""
Implement backtesting functionality
@ -311,6 +312,7 @@ class Backtesting:
:param end_date: backtesting timerange end datetime
:param max_open_trades: maximum number of concurrent trades, <= 0 means unlimited
:param position_stacking: do we allow position stacking?
:param enable_protections: Should protections be enabled?
:return: DataFrame with trades (results of backtesting)
"""
logger.debug(f"Run backtest, stake_amount: {stake_amount}, "
@ -334,6 +336,7 @@ 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):
@ -390,6 +393,7 @@ class Backtesting:
open_trade_count -= 1
open_trades[pair].remove(trade)
trades.append(trade_entry)
if enable_protections:
self.protections.stop_per_pair(pair, row[DATE_IDX])
# Move time one configured time_interval ahead.
@ -446,6 +450,7 @@ class Backtesting:
end_date=max_date.datetime,
max_open_trades=max_open_trades,
position_stacking=position_stacking,
enable_protections=self.config.get('enable_protections'),
)
all_results[self.strategy.get_strategy_name()] = {
'results': results,

View File

@ -542,6 +542,8 @@ class Hyperopt:
end_date=max_date.datetime,
max_open_trades=self.max_open_trades,
position_stacking=self.position_stacking,
enable_protections=self.config.get('enable_protections', False),
)
return self._get_results_dict(backtesting_results, min_date, max_date,
params_dict, params_details)