From a022b1a6c147b7a9ec47064496122ad5c9575419 Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Tue, 23 Apr 2019 21:18:52 +0300 Subject: [PATCH 1/2] --random-state for optimzer to get reproducible results added --- freqtrade/arguments.py | 18 ++++++++++++++++++ freqtrade/configuration.py | 4 ++++ freqtrade/optimize/hyperopt.py | 3 ++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/freqtrade/arguments.py b/freqtrade/arguments.py index 96f080bd2..9fda677ed 100644 --- a/freqtrade/arguments.py +++ b/freqtrade/arguments.py @@ -315,6 +315,14 @@ class Arguments(object): dest='print_all', default=False ) + parser.add_argument( + '--random-state', + help='Set random state to some positive integer for reproducible hyperopt results.', + dest='hyperopt_random_state', + default=None, + type=Arguments.check_positive, + metavar='INT', + ) def _build_subcommands(self) -> None: """ @@ -385,6 +393,16 @@ class Arguments(object): return TimeRange(stype[0], stype[1], start, stop) raise Exception('Incorrect syntax for timerange "%s"' % text) + @staticmethod + def check_positive(value) -> int: + try: + uint = int(value) + if uint <= 0: + raise ValueError + except: + raise argparse.ArgumentTypeError(f"{value} is invalid for this parameter, should be a positive integer value") + return uint + def scripts_options(self) -> None: """ Parses given arguments for scripts. diff --git a/freqtrade/configuration.py b/freqtrade/configuration.py index 65a8d644e..b14e515fb 100644 --- a/freqtrade/configuration.py +++ b/freqtrade/configuration.py @@ -324,6 +324,10 @@ class Configuration(object): config.update({'print_all': self.args.print_all}) logger.info('Parameter --print-all detected: %s', config.get('print_all')) + if 'hyperopt_random_state' in self.args and self.args.hyperopt_random_state is not None: + config.update({'hyperopt_random_state': self.args.hyperopt_random_state}) + logger.info("Parameter --random-state detected: %s", config.get('hyperopt_random_state')) + return config def _validate_config_schema(self, conf: Dict[str, Any]) -> Dict[str, Any]: diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index b37027244..af012d6bf 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -235,7 +235,8 @@ class Hyperopt(Backtesting): base_estimator="ET", acq_optimizer="auto", n_initial_points=30, - acq_optimizer_kwargs={'n_jobs': cpu_count} + acq_optimizer_kwargs={'n_jobs': cpu_count}, + random_state=self.config.get('hyperopt_random_state', None) ) def run_optimizer_parallel(self, parallel, asked) -> List: From a429f83f5e7b54047f15f5eb3fef9766dcc00c3e Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Tue, 23 Apr 2019 22:16:24 +0300 Subject: [PATCH 2/2] flake happy; check_positive() renamed --- freqtrade/arguments.py | 10 ++++++---- freqtrade/configuration.py | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/freqtrade/arguments.py b/freqtrade/arguments.py index ce84d7425..8ae3922ca 100644 --- a/freqtrade/arguments.py +++ b/freqtrade/arguments.py @@ -311,7 +311,7 @@ class Arguments(object): help='Set random state to some positive integer for reproducible hyperopt results.', dest='hyperopt_random_state', default=None, - type=Arguments.check_positive, + type=Arguments.check_int_positive, metavar='INT', ) @@ -385,13 +385,15 @@ class Arguments(object): raise Exception('Incorrect syntax for timerange "%s"' % text) @staticmethod - def check_positive(value) -> int: + def check_int_positive(value) -> int: try: uint = int(value) if uint <= 0: raise ValueError - except: - raise argparse.ArgumentTypeError(f"{value} is invalid for this parameter, should be a positive integer value") + except ValueError: + raise argparse.ArgumentTypeError( + f"{value} is invalid for this parameter, should be a positive integer value" + ) return uint def scripts_options(self) -> None: diff --git a/freqtrade/configuration.py b/freqtrade/configuration.py index 9994d405d..ca3b81279 100644 --- a/freqtrade/configuration.py +++ b/freqtrade/configuration.py @@ -315,7 +315,8 @@ class Configuration(object): if 'hyperopt_random_state' in self.args and self.args.hyperopt_random_state is not None: config.update({'hyperopt_random_state': self.args.hyperopt_random_state}) - logger.info("Parameter --random-state detected: %s", config.get('hyperopt_random_state')) + logger.info("Parameter --random-state detected: %s", + config.get('hyperopt_random_state')) return config