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
commit 35c51c73f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 70 deletions

View File

@ -207,13 +207,7 @@ def scripts_options(parser: argparse.ArgumentParser) -> None:
) )
def backtesting_options(parser: argparse.ArgumentParser) -> None: def optimizer_shared_options(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
'-l', '--live',
action='store_true',
dest='live',
help='using live data',
)
parser.add_argument( parser.add_argument(
'-i', '--ticker-interval', '-i', '--ticker-interval',
help='specify ticker interval in minutes (1, 5, 30, 60, 1440)', help='specify ticker interval in minutes (1, 5, 30, 60, 1440)',
@ -227,6 +221,22 @@ def backtesting_options(parser: argparse.ArgumentParser) -> None:
action='store_true', action='store_true',
dest='realistic_simulation', dest='realistic_simulation',
) )
parser.add_argument(
'--timerange',
help='Specify what timerange of data to use.',
default=None,
type=str,
dest='timerange',
)
def backtesting_options(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
'-l', '--live',
action='store_true',
dest='live',
help='using live data',
)
parser.add_argument( parser.add_argument(
'-r', '--refresh-pairs-cached', '-r', '--refresh-pairs-cached',
help='refresh the pairs files in tests/testdata with the latest data from Bittrex. \ help='refresh the pairs files in tests/testdata with the latest data from Bittrex. \
@ -242,13 +252,6 @@ def backtesting_options(parser: argparse.ArgumentParser) -> None:
default=None, default=None,
dest='export', dest='export',
) )
parser.add_argument(
'--timerange',
help='Specify what timerange of data to use.',
default=None,
type=str,
dest='timerange',
)
def hyperopt_options(parser: argparse.ArgumentParser) -> None: def hyperopt_options(parser: argparse.ArgumentParser) -> None:
@ -266,20 +269,6 @@ def hyperopt_options(parser: argparse.ArgumentParser) -> None:
dest='mongodb', dest='mongodb',
action='store_true', action='store_true',
) )
parser.add_argument(
'-i', '--ticker-interval',
help='specify ticker interval in minutes (1, 5, 30, 60, 1440)',
dest='ticker_interval',
type=int,
metavar='INT',
)
parser.add_argument(
'--timerange',
help='Specify what timerange of data to use.',
default=None,
type=str,
dest='timerange',
)
parser.add_argument( parser.add_argument(
'-s', '--spaces', '-s', '--spaces',
help='Specify which parameters to hyperopt. Space separate list. \ help='Specify which parameters to hyperopt. Space separate list. \
@ -330,11 +319,13 @@ def build_subcommands(parser: argparse.ArgumentParser) -> None:
# Add backtesting subcommand # Add backtesting subcommand
backtesting_cmd = subparsers.add_parser('backtesting', help='backtesting module') backtesting_cmd = subparsers.add_parser('backtesting', help='backtesting module')
backtesting_cmd.set_defaults(func=backtesting.start) backtesting_cmd.set_defaults(func=backtesting.start)
optimizer_shared_options(backtesting_cmd)
backtesting_options(backtesting_cmd) backtesting_options(backtesting_cmd)
# Add hyperopt subcommand # Add hyperopt subcommand
hyperopt_cmd = subparsers.add_parser('hyperopt', help='hyperopt module') hyperopt_cmd = subparsers.add_parser('hyperopt', help='hyperopt module')
hyperopt_cmd.set_defaults(func=hyperopt.start) hyperopt_cmd.set_defaults(func=hyperopt.start)
optimizer_shared_options(hyperopt_cmd)
hyperopt_options(hyperopt_cmd) hyperopt_options(hyperopt_cmd)

View File

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

View File

@ -403,24 +403,24 @@ def buy_strategy_generator(params: Dict[str, Any]) -> Callable:
return populate_buy_trend return populate_buy_trend
def generate_optimizer(args):
def optimizer(params): def optimizer(params):
global _CURRENT_TRIES 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'] strategy.stoploss = params['stoploss']
else:
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}) 'realistic': args.realistic_simulation,
})
result_explanation = format_results(results) result_explanation = format_results(results)
total_profit = results.profit_percent.sum() total_profit = results.profit_percent.sum()
@ -451,6 +451,8 @@ def optimizer(params):
'result': result_explanation, 'result': result_explanation,
} }
return optimizer
def format_results(results: DataFrame): def format_results(results: DataFrame):
return ('{:6d} trades. Avg profit {: 5.2f}%. ' return ('{:6d} trades. Avg profit {: 5.2f}%. '
@ -519,7 +521,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,