Added short and exit_short to strategy

This commit is contained in:
Sam Germain
2021-08-08 03:38:34 -06:00
parent 98fe3e73de
commit d4a7d2d444
24 changed files with 862 additions and 152 deletions

View File

@@ -172,3 +172,125 @@ class SampleHyperOpt(IHyperOpt):
return dataframe
return populate_sell_trend
@staticmethod
def short_strategy_generator(params: Dict[str, Any]) -> Callable:
"""
Define the short strategy parameters to be used by Hyperopt.
"""
def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Buy strategy Hyperopt will build and use.
"""
conditions = []
# GUARDS AND TRENDS
if 'mfi-enabled' in params and params['mfi-enabled']:
conditions.append(dataframe['mfi'] > params['mfi-value'])
if 'fastd-enabled' in params and params['fastd-enabled']:
conditions.append(dataframe['fastd'] > params['fastd-value'])
if 'adx-enabled' in params and params['adx-enabled']:
conditions.append(dataframe['adx'] < params['adx-value'])
if 'rsi-enabled' in params and params['rsi-enabled']:
conditions.append(dataframe['rsi'] > params['rsi-value'])
# TRIGGERS
if 'trigger' in params:
if params['trigger'] == 'bb_upper':
conditions.append(dataframe['close'] > dataframe['bb_upperband'])
if params['trigger'] == 'macd_cross_signal':
conditions.append(qtpylib.crossed_below(
dataframe['macd'], dataframe['macdsignal']
))
if params['trigger'] == 'sar_reversal':
conditions.append(qtpylib.crossed_below(
dataframe['close'], dataframe['sar']
))
if conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
'short'] = 1
return dataframe
return populate_short_trend
@staticmethod
def short_indicator_space() -> List[Dimension]:
"""
Define your Hyperopt space for searching short strategy parameters.
"""
return [
Integer(75, 90, name='mfi-value'),
Integer(55, 85, name='fastd-value'),
Integer(50, 80, name='adx-value'),
Integer(60, 80, name='rsi-value'),
Categorical([True, False], name='mfi-enabled'),
Categorical([True, False], name='fastd-enabled'),
Categorical([True, False], name='adx-enabled'),
Categorical([True, False], name='rsi-enabled'),
Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger')
]
@staticmethod
def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable:
"""
Define the exit_short strategy parameters to be used by Hyperopt.
"""
def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Exit_short strategy Hyperopt will build and use.
"""
conditions = []
# GUARDS AND TRENDS
if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']:
conditions.append(dataframe['mfi'] < params['exit-short-mfi-value'])
if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']:
conditions.append(dataframe['fastd'] < params['exit-short-fastd-value'])
if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']:
conditions.append(dataframe['adx'] > params['exit-short-adx-value'])
if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']:
conditions.append(dataframe['rsi'] < params['exit-short-rsi-value'])
# TRIGGERS
if 'exit-short-trigger' in params:
if params['exit-short-trigger'] == 'exit-short-bb_lower':
conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
if params['exit-short-trigger'] == 'exit-short-macd_cross_signal':
conditions.append(qtpylib.crossed_below(
dataframe['macdsignal'], dataframe['macd']
))
if params['exit-short-trigger'] == 'exit-short-sar_reversal':
conditions.append(qtpylib.crossed_below(
dataframe['sar'], dataframe['close']
))
if conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
'exit_short'] = 1
return dataframe
return populate_exit_short_trend
@staticmethod
def exit_short_indicator_space() -> List[Dimension]:
"""
Define your Hyperopt space for searching exit short strategy parameters.
"""
return [
Integer(1, 25, name='exit_short-mfi-value'),
Integer(1, 50, name='exit_short-fastd-value'),
Integer(1, 50, name='exit_short-adx-value'),
Integer(1, 40, name='exit_short-rsi-value'),
Categorical([True, False], name='exit_short-mfi-enabled'),
Categorical([True, False], name='exit_short-fastd-enabled'),
Categorical([True, False], name='exit_short-adx-enabled'),
Categorical([True, False], name='exit_short-rsi-enabled'),
Categorical(['exit_short-bb_lower',
'exit_short-macd_cross_signal',
'exit_short-sar_reversal'], name='exit_short-trigger')
]

