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,20 +11,20 @@ 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:
for strategy, results in all_results.items():
records = [(t.pair, t.profit_percent, t.open_time.timestamp(), records = [(t.pair, t.profit_percent, t.open_time.timestamp(),
t.close_time.timestamp(), t.open_index - 1, t.trade_duration, t.close_time.timestamp(), t.open_index - 1, t.trade_duration,
t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value) t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value)
for index, t in results.iterrows()] 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)
@ -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'],