condensed strategy methods down to 2

This commit is contained in:
Sam Germain
2021-08-18 04:19:17 -06:00
parent d4a7d2d444
commit 092780df9d
22 changed files with 451 additions and 773 deletions

View File

@@ -167,14 +167,12 @@ class Edge:
pair_data = pair_data.sort_values(by=['date'])
pair_data = pair_data.reset_index(drop=True)
df_analyzed = self.strategy.advise_exit(
dataframe=self.strategy.advise_enter(
df_analyzed = self.strategy.advise_sell(
dataframe=self.strategy.advise_buy(
dataframe=pair_data,
metadata={'pair': pair},
is_short=False
metadata={'pair': pair}
),
metadata={'pair': pair},
is_short=False
metadata={'pair': pair}
)[headers].copy()
trades += self._find_trades_for_stoploss_range(df_analyzed, pair, self._stoploss_range)

View File

@@ -16,4 +16,4 @@ class SignalTagType(Enum):
Enum for signal columns
"""
BUY_TAG = "buy_tag"
SELL_TAG = "sell_tag"
SHORT_TAG = "short_tag"

View File

@@ -231,8 +231,13 @@ class Backtesting:
if has_buy_tag:
pair_data.loc[:, 'buy_tag'] = None # cleanup if buy_tag is exist
df_analyzed = self.strategy.advise_exit(
self.strategy.advise_enter(pair_data, {'pair': pair}), {'pair': pair}).copy()
df_analyzed = self.strategy.advise_sell(
self.strategy.advise_buy(
pair_data,
{'pair': pair}
),
{'pair': pair}
).copy()
# Trim startup period from analyzed dataframe
df_analyzed = trim_dataframe(df_analyzed, self.timerange,
startup_candles=self.required_startup)

View File

@@ -110,7 +110,7 @@ class Hyperopt:
self.backtesting.strategy.advise_indicators = ( # type: ignore
self.custom_hyperopt.populate_indicators) # type: ignore
if hasattr(self.custom_hyperopt, 'populate_buy_trend'):
self.backtesting.strategy.advise_enter = ( # type: ignore
self.backtesting.strategy.advise_buy = ( # type: ignore
self.custom_hyperopt.populate_buy_trend) # type: ignore
if hasattr(self.custom_hyperopt, 'populate_sell_trend'):
self.backtesting.strategy.advise_sell = ( # type: ignore
@@ -283,14 +283,15 @@ class Hyperopt:
params_dict = self._get_params_dict(self.dimensions, raw_params)
# Apply parameters
# TODO-lev: These don't take a side, how can I pass is_short=True/False to it
if HyperoptTools.has_space(self.config, 'buy'):
self.backtesting.strategy.advise_enter = ( # type: ignore
self.custom_hyperopt.buy_strategy_generator(params_dict))
self.backtesting.strategy.advise_buy = ( # type: ignore
self.custom_hyperopt.buy_strategy_generator(params_dict)
)
if HyperoptTools.has_space(self.config, 'sell'):
self.backtesting.strategy.advise_exit = ( # type: ignore
self.custom_hyperopt.sell_strategy_generator(params_dict))
self.backtesting.strategy.advise_sell = ( # type: ignore
self.custom_hyperopt.sell_strategy_generator(params_dict)
)
if HyperoptTools.has_space(self.config, 'protection'):
for attr_name, attr in self.backtesting.strategy.enumerate_parameters('protection'):

View File

@@ -202,14 +202,11 @@ class StrategyResolver(IResolver):
strategy._populate_fun_len = len(getfullargspec(strategy.populate_indicators).args)
strategy._buy_fun_len = len(getfullargspec(strategy.populate_buy_trend).args)
strategy._sell_fun_len = len(getfullargspec(strategy.populate_sell_trend).args)
strategy._short_fun_len = len(getfullargspec(strategy.populate_short_trend).args)
strategy._exit_short_fun_len = len(
getfullargspec(strategy.populate_exit_short_trend).args)
if any(x == 2 for x in [strategy._populate_fun_len,
strategy._buy_fun_len,
strategy._sell_fun_len,
strategy._short_fun_len,
strategy._exit_short_fun_len]):
if any(x == 2 for x in [
strategy._populate_fun_len,
strategy._buy_fun_len,
strategy._sell_fun_len
]):
strategy.INTERFACE_VERSION = 1
return strategy

View File

@@ -44,5 +44,5 @@ class UvicornServer(uvicorn.Server):
time.sleep(1e-3)
def cleanup(self):
self.should_sell = True
self.should_exit = True
self.thread.join()

View File

@@ -62,8 +62,6 @@ class IStrategy(ABC, HyperStrategyMixin):
_populate_fun_len: int = 0
_buy_fun_len: int = 0
_sell_fun_len: int = 0
_short_fun_len: int = 0
_exit_short_fun_len: int = 0
_ft_params_from_file: Dict = {}
# associated minimal roi
minimal_roi: Dict
@@ -145,7 +143,7 @@ class IStrategy(ABC, HyperStrategyMixin):
return dataframe
@abstractmethod
def populate_enter_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the buy signal for the given dataframe
:param dataframe: DataFrame
@@ -155,7 +153,7 @@ class IStrategy(ABC, HyperStrategyMixin):
return dataframe
@abstractmethod
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the sell signal for the given dataframe
:param dataframe: DataFrame
@@ -166,7 +164,7 @@ class IStrategy(ABC, HyperStrategyMixin):
def check_buy_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool:
"""
Check enter timeout function callback.
Check buy timeout function callback.
This method can be used to override the enter-timeout.
It is called whenever a limit buy/short order has been created,
and is not yet fully filled.
@@ -184,7 +182,7 @@ class IStrategy(ABC, HyperStrategyMixin):
def check_sell_timeout(self, pair: str, trade: Trade, order: dict, **kwargs) -> bool:
"""
Check exit timeout function callback.
Check sell timeout function callback.
This method can be used to override the exit-timeout.
It is called whenever a (long) limit sell order or (short) limit buy
has been created, and is not yet fully filled.
@@ -396,10 +394,8 @@ class IStrategy(ABC, HyperStrategyMixin):
"""
logger.debug("TA Analysis Launched")
dataframe = self.advise_indicators(dataframe, metadata)
dataframe = self.advise_enter(dataframe, metadata, is_short=False)
dataframe = self.advise_exit(dataframe, metadata, is_short=False)
dataframe = self.advise_enter(dataframe, metadata, is_short=True)
dataframe = self.advise_exit(dataframe, metadata, is_short=True)
dataframe = self.advise_buy(dataframe, metadata)
dataframe = self.advise_sell(dataframe, metadata)
return dataframe
def _analyze_ticker_internal(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
@@ -426,7 +422,7 @@ class IStrategy(ABC, HyperStrategyMixin):
logger.debug("Skipping TA Analysis for already analyzed candle")
dataframe['buy'] = 0
dataframe['sell'] = 0
dataframe['short'] = 0
dataframe['enter_short'] = 0
dataframe['exit_short'] = 0
dataframe['buy_tag'] = None
dataframe['short_tag'] = None
@@ -572,8 +568,8 @@ class IStrategy(ABC, HyperStrategyMixin):
else:
return False
def should_sell(self, trade: Trade, rate: float, date: datetime, enter: bool,
exit: bool, low: float = None, high: float = None,
def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool,
sell: bool, low: float = None, high: float = None,
force_stoploss: float = 0) -> SellCheckTuple:
"""
This function evaluates if one of the conditions required to trigger a sell/exit_short
@@ -597,7 +593,7 @@ class IStrategy(ABC, HyperStrategyMixin):
current_profit = trade.calc_profit_ratio(current_rate)
# if enter signal and ignore_roi is set, we don't need to evaluate min_roi.
roi_reached = (not (enter and self.ignore_roi_if_buy_signal)
roi_reached = (not (buy and self.ignore_roi_if_buy_signal)
and self.min_roi_reached(trade=trade, current_profit=current_profit,
current_time=date))
@@ -610,8 +606,8 @@ class IStrategy(ABC, HyperStrategyMixin):
if (self.sell_profit_only and current_profit <= self.sell_profit_offset):
# sell_profit_only and profit doesn't reach the offset - ignore sell signal
pass
elif self.use_sell_signal and not enter:
if exit:
elif self.use_sell_signal and not buy:
if sell:
sell_signal = SellType.SELL_SIGNAL
else:
trade_type = "exit_short" if trade.is_short else "sell"
@@ -759,7 +755,7 @@ class IStrategy(ABC, HyperStrategyMixin):
def ohlcvdata_to_dataframe(self, data: Dict[str, DataFrame]) -> Dict[str, DataFrame]:
"""
Populates indicators for given candle (OHLCV) data (for multiple pairs)
Does not run advise_enter or advise_exit!
Does not run advise_buy or advise_sell!
Used by optimize operations only, not during dry / live runs.
Using .copy() to get a fresh copy of the dataframe for every strategy run.
Has positive effects on memory usage for whatever reason - also when
@@ -784,12 +780,7 @@ class IStrategy(ABC, HyperStrategyMixin):
else:
return self.populate_indicators(dataframe, metadata)
def advise_enter(
self,
dataframe: DataFrame,
metadata: dict,
is_short: bool = False
) -> DataFrame:
def advise_buy(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the buy/short signal for the given dataframe
This method should not be overridden.
@@ -798,27 +789,17 @@ class IStrategy(ABC, HyperStrategyMixin):
currently traded pair
:return: DataFrame with buy column
"""
(type, fun_len) = (
("short", self._short_fun_len)
if is_short else
("buy", self._buy_fun_len)
)
logger.debug(f"Populating {type} signals for pair {metadata.get('pair')}.")
logger.debug(f"Populating enter signals for pair {metadata.get('pair')}.")
if fun_len == 2:
if self._buy_fun_len == 2:
warnings.warn("deprecated - check out the Sample strategy to see "
"the current function headers!", DeprecationWarning)
return self.populate_enter_trend(dataframe) # type: ignore
return self.populate_buy_trend(dataframe) # type: ignore
else:
return self.populate_enter_trend(dataframe, metadata)
return self.populate_buy_trend(dataframe, metadata)
def advise_exit(
self,
dataframe: DataFrame,
metadata: dict,
is_short: bool = False
) -> DataFrame:
def advise_sell(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the sell/exit_short signal for the given dataframe
This method should not be overridden.
@@ -828,16 +809,26 @@ class IStrategy(ABC, HyperStrategyMixin):
:return: DataFrame with sell column
"""
(type, fun_len) = (
("exit_short", self._exit_short_fun_len)
if is_short else
("sell", self._sell_fun_len)
)
logger.debug(f"Populating {type} signals for pair {metadata.get('pair')}.")
if fun_len == 2:
logger.debug(f"Populating exit signals for pair {metadata.get('pair')}.")
if self._sell_fun_len == 2:
warnings.warn("deprecated - check out the Sample strategy to see "
"the current function headers!", DeprecationWarning)
return self.populate_exit_trend(dataframe) # type: ignore
return self.populate_sell_trend(dataframe) # type: ignore
else:
return self.populate_exit_trend(dataframe, metadata)
return self.populate_sell_trend(dataframe, metadata)
def leverage(self, pair: str, current_time: datetime, current_rate: float,
proposed_leverage: float, max_leverage: float,
**kwargs) -> float:
"""
Customize leverage for each new trade. This method is not called when edge module is
enabled.
:param pair: Pair that's currently analyzed
:param current_time: datetime object, containing the current datetime
:param current_rate: Rate, calculated based on pricing settings in ask_strategy.
:param proposed_leverage: A leverage proposed by the bot.
:param max_leverage: Max leverage allowed on this pair
:return: A stake size, which is between min_stake and max_stake.
"""
return proposed_leverage

View File

@@ -1,5 +1,6 @@
import pandas as pd
from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes
@@ -83,7 +84,13 @@ def stoploss_from_open(
if current_profit == -1:
return 1
stoploss = 1-((1+open_relative_stop)/(1+current_profit)) # TODO-lev: Is this right?
if for_short is True:
# TODO-lev: How would this be calculated for short
raise OperationalException(
"Freqtrade hasn't figured out how to calculated stoploss on shorts")
# stoploss = 1-((1+open_relative_stop)/(1+current_profit))
else:
stoploss = 1-((1+open_relative_stop)/(1+current_profit))
# negative stoploss values indicate the requested stop price is higher than the current price
if for_short:

View File

@@ -46,7 +46,7 @@ class SampleHyperOpt(IHyperOpt):
"""
@staticmethod
def indicator_space() -> List[Dimension]:
def buy_indicator_space() -> List[Dimension]:
"""
Define your Hyperopt space for searching buy strategy parameters.
"""
@@ -55,11 +55,16 @@ class SampleHyperOpt(IHyperOpt):
Integer(15, 45, name='fastd-value'),
Integer(20, 50, name='adx-value'),
Integer(20, 40, name='rsi-value'),
Integer(75, 90, name='short-mfi-value'),
Integer(55, 85, name='short-fastd-value'),
Integer(50, 80, name='short-adx-value'),
Integer(60, 80, name='short-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_lower', 'macd_cross_signal', 'sar_reversal'], name='trigger')
Categorical(['boll', 'macd_cross_signal', 'sar_reversal'], name='trigger'),
]
@staticmethod
@@ -71,39 +76,61 @@ class SampleHyperOpt(IHyperOpt):
"""
Buy strategy Hyperopt will build and use.
"""
conditions = []
long_conditions = []
short_conditions = []
# GUARDS AND TRENDS
if 'mfi-enabled' in params and params['mfi-enabled']:
conditions.append(dataframe['mfi'] < params['mfi-value'])
long_conditions.append(dataframe['mfi'] < params['mfi-value'])
short_conditions.append(dataframe['mfi'] > params['short-mfi-value'])
if 'fastd-enabled' in params and params['fastd-enabled']:
conditions.append(dataframe['fastd'] < params['fastd-value'])
long_conditions.append(dataframe['fastd'] < params['fastd-value'])
short_conditions.append(dataframe['fastd'] > params['short-fastd-value'])
if 'adx-enabled' in params and params['adx-enabled']:
conditions.append(dataframe['adx'] > params['adx-value'])
long_conditions.append(dataframe['adx'] > params['adx-value'])
short_conditions.append(dataframe['adx'] < params['short-adx-value'])
if 'rsi-enabled' in params and params['rsi-enabled']:
conditions.append(dataframe['rsi'] < params['rsi-value'])
long_conditions.append(dataframe['rsi'] < params['rsi-value'])
short_conditions.append(dataframe['rsi'] > params['short-rsi-value'])
# TRIGGERS
if 'trigger' in params:
if params['trigger'] == 'bb_lower':
conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
if params['trigger'] == 'boll':
long_conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
short_conditions.append(dataframe['close'] > dataframe['bb_upperband'])
if params['trigger'] == 'macd_cross_signal':
conditions.append(qtpylib.crossed_above(
dataframe['macd'], dataframe['macdsignal']
long_conditions.append(qtpylib.crossed_above(
dataframe['macd'],
dataframe['macdsignal']
))
short_conditions.append(qtpylib.crossed_below(
dataframe['macd'],
dataframe['macdsignal']
))
if params['trigger'] == 'sar_reversal':
conditions.append(qtpylib.crossed_above(
dataframe['close'], dataframe['sar']
long_conditions.append(qtpylib.crossed_above(
dataframe['close'],
dataframe['sar']
))
short_conditions.append(qtpylib.crossed_below(
dataframe['close'],
dataframe['sar']
))
# Check that volume is not 0
conditions.append(dataframe['volume'] > 0)
long_conditions.append(dataframe['volume'] > 0)
short_conditions.append(dataframe['volume'] > 0)
if conditions:
if long_conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
reduce(lambda x, y: x & y, long_conditions),
'buy'] = 1
if short_conditions:
dataframe.loc[
reduce(lambda x, y: x & y, short_conditions),
'enter_short'] = 1
return dataframe
return populate_buy_trend
@@ -118,13 +145,19 @@ class SampleHyperOpt(IHyperOpt):
Integer(50, 100, name='sell-fastd-value'),
Integer(50, 100, name='sell-adx-value'),
Integer(60, 100, name='sell-rsi-value'),
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='sell-mfi-enabled'),
Categorical([True, False], name='sell-fastd-enabled'),
Categorical([True, False], name='sell-adx-enabled'),
Categorical([True, False], name='sell-rsi-enabled'),
Categorical(['sell-bb_upper',
Categorical(['sell-boll',
'sell-macd_cross_signal',
'sell-sar_reversal'], name='sell-trigger')
'sell-sar_reversal'],
name='sell-trigger'
),
]
@staticmethod
@@ -136,161 +169,61 @@ class SampleHyperOpt(IHyperOpt):
"""
Sell strategy Hyperopt will build and use.
"""
conditions = []
exit_long_conditions = []
exit_short_conditions = []
# GUARDS AND TRENDS
if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']:
conditions.append(dataframe['mfi'] > params['sell-mfi-value'])
exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value'])
exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value'])
if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']:
conditions.append(dataframe['fastd'] > params['sell-fastd-value'])
exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value'])
exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value'])
if 'sell-adx-enabled' in params and params['sell-adx-enabled']:
conditions.append(dataframe['adx'] < params['sell-adx-value'])
exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value'])
exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value'])
if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']:
conditions.append(dataframe['rsi'] > params['sell-rsi-value'])
exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value'])
exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value'])
# TRIGGERS
if 'sell-trigger' in params:
if params['sell-trigger'] == 'sell-bb_upper':
conditions.append(dataframe['close'] > dataframe['bb_upperband'])
if params['sell-trigger'] == 'sell-boll':
exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband'])
exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
if params['sell-trigger'] == 'sell-macd_cross_signal':
conditions.append(qtpylib.crossed_above(
dataframe['macdsignal'], dataframe['macd']
exit_long_conditions.append(qtpylib.crossed_above(
dataframe['macdsignal'],
dataframe['macd']
))
exit_short_conditions.append(qtpylib.crossed_below(
dataframe['macdsignal'],
dataframe['macd']
))
if params['sell-trigger'] == 'sell-sar_reversal':
conditions.append(qtpylib.crossed_above(
dataframe['sar'], dataframe['close']
exit_long_conditions.append(qtpylib.crossed_above(
dataframe['sar'],
dataframe['close']
))
exit_short_conditions.append(qtpylib.crossed_below(
dataframe['sar'],
dataframe['close']
))
# Check that volume is not 0
conditions.append(dataframe['volume'] > 0)
exit_long_conditions.append(dataframe['volume'] > 0)
exit_short_conditions.append(dataframe['volume'] > 0)
if conditions:
if exit_long_conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
reduce(lambda x, y: x & y, exit_long_conditions),
'sell'] = 1
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:
if exit_short_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),
reduce(lambda x, y: x & y, exit_short_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')
]
return populate_sell_trend

View File

@@ -70,11 +70,15 @@ class AdvancedSampleHyperOpt(IHyperOpt):
Integer(15, 45, name='fastd-value'),
Integer(20, 50, name='adx-value'),
Integer(20, 40, name='rsi-value'),
Integer(75, 90, name='short-mfi-value'),
Integer(55, 85, name='short-fastd-value'),
Integer(50, 80, name='short-adx-value'),
Integer(60, 80, name='short-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_lower', 'macd_cross_signal', 'sar_reversal'], name='trigger')
Categorical(['boll', 'macd_cross_signal', 'sar_reversal'], name='trigger')
]
@staticmethod
@@ -86,38 +90,60 @@ class AdvancedSampleHyperOpt(IHyperOpt):
"""
Buy strategy Hyperopt will build and use
"""
conditions = []
long_conditions = []
short_conditions = []
# GUARDS AND TRENDS
if 'mfi-enabled' in params and params['mfi-enabled']:
conditions.append(dataframe['mfi'] < params['mfi-value'])
long_conditions.append(dataframe['mfi'] < params['mfi-value'])
short_conditions.append(dataframe['mfi'] > params['short-mfi-value'])
if 'fastd-enabled' in params and params['fastd-enabled']:
conditions.append(dataframe['fastd'] < params['fastd-value'])
long_conditions.append(dataframe['fastd'] < params['fastd-value'])
short_conditions.append(dataframe['fastd'] > params['short-fastd-value'])
if 'adx-enabled' in params and params['adx-enabled']:
conditions.append(dataframe['adx'] > params['adx-value'])
long_conditions.append(dataframe['adx'] > params['adx-value'])
short_conditions.append(dataframe['adx'] < params['short-adx-value'])
if 'rsi-enabled' in params and params['rsi-enabled']:
conditions.append(dataframe['rsi'] < params['rsi-value'])
long_conditions.append(dataframe['rsi'] < params['rsi-value'])
short_conditions.append(dataframe['rsi'] > params['short-rsi-value'])
# TRIGGERS
if 'trigger' in params:
if params['trigger'] == 'bb_lower':
conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
if params['trigger'] == 'boll':
long_conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
short_conditions.append(dataframe['close'] > dataframe['bb_upperband'])
if params['trigger'] == 'macd_cross_signal':
conditions.append(qtpylib.crossed_above(
dataframe['macd'], dataframe['macdsignal']
long_conditions.append(qtpylib.crossed_above(
dataframe['macd'],
dataframe['macdsignal']
))
short_conditions.append(qtpylib.crossed_below(
dataframe['macd'],
dataframe['macdsignal']
))
if params['trigger'] == 'sar_reversal':
conditions.append(qtpylib.crossed_above(
dataframe['close'], dataframe['sar']
long_conditions.append(qtpylib.crossed_above(
dataframe['close'],
dataframe['sar']
))
short_conditions.append(qtpylib.crossed_below(
dataframe['close'],
dataframe['sar']
))
# Check that volume is not 0
conditions.append(dataframe['volume'] > 0)
long_conditions.append(dataframe['volume'] > 0)
short_conditions.append(dataframe['volume'] > 0)
if conditions:
if long_conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
reduce(lambda x, y: x & y, long_conditions),
'buy'] = 1
if short_conditions:
dataframe.loc[
reduce(lambda x, y: x & y, short_conditions),
'enter_short'] = 1
return dataframe
return populate_buy_trend
@@ -132,13 +158,18 @@ class AdvancedSampleHyperOpt(IHyperOpt):
Integer(50, 100, name='sell-fastd-value'),
Integer(50, 100, name='sell-adx-value'),
Integer(60, 100, name='sell-rsi-value'),
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='sell-mfi-enabled'),
Categorical([True, False], name='sell-fastd-enabled'),
Categorical([True, False], name='sell-adx-enabled'),
Categorical([True, False], name='sell-rsi-enabled'),
Categorical(['sell-bb_upper',
Categorical(['sell-boll',
'sell-macd_cross_signal',
'sell-sar_reversal'], name='sell-trigger')
'sell-sar_reversal'],
name='sell-trigger')
]
@staticmethod
@@ -151,163 +182,63 @@ class AdvancedSampleHyperOpt(IHyperOpt):
Sell strategy Hyperopt will build and use
"""
# print(params)
conditions = []
exit_long_conditions = []
exit_short_conditions = []
# GUARDS AND TRENDS
if 'sell-mfi-enabled' in params and params['sell-mfi-enabled']:
conditions.append(dataframe['mfi'] > params['sell-mfi-value'])
exit_long_conditions.append(dataframe['mfi'] > params['sell-mfi-value'])
exit_short_conditions.append(dataframe['mfi'] < params['exit-short-mfi-value'])
if 'sell-fastd-enabled' in params and params['sell-fastd-enabled']:
conditions.append(dataframe['fastd'] > params['sell-fastd-value'])
exit_long_conditions.append(dataframe['fastd'] > params['sell-fastd-value'])
exit_short_conditions.append(dataframe['fastd'] < params['exit-short-fastd-value'])
if 'sell-adx-enabled' in params and params['sell-adx-enabled']:
conditions.append(dataframe['adx'] < params['sell-adx-value'])
exit_long_conditions.append(dataframe['adx'] < params['sell-adx-value'])
exit_short_conditions.append(dataframe['adx'] > params['exit-short-adx-value'])
if 'sell-rsi-enabled' in params and params['sell-rsi-enabled']:
conditions.append(dataframe['rsi'] > params['sell-rsi-value'])
exit_long_conditions.append(dataframe['rsi'] > params['sell-rsi-value'])
exit_short_conditions.append(dataframe['rsi'] < params['exit-short-rsi-value'])
# TRIGGERS
if 'sell-trigger' in params:
if params['sell-trigger'] == 'sell-bb_upper':
conditions.append(dataframe['close'] > dataframe['bb_upperband'])
if params['sell-trigger'] == 'sell-boll':
exit_long_conditions.append(dataframe['close'] > dataframe['bb_upperband'])
exit_short_conditions.append(dataframe['close'] < dataframe['bb_lowerband'])
if params['sell-trigger'] == 'sell-macd_cross_signal':
conditions.append(qtpylib.crossed_above(
dataframe['macdsignal'], dataframe['macd']
exit_long_conditions.append(qtpylib.crossed_above(
dataframe['macdsignal'],
dataframe['macd']
))
exit_long_conditions.append(qtpylib.crossed_below(
dataframe['macdsignal'],
dataframe['macd']
))
if params['sell-trigger'] == 'sell-sar_reversal':
conditions.append(qtpylib.crossed_above(
dataframe['sar'], dataframe['close']
exit_long_conditions.append(qtpylib.crossed_above(
dataframe['sar'],
dataframe['close']
))
exit_long_conditions.append(qtpylib.crossed_below(
dataframe['sar'],
dataframe['close']
))
# Check that volume is not 0
conditions.append(dataframe['volume'] > 0)
exit_long_conditions.append(dataframe['volume'] > 0)
exit_short_conditions.append(dataframe['volume'] > 0)
if conditions:
if exit_long_conditions:
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
reduce(lambda x, y: x & y, exit_long_conditions),
'sell'] = 1
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:
if exit_short_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),
reduce(lambda x, y: x & y, exit_short_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')
]
return populate_sell_trend
@staticmethod
def generate_roi_table(params: Dict) -> Dict[int, float]:

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, populate_short_trend, populate_exit_short_trend
- the methods: populate_indicators, populate_buy_trend, populate_sell_trend
You should keep:
- timeframe, minimal_roi, stoploss, trailing_*
"""
@@ -356,6 +356,16 @@ class SampleStrategy(IStrategy):
),
'buy'] = 1
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
),
'enter_short'] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
@@ -374,38 +384,13 @@ class SampleStrategy(IStrategy):
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'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
# Guard: tema below BB middle
(dataframe['tema'] <= dataframe['bb_middleband']) &
(dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising
(dataframe['volume'] > 0) # Make sure Volume is not 0
),