Initial work on csv-file export. Missing docs and tests

This commit is contained in:
Fredrik81 2020-03-05 01:58:33 +01:00
parent 57523d58df
commit 7606d814fa
5 changed files with 81 additions and 1 deletions

View File

@ -69,7 +69,8 @@ ARGS_HYPEROPT_LIST = ["hyperopt_list_best", "hyperopt_list_profitable",
"hyperopt_list_min_avg_time", "hyperopt_list_max_avg_time", "hyperopt_list_min_avg_time", "hyperopt_list_max_avg_time",
"hyperopt_list_min_avg_profit", "hyperopt_list_max_avg_profit", "hyperopt_list_min_avg_profit", "hyperopt_list_max_avg_profit",
"hyperopt_list_min_total_profit", "hyperopt_list_max_total_profit", "hyperopt_list_min_total_profit", "hyperopt_list_max_total_profit",
"print_colorized", "print_json", "hyperopt_list_no_details"] "print_colorized", "print_json", "hyperopt_list_no_details",
"export_csv"]
ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperopt_show_index", ARGS_HYPEROPT_SHOW = ["hyperopt_list_best", "hyperopt_list_profitable", "hyperopt_show_index",
"print_json", "hyperopt_show_no_header"] "print_json", "hyperopt_show_no_header"]

View File

@ -221,6 +221,12 @@ AVAILABLE_CLI_OPTIONS = {
action='store_true', action='store_true',
default=False, default=False,
), ),
"export_csv": Arg(
'--export-csv',
help='Export to CSV-File. Put + in front of filename to overwrite.'
'Example: --export-csv +hyperopt.csv',
metavar='FILE',
),
"hyperopt_jobs": Arg( "hyperopt_jobs": Arg(
'-j', '--job-workers', '-j', '--job-workers',
help='The number of concurrently running jobs for hyperoptimization ' help='The number of concurrently running jobs for hyperoptimization '

View File

@ -21,6 +21,7 @@ def start_hyperopt_list(args: Dict[str, Any]) -> None:
print_colorized = config.get('print_colorized', False) print_colorized = config.get('print_colorized', False)
print_json = config.get('print_json', False) print_json = config.get('print_json', False)
export_csv = config.get('export_csv', None)
no_details = config.get('hyperopt_list_no_details', False) no_details = config.get('hyperopt_list_no_details', False)
no_header = False no_header = False
@ -59,6 +60,17 @@ def start_hyperopt_list(args: Dict[str, Any]) -> None:
sorted_trials = sorted(trials, key=itemgetter('loss')) sorted_trials = sorted(trials, key=itemgetter('loss'))
results = sorted_trials[0] results = sorted_trials[0]
Hyperopt.print_epoch_details(results, total_epochs, print_json, no_header) Hyperopt.print_epoch_details(results, total_epochs, print_json, no_header)
print(export_csv)
if trials and export_csv:
overwrite_csv = False
if export_csv[0] == '+':
overwrite_csv = True
export_csv = export_csv[1:]
Hyperopt.export_csv_file(
config, trials, total_epochs,
not filteroptions['only_best'], export_csv, overwrite_csv
)
def start_hyperopt_show(args: Dict[str, Any]) -> None: def start_hyperopt_show(args: Dict[str, Any]) -> None:

View File

@ -282,6 +282,9 @@ class Configuration:
self._args_to_config(config, argname='print_json', self._args_to_config(config, argname='print_json',
logstring='Parameter --print-json detected ...') logstring='Parameter --print-json detected ...')
self._args_to_config(config, argname='export_csv',
logstring='Parameter --export-csv detected: {}')
self._args_to_config(config, argname='hyperopt_jobs', self._args_to_config(config, argname='hyperopt_jobs',
logstring='Parameter -j/--job-workers detected: {}') logstring='Parameter -j/--job-workers detected: {}')

View File

@ -23,6 +23,8 @@ from joblib import (Parallel, cpu_count, delayed, dump, load,
wrap_non_picklable_objects) wrap_non_picklable_objects)
from pandas import DataFrame, json_normalize, isna from pandas import DataFrame, json_normalize, isna
import tabulate import tabulate
from os import path
import io
from freqtrade.data.converter import trim_dataframe from freqtrade.data.converter import trim_dataframe
from freqtrade.data.history import get_timerange from freqtrade.data.history import get_timerange
@ -381,6 +383,62 @@ class Hyperopt:
) )
print(table) print(table)
@staticmethod
def export_csv_file(config: dict, results: list, total_epochs: int, highlight_best: bool,
csv_file: str, overwrite: bool) -> None:
"""
Log result to csv-file
"""
if not results:
return
# Verification for owerwrite
if not overwrite and path.isfile(csv_file):
logging.error("CSV-File already exists and no overwrite specified!")
return
try:
io.open(csv_file, 'w+').close()
except IOError:
logging.error("Filed to create/overwrite CSV-File!")
return
trials = json_normalize(results, max_level=1)
trials['Best'] = ''
trials['Stake currency'] = config['stake_currency']
trials = trials[['Best', 'current_epoch', 'results_metrics.trade_count',
'results_metrics.avg_profit', 'results_metrics.total_profit',
'Stake currency', 'results_metrics.profit', 'results_metrics.duration',
'loss', 'is_initial_point', 'is_best']]
trials.columns = ['Best', 'Epoch', 'Trades', 'Avg profit', 'Total profit', 'Stake currency',
'Profit', 'Avg duration', 'Objective', 'is_initial_point', 'is_best']
trials['is_profit'] = False
trials.loc[trials['is_initial_point'], 'Best'] = '*'
trials.loc[trials['is_best'], 'Best'] = 'Best'
trials.loc[trials['Total profit'] > 0, 'is_profit'] = True
trials['Epoch'] = trials['Epoch'].astype(str)
trials['Trades'] = trials['Trades'].astype(str)
trials['Total profit'] = trials['Total profit'].apply(
lambda x: '{:,.8f}'.format(x) if x != 0.0 else "--"
)
trials['Profit'] = trials['Profit'].apply(
lambda x: '{:,.2f}'.format(x) if not isna(x) else "--"
)
trials['Avg profit'] = trials['Avg profit'].apply(
lambda x: ('{:,.2f}%'.format(x)) if not isna(x) else "--"
)
trials['Avg duration'] = trials['Avg duration'].apply(
lambda x: ('{:,.1f} m'.format(x)) if not isna(x) else "--"
)
trials['Objective'] = trials['Objective'].apply(
lambda x: '{:,.5f}'.format(x) if x != 100000 else "N/A"
)
trials = trials.drop(columns=['is_initial_point', 'is_best', 'is_profit'])
trials.to_csv(csv_file, index=False, header=True, mode='w', encoding='UTF-8')
print("CSV-File created!")
def has_space(self, space: str) -> bool: def has_space(self, space: str) -> bool:
""" """
Tell if the space value is contained in the configuration Tell if the space value is contained in the configuration