mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-10 02:12:01 +00:00
ruff format: template directory
This commit is contained in:
parent
439b8a0320
commit
5783a44c86
|
@ -61,27 +61,28 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
minimal_roi = {
|
minimal_roi = {
|
||||||
|
# "120": 0.0, # exit after 120 minutes at break even
|
||||||
"60": 0.01,
|
"60": 0.01,
|
||||||
"30": 0.02,
|
"30": 0.02,
|
||||||
"0": 0.04
|
"0": 0.04
|
||||||
}
|
}
|
||||||
|
|
||||||
plot_config = {
|
plot_config = {
|
||||||
'main_plot': {
|
"main_plot": {
|
||||||
'tema': {},
|
"tema": {},
|
||||||
},
|
},
|
||||||
'subplots': {
|
"subplots": {
|
||||||
"MACD": {
|
"MACD": {
|
||||||
'macd': {'color': 'blue'},
|
"macd": {"color": "blue"},
|
||||||
'macdsignal': {'color': 'orange'},
|
"macdsignal": {"color": "orange"},
|
||||||
},
|
},
|
||||||
"RSI": {
|
"RSI": {
|
||||||
'rsi': {'color': 'red'},
|
"rsi": {"color": "red"},
|
||||||
},
|
},
|
||||||
"Up_or_down": {
|
"Up_or_down": {
|
||||||
'&s-up_or_down': {'color': 'green'},
|
"&s-up_or_down": {"color": "green"},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
process_only_new_candles = True
|
process_only_new_candles = True
|
||||||
|
@ -91,13 +92,14 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
||||||
can_short = True
|
can_short = True
|
||||||
|
|
||||||
# Hyperoptable parameters
|
# Hyperoptable parameters
|
||||||
buy_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True)
|
buy_rsi = IntParameter(low=1, high=50, default=30, space="buy", optimize=True, load=True)
|
||||||
sell_rsi = IntParameter(low=50, high=100, default=70, space='sell', optimize=True, load=True)
|
sell_rsi = IntParameter(low=50, high=100, default=70, space="sell", optimize=True, load=True)
|
||||||
short_rsi = IntParameter(low=51, high=100, default=70, space='sell', optimize=True, load=True)
|
short_rsi = IntParameter(low=51, high=100, default=70, space="sell", optimize=True, load=True)
|
||||||
exit_short_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True)
|
exit_short_rsi = IntParameter(low=1, high=50, default=30, space="buy", optimize=True, load=True)
|
||||||
|
|
||||||
def feature_engineering_expand_all(self, dataframe: DataFrame, period: int,
|
def feature_engineering_expand_all(
|
||||||
metadata: Dict, **kwargs) -> DataFrame:
|
self, dataframe: DataFrame, period: int, metadata: Dict, **kwargs
|
||||||
|
) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
*Only functional with FreqAI enabled strategies*
|
*Only functional with FreqAI enabled strategies*
|
||||||
This function will automatically expand the defined features on the config defined
|
This function will automatically expand the defined features on the config defined
|
||||||
|
@ -136,12 +138,9 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
||||||
dataframe["bb_upperband-period"] = bollinger["upper"]
|
dataframe["bb_upperband-period"] = bollinger["upper"]
|
||||||
|
|
||||||
dataframe["%-bb_width-period"] = (
|
dataframe["%-bb_width-period"] = (
|
||||||
dataframe["bb_upperband-period"]
|
dataframe["bb_upperband-period"] - dataframe["bb_lowerband-period"]
|
||||||
- dataframe["bb_lowerband-period"]
|
|
||||||
) / dataframe["bb_middleband-period"]
|
) / dataframe["bb_middleband-period"]
|
||||||
dataframe["%-close-bb_lower-period"] = (
|
dataframe["%-close-bb_lower-period"] = dataframe["close"] / dataframe["bb_lowerband-period"]
|
||||||
dataframe["close"] / dataframe["bb_lowerband-period"]
|
|
||||||
)
|
|
||||||
|
|
||||||
dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period)
|
dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period)
|
||||||
|
|
||||||
|
@ -152,7 +151,8 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def feature_engineering_expand_basic(
|
def feature_engineering_expand_basic(
|
||||||
self, dataframe: DataFrame, metadata: Dict, **kwargs) -> DataFrame:
|
self, dataframe: DataFrame, metadata: Dict, **kwargs
|
||||||
|
) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
*Only functional with FreqAI enabled strategies*
|
*Only functional with FreqAI enabled strategies*
|
||||||
This function will automatically expand the defined features on the config defined
|
This function will automatically expand the defined features on the config defined
|
||||||
|
@ -185,7 +185,8 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def feature_engineering_standard(
|
def feature_engineering_standard(
|
||||||
self, dataframe: DataFrame, metadata: Dict, **kwargs) -> DataFrame:
|
self, dataframe: DataFrame, metadata: Dict, **kwargs
|
||||||
|
) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
*Only functional with FreqAI enabled strategies*
|
*Only functional with FreqAI enabled strategies*
|
||||||
This optional function will be called once with the dataframe of the base timeframe.
|
This optional function will be called once with the dataframe of the base timeframe.
|
||||||
|
@ -226,13 +227,13 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
||||||
usage example: dataframe["&-target"] = dataframe["close"].shift(-1) / dataframe["close"]
|
usage example: dataframe["&-target"] = dataframe["close"].shift(-1) / dataframe["close"]
|
||||||
"""
|
"""
|
||||||
self.freqai.class_names = ["down", "up"]
|
self.freqai.class_names = ["down", "up"]
|
||||||
dataframe['&s-up_or_down'] = np.where(dataframe["close"].shift(-50) >
|
dataframe["&s-up_or_down"] = np.where(
|
||||||
dataframe["close"], 'up', 'down')
|
dataframe["close"].shift(-50) > dataframe["close"], "up", "down"
|
||||||
|
)
|
||||||
|
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # noqa: C901
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # noqa: C901
|
||||||
|
|
||||||
# User creates their own custom strat here. Present example is a supertrend
|
# User creates their own custom strat here. Present example is a supertrend
|
||||||
# based strategy.
|
# based strategy.
|
||||||
|
|
||||||
|
@ -240,78 +241,81 @@ class FreqaiExampleHybridStrategy(IStrategy):
|
||||||
|
|
||||||
# TA indicators to combine with the Freqai targets
|
# TA indicators to combine with the Freqai targets
|
||||||
# RSI
|
# RSI
|
||||||
dataframe['rsi'] = ta.RSI(dataframe)
|
dataframe["rsi"] = ta.RSI(dataframe)
|
||||||
|
|
||||||
# Bollinger Bands
|
# Bollinger Bands
|
||||||
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
||||||
dataframe['bb_lowerband'] = bollinger['lower']
|
dataframe["bb_lowerband"] = bollinger["lower"]
|
||||||
dataframe['bb_middleband'] = bollinger['mid']
|
dataframe["bb_middleband"] = bollinger["mid"]
|
||||||
dataframe['bb_upperband'] = bollinger['upper']
|
dataframe["bb_upperband"] = bollinger["upper"]
|
||||||
dataframe["bb_percent"] = (
|
dataframe["bb_percent"] = (dataframe["close"] - dataframe["bb_lowerband"]) / (
|
||||||
(dataframe["close"] - dataframe["bb_lowerband"]) /
|
dataframe["bb_upperband"] - dataframe["bb_lowerband"]
|
||||||
(dataframe["bb_upperband"] - dataframe["bb_lowerband"])
|
|
||||||
)
|
|
||||||
dataframe["bb_width"] = (
|
|
||||||
(dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe["bb_middleband"]
|
|
||||||
)
|
)
|
||||||
|
dataframe["bb_width"] = (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe[
|
||||||
|
"bb_middleband"
|
||||||
|
]
|
||||||
|
|
||||||
# TEMA - Triple Exponential Moving Average
|
# TEMA - Triple Exponential Moving Average
|
||||||
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
|
dataframe["tema"] = ta.TEMA(dataframe, timeperiod=9)
|
||||||
|
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
|
||||||
df.loc[
|
df.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 30
|
# Signal: RSI crosses above 30
|
||||||
(qtpylib.crossed_above(df['rsi'], self.buy_rsi.value)) &
|
(qtpylib.crossed_above(df["rsi"], self.buy_rsi.value))
|
||||||
(df['tema'] <= df['bb_middleband']) & # Guard: tema below BB middle
|
& (df["tema"] <= df["bb_middleband"]) # Guard: tema below BB middle
|
||||||
(df['tema'] > df['tema'].shift(1)) & # Guard: tema is raising
|
& (df["tema"] > df["tema"].shift(1)) # Guard: tema is raising
|
||||||
(df['volume'] > 0) & # Make sure Volume is not 0
|
& (df["volume"] > 0) # Make sure Volume is not 0
|
||||||
(df['do_predict'] == 1) & # Make sure Freqai is confident in the prediction
|
& (df["do_predict"] == 1) # Make sure Freqai is confident in the prediction
|
||||||
|
&
|
||||||
# Only enter trade if Freqai thinks the trend is in this direction
|
# Only enter trade if Freqai thinks the trend is in this direction
|
||||||
(df['&s-up_or_down'] == 'up')
|
(df["&s-up_or_down"] == "up")
|
||||||
),
|
),
|
||||||
'enter_long'] = 1
|
"enter_long",
|
||||||
|
] = 1
|
||||||
|
|
||||||
df.loc[
|
df.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 70
|
# Signal: RSI crosses above 70
|
||||||
(qtpylib.crossed_above(df['rsi'], self.short_rsi.value)) &
|
(qtpylib.crossed_above(df["rsi"], self.short_rsi.value))
|
||||||
(df['tema'] > df['bb_middleband']) & # Guard: tema above BB middle
|
& (df["tema"] > df["bb_middleband"]) # Guard: tema above BB middle
|
||||||
(df['tema'] < df['tema'].shift(1)) & # Guard: tema is falling
|
& (df["tema"] < df["tema"].shift(1)) # Guard: tema is falling
|
||||||
(df['volume'] > 0) & # Make sure Volume is not 0
|
& (df["volume"] > 0) # Make sure Volume is not 0
|
||||||
(df['do_predict'] == 1) & # Make sure Freqai is confident in the prediction
|
& (df["do_predict"] == 1) # Make sure Freqai is confident in the prediction
|
||||||
|
&
|
||||||
# Only enter trade if Freqai thinks the trend is in this direction
|
# Only enter trade if Freqai thinks the trend is in this direction
|
||||||
(df['&s-up_or_down'] == 'down')
|
(df["&s-up_or_down"] == "down")
|
||||||
),
|
),
|
||||||
'enter_short'] = 1
|
"enter_short",
|
||||||
|
] = 1
|
||||||
|
|
||||||
return df
|
return df
|
||||||
|
|
||||||
def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
|
||||||
df.loc[
|
df.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 70
|
# Signal: RSI crosses above 70
|
||||||
(qtpylib.crossed_above(df['rsi'], self.sell_rsi.value)) &
|
(qtpylib.crossed_above(df["rsi"], self.sell_rsi.value))
|
||||||
(df['tema'] > df['bb_middleband']) & # Guard: tema above BB middle
|
& (df["tema"] > df["bb_middleband"]) # Guard: tema above BB middle
|
||||||
(df['tema'] < df['tema'].shift(1)) & # Guard: tema is falling
|
& (df["tema"] < df["tema"].shift(1)) # Guard: tema is falling
|
||||||
(df['volume'] > 0) # Make sure Volume is not 0
|
& (df["volume"] > 0) # Make sure Volume is not 0
|
||||||
),
|
),
|
||||||
|
"exit_long",
|
||||||
'exit_long'] = 1
|
] = 1
|
||||||
|
|
||||||
df.loc[
|
df.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 30
|
# Signal: RSI crosses above 30
|
||||||
(qtpylib.crossed_above(df['rsi'], self.exit_short_rsi.value)) &
|
(qtpylib.crossed_above(df["rsi"], self.exit_short_rsi.value))
|
||||||
|
&
|
||||||
# Guard: tema below BB middle
|
# Guard: tema below BB middle
|
||||||
(df['tema'] <= df['bb_middleband']) &
|
(df["tema"] <= df["bb_middleband"])
|
||||||
(df['tema'] > df['tema'].shift(1)) & # Guard: tema is raising
|
& (df["tema"] > df["tema"].shift(1)) # Guard: tema is raising
|
||||||
(df['volume'] > 0) # Make sure Volume is not 0
|
& (df["volume"] > 0) # Make sure Volume is not 0
|
||||||
),
|
),
|
||||||
'exit_short'] = 1
|
"exit_short",
|
||||||
|
] = 1
|
||||||
|
|
||||||
return df
|
return df
|
||||||
|
|
|
@ -45,8 +45,9 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
startup_candle_count: int = 40
|
startup_candle_count: int = 40
|
||||||
can_short = True
|
can_short = True
|
||||||
|
|
||||||
def feature_engineering_expand_all(self, dataframe: DataFrame, period: int,
|
def feature_engineering_expand_all(
|
||||||
metadata: Dict, **kwargs) -> DataFrame:
|
self, dataframe: DataFrame, period: int, metadata: Dict, **kwargs
|
||||||
|
) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
*Only functional with FreqAI enabled strategies*
|
*Only functional with FreqAI enabled strategies*
|
||||||
This function will automatically expand the defined features on the config defined
|
This function will automatically expand the defined features on the config defined
|
||||||
|
@ -89,12 +90,9 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
dataframe["bb_upperband-period"] = bollinger["upper"]
|
dataframe["bb_upperband-period"] = bollinger["upper"]
|
||||||
|
|
||||||
dataframe["%-bb_width-period"] = (
|
dataframe["%-bb_width-period"] = (
|
||||||
dataframe["bb_upperband-period"]
|
dataframe["bb_upperband-period"] - dataframe["bb_lowerband-period"]
|
||||||
- dataframe["bb_lowerband-period"]
|
|
||||||
) / dataframe["bb_middleband-period"]
|
) / dataframe["bb_middleband-period"]
|
||||||
dataframe["%-close-bb_lower-period"] = (
|
dataframe["%-close-bb_lower-period"] = dataframe["close"] / dataframe["bb_lowerband-period"]
|
||||||
dataframe["close"] / dataframe["bb_lowerband-period"]
|
|
||||||
)
|
|
||||||
|
|
||||||
dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period)
|
dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period)
|
||||||
|
|
||||||
|
@ -105,7 +103,8 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def feature_engineering_expand_basic(
|
def feature_engineering_expand_basic(
|
||||||
self, dataframe: DataFrame, metadata: Dict, **kwargs) -> DataFrame:
|
self, dataframe: DataFrame, metadata: Dict, **kwargs
|
||||||
|
) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
*Only functional with FreqAI enabled strategies*
|
*Only functional with FreqAI enabled strategies*
|
||||||
This function will automatically expand the defined features on the config defined
|
This function will automatically expand the defined features on the config defined
|
||||||
|
@ -142,7 +141,8 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def feature_engineering_standard(
|
def feature_engineering_standard(
|
||||||
self, dataframe: DataFrame, metadata: Dict, **kwargs) -> DataFrame:
|
self, dataframe: DataFrame, metadata: Dict, **kwargs
|
||||||
|
) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
*Only functional with FreqAI enabled strategies*
|
*Only functional with FreqAI enabled strategies*
|
||||||
This optional function will be called once with the dataframe of the base timeframe.
|
This optional function will be called once with the dataframe of the base timeframe.
|
||||||
|
@ -197,7 +197,7 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
.mean()
|
.mean()
|
||||||
/ dataframe["close"]
|
/ dataframe["close"]
|
||||||
- 1
|
- 1
|
||||||
)
|
)
|
||||||
|
|
||||||
# Classifiers are typically set up with strings as targets:
|
# Classifiers are typically set up with strings as targets:
|
||||||
# df['&s-up_or_down'] = np.where( df["close"].shift(-100) >
|
# df['&s-up_or_down'] = np.where( df["close"].shift(-100) >
|
||||||
|
@ -224,7 +224,6 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
|
||||||
# All indicators must be populated by feature_engineering_*() functions
|
# All indicators must be populated by feature_engineering_*() functions
|
||||||
|
|
||||||
# the model will return all labels created by user in `set_freqai_targets()`
|
# the model will return all labels created by user in `set_freqai_targets()`
|
||||||
|
@ -237,11 +236,10 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||||
|
|
||||||
enter_long_conditions = [
|
enter_long_conditions = [
|
||||||
df["do_predict"] == 1,
|
df["do_predict"] == 1,
|
||||||
df["&-s_close"] > 0.01,
|
df["&-s_close"] > 0.01,
|
||||||
]
|
]
|
||||||
|
|
||||||
if enter_long_conditions:
|
if enter_long_conditions:
|
||||||
df.loc[
|
df.loc[
|
||||||
|
@ -251,7 +249,7 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
enter_short_conditions = [
|
enter_short_conditions = [
|
||||||
df["do_predict"] == 1,
|
df["do_predict"] == 1,
|
||||||
df["&-s_close"] < -0.01,
|
df["&-s_close"] < -0.01,
|
||||||
]
|
]
|
||||||
|
|
||||||
if enter_short_conditions:
|
if enter_short_conditions:
|
||||||
df.loc[
|
df.loc[
|
||||||
|
@ -261,17 +259,11 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
return df
|
return df
|
||||||
|
|
||||||
def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||||
exit_long_conditions = [
|
exit_long_conditions = [df["do_predict"] == 1, df["&-s_close"] < 0]
|
||||||
df["do_predict"] == 1,
|
|
||||||
df["&-s_close"] < 0
|
|
||||||
]
|
|
||||||
if exit_long_conditions:
|
if exit_long_conditions:
|
||||||
df.loc[reduce(lambda x, y: x & y, exit_long_conditions), "exit_long"] = 1
|
df.loc[reduce(lambda x, y: x & y, exit_long_conditions), "exit_long"] = 1
|
||||||
|
|
||||||
exit_short_conditions = [
|
exit_short_conditions = [df["do_predict"] == 1, df["&-s_close"] > 0]
|
||||||
df["do_predict"] == 1,
|
|
||||||
df["&-s_close"] > 0
|
|
||||||
]
|
|
||||||
if exit_short_conditions:
|
if exit_short_conditions:
|
||||||
df.loc[reduce(lambda x, y: x & y, exit_short_conditions), "exit_short"] = 1
|
df.loc[reduce(lambda x, y: x & y, exit_short_conditions), "exit_short"] = 1
|
||||||
|
|
||||||
|
@ -289,7 +281,6 @@ class FreqaiExampleStrategy(IStrategy):
|
||||||
side: str,
|
side: str,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
|
||||||
df, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
|
df, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
|
||||||
last_candle = df.iloc[-1].squeeze()
|
last_candle = df.iloc[-1].squeeze()
|
||||||
|
|
||||||
|
|
|
@ -35,17 +35,23 @@ class SampleHyperOptLoss(IHyperOptLoss):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def hyperopt_loss_function(results: DataFrame, trade_count: int,
|
def hyperopt_loss_function(
|
||||||
min_date: datetime, max_date: datetime,
|
results: DataFrame,
|
||||||
config: Config, processed: Dict[str, DataFrame],
|
trade_count: int,
|
||||||
*args, **kwargs) -> float:
|
min_date: datetime,
|
||||||
|
max_date: datetime,
|
||||||
|
config: Config,
|
||||||
|
processed: Dict[str, DataFrame],
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
) -> float:
|
||||||
"""
|
"""
|
||||||
Objective function, returns smaller number for better results
|
Objective function, returns smaller number for better results
|
||||||
"""
|
"""
|
||||||
total_profit = results['profit_ratio'].sum()
|
total_profit = results["profit_ratio"].sum()
|
||||||
trade_duration = results['trade_duration'].mean()
|
trade_duration = results["trade_duration"].mean()
|
||||||
|
|
||||||
trade_loss = 1 - 0.25 * exp(-(trade_count - TARGET_TRADES) ** 2 / 10 ** 5.8)
|
trade_loss = 1 - 0.25 * exp(-((trade_count - TARGET_TRADES) ** 2) / 10**5.8)
|
||||||
profit_loss = max(0, 1 - total_profit / EXPECTED_MAX_PROFIT)
|
profit_loss = max(0, 1 - total_profit / EXPECTED_MAX_PROFIT)
|
||||||
duration_loss = 0.4 * min(trade_duration / MAX_ACCEPTED_TRADE_DURATION, 1)
|
duration_loss = 0.4 * min(trade_duration / MAX_ACCEPTED_TRADE_DURATION, 1)
|
||||||
result = trade_loss + profit_loss + duration_loss
|
result = trade_loss + profit_loss + duration_loss
|
||||||
|
|
|
@ -7,8 +7,13 @@ import pandas as pd # noqa
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
from freqtrade.strategy import (BooleanParameter, CategoricalParameter, DecimalParameter,
|
from freqtrade.strategy import (
|
||||||
IStrategy, IntParameter)
|
BooleanParameter,
|
||||||
|
CategoricalParameter,
|
||||||
|
DecimalParameter,
|
||||||
|
IStrategy,
|
||||||
|
IntParameter,
|
||||||
|
)
|
||||||
|
|
||||||
# --------------------------------
|
# --------------------------------
|
||||||
# Add your lib to import here
|
# Add your lib to import here
|
||||||
|
@ -34,6 +39,7 @@ class SampleStrategy(IStrategy):
|
||||||
You should keep:
|
You should keep:
|
||||||
- timeframe, minimal_roi, stoploss, trailing_*
|
- timeframe, minimal_roi, stoploss, trailing_*
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Strategy interface version - allow new iterations of the strategy interface.
|
# Strategy interface version - allow new iterations of the strategy interface.
|
||||||
# Check the documentation or the Sample strategy to get the latest version.
|
# Check the documentation or the Sample strategy to get the latest version.
|
||||||
INTERFACE_VERSION = 3
|
INTERFACE_VERSION = 3
|
||||||
|
@ -44,6 +50,7 @@ class SampleStrategy(IStrategy):
|
||||||
# Minimal ROI designed for the strategy.
|
# Minimal ROI designed for the strategy.
|
||||||
# This attribute will be overridden if the config file contains "minimal_roi".
|
# This attribute will be overridden if the config file contains "minimal_roi".
|
||||||
minimal_roi = {
|
minimal_roi = {
|
||||||
|
# "120": 0.0, # exit after 120 minutes at break even
|
||||||
"60": 0.01,
|
"60": 0.01,
|
||||||
"30": 0.02,
|
"30": 0.02,
|
||||||
"0": 0.04
|
"0": 0.04
|
||||||
|
@ -60,7 +67,7 @@ class SampleStrategy(IStrategy):
|
||||||
# trailing_stop_positive_offset = 0.0 # Disabled / not configured
|
# trailing_stop_positive_offset = 0.0 # Disabled / not configured
|
||||||
|
|
||||||
# Optimal timeframe for the strategy.
|
# Optimal timeframe for the strategy.
|
||||||
timeframe = '5m'
|
timeframe = "5m"
|
||||||
|
|
||||||
# Run "populate_indicators()" only for new candle.
|
# Run "populate_indicators()" only for new candle.
|
||||||
process_only_new_candles = True
|
process_only_new_candles = True
|
||||||
|
@ -71,42 +78,39 @@ class SampleStrategy(IStrategy):
|
||||||
ignore_roi_if_entry_signal = False
|
ignore_roi_if_entry_signal = False
|
||||||
|
|
||||||
# Hyperoptable parameters
|
# Hyperoptable parameters
|
||||||
buy_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True)
|
buy_rsi = IntParameter(low=1, high=50, default=30, space="buy", optimize=True, load=True)
|
||||||
sell_rsi = IntParameter(low=50, high=100, default=70, space='sell', optimize=True, load=True)
|
sell_rsi = IntParameter(low=50, high=100, default=70, space="sell", optimize=True, load=True)
|
||||||
short_rsi = IntParameter(low=51, high=100, default=70, space='sell', optimize=True, load=True)
|
short_rsi = IntParameter(low=51, high=100, default=70, space="sell", optimize=True, load=True)
|
||||||
exit_short_rsi = IntParameter(low=1, high=50, default=30, space='buy', optimize=True, load=True)
|
exit_short_rsi = IntParameter(low=1, high=50, default=30, space="buy", optimize=True, load=True)
|
||||||
|
|
||||||
# Number of candles the strategy requires before producing valid signals
|
# Number of candles the strategy requires before producing valid signals
|
||||||
startup_candle_count: int = 200
|
startup_candle_count: int = 200
|
||||||
|
|
||||||
# Optional order type mapping.
|
# Optional order type mapping.
|
||||||
order_types = {
|
order_types = {
|
||||||
'entry': 'limit',
|
"entry": "limit",
|
||||||
'exit': 'limit',
|
"exit": "limit",
|
||||||
'stoploss': 'market',
|
"stoploss": "market",
|
||||||
'stoploss_on_exchange': False
|
"stoploss_on_exchange": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Optional order time in force.
|
# Optional order time in force.
|
||||||
order_time_in_force = {
|
order_time_in_force = {"entry": "GTC", "exit": "GTC"}
|
||||||
'entry': 'GTC',
|
|
||||||
'exit': 'GTC'
|
|
||||||
}
|
|
||||||
|
|
||||||
plot_config = {
|
plot_config = {
|
||||||
'main_plot': {
|
"main_plot": {
|
||||||
'tema': {},
|
"tema": {},
|
||||||
'sar': {'color': 'white'},
|
"sar": {"color": "white"},
|
||||||
},
|
},
|
||||||
'subplots': {
|
"subplots": {
|
||||||
"MACD": {
|
"MACD": {
|
||||||
'macd': {'color': 'blue'},
|
"macd": {"color": "blue"},
|
||||||
'macdsignal': {'color': 'orange'},
|
"macdsignal": {"color": "orange"},
|
||||||
},
|
},
|
||||||
"RSI": {
|
"RSI": {
|
||||||
'rsi': {'color': 'red'},
|
"rsi": {"color": "red"},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
def informative_pairs(self):
|
def informative_pairs(self):
|
||||||
|
@ -138,7 +142,7 @@ class SampleStrategy(IStrategy):
|
||||||
# ------------------------------------
|
# ------------------------------------
|
||||||
|
|
||||||
# ADX
|
# ADX
|
||||||
dataframe['adx'] = ta.ADX(dataframe)
|
dataframe["adx"] = ta.ADX(dataframe)
|
||||||
|
|
||||||
# # Plus Directional Indicator / Movement
|
# # Plus Directional Indicator / Movement
|
||||||
# dataframe['plus_dm'] = ta.PLUS_DM(dataframe)
|
# dataframe['plus_dm'] = ta.PLUS_DM(dataframe)
|
||||||
|
@ -177,7 +181,7 @@ class SampleStrategy(IStrategy):
|
||||||
# dataframe['cci'] = ta.CCI(dataframe)
|
# dataframe['cci'] = ta.CCI(dataframe)
|
||||||
|
|
||||||
# RSI
|
# RSI
|
||||||
dataframe['rsi'] = ta.RSI(dataframe)
|
dataframe["rsi"] = ta.RSI(dataframe)
|
||||||
|
|
||||||
# # Inverse Fisher transform on RSI: values [-1.0, 1.0] (https://goo.gl/2JGGoy)
|
# # Inverse Fisher transform on RSI: values [-1.0, 1.0] (https://goo.gl/2JGGoy)
|
||||||
# rsi = 0.1 * (dataframe['rsi'] - 50)
|
# rsi = 0.1 * (dataframe['rsi'] - 50)
|
||||||
|
@ -193,8 +197,8 @@ class SampleStrategy(IStrategy):
|
||||||
|
|
||||||
# Stochastic Fast
|
# Stochastic Fast
|
||||||
stoch_fast = ta.STOCHF(dataframe)
|
stoch_fast = ta.STOCHF(dataframe)
|
||||||
dataframe['fastd'] = stoch_fast['fastd']
|
dataframe["fastd"] = stoch_fast["fastd"]
|
||||||
dataframe['fastk'] = stoch_fast['fastk']
|
dataframe["fastk"] = stoch_fast["fastk"]
|
||||||
|
|
||||||
# # Stochastic RSI
|
# # Stochastic RSI
|
||||||
# Please read https://github.com/freqtrade/freqtrade/issues/2961 before using this.
|
# Please read https://github.com/freqtrade/freqtrade/issues/2961 before using this.
|
||||||
|
@ -205,12 +209,12 @@ class SampleStrategy(IStrategy):
|
||||||
|
|
||||||
# MACD
|
# MACD
|
||||||
macd = ta.MACD(dataframe)
|
macd = ta.MACD(dataframe)
|
||||||
dataframe['macd'] = macd['macd']
|
dataframe["macd"] = macd["macd"]
|
||||||
dataframe['macdsignal'] = macd['macdsignal']
|
dataframe["macdsignal"] = macd["macdsignal"]
|
||||||
dataframe['macdhist'] = macd['macdhist']
|
dataframe["macdhist"] = macd["macdhist"]
|
||||||
|
|
||||||
# MFI
|
# MFI
|
||||||
dataframe['mfi'] = ta.MFI(dataframe)
|
dataframe["mfi"] = ta.MFI(dataframe)
|
||||||
|
|
||||||
# # ROC
|
# # ROC
|
||||||
# dataframe['roc'] = ta.ROC(dataframe)
|
# dataframe['roc'] = ta.ROC(dataframe)
|
||||||
|
@ -220,16 +224,15 @@ class SampleStrategy(IStrategy):
|
||||||
|
|
||||||
# Bollinger Bands
|
# Bollinger Bands
|
||||||
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
|
||||||
dataframe['bb_lowerband'] = bollinger['lower']
|
dataframe["bb_lowerband"] = bollinger["lower"]
|
||||||
dataframe['bb_middleband'] = bollinger['mid']
|
dataframe["bb_middleband"] = bollinger["mid"]
|
||||||
dataframe['bb_upperband'] = bollinger['upper']
|
dataframe["bb_upperband"] = bollinger["upper"]
|
||||||
dataframe["bb_percent"] = (
|
dataframe["bb_percent"] = (dataframe["close"] - dataframe["bb_lowerband"]) / (
|
||||||
(dataframe["close"] - dataframe["bb_lowerband"]) /
|
dataframe["bb_upperband"] - dataframe["bb_lowerband"]
|
||||||
(dataframe["bb_upperband"] - dataframe["bb_lowerband"])
|
|
||||||
)
|
|
||||||
dataframe["bb_width"] = (
|
|
||||||
(dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe["bb_middleband"]
|
|
||||||
)
|
)
|
||||||
|
dataframe["bb_width"] = (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe[
|
||||||
|
"bb_middleband"
|
||||||
|
]
|
||||||
|
|
||||||
# Bollinger Bands - Weighted (EMA based instead of SMA)
|
# Bollinger Bands - Weighted (EMA based instead of SMA)
|
||||||
# weighted_bollinger = qtpylib.weighted_bollinger_bands(
|
# weighted_bollinger = qtpylib.weighted_bollinger_bands(
|
||||||
|
@ -264,17 +267,17 @@ class SampleStrategy(IStrategy):
|
||||||
# dataframe['sma100'] = ta.SMA(dataframe, timeperiod=100)
|
# dataframe['sma100'] = ta.SMA(dataframe, timeperiod=100)
|
||||||
|
|
||||||
# Parabolic SAR
|
# Parabolic SAR
|
||||||
dataframe['sar'] = ta.SAR(dataframe)
|
dataframe["sar"] = ta.SAR(dataframe)
|
||||||
|
|
||||||
# TEMA - Triple Exponential Moving Average
|
# TEMA - Triple Exponential Moving Average
|
||||||
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
|
dataframe["tema"] = ta.TEMA(dataframe, timeperiod=9)
|
||||||
|
|
||||||
# Cycle Indicator
|
# Cycle Indicator
|
||||||
# ------------------------------------
|
# ------------------------------------
|
||||||
# Hilbert Transform Indicator - SineWave
|
# Hilbert Transform Indicator - SineWave
|
||||||
hilbert = ta.HT_SINE(dataframe)
|
hilbert = ta.HT_SINE(dataframe)
|
||||||
dataframe['htsine'] = hilbert['sine']
|
dataframe["htsine"] = hilbert["sine"]
|
||||||
dataframe['htleadsine'] = hilbert['leadsine']
|
dataframe["htleadsine"] = hilbert["leadsine"]
|
||||||
|
|
||||||
# Pattern Recognition - Bullish candlestick patterns
|
# Pattern Recognition - Bullish candlestick patterns
|
||||||
# ------------------------------------
|
# ------------------------------------
|
||||||
|
@ -353,22 +356,24 @@ class SampleStrategy(IStrategy):
|
||||||
dataframe.loc[
|
dataframe.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 30
|
# Signal: RSI crosses above 30
|
||||||
(qtpylib.crossed_above(dataframe['rsi'], self.buy_rsi.value)) &
|
(qtpylib.crossed_above(dataframe["rsi"], self.buy_rsi.value))
|
||||||
(dataframe['tema'] <= dataframe['bb_middleband']) & # Guard: tema below BB middle
|
& (dataframe["tema"] <= dataframe["bb_middleband"]) # Guard: tema below BB middle
|
||||||
(dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising
|
& (dataframe["tema"] > dataframe["tema"].shift(1)) # Guard: tema is raising
|
||||||
(dataframe['volume'] > 0) # Make sure Volume is not 0
|
& (dataframe["volume"] > 0) # Make sure Volume is not 0
|
||||||
),
|
),
|
||||||
'enter_long'] = 1
|
"enter_long",
|
||||||
|
] = 1
|
||||||
|
|
||||||
dataframe.loc[
|
dataframe.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 70
|
# Signal: RSI crosses above 70
|
||||||
(qtpylib.crossed_above(dataframe['rsi'], self.short_rsi.value)) &
|
(qtpylib.crossed_above(dataframe["rsi"], self.short_rsi.value))
|
||||||
(dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle
|
& (dataframe["tema"] > dataframe["bb_middleband"]) # Guard: tema above BB middle
|
||||||
(dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling
|
& (dataframe["tema"] < dataframe["tema"].shift(1)) # Guard: tema is falling
|
||||||
(dataframe['volume'] > 0) # Make sure Volume is not 0
|
& (dataframe["volume"] > 0) # Make sure Volume is not 0
|
||||||
),
|
),
|
||||||
'enter_short'] = 1
|
"enter_short",
|
||||||
|
] = 1
|
||||||
|
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
|
@ -382,23 +387,25 @@ class SampleStrategy(IStrategy):
|
||||||
dataframe.loc[
|
dataframe.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 70
|
# Signal: RSI crosses above 70
|
||||||
(qtpylib.crossed_above(dataframe['rsi'], self.sell_rsi.value)) &
|
(qtpylib.crossed_above(dataframe["rsi"], self.sell_rsi.value))
|
||||||
(dataframe['tema'] > dataframe['bb_middleband']) & # Guard: tema above BB middle
|
& (dataframe["tema"] > dataframe["bb_middleband"]) # Guard: tema above BB middle
|
||||||
(dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard: tema is falling
|
& (dataframe["tema"] < dataframe["tema"].shift(1)) # Guard: tema is falling
|
||||||
(dataframe['volume'] > 0) # Make sure Volume is not 0
|
& (dataframe["volume"] > 0) # Make sure Volume is not 0
|
||||||
),
|
),
|
||||||
|
"exit_long",
|
||||||
'exit_long'] = 1
|
] = 1
|
||||||
|
|
||||||
dataframe.loc[
|
dataframe.loc[
|
||||||
(
|
(
|
||||||
# Signal: RSI crosses above 30
|
# Signal: RSI crosses above 30
|
||||||
(qtpylib.crossed_above(dataframe['rsi'], self.exit_short_rsi.value)) &
|
(qtpylib.crossed_above(dataframe["rsi"], self.exit_short_rsi.value))
|
||||||
|
&
|
||||||
# Guard: tema below BB middle
|
# Guard: tema below BB middle
|
||||||
(dataframe['tema'] <= dataframe['bb_middleband']) &
|
(dataframe["tema"] <= dataframe["bb_middleband"])
|
||||||
(dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard: tema is raising
|
& (dataframe["tema"] > dataframe["tema"].shift(1)) # Guard: tema is raising
|
||||||
(dataframe['volume'] > 0) # Make sure Volume is not 0
|
& (dataframe["volume"] > 0) # Make sure Volume is not 0
|
||||||
),
|
),
|
||||||
'exit_short'] = 1
|
"exit_short",
|
||||||
|
] = 1
|
||||||
|
|
||||||
return dataframe
|
return dataframe
|
||||||
|
|
Loading…
Reference in New Issue
Block a user