View File

@@ -187,9 +187,132 @@ class AdvancedSampleHyperOpt(IHyperOpt):
return populate_sell_trend
@staticmethod
def short_strategy_generator(params: Dict[str, Any]) -> Callable:
"""
Define the short strategy parameters to be used by Hyperopt.
"""
def populate_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Buy strategy Hyperopt will build and use.
"""
conditions = []
# GUARDS AND TRENDS
if 'mfi-enabled' in params and params['mfi-enabled']:
conditions.append(dataframe['mfi'] > params['mfi-value'])
if 'fastd-enabled' in params and params['fastd-enabled']:
conditions.append(dataframe['fastd'] > params['fastd-value'])
if 'adx-enabled' in params and params['adx-enabled']:
conditions.append(dataframe['adx'] < params['adx-value'])
if 'rsi-enabled' in params and params['rsi-enabled']:
conditions.append(dataframe['rsi'] > params['rsi-value'])
# TRIGGERS
if 'trigger' in params:
if params['trigger'] == 'bb_upper':
conditions.append(dataframe['close'] > dataframe['bb_upperband'])
if params['trigger'] == 'macd_cross_signal':
conditions.append(qtpylib.crossed_below(
dataframe['macd'], dataframe['macdsignal']
))
if params['trigger'] == 'sar_reversal':
conditions.append(qtpylib.crossed_below(
dataframe['close'], dataframe['sar']
))
if conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
'short'] = 1
return dataframe
return populate_short_trend
@staticmethod
def short_indicator_space() -> List[Dimension]:
"""
Define your Hyperopt space for searching short strategy parameters.
"""
return [
Integer(75, 90, name='mfi-value'),
Integer(55, 85, name='fastd-value'),
Integer(50, 80, name='adx-value'),
Integer(60, 80, name='rsi-value'),
Categorical([True, False], name='mfi-enabled'),
Categorical([True, False], name='fastd-enabled'),
Categorical([True, False], name='adx-enabled'),
Categorical([True, False], name='rsi-enabled'),
Categorical(['bb_upper', 'macd_cross_signal', 'sar_reversal'], name='trigger')
]
@staticmethod
def exit_short_strategy_generator(params: Dict[str, Any]) -> Callable:
"""
Define the exit_short strategy parameters to be used by Hyperopt.
"""
def populate_exit_short_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Exit_short strategy Hyperopt will build and use.
"""
conditions = []
# GUARDS AND TRENDS
if 'exit-short-mfi-enabled' in params and params['exit-short-mfi-enabled']:
conditions.append(dataframe['mfi'] < params['exit-short-mfi-value'])
if 'exit-short-fastd-enabled' in params and params['exit-short-fastd-enabled']:
conditions.append(dataframe['fastd'] < params['exit-short-fastd-value'])
if 'exit-short-adx-enabled' in params and params['exit-short-adx-enabled']:
conditions.append(dataframe['adx'] > params['exit-short-adx-value'])
if 'exit-short-rsi-enabled' in params and params['exit-short-rsi-enabled']:
conditions.append(dataframe['rsi'] < params['exit-short-rsi-value'])
# TRIGGERS
if 'exit-short-trigger' in params:
if params['exit-short-trigger'] == 'exit-short-bb_lower':
conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
if params['exit-short-trigger'] == 'exit-short-macd_cross_signal':
conditions.append(qtpylib.crossed_below(
dataframe['macdsignal'], dataframe['macd']
))
if params['exit-short-trigger'] == 'exit-short-sar_reversal':
conditions.append(qtpylib.crossed_below(
dataframe['sar'], dataframe['close']
))
if conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
'exit_short'] = 1
return dataframe
return populate_exit_short_trend
@staticmethod
def exit_short_indicator_space() -> List[Dimension]:
"""
Define your Hyperopt space for searching exit short strategy parameters.
"""
return [
Integer(1, 25, name='exit_short-mfi-value'),
Integer(1, 50, name='exit_short-fastd-value'),
Integer(1, 50, name='exit_short-adx-value'),
Integer(1, 40, name='exit_short-rsi-value'),
Categorical([True, False], name='exit_short-mfi-enabled'),
Categorical([True, False], name='exit_short-fastd-enabled'),
Categorical([True, False], name='exit_short-adx-enabled'),
Categorical([True, False], name='exit_short-rsi-enabled'),
Categorical(['exit_short-bb_lower',
'exit_short-macd_cross_signal',
'exit_short-sar_reversal'], name='exit_short-trigger')
]
@staticmethod
def generate_roi_table(params: Dict) -> Dict[int, float]:
"""
# TODO-lev?
Generate the ROI table that will be used by Hyperopt
This implementation generates the default legacy Freqtrade ROI tables.
@@ -211,6 +334,7 @@ class AdvancedSampleHyperOpt(IHyperOpt):
@staticmethod
def roi_space() -> List[Dimension]:
"""
# TODO-lev?
Values to search for each ROI steps
Override it if you need some different ranges for the parameters in the
@@ -231,6 +355,7 @@ class AdvancedSampleHyperOpt(IHyperOpt):
@staticmethod
def stoploss_space() -> List[Dimension]:
"""
# TODO-lev?
Stoploss Value to search
Override it if you need some different range for the parameter in the
@@ -243,6 +368,7 @@ class AdvancedSampleHyperOpt(IHyperOpt):
@staticmethod
def trailing_space() -> List[Dimension]:
"""
# TODO-lev?
Create a trailing stoploss space.
You may override it in your custom Hyperopt class.

