2022-05-03 08:14:17 +00:00
|
|
|
import logging
|
2022-05-04 15:42:34 +00:00
|
|
|
from functools import reduce
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pandas as pd
|
2022-05-03 08:14:17 +00:00
|
|
|
import talib.abstract as ta
|
|
|
|
from pandas import DataFrame
|
|
|
|
from technical import qtpylib
|
2022-05-04 15:42:34 +00:00
|
|
|
|
2022-05-03 08:14:17 +00:00
|
|
|
from freqtrade.freqai.strategy_bridge import CustomModel
|
2022-05-04 15:42:34 +00:00
|
|
|
from freqtrade.strategy import merge_informative_pair
|
|
|
|
from freqtrade.strategy.interface import IStrategy
|
|
|
|
|
|
|
|
|
2022-05-03 08:14:17 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
|
2022-05-03 08:14:17 +00:00
|
|
|
class FreqaiExampleStrategy(IStrategy):
|
|
|
|
"""
|
2022-05-04 15:42:34 +00:00
|
|
|
Example strategy showing how the user connects their own
|
2022-05-03 08:14:17 +00:00
|
|
|
IFreqaiModel to the strategy. Namely, the user uses:
|
|
|
|
self.model = CustomModel(self.config)
|
|
|
|
self.model.bridge.start(dataframe, metadata)
|
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
to make predictions on their data. populate_any_indicators() automatically
|
2022-05-03 08:14:17 +00:00
|
|
|
generates the variety of features indicated by the user in the
|
|
|
|
canonical freqtrade configuration file under config['freqai'].
|
|
|
|
"""
|
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
minimal_roi = {"0": 0.01, "240": -1}
|
2022-05-03 08:14:17 +00:00
|
|
|
|
|
|
|
plot_config = {
|
2022-05-04 15:42:34 +00:00
|
|
|
"main_plot": {},
|
|
|
|
"subplots": {
|
|
|
|
"prediction": {"prediction": {"color": "blue"}},
|
|
|
|
"target_roi": {
|
|
|
|
"target_roi": {"color": "brown"},
|
2022-05-03 08:14:17 +00:00
|
|
|
},
|
2022-05-04 15:42:34 +00:00
|
|
|
"do_predict": {
|
|
|
|
"do_predict": {"color": "brown"},
|
2022-05-03 08:14:17 +00:00
|
|
|
},
|
2022-05-04 15:42:34 +00:00
|
|
|
},
|
2022-05-03 08:14:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 08:15:59 +00:00
|
|
|
process_only_new_candles = False
|
2022-05-03 08:14:17 +00:00
|
|
|
stoploss = -0.05
|
|
|
|
use_sell_signal = True
|
2022-05-09 13:25:00 +00:00
|
|
|
startup_candle_count: int = 300
|
2022-05-03 08:14:17 +00:00
|
|
|
|
|
|
|
def informative_pairs(self):
|
2022-05-10 09:39:01 +00:00
|
|
|
whitelist_pairs = self.dp.current_whitelist()
|
|
|
|
corr_pairs = self.config["freqai"]["corr_pairlist"]
|
2022-05-03 08:14:17 +00:00
|
|
|
informative_pairs = []
|
2022-05-09 13:25:00 +00:00
|
|
|
for tf in self.config["freqai"]["timeframes"]:
|
2022-05-10 09:39:01 +00:00
|
|
|
for pair in whitelist_pairs:
|
|
|
|
informative_pairs.append((pair, tf))
|
|
|
|
for pair in corr_pairs:
|
|
|
|
if pair in whitelist_pairs:
|
|
|
|
continue # avoid duplication
|
2022-05-09 13:25:00 +00:00
|
|
|
informative_pairs.append((pair, tf))
|
2022-05-03 08:14:17 +00:00
|
|
|
return informative_pairs
|
|
|
|
|
2022-05-19 17:27:38 +00:00
|
|
|
def bot_start(self):
|
|
|
|
self.model = CustomModel(self.config)
|
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
def populate_any_indicators(self, pair, df, tf, informative=None, coin=""):
|
2022-05-03 08:14:17 +00:00
|
|
|
"""
|
|
|
|
Function designed to automatically generate, name and merge features
|
2022-05-17 16:15:03 +00:00
|
|
|
from user indicated timeframes in the configuration file. User controls the indicators
|
|
|
|
passed to the training/prediction by prepending indicators with `'%-' + coin `
|
|
|
|
(see convention below). I.e. user should not prepend any supporting metrics
|
|
|
|
(e.g. bb_lowerband below) with % unless they explicitly want to pass that metric to the
|
|
|
|
model.
|
2022-05-03 08:14:17 +00:00
|
|
|
:params:
|
|
|
|
:pair: pair to be used as informative
|
|
|
|
:df: strategy dataframe which will receive merges from informatives
|
|
|
|
:tf: timeframe of the dataframe which will modify the feature names
|
|
|
|
:informative: the dataframe associated with the informative pair
|
|
|
|
:coin: the name of the coin which will modify the feature names.
|
|
|
|
"""
|
|
|
|
if informative is None:
|
|
|
|
informative = self.dp.get_pair_dataframe(pair, tf)
|
|
|
|
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "rsi"] = ta.RSI(informative, timeperiod=14)
|
|
|
|
informative['%-' + coin + "mfi"] = ta.MFI(informative, timeperiod=25)
|
|
|
|
informative['%-' + coin + "adx"] = ta.ADX(informative, window=20)
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
informative[coin + "20sma"] = ta.SMA(informative, timeperiod=20)
|
|
|
|
informative[coin + "21ema"] = ta.EMA(informative, timeperiod=21)
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "bmsb"] = np.where(
|
2022-05-04 15:42:34 +00:00
|
|
|
informative[coin + "20sma"].lt(informative[coin + "21ema"]), 1, 0
|
|
|
|
)
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "close_over_20sma"] = informative["close"] / informative[
|
|
|
|
coin + "20sma"]
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "mfi"] = ta.MFI(informative, timeperiod=25)
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
informative[coin + "ema21"] = ta.EMA(informative, timeperiod=21)
|
|
|
|
informative[coin + "sma20"] = ta.SMA(informative, timeperiod=20)
|
2022-05-03 08:14:17 +00:00
|
|
|
stoch = ta.STOCHRSI(informative, 15, 20, 2, 2)
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "srsi-fk"] = stoch["fastk"]
|
|
|
|
informative['%-' + coin + "srsi-fd"] = stoch["fastd"]
|
2022-05-03 08:14:17 +00:00
|
|
|
|
|
|
|
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(informative), window=14, stds=2.2)
|
2022-05-04 15:42:34 +00:00
|
|
|
informative[coin + "bb_lowerband"] = bollinger["lower"]
|
|
|
|
informative[coin + "bb_middleband"] = bollinger["mid"]
|
|
|
|
informative[coin + "bb_upperband"] = bollinger["upper"]
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "bb_width"] = (
|
2022-05-04 15:42:34 +00:00
|
|
|
informative[coin + "bb_upperband"] - informative[coin + "bb_lowerband"]
|
|
|
|
) / informative[coin + "bb_middleband"]
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "close-bb_lower"] = (
|
2022-05-04 15:42:34 +00:00
|
|
|
informative["close"] / informative[coin + "bb_lowerband"]
|
|
|
|
)
|
|
|
|
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "roc"] = ta.ROC(informative, timeperiod=3)
|
|
|
|
informative['%-' + coin + "adx"] = ta.ADX(informative, window=14)
|
2022-05-03 08:14:17 +00:00
|
|
|
|
|
|
|
macd = ta.MACD(informative)
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "macd"] = macd["macd"]
|
2022-05-04 15:42:34 +00:00
|
|
|
informative[coin + "pct-change"] = informative["close"].pct_change()
|
2022-05-17 16:15:03 +00:00
|
|
|
informative['%-' + coin + "relative_volume"] = (
|
2022-05-04 15:42:34 +00:00
|
|
|
informative["volume"] / informative["volume"].rolling(10).mean()
|
|
|
|
)
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
informative[coin + "pct-change"] = informative["close"].pct_change()
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-17 16:15:03 +00:00
|
|
|
indicators = [col for col in informative if col.startswith('%')]
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
for n in range(self.freqai_info["feature_parameters"]["shift"] + 1):
|
|
|
|
if n == 0:
|
|
|
|
continue
|
2022-05-03 08:14:17 +00:00
|
|
|
informative_shift = informative[indicators].shift(n)
|
2022-05-04 15:42:34 +00:00
|
|
|
informative_shift = informative_shift.add_suffix("_shift-" + str(n))
|
|
|
|
informative = pd.concat((informative, informative_shift), axis=1)
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
df = merge_informative_pair(df, informative, self.config["timeframe"], tf, ffill=True)
|
|
|
|
skip_columns = [(s + "_" + tf) for s in ["date", "open", "high", "low", "close", "volume"]]
|
2022-05-03 08:14:17 +00:00
|
|
|
df = df.drop(columns=skip_columns)
|
|
|
|
|
|
|
|
return df
|
|
|
|
|
|
|
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
|
|
|
|
|
|
|
# the configuration file parameters are stored here
|
2022-05-04 15:42:34 +00:00
|
|
|
self.freqai_info = self.config["freqai"]
|
2022-05-09 13:25:00 +00:00
|
|
|
self.pair = metadata['pair']
|
2022-05-03 08:14:17 +00:00
|
|
|
|
2022-05-04 15:42:34 +00:00
|
|
|
# the following loops are necessary for building the features
|
2022-05-03 08:14:17 +00:00
|
|
|
# indicated by the user in the configuration file.
|
2022-05-04 15:42:34 +00:00
|
|
|
for tf in self.freqai_info["timeframes"]:
|
2022-05-09 15:01:49 +00:00
|
|
|
dataframe = self.populate_any_indicators(self.pair, dataframe.copy(), tf,
|
|
|
|
coin=self.pair.split("/")[0] + "-")
|
2022-05-09 13:25:00 +00:00
|
|
|
for pair in self.freqai_info["corr_pairlist"]:
|
2022-05-09 15:01:49 +00:00
|
|
|
if metadata['pair'] in pair:
|
|
|
|
continue # do not include whitelisted pair twice if it is in corr_pairlist
|
2022-05-04 15:42:34 +00:00
|
|
|
dataframe = self.populate_any_indicators(
|
2022-05-09 13:25:00 +00:00
|
|
|
pair, dataframe.copy(), tf, coin=pair.split("/")[0] + "-"
|
2022-05-04 15:42:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# the model will return 4 values, its prediction, an indication of whether or not the
|
|
|
|
# prediction should be accepted, the target mean/std values from the labels used during
|
|
|
|
# each training period.
|
|
|
|
(
|
|
|
|
dataframe["prediction"],
|
|
|
|
dataframe["do_predict"],
|
|
|
|
dataframe["target_mean"],
|
|
|
|
dataframe["target_std"],
|
2022-05-09 13:25:00 +00:00
|
|
|
) = self.model.bridge.start(dataframe, metadata, self)
|
2022-05-04 15:42:34 +00:00
|
|
|
|
2022-05-22 15:51:49 +00:00
|
|
|
dataframe["target_roi"] = dataframe["target_mean"] + dataframe["target_std"] * 1.5
|
|
|
|
dataframe["sell_roi"] = dataframe["target_mean"] - dataframe["target_std"] * 1
|
2022-05-03 08:14:17 +00:00
|
|
|
return dataframe
|
|
|
|
|
|
|
|
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
|
|
|
|
|
|
|
buy_conditions = [
|
2022-05-04 15:42:34 +00:00
|
|
|
(dataframe["prediction"] > dataframe["target_roi"]) & (dataframe["do_predict"] == 1)
|
2022-05-03 08:14:17 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if buy_conditions:
|
2022-05-04 15:42:34 +00:00
|
|
|
dataframe.loc[reduce(lambda x, y: x | y, buy_conditions), "buy"] = 1
|
2022-05-03 08:14:17 +00:00
|
|
|
|
|
|
|
return dataframe
|
|
|
|
|
|
|
|
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
|
|
|
sell_conditions = [
|
2022-05-22 15:51:49 +00:00
|
|
|
(dataframe["do_predict"] <= 0)
|
2022-05-03 08:14:17 +00:00
|
|
|
]
|
|
|
|
if sell_conditions:
|
2022-05-04 15:42:34 +00:00
|
|
|
dataframe.loc[reduce(lambda x, y: x | y, sell_conditions), "sell"] = 1
|
2022-05-03 08:14:17 +00:00
|
|
|
|
|
|
|
return dataframe
|
|
|
|
|
|
|
|
def get_ticker_indicator(self):
|
2022-05-04 15:42:34 +00:00
|
|
|
return int(self.config["timeframe"][:-1])
|