unit tests for optimize.hyperopt
This commit is contained in:
parent
7b0beb0afa
commit
7f44ba6df4
@ -25,12 +25,10 @@ logging.getLogger('hyperopt.tpe').setLevel(logging.WARNING)
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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 = None
|
||||||
_CURRENT_TRIES = 0
|
_CURRENT_TRIES = 0
|
||||||
|
|
||||||
CURRENT_BEST_LOSS = 100
|
CURRENT_BEST_LOSS = 100
|
||||||
|
|
||||||
# this is expexted avg profit * expected trade count
|
# this is expexted avg profit * expected trade count
|
||||||
@ -111,6 +109,13 @@ def log_results(results):
|
|||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_loss(total_profit: float, trade_count: int):
|
||||||
|
""" objective function, returns smaller number for more optimal results """
|
||||||
|
trade_loss = 1 - 0.35 * exp(-(trade_count - TARGET_TRADES) ** 2 / 10 ** 5.2)
|
||||||
|
profit_loss = max(0, 1 - total_profit / EXPECTED_MAX_PROFIT)
|
||||||
|
return trade_loss + profit_loss
|
||||||
|
|
||||||
|
|
||||||
def optimizer(params):
|
def optimizer(params):
|
||||||
global _CURRENT_TRIES
|
global _CURRENT_TRIES
|
||||||
|
|
||||||
@ -129,9 +134,8 @@ def optimizer(params):
|
|||||||
'loss': float('inf')
|
'loss': float('inf')
|
||||||
}
|
}
|
||||||
|
|
||||||
trade_loss = 1 - 0.35 * exp(-(trade_count - TARGET_TRADES) ** 2 / 10 ** 5.2)
|
loss = calculate_loss(total_profit, trade_count)
|
||||||
profit_loss = max(0, 1 - total_profit / EXPECTED_MAX_PROFIT)
|
|
||||||
loss = trade_loss + profit_loss
|
|
||||||
_CURRENT_TRIES += 1
|
_CURRENT_TRIES += 1
|
||||||
|
|
||||||
log_results({
|
log_results({
|
||||||
|
@ -1,6 +1,79 @@
|
|||||||
# pragma pylint: disable=missing-docstring,W0212
|
# pragma pylint: disable=missing-docstring,W0212,C0103
|
||||||
|
|
||||||
|
from freqtrade.optimize.hyperopt import calculate_loss, TARGET_TRADES, EXPECTED_MAX_PROFIT, start, \
|
||||||
|
log_results
|
||||||
|
|
||||||
|
|
||||||
def test_optimizer(default_conf, mocker):
|
def test_loss_calculation_prefer_correct_trade_count():
|
||||||
# TODO: implement test
|
correct = calculate_loss(1, TARGET_TRADES)
|
||||||
pass
|
over = calculate_loss(1, TARGET_TRADES + 100)
|
||||||
|
under = calculate_loss(1, TARGET_TRADES - 100)
|
||||||
|
assert over > correct
|
||||||
|
assert under > correct
|
||||||
|
|
||||||
|
|
||||||
|
def test_loss_calculation_has_limited_profit():
|
||||||
|
correct = calculate_loss(EXPECTED_MAX_PROFIT, TARGET_TRADES)
|
||||||
|
over = calculate_loss(EXPECTED_MAX_PROFIT * 2, TARGET_TRADES)
|
||||||
|
under = calculate_loss(EXPECTED_MAX_PROFIT / 2, TARGET_TRADES)
|
||||||
|
assert over == correct
|
||||||
|
assert under > correct
|
||||||
|
|
||||||
|
|
||||||
|
def create_trials(mocker):
|
||||||
|
return mocker.Mock(
|
||||||
|
results=[{
|
||||||
|
'loss': 1,
|
||||||
|
'result': 'foo'
|
||||||
|
}]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_calls_fmin(mocker):
|
||||||
|
mocker.patch('freqtrade.optimize.hyperopt.Trials', return_value=create_trials(mocker))
|
||||||
|
mocker.patch('freqtrade.optimize.preprocess')
|
||||||
|
mocker.patch('freqtrade.optimize.load_data')
|
||||||
|
mock_fmin = mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
||||||
|
|
||||||
|
args = mocker.Mock(epochs=1, config='config.json.example', mongodb=False)
|
||||||
|
start(args)
|
||||||
|
|
||||||
|
mock_fmin.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_uses_mongotrials(mocker):
|
||||||
|
mock_mongotrials = mocker.patch('freqtrade.optimize.hyperopt.MongoTrials',
|
||||||
|
return_value=create_trials(mocker))
|
||||||
|
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=True)
|
||||||
|
start(args)
|
||||||
|
|
||||||
|
mock_mongotrials.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_results_if_loss_improves(mocker):
|
||||||
|
logger = mocker.patch('freqtrade.optimize.hyperopt.logger.info')
|
||||||
|
global CURRENT_BEST_LOSS
|
||||||
|
CURRENT_BEST_LOSS = 2
|
||||||
|
log_results({
|
||||||
|
'loss': 1,
|
||||||
|
'current_tries': 1,
|
||||||
|
'total_tries': 2,
|
||||||
|
'result': 'foo'
|
||||||
|
})
|
||||||
|
|
||||||
|
logger.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_log_if_loss_does_not_improve(mocker):
|
||||||
|
logger = mocker.patch('freqtrade.optimize.hyperopt.logger.info')
|
||||||
|
global CURRENT_BEST_LOSS
|
||||||
|
CURRENT_BEST_LOSS = 2
|
||||||
|
log_results({
|
||||||
|
'loss': 3,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert not logger.called
|
||||||
|
Loading…
Reference in New Issue
Block a user