Make backtestresult storing independent from printing

This commit is contained in:
Matthias 2020-03-15 15:36:23 +01:00
parent a13d581658
commit e95665ceca
2 changed files with 21 additions and 21 deletions

View File

@ -18,7 +18,8 @@ from freqtrade.data.converter import trim_dataframe
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.optimize.optimize_reports import show_backtest_results from freqtrade.optimize.optimize_reports import (show_backtest_results,
store_backtest_result)
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
from freqtrade.state import RunMode from freqtrade.state import RunMode
@ -396,5 +397,8 @@ class Backtesting:
max_open_trades=max_open_trades, max_open_trades=max_open_trades,
position_stacking=position_stacking, position_stacking=position_stacking,
) )
if self.config.get('export', False):
store_backtest_result(self.config['exportfilename'], all_results)
# Show backtest results # Show backtest results
show_backtest_results(self.config, data, all_results) show_backtest_results(self.config, data, all_results)

View File

@ -11,22 +11,22 @@ from freqtrade.misc import file_dump_json
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def store_backtest_result(recordfilename: Path, results: DataFrame, def store_backtest_result(recordfilename: Path, all_results: Dict[str, DataFrame]) -> None:
strategyname: Optional[str] = None) -> None:
records = [(t.pair, t.profit_percent, t.open_time.timestamp(), for strategy, results in all_results.items():
t.close_time.timestamp(), t.open_index - 1, t.trade_duration, records = [(t.pair, t.profit_percent, t.open_time.timestamp(),
t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value) t.close_time.timestamp(), t.open_index - 1, t.trade_duration,
for index, t in results.iterrows()] t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value)
for index, t in results.iterrows()]
if records: if records:
if strategyname: if len(all_results) > 1:
# Inject strategyname to filename # Inject strategy to filename
recordfilename = Path.joinpath( recordfilename = Path.joinpath(
recordfilename.parent, recordfilename.parent,
f'{recordfilename.stem}-{strategyname}').with_suffix(recordfilename.suffix) f'{recordfilename.stem}-{strategy}').with_suffix(recordfilename.suffix)
logger.info(f'Dumping backtest results to {recordfilename}') logger.info(f'Dumping backtest results to {recordfilename}')
file_dump_json(recordfilename, records) file_dump_json(recordfilename, records)
def generate_text_table(data: Dict[str, Dict], stake_currency: str, max_open_trades: int, def generate_text_table(data: Dict[str, Dict], stake_currency: str, max_open_trades: int,
@ -203,10 +203,6 @@ def show_backtest_results(config: Dict, btdata: Dict[str, DataFrame],
all_results: Dict[str, DataFrame]): all_results: Dict[str, DataFrame]):
for strategy, results in all_results.items(): for strategy, results in all_results.items():
if config.get('export', False):
store_backtest_result(config['exportfilename'], results,
strategy if len(all_results) > 1 else None)
print(f"Result for strategy {strategy}") print(f"Result for strategy {strategy}")
table = generate_text_table(btdata, stake_currency=config['stake_currency'], table = generate_text_table(btdata, stake_currency=config['stake_currency'],
max_open_trades=config['max_open_trades'], max_open_trades=config['max_open_trades'],
@ -235,8 +231,8 @@ def show_backtest_results(config: Dict, btdata: Dict[str, DataFrame],
if len(all_results) > 1: if len(all_results) > 1:
# Print Strategy summary table # Print Strategy summary table
table = generate_text_table_strategy(config['stake_currency'], table = generate_text_table_strategy(config['stake_currency'],
config['max_open_trades'], config['max_open_trades'],
all_results=all_results) all_results=all_results)
print(' STRATEGY SUMMARY '.center(len(table.splitlines()[0]), '=')) print(' STRATEGY SUMMARY '.center(len(table.splitlines()[0]), '='))
print(table) print(table)
print('=' * len(table.splitlines()[0])) print('=' * len(table.splitlines()[0]))