2022-07-26 08:24:14 +00:00
|
|
|
import logging
|
|
|
|
from functools import reduce
|
2023-02-04 19:04:16 +00:00
|
|
|
from typing import Dict
|
2022-07-26 08:24:14 +00:00
|
|
|
|
|
|
|
import talib.abstract as ta
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
2022-12-28 12:25:40 +00:00
|
|
|
from freqtrade.strategy import DecimalParameter, IntParameter, IStrategy
|
2022-07-26 08:24:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class freqai_test_multimodel_strat(IStrategy):
|
|
|
|
"""
|
2022-08-04 05:17:26 +00:00
|
|
|
Test strategy - used for testing freqAI multimodel functionalities.
|
|
|
|
DO not use in production.
|
2022-07-26 08:24:14 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
minimal_roi = {"0": 0.1, "240": -1}
|
|
|
|
|
|
|
|
plot_config = {
|
|
|
|
"main_plot": {},
|
|
|
|
"subplots": {
|
|
|
|
"prediction": {"prediction": {"color": "blue"}},
|
|
|
|
"target_roi": {
|
|
|
|
"target_roi": {"color": "brown"},
|
|
|
|
},
|
|
|
|
"do_predict": {
|
|
|
|
"do_predict": {"color": "brown"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
process_only_new_candles = True
|
|
|
|
stoploss = -0.05
|
|
|
|
use_exit_signal = True
|
|
|
|
startup_candle_count: int = 300
|
|
|
|
can_short = False
|
|
|
|
|
|
|
|
linear_roi_offset = DecimalParameter(
|
|
|
|
0.00, 0.02, default=0.005, space="sell", optimize=False, load=True
|
|
|
|
)
|
|
|
|
max_roi_time_long = IntParameter(0, 800, default=400, space="sell", optimize=False, load=True)
|
|
|
|
|
2023-02-04 19:04:16 +00:00
|
|
|
def feature_engineering_expand_all(self, dataframe: DataFrame, period: int,
|
|
|
|
metadata: Dict, **kwargs):
|
2022-12-28 12:25:40 +00:00
|
|
|
|
|
|
|
dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period)
|
|
|
|
dataframe["%-mfi-period"] = ta.MFI(dataframe, timeperiod=period)
|
|
|
|
dataframe["%-adx-period"] = ta.ADX(dataframe, timeperiod=period)
|
|
|
|
|
|
|
|
return dataframe
|
|
|
|
|
2023-02-04 19:04:16 +00:00
|
|
|
def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: Dict, **kwargs):
|
2022-12-28 12:25:40 +00:00
|
|
|
|
|
|
|
dataframe["%-pct-change"] = dataframe["close"].pct_change()
|
|
|
|
dataframe["%-raw_volume"] = dataframe["volume"]
|
|
|
|
dataframe["%-raw_price"] = dataframe["close"]
|
|
|
|
|
|
|
|
return dataframe
|
2022-08-14 15:19:50 +00:00
|
|
|
|
2023-02-04 19:04:16 +00:00
|
|
|
def feature_engineering_standard(self, dataframe: DataFrame, metadata: Dict, **kwargs):
|
2022-12-28 12:25:40 +00:00
|
|
|
|
|
|
|
dataframe["%-day_of_week"] = dataframe["date"].dt.dayofweek
|
|
|
|
dataframe["%-hour_of_day"] = dataframe["date"].dt.hour
|
|
|
|
|
|
|
|
return dataframe
|
|
|
|
|
2023-02-04 19:04:16 +00:00
|
|
|
def set_freqai_targets(self, dataframe: DataFrame, metadata: Dict, **kwargs):
|
2022-12-28 12:25:40 +00:00
|
|
|
|
|
|
|
dataframe["&-s_close"] = (
|
|
|
|
dataframe["close"]
|
|
|
|
.shift(-self.freqai_info["feature_parameters"]["label_period_candles"])
|
|
|
|
.rolling(self.freqai_info["feature_parameters"]["label_period_candles"])
|
|
|
|
.mean()
|
|
|
|
/ dataframe["close"]
|
|
|
|
- 1
|
2022-08-14 15:19:50 +00:00
|
|
|
)
|
2022-07-26 08:24:14 +00:00
|
|
|
|
2022-12-28 12:25:40 +00:00
|
|
|
dataframe["&-s_range"] = (
|
|
|
|
dataframe["close"]
|
|
|
|
.shift(-self.freqai_info["feature_parameters"]["label_period_candles"])
|
|
|
|
.rolling(self.freqai_info["feature_parameters"]["label_period_candles"])
|
|
|
|
.max()
|
|
|
|
-
|
|
|
|
dataframe["close"]
|
|
|
|
.shift(-self.freqai_info["feature_parameters"]["label_period_candles"])
|
|
|
|
.rolling(self.freqai_info["feature_parameters"]["label_period_candles"])
|
|
|
|
.min()
|
|
|
|
)
|
|
|
|
|
|
|
|
return dataframe
|
2022-07-26 08:24:14 +00:00
|
|
|
|
|
|
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
|
|
|
|
|
|
|
self.freqai_info = self.config["freqai"]
|
|
|
|
|
|
|
|
dataframe = self.freqai.start(dataframe, metadata, self)
|
|
|
|
|
|
|
|
dataframe["target_roi"] = dataframe["&-s_close_mean"] + dataframe["&-s_close_std"] * 1.25
|
|
|
|
dataframe["sell_roi"] = dataframe["&-s_close_mean"] - dataframe["&-s_close_std"] * 1.25
|
|
|
|
return dataframe
|
|
|
|
|
|
|
|
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
|
|
|
|
|
|
|
enter_long_conditions = [df["do_predict"] == 1, df["&-s_close"] > df["target_roi"]]
|
|
|
|
|
|
|
|
if enter_long_conditions:
|
|
|
|
df.loc[
|
|
|
|
reduce(lambda x, y: x & y, enter_long_conditions), ["enter_long", "enter_tag"]
|
|
|
|
] = (1, "long")
|
|
|
|
|
|
|
|
enter_short_conditions = [df["do_predict"] == 1, df["&-s_close"] < df["sell_roi"]]
|
|
|
|
|
|
|
|
if enter_short_conditions:
|
|
|
|
df.loc[
|
|
|
|
reduce(lambda x, y: x & y, enter_short_conditions), ["enter_short", "enter_tag"]
|
|
|
|
] = (1, "short")
|
|
|
|
|
|
|
|
return df
|
|
|
|
|
|
|
|
def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
|
|
|
exit_long_conditions = [df["do_predict"] == 1, df["&-s_close"] < df["sell_roi"] * 0.25]
|
|
|
|
if exit_long_conditions:
|
|
|
|
df.loc[reduce(lambda x, y: x & y, exit_long_conditions), "exit_long"] = 1
|
|
|
|
|
|
|
|
exit_short_conditions = [df["do_predict"] == 1, df["&-s_close"] > df["target_roi"] * 0.25]
|
|
|
|
if exit_short_conditions:
|
|
|
|
df.loc[reduce(lambda x, y: x & y, exit_short_conditions), "exit_short"] = 1
|
|
|
|
|
|
|
|
return df
|