Merge pull request #518 from gcarq/cleaning_up_backtesting

Cleaning up backtesting/hyperopt
This commit is contained in:
Samuel Husso
2018-02-18 10:18:00 +02:00
committed by GitHub
3 changed files with 61 additions and 70 deletions

View File

@@ -103,13 +103,12 @@ def backtest(args) -> DataFrame:
realistic: do we try to simulate realistic trades? (default: True)
sell_profit_only: sell if profit only
use_sell_signal: act on sell-signal
stoploss: use stoploss
:return: DataFrame
"""
headers = ['date', 'buy', 'open', 'close', 'sell']
processed = args['processed']
max_open_trades = args.get('max_open_trades', 0)
realistic = args.get('realistic', True)
realistic = args.get('realistic', False)
record = args.get('record', None)
records = []
trades = []
@@ -224,7 +223,6 @@ def start(args):
'realistic': args.realistic_simulation,
'sell_profit_only': sell_profit_only,
'use_sell_signal': use_sell_signal,
'stoploss': strategy.stoploss,
'record': args.export
})
logger.info(

View File

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