From e3227a741c650cd60cf46273e40f55645e2b6164 Mon Sep 17 00:00:00 2001 From: xmatthias Date: Sun, 3 Jun 2018 14:52:03 +0200 Subject: [PATCH] add --export-filename for backtesting --- freqtrade/arguments.py | 10 ++++++++++ freqtrade/configuration.py | 5 +++++ freqtrade/optimize/backtesting.py | 8 +++++--- freqtrade/tests/optimize/test_backtesting.py | 8 +++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/freqtrade/arguments.py b/freqtrade/arguments.py index 97c3d8cb2..7e895177a 100644 --- a/freqtrade/arguments.py +++ b/freqtrade/arguments.py @@ -137,6 +137,16 @@ class Arguments(object): default=None, dest='export', ) + parser.add_argument( + '--export-filename', + help='Save backtest results to this filename \ + requires --export to be set as well\ + Example --export-filename=backtest_today.json\ + (default: %(default)s', + type=str, + default='backtest-result.json', + dest='exportfilename', + ) @staticmethod def optimizer_shared_options(parser: argparse.ArgumentParser) -> None: diff --git a/freqtrade/configuration.py b/freqtrade/configuration.py index a800bde78..77b5b4447 100644 --- a/freqtrade/configuration.py +++ b/freqtrade/configuration.py @@ -157,6 +157,11 @@ class Configuration(object): config.update({'export': self.args.export}) logger.info('Parameter --export detected: %s ...', self.args.export) + # If --export-filename is used we add it to the configuration + if 'export' in config and 'exportfilename' in self.args and self.args.exportfilename: + config.update({'exportfilename': self.args.exportfilename}) + logger.info('Storing backtest results to %s ...', self.args.exportfilename) + return config def _load_hyperopt_config(self, config: Dict[str, Any]) -> Dict[str, Any]: diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 1d560d309..d7ed45955 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -154,6 +154,7 @@ class Backtesting(object): max_open_trades = args.get('max_open_trades', 0) realistic = args.get('realistic', False) record = args.get('record', None) + recordfilename = args.get('recordfn', 'backtest-result.json') records = [] trades = [] trade_count_lock: Dict = {} @@ -196,8 +197,8 @@ class Backtesting(object): # For now export inside backtest(), maybe change so that backtest() # returns a tuple like: (dataframe, records, logs, etc) if record and record.find('trades') >= 0: - logger.info('Dumping backtest results') - file_dump_json('backtest-result.json', records) + logger.info('Dumping backtest results to %s', recordfilename) + file_dump_json(recordfilename, records) labels = ['currency', 'profit_percent', 'profit_BTC', 'duration'] return DataFrame.from_records(trades, columns=labels) @@ -257,7 +258,8 @@ class Backtesting(object): 'realistic': self.config.get('realistic_simulation', False), 'sell_profit_only': sell_profit_only, 'use_sell_signal': use_sell_signal, - 'record': self.config.get('export') + 'record': self.config.get('export'), + 'recordfn': self.config.get('exportfilename'), } ) logger.info( diff --git a/freqtrade/tests/optimize/test_backtesting.py b/freqtrade/tests/optimize/test_backtesting.py index 1b1872404..65820ac09 100644 --- a/freqtrade/tests/optimize/test_backtesting.py +++ b/freqtrade/tests/optimize/test_backtesting.py @@ -218,7 +218,8 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non '--realistic-simulation', '--refresh-pairs-cached', '--timerange', ':100', - '--export', '/bar/foo' + '--export', '/bar/foo', + '--export-filename', 'foo_bar.json' ] config = setup_configuration(get_args(args)) @@ -259,6 +260,11 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non 'Parameter --export detected: {} ...'.format(config['export']), caplog.record_tuples ) + assert 'exportfilename' in config + assert log_has( + 'Storing backtest results to {} ...'.format(config['exportfilename']), + caplog.record_tuples + ) def test_start(mocker, fee, default_conf, caplog) -> None: