Simplify calling backtesting by returning the proper result

This commit is contained in:
Matthias
2021-04-30 07:31:57 +02:00
parent e2e1d34828
commit f2e182002d
4 changed files with 83 additions and 66 deletions

View File

@@ -330,7 +330,7 @@ class Backtesting:
def backtest(self, processed: Dict,
start_date: datetime, end_date: datetime,
max_open_trades: int = 0, position_stacking: bool = False,
enable_protections: bool = False) -> DataFrame:
enable_protections: bool = False) -> Dict[str, Any]:
"""
Implement backtesting functionality
@@ -417,7 +417,13 @@ class Backtesting:
trades += self.handle_left_open(open_trades, data=data)
self.wallets.update()
return trade_list_to_dataframe(trades)
results = trade_list_to_dataframe(trades)
return {
'results': results,
'config': self.strategy.config,
'locks': PairLocks.get_all_locks(),
'final_balance': self.wallets.get_total(self.strategy.config['stake_currency']),
}
def backtest_one_strategy(self, strat: IStrategy, data: Dict[str, Any], timerange: TimeRange):
logger.info("Running backtesting for Strategy %s", strat.get_strategy_name())
@@ -457,14 +463,12 @@ class Backtesting:
enable_protections=self.config.get('enable_protections', False),
)
backtest_end_time = datetime.now(timezone.utc)
self.all_results[self.strategy.get_strategy_name()] = {
'results': results,
'config': self.strategy.config,
'locks': PairLocks.get_all_locks(),
'final_balance': self.wallets.get_total(self.strategy.config['stake_currency']),
results.update({
'backtest_start_time': int(backtest_start_time.timestamp()),
'backtest_end_time': int(backtest_end_time.timestamp()),
}
})
self.all_results[self.strategy.get_strategy_name()] = results
return min_date, max_date
def start(self) -> None:

View File

@@ -31,7 +31,6 @@ from freqtrade.optimize.hyperopt_interface import IHyperOpt # noqa: F401
from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss # noqa: F401
from freqtrade.optimize.hyperopt_tools import HyperoptTools
from freqtrade.optimize.optimize_reports import generate_strategy_stats
from freqtrade.persistence.pairlock_middleware import PairLocks
from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver, HyperOptResolver
from freqtrade.strategy import IStrategy
@@ -279,7 +278,7 @@ class Hyperopt:
min_date, max_date = get_timerange(processed)
backtesting_results = self.backtesting.backtest(
bt_results = self.backtesting.backtest(
processed=processed,
start_date=min_date.datetime,
end_date=max_date.datetime,
@@ -288,17 +287,12 @@ class Hyperopt:
enable_protections=self.config.get('enable_protections', False),
)
backtest_end_time = datetime.now(timezone.utc)
bt_result = {
'results': backtesting_results,
'config': self.backtesting.strategy.config,
'locks': PairLocks.get_all_locks(),
'final_balance': self.backtesting.wallets.get_total(
self.backtesting.strategy.config['stake_currency']),
bt_results.update({
'backtest_start_time': int(backtest_start_time.timestamp()),
'backtest_end_time': int(backtest_end_time.timestamp()),
}
return self._get_results_dict(bt_result, min_date, max_date,
})
return self._get_results_dict(bt_results, min_date, max_date,
params_dict, params_details,
processed=processed)

View File

@@ -274,7 +274,7 @@ def generate_strategy_stats(btdata: Dict[str, DataFrame],
"""
results: Dict[str, DataFrame] = content['results']
if not isinstance(results, DataFrame):
return
return {}
config = content['config']
max_open_trades = min(config['max_open_trades'], len(btdata.keys()))
starting_balance = config['dry_run_wallet']