Extract generation of report for one strategy to it's own method

This commit is contained in:
Matthias 2021-04-25 15:44:21 +02:00
parent b125c975c7
commit 9994fce577

View File

@ -235,26 +235,25 @@ def generate_daily_stats(results: DataFrame) -> Dict[str, Any]:
} }
def generate_backtest_stats(btdata: Dict[str, DataFrame], def generate_strategy_stats(btdata: Dict[str, DataFrame],
all_results: Dict[str, Dict[str, Union[DataFrame, Dict]]], strategy: str,
min_date: Arrow, max_date: Arrow content: Dict[str, Any],
min_date: Arrow, max_date: Arrow,
market_change: float
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
:param btdata: Backtest data :param btdata: Backtest data
:param all_results: backtest result - dictionary in the form: :param strategy: Strategy name
{ Strategy: {'results: results, 'config: config}}. :param content: Backtest result data in the format:
{'results: results, 'config: config}}.
:param min_date: Backtest start date :param min_date: Backtest start date
:param max_date: Backtest end date :param max_date: Backtest end date
:return: :param market_change: float indicating the market change
Dictionary containing results per strategy and a stratgy summary. :return: Dictionary containing results per strategy and a stratgy summary.
""" """
result: Dict[str, Any] = {'strategy': {}}
market_change = calculate_market_change(btdata, 'close')
for strategy, content in all_results.items():
results: Dict[str, DataFrame] = content['results'] results: Dict[str, DataFrame] = content['results']
if not isinstance(results, DataFrame): if not isinstance(results, DataFrame):
continue return
config = content['config'] config = content['config']
max_open_trades = min(config['max_open_trades'], len(btdata.keys())) max_open_trades = min(config['max_open_trades'], len(btdata.keys()))
starting_balance = config['dry_run_wallet'] starting_balance = config['dry_run_wallet']
@ -331,7 +330,6 @@ def generate_backtest_stats(btdata: Dict[str, DataFrame],
'ignore_roi_if_buy_signal': config['ask_strategy']['ignore_roi_if_buy_signal'], 'ignore_roi_if_buy_signal': config['ask_strategy']['ignore_roi_if_buy_signal'],
**daily_stats, **daily_stats,
} }
result['strategy'][strategy] = strat_stats
try: try:
max_drawdown, _, _, _, _ = calculate_max_drawdown( max_drawdown, _, _, _, _ = calculate_max_drawdown(
@ -370,6 +368,29 @@ def generate_backtest_stats(btdata: Dict[str, DataFrame],
'csum_max': 0 'csum_max': 0
}) })
return strat_stats
def generate_backtest_stats(btdata: Dict[str, DataFrame],
all_results: Dict[str, Dict[str, Union[DataFrame, Dict]]],
min_date: Arrow, max_date: Arrow
) -> Dict[str, Any]:
"""
:param btdata: Backtest data
:param all_results: backtest result - dictionary in the form:
{ Strategy: {'results: results, 'config: config}}.
:param min_date: Backtest start date
:param max_date: Backtest end date
:return: Dictionary containing results per strategy and a stratgy summary.
"""
result: Dict[str, Any] = {'strategy': {}}
market_change = calculate_market_change(btdata, 'close')
for strategy, content in all_results.items():
strat_stats = generate_strategy_stats(btdata, strategy, content,
min_date, max_date, market_change=market_change)
result['strategy'][strategy] = strat_stats
strategy_results = generate_strategy_comparison(all_results=all_results) strategy_results = generate_strategy_comparison(all_results=all_results)
result['strategy_comparison'] = strategy_results result['strategy_comparison'] = strategy_results