full automation(build-hyperopt) prototype of strategy->hyperopt done

This commit is contained in:
Bo van Hasselt 2021-02-14 02:07:52 +01:00
parent 1fe63f3583
commit cdd0168608
4 changed files with 89 additions and 30 deletions

View File

@ -12,7 +12,7 @@ from freqtrade.commands.data_commands import (start_convert_data, start_download
start_list_data) start_list_data)
from freqtrade.commands.deploy_commands import (start_create_userdir, start_install_ui, from freqtrade.commands.deploy_commands import (start_create_userdir, start_install_ui,
start_new_hyperopt, start_new_strategy) start_new_hyperopt, start_new_strategy)
from freqtrade.commands.automation_commands import (start_custom_hyperopt, start_extract_strategy) from freqtrade.commands.automation_commands import (start_build_hyperopt, start_custom_hyperopt, start_extract_strategy)
from freqtrade.commands.hyperopt_commands import start_hyperopt_list, start_hyperopt_show from freqtrade.commands.hyperopt_commands import start_hyperopt_list, start_hyperopt_show
from freqtrade.commands.list_commands import (start_list_exchanges, start_list_hyperopts, from freqtrade.commands.list_commands import (start_list_exchanges, start_list_hyperopts,
start_list_markets, start_list_strategies, start_list_markets, start_list_strategies,

View File

@ -60,6 +60,8 @@ ARGS_BUILD_CUSTOM_HYPEROPT = ["buy_indicators", "sell_indicators", "hyperopt"]
ARGS_EXTRACT_STRATEGY = ["strategy", "extract_name"] ARGS_EXTRACT_STRATEGY = ["strategy", "extract_name"]
ARGS_BUILD_BUILD_HYPEROPT = ["strategy", "hyperopt"]
ARGS_CONVERT_DATA = ["pairs", "format_from", "format_to", "erase"] ARGS_CONVERT_DATA = ["pairs", "format_from", "format_to", "erase"]
ARGS_CONVERT_DATA_OHLCV = ARGS_CONVERT_DATA + ["timeframes"] ARGS_CONVERT_DATA_OHLCV = ARGS_CONVERT_DATA + ["timeframes"]
@ -178,7 +180,7 @@ class Arguments:
start_list_data, start_list_exchanges, start_list_hyperopts, start_list_data, start_list_exchanges, start_list_hyperopts,
start_list_markets, start_list_strategies, start_list_markets, start_list_strategies,
start_list_timeframes, start_new_config, start_new_hyperopt, start_list_timeframes, start_new_config, start_new_hyperopt,
start_custom_hyperopt, start_extract_strategy, start_build_hyperopt, start_custom_hyperopt, start_extract_strategy,
start_new_strategy, start_plot_dataframe, start_plot_profit, start_new_strategy, start_plot_dataframe, start_plot_profit,
start_show_trades, start_test_pairlist, start_trading) start_show_trades, start_test_pairlist, start_trading)
@ -213,18 +215,24 @@ class Arguments:
build_hyperopt_cmd.set_defaults(func=start_new_hyperopt) build_hyperopt_cmd.set_defaults(func=start_new_hyperopt)
self._build_args(optionlist=ARGS_BUILD_HYPEROPT, parser=build_hyperopt_cmd) self._build_args(optionlist=ARGS_BUILD_HYPEROPT, parser=build_hyperopt_cmd)
# add build-hyperopt subcommand # add custom-hyperopt subcommand
build_custom_hyperopt_cmd = subparsers.add_parser('custom-hyperopt', build_custom_hyperopt_cmd = subparsers.add_parser('custom-hyperopt',
help="Build a custom hyperopt") help="Build a custom hyperopt")
build_custom_hyperopt_cmd.set_defaults(func=start_custom_hyperopt) build_custom_hyperopt_cmd.set_defaults(func=start_custom_hyperopt)
self._build_args(optionlist=ARGS_BUILD_CUSTOM_HYPEROPT, parser=build_custom_hyperopt_cmd) self._build_args(optionlist=ARGS_BUILD_CUSTOM_HYPEROPT, parser=build_custom_hyperopt_cmd)
# add build-hyperopt subcommand # add extract-strategy subcommand
extract_strategy_cmd = subparsers.add_parser('extract-strategy', extract_strategy_cmd = subparsers.add_parser('extract-strategy',
help="Extract data dictionaries for build-hyperopt from strategy") help="Extract data dictionaries for custom-hyperopt from strategy")
extract_strategy_cmd.set_defaults(func=start_extract_strategy) extract_strategy_cmd.set_defaults(func=start_extract_strategy)
self._build_args(optionlist=ARGS_EXTRACT_STRATEGY, parser=extract_strategy_cmd) self._build_args(optionlist=ARGS_EXTRACT_STRATEGY, parser=extract_strategy_cmd)
# add build-hyperopt subcommand
build_extracted_hyperopt_cmd = subparsers.add_parser('build-hyperopt',
help="Create a hyperopt for a strategy")
build_extracted_hyperopt_cmd.set_defaults(func=start_build_hyperopt)
self._build_args(optionlist=ARGS_BUILD_BUILD_HYPEROPT, parser=build_extracted_hyperopt_cmd)
# add new-strategy subcommand # add new-strategy subcommand
build_strategy_cmd = subparsers.add_parser('new-strategy', build_strategy_cmd = subparsers.add_parser('new-strategy',
help="Create new strategy") help="Create new strategy")

