make args available for optimizer and use them instead of guessing from params

This commit is contained in:
Janne Sinivirta 2018-02-16 14:00:12 +02:00
parent ec8bf82695
commit bf72b5bc37

View File

@ -403,53 +403,58 @@ def buy_strategy_generator(params: Dict[str, Any]) -> Callable:
return populate_buy_trend return populate_buy_trend
def optimizer(params): def generate_optimizer(args):
global _CURRENT_TRIES def optimizer(params):
global _CURRENT_TRIES
strategy = Strategy() strategy = Strategy()
if 'roi_t1' in params: if has_space(args.spaces, 'roi'):
strategy.minimal_roi = generate_roi_table(params) strategy.minimal_roi = generate_roi_table(params)
if 'trigger' in params: if has_space(args.spaces, 'buy'):
backtesting.populate_buy_trend = buy_strategy_generator(params) backtesting.populate_buy_trend = buy_strategy_generator(params)
if 'stoploss' in params: if has_space(args.spaces, 'stoploss'):
stoploss = params['stoploss'] stoploss = params['stoploss']
else: else:
stoploss = strategy.stoploss stoploss = strategy.stoploss
results = backtest({'stake_amount': OPTIMIZE_CONFIG['stake_amount'], results = backtest({'stake_amount': OPTIMIZE_CONFIG['stake_amount'],
'processed': PROCESSED, 'processed': PROCESSED,
'stoploss': stoploss}) 'stoploss': stoploss,
result_explanation = format_results(results) 'realistic': args.realistic_simulation,
})
result_explanation = format_results(results)
total_profit = results.profit_percent.sum() total_profit = results.profit_percent.sum()
trade_count = len(results.index) trade_count = len(results.index)
trade_duration = results.duration.mean() trade_duration = results.duration.mean()
if trade_count == 0 or trade_duration > MAX_ACCEPTED_TRADE_DURATION:
print('.', end='')
return {
'status': STATUS_FAIL,
'loss': float('inf')
}
loss = calculate_loss(total_profit, trade_count, trade_duration)
_CURRENT_TRIES += 1
log_results({
'loss': loss,
'current_tries': _CURRENT_TRIES,
'total_tries': TOTAL_TRIES,
'result': result_explanation,
})
if trade_count == 0 or trade_duration > MAX_ACCEPTED_TRADE_DURATION:
print('.', end='')
return { return {
'status': STATUS_FAIL, 'loss': loss,
'loss': float('inf') 'status': STATUS_OK,
'result': result_explanation,
} }
loss = calculate_loss(total_profit, trade_count, trade_duration) return optimizer
_CURRENT_TRIES += 1
log_results({
'loss': loss,
'current_tries': _CURRENT_TRIES,
'total_tries': TOTAL_TRIES,
'result': result_explanation,
})
return {
'loss': loss,
'status': STATUS_OK,
'result': result_explanation,
}
def format_results(results: DataFrame): def format_results(results: DataFrame):
@ -519,7 +524,7 @@ def start(args):
try: try:
best_parameters = fmin( best_parameters = fmin(
fn=optimizer, fn=generate_optimizer(args),
space=hyperopt_space(args.spaces), space=hyperopt_space(args.spaces),
algo=tpe.suggest, algo=tpe.suggest,
max_evals=TOTAL_TRIES, max_evals=TOTAL_TRIES,