Hyperopt to handle SIGINT by saving/reading the trials file

This commit is contained in:
Samuel Husso 2018-01-06 11:44:41 +02:00
parent bbcf6943ce
commit ca8cab0ce9
2 changed files with 46 additions and 3 deletions

2
.gitignore vendored
View File

@ -85,3 +85,5 @@ target/
.venv .venv
.idea .idea
.vscode .vscode
hyperopt_trials.pickle

View File

@ -4,6 +4,9 @@
import json import json
import logging import logging
import sys import sys
import pickle
import signal
import os
from functools import reduce from functools import reduce
from math import exp from math import exp
from operator import itemgetter from operator import itemgetter
@ -43,6 +46,10 @@ EXPECTED_MAX_PROFIT = 3.85
PROCESSED = None # optimize.preprocess(optimize.load_data()) PROCESSED = None # optimize.preprocess(optimize.load_data())
OPTIMIZE_CONFIG = hyperopt_optimize_conf() OPTIMIZE_CONFIG = hyperopt_optimize_conf()
# Hyperopt Trials
TRIALS_FILE = 'freqtrade/optimize/hyperopt_trials.pickle'
TRIALS = Trials()
# Monkey patch config # Monkey patch config
from freqtrade import main # noqa from freqtrade import main # noqa
main._CONF = OPTIMIZE_CONFIG main._CONF = OPTIMIZE_CONFIG
@ -99,6 +106,19 @@ SPACE = {
} }
def save_trials(trials, trials_path=TRIALS_FILE):
"Save hyperopt trials to file"
logger.info('Saving Trials to \'{}\''.format(trials_path))
pickle.dump(trials, open(trials_path, 'wb'))
def read_trials(trials_path=TRIALS_FILE):
"Read hyperopt trials file"
logger.info('Reading Trials from \'{}\''.format(trials_path))
trials = pickle.load(open(trials_path, 'rb'))
# os.remove(trials_path)
return trials
def log_results(results): def log_results(results):
""" log results if it is better than any previous evaluation """ """ log results if it is better than any previous evaluation """
global CURRENT_BEST_LOSS global CURRENT_BEST_LOSS
@ -216,7 +236,12 @@ def buy_strategy_generator(params):
def start(args): def start(args):
<<<<<<< HEAD
global TOTAL_TRIES, PROCESSED, SPACE global TOTAL_TRIES, PROCESSED, SPACE
=======
global TOTAL_TRIES, PROCESSED, TRIALS, _CURRENT_TRIES
>>>>>>> Hyperopt to handle SIGINT by saving/reading the trials file
TOTAL_TRIES = args.epochs TOTAL_TRIES = args.epochs
exchange._API = Bittrex({'key': '', 'secret': ''}) exchange._API = Bittrex({'key': '', 'secret': ''})
@ -238,9 +263,14 @@ def start(args):
logger.info('Start scripts/start-mongodb.sh and start-hyperopt-worker.sh manually!') logger.info('Start scripts/start-mongodb.sh and start-hyperopt-worker.sh manually!')
db_name = 'freqtrade_hyperopt' db_name = 'freqtrade_hyperopt'
trials = MongoTrials('mongo://127.0.0.1:1234/{}/jobs'.format(db_name), exp_key='exp1') TRIALS = MongoTrials('mongo://127.0.0.1:1234/{}/jobs'.format(db_name), exp_key='exp1')
else: else:
trials = Trials() logger.info('Preparing Trials..')
signal.signal(signal.SIGINT, signal_handler)
# read trials file if we have one
if os.path.exists(TRIALS_FILE):
TRIALS = read_trials()
_CURRENT_TRIES = len([result for result in TRIALS.results if result['status'] == 'ok'])
try: try:
best_parameters = fmin( best_parameters = fmin(
@ -248,7 +278,7 @@ def start(args):
space=SPACE, space=SPACE,
algo=tpe.suggest, algo=tpe.suggest,
max_evals=TOTAL_TRIES, max_evals=TOTAL_TRIES,
trials=trials trials=TRIALS
) )
results = sorted(trials.results, key=itemgetter('loss')) results = sorted(trials.results, key=itemgetter('loss'))
@ -265,3 +295,14 @@ def start(args):
logger.info('Best parameters:\n%s', json.dumps(best_parameters, indent=4)) logger.info('Best parameters:\n%s', json.dumps(best_parameters, indent=4))
logger.info('Best Result:\n%s', best_result) logger.info('Best Result:\n%s', best_result)
# Store trials result to file to resume next time
save_trials(TRIALS)
def signal_handler(sig, frame):
"Hyperopt SIGINT handler"
logger.info('Hyperopt received {}'.format(signal.Signals(sig).name))
save_trials(TRIALS)
sys.exit(0)