update fix failing tests, unitest that resume hyperopt functionality works
This commit is contained in:
parent
b35fa4c9f6
commit
1647e7a0c1
@ -30,7 +30,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
# set TARGET_TRADES to suit your number concurrent trades so its realistic to 20days of data
|
# set TARGET_TRADES to suit your number concurrent trades so its realistic to 20days of data
|
||||||
TARGET_TRADES = 1100
|
TARGET_TRADES = 1100
|
||||||
TOTAL_TRIES = None
|
TOTAL_TRIES = 0
|
||||||
_CURRENT_TRIES = 0
|
_CURRENT_TRIES = 0
|
||||||
CURRENT_BEST_LOSS = 100
|
CURRENT_BEST_LOSS = 100
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ def read_trials(trials_path=TRIALS_FILE):
|
|||||||
"Read hyperopt trials file"
|
"Read hyperopt trials file"
|
||||||
logger.info('Reading Trials from \'{}\''.format(trials_path))
|
logger.info('Reading Trials from \'{}\''.format(trials_path))
|
||||||
trials = pickle.load(open(trials_path, 'rb'))
|
trials = pickle.load(open(trials_path, 'rb'))
|
||||||
# os.remove(trials_path)
|
os.remove(trials_path)
|
||||||
return trials
|
return trials
|
||||||
|
|
||||||
|
|
||||||
@ -125,6 +125,7 @@ def log_trials_result(trials):
|
|||||||
results = trials.best_trial['result']['result']
|
results = trials.best_trial['result']['result']
|
||||||
logger.info('Best result:\n%s\nwith values:\n%s', results, vals)
|
logger.info('Best result:\n%s\nwith values:\n%s', results, vals)
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@ -242,12 +243,8 @@ def buy_strategy_generator(params):
|
|||||||
|
|
||||||
|
|
||||||
def start(args):
|
def start(args):
|
||||||
<<<<<<< HEAD
|
global TOTAL_TRIES, PROCESSED, SPACE, TRIALS, _CURRENT_TRIES
|
||||||
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': ''})
|
||||||
@ -276,6 +273,7 @@ def start(args):
|
|||||||
# read trials file if we have one
|
# read trials file if we have one
|
||||||
if os.path.exists(TRIALS_FILE):
|
if os.path.exists(TRIALS_FILE):
|
||||||
TRIALS = read_trials()
|
TRIALS = read_trials()
|
||||||
|
|
||||||
_CURRENT_TRIES = len(TRIALS.results)
|
_CURRENT_TRIES = len(TRIALS.results)
|
||||||
TOTAL_TRIES = TOTAL_TRIES + _CURRENT_TRIES
|
TOTAL_TRIES = TOTAL_TRIES + _CURRENT_TRIES
|
||||||
logger.info(
|
logger.info(
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
# pragma pylint: disable=missing-docstring,W0212,C0103
|
# pragma pylint: disable=missing-docstring,W0212,C0103
|
||||||
|
import pickle
|
||||||
|
import os
|
||||||
|
import pytest
|
||||||
|
import freqtrade.optimize.hyperopt
|
||||||
from freqtrade.optimize.hyperopt import calculate_loss, TARGET_TRADES, EXPECTED_MAX_PROFIT, start, \
|
from freqtrade.optimize.hyperopt import calculate_loss, TARGET_TRADES, EXPECTED_MAX_PROFIT, start, \
|
||||||
log_results
|
log_results
|
||||||
|
|
||||||
@ -27,16 +30,35 @@ def test_loss_calculation_has_limited_profit():
|
|||||||
|
|
||||||
|
|
||||||
def create_trials(mocker):
|
def create_trials(mocker):
|
||||||
|
"""
|
||||||
|
When creating trials, mock the hyperopt Trials so that *by default*
|
||||||
|
- we don't create any pickle'd files in the filesystem
|
||||||
|
- we might have a pickle'd file so make sure that we return
|
||||||
|
false when looking for it
|
||||||
|
"""
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.TRIALS_FILE',
|
||||||
|
return_value='freqtrade/tests/optimize/ut_trials.pickle')
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists',
|
||||||
|
return_value=False)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.save_trials',
|
||||||
|
return_value=None)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.read_trials',
|
||||||
|
return_value=None)
|
||||||
return mocker.Mock(
|
return mocker.Mock(
|
||||||
results=[{
|
results=[{
|
||||||
'loss': 1,
|
'loss': 1,
|
||||||
'result': 'foo'
|
'result': 'foo',
|
||||||
}]
|
'status': 'ok'
|
||||||
|
}],
|
||||||
|
best_trial={'misc': {'vals': {'adx': 999}}}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_start_calls_fmin(mocker):
|
def test_start_calls_fmin(mocker):
|
||||||
mocker.patch('freqtrade.optimize.hyperopt.Trials', return_value=create_trials(mocker))
|
trials = create_trials(mocker)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.TRIALS', return_value=trials)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.sorted',
|
||||||
|
return_value=trials.results)
|
||||||
mocker.patch('freqtrade.optimize.preprocess')
|
mocker.patch('freqtrade.optimize.preprocess')
|
||||||
mocker.patch('freqtrade.optimize.load_data')
|
mocker.patch('freqtrade.optimize.load_data')
|
||||||
mock_fmin = mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
mock_fmin = mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
||||||
@ -141,3 +163,39 @@ def test_fmin_throw_value_error(mocker, caplog):
|
|||||||
|
|
||||||
for line in exists:
|
for line in exists:
|
||||||
assert line in caplog.text
|
assert line in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_resuming_previous_hyperopt_results_succeeds(mocker):
|
||||||
|
import freqtrade.optimize.hyperopt as hyperopt
|
||||||
|
trials = create_trials(mocker)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.TRIALS',
|
||||||
|
return_value=trials)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists',
|
||||||
|
return_value=True)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.len',
|
||||||
|
return_value=len(trials.results))
|
||||||
|
mock_read = mocker.patch('freqtrade.optimize.hyperopt.read_trials',
|
||||||
|
return_value=trials)
|
||||||
|
mock_save = mocker.patch('freqtrade.optimize.hyperopt.save_trials',
|
||||||
|
return_value=None)
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.sorted',
|
||||||
|
return_value=trials.results)
|
||||||
|
mocker.patch('freqtrade.optimize.preprocess')
|
||||||
|
mocker.patch('freqtrade.optimize.load_data')
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.fmin',
|
||||||
|
return_value={})
|
||||||
|
args = mocker.Mock(epochs=1,
|
||||||
|
config='config.json.example',
|
||||||
|
mongodb=False)
|
||||||
|
|
||||||
|
start(args)
|
||||||
|
|
||||||
|
mock_read.assert_called_once()
|
||||||
|
mock_save.assert_called_once()
|
||||||
|
|
||||||
|
current_tries = hyperopt._CURRENT_TRIES
|
||||||
|
total_tries = hyperopt.TOTAL_TRIES
|
||||||
|
|
||||||
|
assert current_tries == len(trials.results)
|
||||||
|
assert total_tries == (current_tries + len(trials.results))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user