let hyperopt optimize ROI table

This commit is contained in:
Janne Sinivirta 2018-01-25 10:45:53 +02:00
parent 5007165908
commit 42087c9bfe
2 changed files with 36 additions and 3 deletions

View File

@ -225,6 +225,16 @@ def calculate_loss(total_profit: float, trade_count: int, trade_duration: float)
return trade_loss + profit_loss + duration_loss
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),
@ -236,6 +246,12 @@ def roi_space() -> List[Dict]:
}
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
@ -293,12 +309,11 @@ def indicator_space() -> List[Dict]:
{'type': 'heiken_reversal_bull'},
{'type': 'di_cross'},
]),
'stoploss': hp.uniform('stoploss', -0.5, -0.02),
}
def hyperopt_space() -> List[Dict]:
return {**indicator_space(), **roi_space()}
return {**indicator_space(), **roi_space(), **stoploss_space()}
def buy_strategy_generator(params) -> None:
@ -378,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'],
@ -498,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():
@ -234,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}