2021-03-27 10:26:26 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
|
|
|
|
|
|
|
|
import talib.abstract as ta
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
|
|
import freqtrade.vendor.qtpylib.indicators as qtpylib
|
2021-08-04 18:52:56 +00:00
|
|
|
from freqtrade.strategy import (BooleanParameter, DecimalParameter, IntParameter, IStrategy,
|
|
|
|
RealParameter)
|
2021-03-27 10:26:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HyperoptableStrategy(IStrategy):
|
|
|
|
"""
|
|
|
|
Default Strategy provided by freqtrade bot.
|
|
|
|
Please do not modify this strategy, it's intended for internal use only.
|
|
|
|
Please look at the SampleStrategy in the user_data/strategy directory
|
|
|
|
or strategy repository https://github.com/freqtrade/freqtrade-strategies
|
|
|
|
for samples and inspiration.
|
|
|
|
"""
|
|
|
|
INTERFACE_VERSION = 2
|
|
|
|
|
|
|
|
# Minimal ROI designed for the strategy
|
|
|
|
minimal_roi = {
|
|
|
|
"40": 0.0,
|
|
|
|
"30": 0.01,
|
|
|
|
"20": 0.02,
|
|
|
|
"0": 0.04
|
|
|
|
}
|
|
|
|
|
|
|
|
# Optimal stoploss designed for the strategy
|
|
|
|
stoploss = -0.10
|
|
|
|
|
|
|
|
# Optimal ticker interval for the strategy
|
|
|
|
timeframe = '5m'
|
|
|
|
|
|
|
|
# Optional order type mapping
|
|
|
|
order_types = {
|
|
|
|
'buy': 'limit',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': False
|
|
|
|
}
|
|
|
|
|
|
|
|
# Number of candles the strategy requires before producing valid signals
|
|
|
|
startup_candle_count: int = 20
|
|
|
|
|
|
|
|
# Optional time in force for orders
|
|
|
|
order_time_in_force = {
|
2022-03-07 06:09:01 +00:00
|
|
|
'entry': 'gtc',
|
|
|
|
'exit': 'gtc',
|
2021-03-27 10:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buy_params = {
|
|
|
|
'buy_rsi': 35,
|
|
|
|
# Intentionally not specified, so "default" is tested
|
|
|
|
# 'buy_plusdi': 0.4
|
|
|
|
}
|
|
|
|
|
|
|
|
sell_params = {
|
2021-03-28 17:49:20 +00:00
|
|
|
'sell_rsi': 74,
|
|
|
|
'sell_minusdi': 0.4
|
2021-03-27 10:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buy_rsi = IntParameter([0, 50], default=30, space='buy')
|
2021-04-01 07:17:39 +00:00
|
|
|
buy_plusdi = RealParameter(low=0, high=1, default=0.5, space='buy')
|
2021-03-27 10:26:26 +00:00
|
|
|
sell_rsi = IntParameter(low=50, high=100, default=70, space='sell')
|
2021-04-01 07:17:39 +00:00
|
|
|
sell_minusdi = DecimalParameter(low=0, high=1, default=0.5001, decimals=3, space='sell',
|
|
|
|
load=False)
|
2021-08-04 18:52:56 +00:00
|
|
|
protection_enabled = BooleanParameter(default=True)
|
2021-08-04 18:01:28 +00:00
|
|
|
protection_cooldown_lookback = IntParameter([0, 50], default=30)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def protections(self):
|
|
|
|
prot = []
|
2021-08-04 18:52:56 +00:00
|
|
|
if self.protection_enabled.value:
|
|
|
|
prot.append({
|
|
|
|
"method": "CooldownPeriod",
|
|
|
|
"stop_duration_candles": self.protection_cooldown_lookback.value
|
|
|
|
})
|
2021-08-04 18:01:28 +00:00
|
|
|
return prot
|
2021-03-27 10:26:26 +00:00
|
|
|
|
|
|
|
def informative_pairs(self):
|
|
|
|
"""
|
|
|
|
Define additional, informative pair/interval combinations to be cached from the exchange.
|
|
|
|
These pair/interval combinations are non-tradeable, unless they are part
|
|
|
|
of the whitelist as well.
|
|
|
|
For more information, please consult the documentation
|
|
|
|
:return: List of tuples in the format (pair, interval)
|
|
|
|
Sample: return [("ETH/USDT", "5m"),
|
|
|
|
("BTC/USDT", "15m"),
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
|
|
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
|
|
|
"""
|
|
|
|
Adds several different TA indicators to the given DataFrame
|
|
|
|
|
|
|
|
Performance Note: For the best performance be frugal on the number of indicators
|
|
|
|
you are using. Let uncomment only the indicator you are using in your strategies
|
|
|
|
or your hyperopt configuration, otherwise you will waste your memory and CPU usage.
|
|
|
|
:param dataframe: Dataframe with data from the exchange
|
|
|
|
:param metadata: Additional information, like the currently traded pair
|
|
|
|
:return: a Dataframe with all mandatory indicators for the strategies
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Momentum Indicator
|
|
|
|
# ------------------------------------
|
|
|
|
|
|
|
|
# ADX
|
|
|
|
dataframe['adx'] = ta.ADX(dataframe)
|
|
|
|
|
|
|
|
# MACD
|
|
|
|
macd = ta.MACD(dataframe)
|
|
|
|
dataframe['macd'] = macd['macd']
|
|
|
|
dataframe['macdsignal'] = macd['macdsignal']
|
|
|
|
dataframe['macdhist'] = macd['macdhist']
|
|
|
|
|
|
|
|
# Minus Directional Indicator / Movement
|
|
|
|
dataframe['minus_di'] = ta.MINUS_DI(dataframe)
|
|
|
|
|
|
|
|
# Plus Directional Indicator / Movement
|
|
|
|
dataframe['plus_di'] = ta.PLUS_DI(dataframe)
|
|
|
|
|
|
|
|
# RSI
|
|
|
|
dataframe['rsi'] = ta.RSI(dataframe)
|
|
|
|
|
|
|
|
# Stoch fast
|
|
|
|
stoch_fast = ta.STOCHF(dataframe)
|
|
|
|
dataframe['fastd'] = stoch_fast['fastd']
|
|
|
|
dataframe['fastk'] = stoch_fast['fastk']
|
|
|
|
|
|
|
|
# Bollinger bands
|
|
|
|
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
|
|
|
dataframe['bb_lowerband'] = bollinger['lower']
|
|
|
|
dataframe['bb_middleband'] = bollinger['mid']
|
|
|
|
dataframe['bb_upperband'] = bollinger['upper']
|
|
|
|
|
|
|
|
# EMA - Exponential Moving Average
|
|
|
|
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
|
|
|
|
|
|
|
|
return 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
|
|
|
|
:param metadata: Additional information, like the currently traded pair
|
|
|
|
:return: DataFrame with buy column
|
|
|
|
"""
|
|
|
|
dataframe.loc[
|
|
|
|
(
|
|
|
|
(dataframe['rsi'] < self.buy_rsi.value) &
|
|
|
|
(dataframe['fastd'] < 35) &
|
|
|
|
(dataframe['adx'] > 30) &
|
|
|
|
(dataframe['plus_di'] > self.buy_plusdi.value)
|
|
|
|
) |
|
|
|
|
(
|
|
|
|
(dataframe['adx'] > 65) &
|
|
|
|
(dataframe['plus_di'] > self.buy_plusdi.value)
|
|
|
|
),
|
|
|
|
'buy'] = 1
|
|
|
|
|
|
|
|
return 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
|
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2021-08-18 12:03:44 +00:00
|
|
|
:return: DataFrame with sell column
|
2021-03-27 10:26:26 +00:00
|
|
|
"""
|
|
|
|
dataframe.loc[
|
|
|
|
(
|
|
|
|
(
|
|
|
|
(qtpylib.crossed_above(dataframe['rsi'], self.sell_rsi.value)) |
|
|
|
|
(qtpylib.crossed_above(dataframe['fastd'], 70))
|
|
|
|
) &
|
|
|
|
(dataframe['adx'] > 10) &
|
|
|
|
(dataframe['minus_di'] > 0)
|
|
|
|
) |
|
|
|
|
(
|
|
|
|
(dataframe['adx'] > 70) &
|
2021-03-28 17:49:20 +00:00
|
|
|
(dataframe['minus_di'] > self.sell_minusdi.value)
|
2021-03-27 10:26:26 +00:00
|
|
|
),
|
|
|
|
'sell'] = 1
|
|
|
|
return dataframe
|