Merge pull request #436 from gcarq/roi_hyperopt

ROI table Hyperopting
This commit is contained in:
Samuel Husso 2018-01-25 13:34:37 +02:00 committed by GitHub
commit 58d07eeb87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 5 deletions

View File

@ -225,11 +225,38 @@ def calculate_loss(total_profit: float, trade_count: int, trade_duration: float)
return trade_loss + profit_loss + duration_loss
def hyperopt_space() -> List[Dict]:
def generate_roi_table(params):
roi_table = {}
roi_table["0"] = params['roi_p1'] + params['roi_p2'] + params['roi_p3']
roi_table[str(params['roi_t3'])] = params['roi_p1'] + params['roi_p2']
roi_table[str(params['roi_t3'] + params['roi_t2'])] = params['roi_p1']
roi_table[str(params['roi_t3'] + params['roi_t2'] + params['roi_t1'])] = 0
return roi_table
def roi_space() -> List[Dict]:
return {
'roi_t1': hp.quniform('roi_t1', 10, 220, 10),
'roi_t2': hp.quniform('roi_t2', 10, 120, 10),
'roi_t3': hp.quniform('roi_t3', 10, 120, 10),
'roi_p1': hp.quniform('roi_p1', 1, 5, 1),
'roi_p2': hp.quniform('roi_p2', 1, 5, 1),
'roi_p3': hp.quniform('roi_p3', 1, 10, 1),
}
def stoploss_space() -> Dict:
return {
'stoploss': hp.uniform('stoploss', -0.5, -0.02),
}
def indicator_space() -> List[Dict]:
"""
Define your Hyperopt space for searching strategy parameters
"""
space = {
return {
'macd_below_zero': hp.choice('macd_below_zero', [
{'enabled': False},
{'enabled': True}
@ -282,9 +309,11 @@ def hyperopt_space() -> List[Dict]:
{'type': 'heiken_reversal_bull'},
{'type': 'di_cross'},
]),
'stoploss': hp.uniform('stoploss', -0.5, -0.02),
}
return space
def hyperopt_space() -> List[Dict]:
return {**indicator_space(), **roi_space(), **stoploss_space()}
def buy_strategy_generator(params) -> None:
@ -364,6 +393,10 @@ def buy_strategy_generator(params) -> None:
def optimizer(params):
global _CURRENT_TRIES
if 'roi_t1' in params:
strategy = Strategy()
strategy.minimal_roi = generate_roi_table(params)
backtesting.populate_buy_trend = buy_strategy_generator(params)
results = backtest({'stake_amount': OPTIMIZE_CONFIG['stake_amount'],
@ -484,6 +517,8 @@ def start(args):
)
logger.info('Best parameters:\n%s', json.dumps(best_parameters, indent=4))
if 'roi_t1' in best_parameters:
logger.info('ROI table:\n%s', generate_roi_table(best_parameters))
logger.info('Best Result:\n%s', best_result)
# Store trials result to file to resume next time

View File

@ -1,6 +1,6 @@
# pragma pylint: disable=missing-docstring,W0212,C0103
from freqtrade.optimize.hyperopt import calculate_loss, TARGET_TRADES, EXPECTED_MAX_PROFIT, start, \
log_results, save_trials, read_trials
log_results, save_trials, read_trials, generate_roi_table
def test_loss_calculation_prefer_correct_trade_count():
@ -125,6 +125,12 @@ def test_fmin_best_results(mocker, caplog):
"uptrend_short_ema": 0,
"uptrend_sma": 0,
"stoploss": -0.1,
"roi_t1": 1,
"roi_t2": 2,
"roi_t3": 3,
"roi_p1": 1,
"roi_p2": 2,
"roi_p3": 3,
}
mocker.patch('freqtrade.optimize.hyperopt.MongoTrials', return_value=create_trials(mocker))
@ -228,3 +234,15 @@ def test_read_trials_returns_trials_file(mocker):
assert read_trials() == trials
mock_open.assert_called_once()
mock_load.assert_called_once()
def test_roi_table_generation():
params = {
'roi_t1': 5,
'roi_t2': 10,
'roi_t3': 15,
'roi_p1': 1,
'roi_p2': 2,
'roi_p3': 3,
}
assert generate_roi_table(params) == {'0': 6, '15': 3, '25': 1, '30': 0}