View File

@@ -29,7 +29,7 @@ class SampleStrategy(IStrategy):
You must keep:
- the lib in the section "Do not remove these libs"
- the methods: populate_indicators, populate_buy_trend, populate_sell_trend
- the methods: populate_indicators, populate_buy_trend, populate_sell_trend, populate_short_trend, populate_exit_short_trend
You should keep:
- timeframe, minimal_roi, stoploss, trailing_*
"""
@@ -58,6 +58,8 @@ class SampleStrategy(IStrategy):
# Hyperoptable parameters
buy_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True)
sell_rsi = IntParameter(low=50, high=100, default=70, space='sell', optimize=True, load=True)
short_rsi = IntParameter(low=51, high=100, default=70, space='sell', optimize=True, load=True)
exit_short_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True)
# Optimal timeframe for the strategy.
timeframe = '5m'
@@ -373,3 +375,40 @@ class SampleStrategy(IStrategy):
),
'sell'] = 1
return dataframe
def populate_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the short signal for the given dataframe
:param dataframe: DataFrame populated with indicators
:param metadata: Additional information, like the currently traded pair
:return: DataFrame with short column
"""
dataframe.loc[
(
# Signal: RSI crosses above 70
(qtpylib.crossed_above(dataframe['rsi'], self.short_rsi.value)) &
(dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle
(dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'short'] = 1
return dataframe
def populate_exit_short_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the exit_short signal for the given dataframe
:param dataframe: DataFrame populated with indicators
:param metadata: Additional information, like the currently traded pair
:return: DataFrame with exit_short column
"""
dataframe.loc[
(
# Signal: RSI crosses above 30
(qtpylib.crossed_above(dataframe['rsi'], self.exit_short_rsi.value)) &
(dataframe['tema'] <= dataframe['bb_middleband']) & # Guard: tema below BB middle
(dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'exit_short'] = 1
return dataframe