View File

@ -17,6 +17,13 @@ logger = logging.getLogger(__name__)
# ---------------------------------------------------extract-strategy------------------------------------------------------ # ---------------------------------------------------extract-strategy------------------------------------------------------
'''
TODO
- get the stoploss value to optimize
- get the values from the guards to specify the optimzation range (cooperation with custom-hyperopt)
'''
def extract_dicts(strategypath: Path): def extract_dicts(strategypath: Path):
# store the file in a list for reference # store the file in a list for reference
stored_file = [] stored_file = []
@ -117,11 +124,11 @@ def start_extract_strategy(args: Dict[str, Any]) -> None:
''' '''
TODO TODO
-make the code below more dynamic with a large list of indicators and aims -make the code below more dynamic with a large list of indicators and aims
-buy_space integer values variation based on aim(later deep learning) -buy_space integer values variation based on aim and input value form extract-strategy(later deep learning)
-add --mode , see notes -add --mode , see notes (denk hierbij ook aan value range bij guards)
-when making the strategy reading tool, make sure that the populate indicators gets copied to here -Custom stoploss and roi (based on input from extract-strategy)
-Custom stoploss and roi
-cli option to read extracted strategies files (--extraction) -cli option to read extracted strategies files (--extraction)
-code cleanup (maybe the two elements functions can be combined)
''' '''
@ -144,20 +151,20 @@ def custom_hyperopt_buyelements(buy_indicators: Dict[str, str]):
# If the indicator is a guard # If the indicator is a guard
elif indicator in POSSIBLE_GUARDS: elif indicator in POSSIBLE_GUARDS:
# get the symbol corrosponding to the value # get the symbol corrosponding to the value
aim = POSSIBLE_AIMS[buy_indicators[indicator]] aim = buy_indicators[indicator]
# add the guard to its argument # add the guard to its argument
buy_guards += f"if '{indicator}-enabled' in params and params['{indicator}-enabled']: conditions.append(dataframe['{indicator}'] {aim} params['{indicator}-value'])" buy_guards += f"if '{indicator}-enabled' in params and params['{indicator}-enabled']:\n conditions.append(dataframe['{indicator}'] {aim} params['{indicator}-value'])\n"
# add the space to its argument # add the space to its argument
buy_space += f"Integer(10, 90, name='{indicator}-value'), Categorical([True, False], name='{indicator}-enabled')," buy_space += f"Integer(10, 90, name='{indicator}-value'),\nCategorical([True, False], name='{indicator}-enabled'),\n"
# If the indicator is a trigger # If the indicator is a trigger
elif indicator in POSSIBLE_TRIGGERS: elif indicator in POSSIBLE_TRIGGERS:
# get the symbol corrosponding to the value # get the symbol corrosponding to the value
aim = POSSIBLE_AIMS[buy_indicators[indicator]] aim = buy_indicators[indicator]
# add the trigger to its argument # add the trigger to its argument
buy_triggers += f"if params['trigger'] == '{indicator}': conditions.append(dataframe['{indicator}'] {aim} dataframe['close'])" buy_triggers += f"if params['trigger'] == '{indicator}':\n conditions.append(dataframe['{indicator}'] {aim} dataframe['close'])\n"
# Final line of indicator space makes all triggers # Final line of indicator space makes all triggers
@ -194,20 +201,20 @@ def custom_hyperopt_sellelements(sell_indicators: Dict[str, str]):
# If indicator is a guard # If indicator is a guard
elif indicator in POSSIBLE_GUARDS: elif indicator in POSSIBLE_GUARDS:
# get the symbol corrosponding to the value # get the symbol corrosponding to the value
aim = POSSIBLE_AIMS[sell_indicators[indicator]] aim = sell_indicators[indicator]
# add the guard to its argument # add the guard to its argument
sell_guards += f"if '{indicator}-enabled' in params and params['sell-{indicator}-enabled']: conditions.append(dataframe['{indicator}'] {aim} params['sell-{indicator}-value'])" sell_guards += f"if '{indicator}-enabled' in params and params['sell-{indicator}-enabled']:\n conditions.append(dataframe['{indicator}'] {aim} params['sell-{indicator}-value'])\n"
# add the space to its argument # add the space to its argument
sell_space += f"Integer(10, 90, name='sell-{indicator}-value'), Categorical([True, False], name='sell-{indicator}-enabled')," sell_space += f"Integer(10, 90, name='sell-{indicator}-value'),\nCategorical([True, False], name='sell-{indicator}-enabled'),\n"
# If the indicator is a trigger # If the indicator is a trigger
elif indicator in POSSIBLE_TRIGGERS: elif indicator in POSSIBLE_TRIGGERS:
# get the symbol corrosponding to the value # get the symbol corrosponding to the value
aim = POSSIBLE_AIMS[sell_indicators[indicator]] aim = sell_indicators[indicator]
# add the trigger to its argument # add the trigger to its argument
sell_triggers += f"if params['sell-trigger'] == 'sell-{indicator}': conditions.append(dataframe['{indicator}'] {aim} dataframe['close'])" sell_triggers += f"if params['sell-trigger'] == 'sell-{indicator}':\n conditions.append(dataframe['{indicator}'] {aim} dataframe['close'])\n"
# Final line of indicator space makes all triggers # Final line of indicator space makes all triggers
@ -276,4 +283,44 @@ def start_custom_hyperopt(args: Dict[str, Any]) -> None:
deploy_custom_hyperopt(args['hyperopt'], new_path, deploy_custom_hyperopt(args['hyperopt'], new_path,
buy_indicators, sell_indicators) buy_indicators, sell_indicators)
# --------------------------------------------------build-hyperopt------------------------------------------------------ # --------------------------------------------------build-hyperopt------------------------------------------------------
'''
TODO
-hyperopt optional (door bv standaard naming toe te passen)
'''
def start_build_hyperopt(args: Dict[str, Any]) -> None:
"""
Check if the right subcommands where passed and start building the hyperopt
"""
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
# strategy and hyperopt need to be defined
if not 'strategy' in args or not args['strategy']:
raise OperationalException("`build-hyperopt` requires --strategy to be set.")
if not 'hyperopt' in args or not args['hyperopt']:
raise OperationalException("`build-hyperopt` requires --hyperopt to be set.")
else:
if args['hyperopt'] == 'DefaultHyperopt':
raise OperationalException("DefaultHyperopt is not allowed as name.")
# the path of the chosen strategy
strategy_path = config['user_data_dir'] / USERPATH_STRATEGIES / (args['strategy'] + '.py')
# the path where the hyperopt should be written
new_path = config['user_data_dir'] / USERPATH_HYPEROPTS / (args['hyperopt'] + '.py')
if new_path.exists():
raise OperationalException(f"`{new_path}` already exists. "
"Please choose another Hyperopt Name.")
# extract the buy and sell indicators as dicts
extracted_dicts = extract_dicts(strategy_path)
buy_indicators = extracted_dicts[0]
sell_indicators = extracted_dicts[1]
# use the dicts to write the hyperopt
deploy_custom_hyperopt(args['hyperopt'], new_path,
buy_indicators, sell_indicators)

View File

@ -388,14 +388,18 @@ TradeList = List[List]
POSSIBLE_GUARDS = [ POSSIBLE_GUARDS = [
"rsi", "rsi",
"mfi", "mfi",
"fastd" "fastd",
"slowk",
"CDLHAMMER",
"fisher_rsi"
] ]
POSSIBLE_TRIGGERS = [ POSSIBLE_TRIGGERS = [
"sar",
"bb_lowerband", "bb_lowerband",
"bb_upperband" "bb_upperband"
] ]
POSSIBLE_AIMS = { POSSIBLE_AIMS = [
"above": ">", ">",
"below": "<", "<",
"equal": "==" "=="
} ]