Refactor log_trials_result()
This commit is contained in:
parent
32c9b5f415
commit
668d42447f
@ -156,7 +156,7 @@ class Hyperopt:
|
|||||||
self.trials_file.unlink()
|
self.trials_file.unlink()
|
||||||
return trials
|
return trials
|
||||||
|
|
||||||
def log_trials_result(self) -> None: # noqa: C901
|
def log_trials_result(self) -> None:
|
||||||
"""
|
"""
|
||||||
Display Best hyperopt result
|
Display Best hyperopt result
|
||||||
"""
|
"""
|
||||||
@ -170,57 +170,43 @@ class Hyperopt:
|
|||||||
best_result = results[0]
|
best_result = results[0]
|
||||||
params = best_result['params']
|
params = best_result['params']
|
||||||
log_str = self.format_results_logstring(best_result)
|
log_str = self.format_results_logstring(best_result)
|
||||||
|
|
||||||
print(f"\nBest result:\n\n{log_str}\n")
|
print(f"\nBest result:\n\n{log_str}\n")
|
||||||
|
|
||||||
if self.config.get('print_json'):
|
if self.config.get('print_json'):
|
||||||
result_dict: Dict = {}
|
result_dict: Dict = {}
|
||||||
|
for s in ['buy', 'sell', 'roi', 'stoploss', 'trailing']:
|
||||||
|
self._params_update_for_json(result_dict, params, s)
|
||||||
|
print(rapidjson.dumps(result_dict, default=str, number_mode=rapidjson.NM_NATIVE))
|
||||||
|
|
||||||
if self.has_space('buy') or self.has_space('sell'):
|
else:
|
||||||
result_dict['params'] = {}
|
self._params_pretty_print(params, 'buy', "Buy hyperspace params:")
|
||||||
|
self._params_pretty_print(params, 'sell', "Sell hyperspace params:")
|
||||||
|
self._params_pretty_print(params, 'roi', "ROI table:")
|
||||||
|
self._params_pretty_print(params, 'stoploss', "Stoploss:")
|
||||||
|
self._params_pretty_print(params, 'trailing', "Trailing stop:")
|
||||||
|
|
||||||
if self.has_space('buy'):
|
def _params_update_for_json(self, result_dict, params, space: str):
|
||||||
result_dict['params'].update(self.space_params(params, 'buy'))
|
if self.has_space(space):
|
||||||
|
space_params = self.space_params(params, space)
|
||||||
if self.has_space('sell'):
|
if space in ['buy', 'sell']:
|
||||||
result_dict['params'].update(self.space_params(params, 'sell'))
|
result_dict.setdefault('params', {}).update(space_params)
|
||||||
|
elif space == 'roi':
|
||||||
if self.has_space('roi'):
|
|
||||||
# Convert keys in min_roi dict to strings because
|
# Convert keys in min_roi dict to strings because
|
||||||
# rapidjson cannot dump dicts with integer keys...
|
# rapidjson cannot dump dicts with integer keys...
|
||||||
# OrderedDict is used to keep the numeric order of the items
|
# OrderedDict is used to keep the numeric order of the items
|
||||||
# in the dict.
|
# in the dict.
|
||||||
result_dict['minimal_roi'] = OrderedDict(
|
result_dict['minimal_roi'] = OrderedDict(
|
||||||
(str(k), v) for k, v in self.custom_hyperopt.generate_roi_table(params).items()
|
(str(k), v) for k, v in space_params.items()
|
||||||
)
|
)
|
||||||
|
else: # 'stoploss', 'trailing'
|
||||||
|
result_dict.update(space_params)
|
||||||
|
|
||||||
if self.has_space('stoploss'):
|
def _params_pretty_print(self, params, space: str, header: str):
|
||||||
result_dict.update(self.space_params(params, 'stoploss'))
|
if self.has_space(space):
|
||||||
|
space_params = self.space_params(params, space, 5)
|
||||||
if self.has_space('trailing'):
|
print(header)
|
||||||
result_dict.update(self.space_params(params, 'trailing'))
|
pprint(space_params, indent=4)
|
||||||
|
|
||||||
print(rapidjson.dumps(result_dict, default=str, number_mode=rapidjson.NM_NATIVE))
|
|
||||||
else:
|
|
||||||
if self.has_space('buy'):
|
|
||||||
print('Buy hyperspace params:')
|
|
||||||
pprint(self.space_params(params, 'buy', 5), indent=4)
|
|
||||||
|
|
||||||
if self.has_space('sell'):
|
|
||||||
print('Sell hyperspace params:')
|
|
||||||
pprint(self.space_params(params, 'sell', 5), indent=4)
|
|
||||||
|
|
||||||
if self.has_space('roi'):
|
|
||||||
print("ROI table:")
|
|
||||||
# Round printed values to 5 digits after the decimal point
|
|
||||||
pprint(round_dict(self.custom_hyperopt.generate_roi_table(params), 5), indent=4)
|
|
||||||
|
|
||||||
if self.has_space('stoploss'):
|
|
||||||
print(f"Stoploss:")
|
|
||||||
pprint(self.space_params(params, 'stoploss', 5), indent=4)
|
|
||||||
|
|
||||||
if self.has_space('trailing'):
|
|
||||||
print('Trailing stop:')
|
|
||||||
pprint(self.space_params(params, 'trailing', 5), indent=4)
|
|
||||||
|
|
||||||
def is_best(self, results) -> bool:
|
def is_best(self, results) -> bool:
|
||||||
return results['loss'] < self.current_best_loss
|
return results['loss'] < self.current_best_loss
|
||||||
@ -302,7 +288,10 @@ class Hyperopt:
|
|||||||
return spaces
|
return spaces
|
||||||
|
|
||||||
def space_params(self, params, space: str, r: int = None) -> Dict:
|
def space_params(self, params, space: str, r: int = None) -> Dict:
|
||||||
d = {p.name: params.get(p.name) for p in self.hyperopt_space(space)}
|
if space == 'roi':
|
||||||
|
d = self.custom_hyperopt.generate_roi_table(params)
|
||||||
|
else:
|
||||||
|
d = {p.name: params.get(p.name) for p in self.hyperopt_space(space)}
|
||||||
# Round floats to `r` digits after the decimal point if requested
|
# Round floats to `r` digits after the decimal point if requested
|
||||||
return round_dict(d, r) if r else d
|
return round_dict(d, r) if r else d
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user