Save backtest-stats

This commit is contained in:
Matthias
2020-06-26 07:46:59 +02:00
parent 81c8e8677d
commit 415853583b
3 changed files with 38 additions and 8 deletions

View File

@@ -11,9 +11,9 @@ from typing import Any, Dict, List, NamedTuple, Optional, Tuple
import arrow
from pandas import DataFrame
from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.configuration import (TimeRange, remove_credentials,
validate_config_consistency)
from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.data import history
from freqtrade.data.converter import trim_dataframe
from freqtrade.data.dataprovider import DataProvider
@@ -21,7 +21,8 @@ from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.optimize.optimize_reports import (generate_backtest_stats,
show_backtest_results,
store_backtest_result)
store_backtest_result,
store_backtest_stats)
from freqtrade.pairlist.pairlistmanager import PairListManager
from freqtrade.persistence import Trade
from freqtrade.resolvers import ExchangeResolver, StrategyResolver
@@ -420,3 +421,4 @@ class Backtesting:
stats = generate_backtest_stats(self.config, data, all_results,
min_date=min_date, max_date=max_date)
show_backtest_results(self.config, stats)
store_backtest_stats(self.config['exportfilename'], stats)

View File

@@ -14,6 +14,18 @@ from freqtrade.misc import file_dump_json
logger = logging.getLogger(__name__)
def store_backtest_stats(recordfilename: Path, stats: Dict[str, DataFrame]) -> None:
filename = Path.joinpath(recordfilename.parent,
f'{recordfilename.stem}-{datetime.now().isoformat()}'
).with_suffix(recordfilename.suffix)
file_dump_json(filename, stats)
latest_filename = Path.joinpath(recordfilename.parent,
'.last_result.json')
file_dump_json(latest_filename, {'latest_backtest': str(filename.name)})
def store_backtest_result(recordfilename: Path, all_results: Dict[str, DataFrame]) -> None:
"""
Stores backtest results to file (one file per strategy)
@@ -224,7 +236,7 @@ def generate_backtest_stats(config: Dict, btdata: Dict[str, DataFrame],
backtest_days = (max_date - min_date).days
strat_stats = {
'trades': backtest_result_to_list(results),
'trades': results.to_dict(orient='records'),
'results_per_pair': pair_results,
'sell_reason_summary': sell_reason_stats,
'left_open_trades': left_open_results,
@@ -338,12 +350,11 @@ def text_table_strategy(strategy_results, stake_currency: str) -> str:
def text_table_add_metrics(strategy_results: Dict) -> str:
if len(strategy_results['trades']) > 0:
min_trade = min(strategy_results['trades'], key=lambda x: x[2])
min_trade = min(strategy_results['trades'], key=lambda x: x['open_time'])
metrics = [
('Total trades', strategy_results['total_trades']),
('First trade', datetime.fromtimestamp(min_trade[2],
tz=timezone.utc).strftime(DATETIME_PRINT_FORMAT)),
('First trade Pair', min_trade[0]),
('First trade', min_trade['open_time'].strftime(DATETIME_PRINT_FORMAT)),
('First trade Pair', min_trade['pair']),
('Backtesting from', strategy_results['backtest_start'].strftime(DATETIME_PRINT_FORMAT)),
('Backtesting to', strategy_results['backtest_end'].strftime(DATETIME_PRINT_FORMAT)),
('Trades per day', strategy_results['trades_per_day']),