Add callback functions to new-strategy --template advanced

This commit is contained in:
Matthias 2020-03-01 09:35:53 +01:00
parent 7736f8d018
commit 791148176c
5 changed files with 80 additions and 16 deletions

View File

@ -379,9 +379,9 @@ AVAILABLE_CLI_OPTIONS = {
# Templating options # Templating options
"template": Arg( "template": Arg(
'--template', '--template',
help='Use a template which is either `minimal` or ' help='Use a template which is either `minimal`, '
'`full` (containing multiple sample indicators). Default: `%(default)s`.', '`full` (containing multiple sample indicators) or `advanced`. Default: `%(default)s`.',
choices=['full', 'minimal'], choices=['full', 'minimal', 'advanced'],
default='full', default='full',
), ),
# Plot dataframe # Plot dataframe

View File

@ -8,7 +8,7 @@ from freqtrade.configuration.directory_operations import (copy_sample_files,
create_userdata_dir) create_userdata_dir)
from freqtrade.constants import USERPATH_HYPEROPTS, USERPATH_STRATEGIES from freqtrade.constants import USERPATH_HYPEROPTS, USERPATH_STRATEGIES
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.misc import render_template from freqtrade.misc import render_template, render_template_with_fallback
from freqtrade.state import RunMode from freqtrade.state import RunMode
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -32,10 +32,27 @@ def deploy_new_strategy(strategy_name: str, strategy_path: Path, subtemplate: st
""" """
Deploy new strategy from template to strategy_path Deploy new strategy from template to strategy_path
""" """
indicators = render_template(templatefile=f"subtemplates/indicators_{subtemplate}.j2",) fallback = 'full'
buy_trend = render_template(templatefile=f"subtemplates/buy_trend_{subtemplate}.j2",) indicators = render_template_with_fallback(
sell_trend = render_template(templatefile=f"subtemplates/sell_trend_{subtemplate}.j2",) templatefile=f"subtemplates/indicators_{subtemplate}.j2",
plot_config = render_template(templatefile=f"subtemplates/plot_config_{subtemplate}.j2",) templatefallbackfile=f"subtemplates/indicators_{fallback}.j2",
)
buy_trend = render_template_with_fallback(
templatefile=f"subtemplates/buy_trend_{subtemplate}.j2",
templatefallbackfile=f"subtemplates/buy_trend_{fallback}.j2",
)
sell_trend = render_template_with_fallback(
templatefile=f"subtemplates/sell_trend_{subtemplate}.j2",
templatefallbackfile=f"subtemplates/sell_trend_{fallback}.j2",
)
plot_config = render_template_with_fallback(
templatefile=f"subtemplates/plot_config_{subtemplate}.j2",
templatefallbackfile=f"subtemplates/plot_config_{fallback}.j2",
)
additional_methods = render_template_with_fallback(
templatefile=f"subtemplates/strategy_methods_{subtemplate}.j2",
templatefallbackfile=f"subtemplates/strategy_methods_empty.j2",
)
strategy_text = render_template(templatefile='base_strategy.py.j2', strategy_text = render_template(templatefile='base_strategy.py.j2',
arguments={"strategy": strategy_name, arguments={"strategy": strategy_name,
@ -43,6 +60,7 @@ def deploy_new_strategy(strategy_name: str, strategy_path: Path, subtemplate: st
"buy_trend": buy_trend, "buy_trend": buy_trend,
"sell_trend": sell_trend, "sell_trend": sell_trend,
"plot_config": plot_config, "plot_config": plot_config,
"additional_methods": additional_methods,
}) })
logger.info(f"Writing strategy to `{strategy_path}`.") logger.info(f"Writing strategy to `{strategy_path}`.")
@ -73,14 +91,23 @@ def deploy_new_hyperopt(hyperopt_name: str, hyperopt_path: Path, subtemplate: st
""" """
Deploys a new hyperopt template to hyperopt_path Deploys a new hyperopt template to hyperopt_path
""" """
buy_guards = render_template( fallback = 'full'
templatefile=f"subtemplates/hyperopt_buy_guards_{subtemplate}.j2",) buy_guards = render_template_with_fallback(
sell_guards = render_template( templatefile=f"subtemplates/hyperopt_buy_guards_{subtemplate}.j2",
templatefile=f"subtemplates/hyperopt_sell_guards_{subtemplate}.j2",) templatefallbackfile=f"subtemplates/hyperopt_buy_guards_{fallback}.j2",
buy_space = render_template( )
templatefile=f"subtemplates/hyperopt_buy_space_{subtemplate}.j2",) sell_guards = render_template_with_fallback(
sell_space = render_template( templatefile=f"subtemplates/hyperopt_sell_guards_{subtemplate}.j2",
templatefile=f"subtemplates/hyperopt_sell_space_{subtemplate}.j2",) templatefallbackfile=f"subtemplates/hyperopt_sell_guards_{fallback}.j2",
)
buy_space = render_template_with_fallback(
templatefile=f"subtemplates/hyperopt_buy_space_{subtemplate}.j2",
templatefallbackfile=f"subtemplates/hyperopt_buy_space_{fallback}.j2",
)
sell_space = render_template_with_fallback(
templatefile=f"subtemplates/hyperopt_sell_space_{subtemplate}.j2",
templatefallbackfile=f"subtemplates/hyperopt_sell_space_{fallback}.j2",
)
strategy_text = render_template(templatefile='base_hyperopt.py.j2', strategy_text = render_template(templatefile='base_hyperopt.py.j2',
arguments={"hyperopt": hyperopt_name, arguments={"hyperopt": hyperopt_name,

View File

@ -137,3 +137,4 @@ class {{ strategy }}(IStrategy):
), ),
'sell'] = 1 'sell'] = 1
return dataframe return dataframe
{{ additional_methods | indent(4) }}

View File

@ -0,0 +1,36 @@
def check_buy_timeout(self, pair: str, trade: Trade, order: Dict, **kwargs) -> bool:
"""
Check buy timeout function callback.
This method can be used to override the buy-timeout.
It is called whenever a limit buy order has been created,
and is not yet fully filled.
Configuration options in `unfilledtimeout` will be verified before this,
so ensure to set these timeouts high enough.
When not implemented by a strategy, this simply returns False.
:param pair: Pair the trade is for
:param trade: trade object.
:param order: Order dictionary as returned from CCXT.
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the buy-order is cancelled.
"""
return False
def check_sell_timeout(self, pair: str, trade: Trade, order: Dict, **kwargs) -> bool:
"""
Check sell timeout function callback.
This method can be used to override the sell-timeout.
It is called whenever a limit sell order has been created,
and is not yet fully filled.
Configuration options in `unfilledtimeout` will be verified before this,
so ensure to set these timeouts high enough.
When not implemented by a strategy, this simply returns False.
:param pair: Pair the trade is for
:param trade: trade object.
:param order: Order dictionary as returned from CCXT.
:param **kwargs: Ensure to keep this here so updates to this won't break your strategy.
:return bool: When True is returned, then the sell-order is cancelled.
"""
return False