2018-01-28 01:33:04 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
|
|
|
|
|
2021-12-18 09:15:59 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
import talib.abstract as ta
|
2018-01-28 01:33:04 +00:00
|
|
|
from pandas import DataFrame
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
import freqtrade.vendor.qtpylib.indicators as qtpylib
|
2021-12-13 00:27:09 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2021-09-21 05:11:53 +00:00
|
|
|
from freqtrade.strategy import IStrategy
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
|
2021-08-26 05:25:53 +00:00
|
|
|
class StrategyTestV2(IStrategy):
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
2021-08-26 05:25:53 +00:00
|
|
|
Strategy used by tests freqtrade bot.
|
2019-09-14 08:00:32 +00:00
|
|
|
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.
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
2019-08-26 18:16:03 +00:00
|
|
|
INTERFACE_VERSION = 2
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
# Minimal ROI designed for the strategy
|
|
|
|
minimal_roi = {
|
2018-11-25 19:44:40 +00:00
|
|
|
"40": 0.0,
|
|
|
|
"30": 0.01,
|
|
|
|
"20": 0.02,
|
|
|
|
"0": 0.04
|
2018-01-15 08:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Optimal stoploss designed for the strategy
|
|
|
|
stoploss = -0.10
|
|
|
|
|
2021-04-03 14:54:47 +00:00
|
|
|
# Optimal timeframe for the strategy
|
2020-06-02 07:36:04 +00:00
|
|
|
timeframe = '5m'
|
2018-01-20 22:40:41 +00:00
|
|
|
|
2018-11-17 09:26:15 +00:00
|
|
|
# Optional order type mapping
|
2018-11-15 05:58:24 +00:00
|
|
|
order_types = {
|
|
|
|
'buy': 'limit',
|
|
|
|
'sell': 'limit',
|
2018-11-25 16:22:56 +00:00
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': False
|
2018-11-15 05:58:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 15:58:26 +00:00
|
|
|
# Number of candles the strategy requires before producing valid signals
|
2019-10-23 15:57:38 +00:00
|
|
|
startup_candle_count: int = 20
|
|
|
|
|
2018-11-25 19:44:40 +00:00
|
|
|
# Optional time in force for orders
|
|
|
|
order_time_in_force = {
|
2022-03-07 06:09:01 +00:00
|
|
|
'entry': 'gtc',
|
|
|
|
'exit': 'gtc',
|
2018-11-25 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 10:38:43 +00:00
|
|
|
# By default this strategy does not use Position Adjustments
|
|
|
|
position_adjustment_enable = False
|
|
|
|
|
2019-01-26 18:22:45 +00:00
|
|
|
def informative_pairs(self):
|
2019-01-22 18:17:08 +00:00
|
|
|
"""
|
2019-01-26 18:22:45 +00:00
|
|
|
Define additional, informative pair/interval combinations to be cached from the exchange.
|
2019-01-22 18:17:08 +00:00
|
|
|
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 []
|
|
|
|
|
2018-07-29 18:36:03 +00:00
|
|
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
|
|
|
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.
|
2020-03-08 10:35:31 +00:00
|
|
|
:param dataframe: Dataframe with data from the exchange
|
2018-07-29 18:36:03 +00:00
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2018-07-25 06:54:01 +00:00
|
|
|
:return: a Dataframe with all mandatory indicators for the strategies
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# 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)
|
2018-02-14 11:01:30 +00:00
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
# Stoch fast
|
|
|
|
stoch_fast = ta.STOCHF(dataframe)
|
|
|
|
dataframe['fastd'] = stoch_fast['fastd']
|
|
|
|
dataframe['fastk'] = stoch_fast['fastk']
|
2018-01-18 05:44:37 +00:00
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
# 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']
|
2019-09-14 08:00:32 +00:00
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
# EMA - Exponential Moving Average
|
2019-09-13 17:49:34 +00:00
|
|
|
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
|
2018-01-15 08:35:11 +00:00
|
|
|
|
|
|
|
return dataframe
|
|
|
|
|
2018-07-29 18:36:03 +00:00
|
|
|
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
|
|
|
Based on TA indicators, populates the buy signal for the given dataframe
|
|
|
|
:param dataframe: DataFrame
|
2018-07-29 18:36:03 +00:00
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2018-01-15 08:35:11 +00:00
|
|
|
:return: DataFrame with buy column
|
|
|
|
"""
|
|
|
|
dataframe.loc[
|
|
|
|
(
|
|
|
|
(dataframe['rsi'] < 35) &
|
|
|
|
(dataframe['fastd'] < 35) &
|
|
|
|
(dataframe['adx'] > 30) &
|
|
|
|
(dataframe['plus_di'] > 0.5)
|
|
|
|
) |
|
|
|
|
(
|
|
|
|
(dataframe['adx'] > 65) &
|
|
|
|
(dataframe['plus_di'] > 0.5)
|
|
|
|
),
|
|
|
|
'buy'] = 1
|
|
|
|
|
|
|
|
return dataframe
|
|
|
|
|
2018-07-29 18:36:03 +00:00
|
|
|
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
|
|
|
Based on TA indicators, populates the sell signal for the given dataframe
|
|
|
|
:param dataframe: DataFrame
|
2018-07-29 18:36:03 +00:00
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2018-01-15 08:35:11 +00:00
|
|
|
:return: DataFrame with buy column
|
|
|
|
"""
|
|
|
|
dataframe.loc[
|
|
|
|
(
|
|
|
|
(
|
|
|
|
(qtpylib.crossed_above(dataframe['rsi'], 70)) |
|
|
|
|
(qtpylib.crossed_above(dataframe['fastd'], 70))
|
|
|
|
) &
|
|
|
|
(dataframe['adx'] > 10) &
|
|
|
|
(dataframe['minus_di'] > 0)
|
|
|
|
) |
|
|
|
|
(
|
|
|
|
(dataframe['adx'] > 70) &
|
|
|
|
(dataframe['minus_di'] > 0.5)
|
|
|
|
),
|
|
|
|
'sell'] = 1
|
|
|
|
return dataframe
|
2021-12-13 00:27:09 +00:00
|
|
|
|
2022-01-08 15:20:02 +00:00
|
|
|
def adjust_trade_position(self, trade: Trade, current_time: datetime, current_rate: float,
|
|
|
|
current_profit: float, min_stake: float, max_stake: float, **kwargs):
|
2021-12-13 00:27:09 +00:00
|
|
|
|
|
|
|
if current_profit < -0.0075:
|
2022-01-15 16:36:13 +00:00
|
|
|
orders = trade.select_filled_orders('buy')
|
|
|
|
return round(orders[0].cost, 0)
|
2021-12-13 00:27:09 +00:00
|
|
|
|
|
|
|
return None
|