Merge pull request #2089 from hroff-1902/hyperopt-print-colorized

Hyperopt print colorized results
This commit is contained in:
Matthias
2019-08-13 19:36:06 +02:00
committed by GitHub
9 changed files with 47 additions and 5 deletions

View File

@@ -13,6 +13,8 @@ from pathlib import Path
from pprint import pprint
from typing import Any, Dict, List, Optional
from colorama import init as colorama_init
from colorama import Fore, Style
from joblib import Parallel, delayed, dump, load, wrap_non_picklable_objects, cpu_count
from pandas import DataFrame
from skopt import Optimizer
@@ -153,8 +155,17 @@ class Hyperopt(Backtesting):
Log results if it is better than any previous evaluation
"""
print_all = self.config.get('print_all', False)
if print_all or results['loss'] < self.current_best_loss:
is_best_loss = results['loss'] < self.current_best_loss
if print_all or is_best_loss:
if is_best_loss:
self.current_best_loss = results['loss']
log_str = self.format_results_logstring(results)
# Colorize output
if self.config.get('print_colorized', False):
if results['total_profit'] > 0:
log_str = Fore.GREEN + log_str
if print_all and is_best_loss:
log_str = Style.BRIGHT + log_str
if print_all:
print(log_str)
else:
@@ -169,7 +180,6 @@ class Hyperopt(Backtesting):
total = self.total_epochs
res = results['results_explanation']
loss = results['loss']
self.current_best_loss = results['loss']
log_str = f'{current:5d}/{total}: {res} Objective: {loss:.5f}'
log_str = f'*{log_str}' if results['is_initial_point'] else f' {log_str}'
return log_str
@@ -237,6 +247,7 @@ class Hyperopt(Backtesting):
results_explanation = self.format_results(results)
trade_count = len(results.index)
total_profit = results.profit_abs.sum()
# If this evaluation contains too short amount of trades to be
# interesting -- consider it as 'bad' (assigned max. loss value)
@@ -247,6 +258,7 @@ class Hyperopt(Backtesting):
'loss': MAX_LOSS,
'params': params,
'results_explanation': results_explanation,
'total_profit': total_profit,
}
loss = self.calculate_loss(results=results, trade_count=trade_count,
@@ -256,6 +268,7 @@ class Hyperopt(Backtesting):
'loss': loss,
'params': params,
'results_explanation': results_explanation,
'total_profit': total_profit,
}
def format_results(self, results: DataFrame) -> str:
@@ -339,6 +352,10 @@ class Hyperopt(Backtesting):
logger.info(f'Number of parallel jobs set as: {config_jobs}')
opt = self.get_optimizer(config_jobs)
if self.config.get('print_colorized', False):
colorama_init(autoreset=True)
try:
with Parallel(n_jobs=config_jobs) as parallel:
jobs = parallel._effective_n_jobs()