MultiMa V2(Errors fixed+Shift removed+offline indicator population)

This commit is contained in:
girdakan
2021-11-05 20:02:35 +00:00
parent 4c07a42cb7
commit 492270e631
+58 -47
View File
@@ -1,8 +1,7 @@
# MultiMa Strategy
# MultiMa Strategy V2
# Author: @Mablue (Masoud Azizi)
# github: https://github.com/mablue/
# (First Hyperopt it.A hyperopt file is available)
#
# --- Do not remove these libs ---
from freqtrade.strategy.hyper import IntParameter
from freqtrade.strategy.interface import IStrategy
@@ -17,72 +16,84 @@ from functools import reduce
class MultiMa(IStrategy):
# 111/2000: 18 trades. 12/4/2 Wins/Draws/Losses. Avg profit 9.72%. Median profit 3.01%. Total profit 733.01234143 USDT ( 73.30%). Avg duration 2 days, 18:40:00 min. Objective: 1.67048
buy_ma_count = IntParameter(0, 10, default=10, space="buy")
buy_ma_gap = IntParameter(2, 10, default=2, space="buy")
buy_ma_shift = IntParameter(0, 10, default=0, space="buy")
# buy_ma_rolling = IntParameter(0, 10, default=0, space='buy')
# Buy hyperspace params:
buy_params = {
"buy_ma_count": 4,
"buy_ma_gap": 15,
}
sell_ma_count = IntParameter(0, 10, default=10, space="sell")
sell_ma_gap = IntParameter(2, 10, default=2, space="sell")
sell_ma_shift = IntParameter(0, 10, default=0, space="sell")
# sell_ma_rolling = IntParameter(0, 10, default=0, space='sell')
# Sell hyperspace params:
sell_params = {
"sell_ma_count": 12,
"sell_ma_gap": 68,
}
# ROI table:
minimal_roi = {"0": 0.30873, "569": 0.16689, "3211": 0.06473, "7617": 0}
minimal_roi = {
"0": 0.523,
"1553": 0.123,
"2332": 0.076,
"3169": 0
}
# Stoploss:
stoploss = -0.1
stoploss = -0.345
# Buy hypers
# Trailing stop:
trailing_stop = False # value loaded from strategy
trailing_stop_positive = None # value loaded from strategy
trailing_stop_positive_offset = 0.0 # value loaded from strategy
trailing_only_offset_is_reached = False # value loaded from strategy
# Opimal Timeframe
timeframe = "4h"
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
count_max = 20
gap_max = 100
# We will dinamicly generate the indicators
# cuz this method just run one time in hyperopts
# if you have static timeframes you can move first loop of buy and sell trends populators inside this method
buy_ma_count = IntParameter(1, count_max, default=7, space="buy")
buy_ma_gap = IntParameter(1, gap_max, default=7, space="buy")
sell_ma_count = IntParameter(1, count_max, default=7, space="sell")
sell_ma_gap = IntParameter(1, gap_max, default=94, space="sell")
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
for count in range(self.count_max):
for gap in range(self.gap_max):
if count*gap > 1 and count*gap not in dataframe.keys():
dataframe[count*gap] = ta.TEMA(
dataframe, timeperiod=int(count*gap)
)
print(" ", metadata['pair'], end="\t\r")
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
for i in self.buy_ma_count.range:
dataframe[f"buy-ma-{i+1}"] = ta.SMA(
dataframe, timeperiod=int((i + 1) * self.buy_ma_gap.value)
)
conditions = []
# I used range(self.buy_ma_count.value) instade of self.buy_ma_count.range
# Cuz it returns range(7,8) but we need range(8) for all modes hyperopt, backtest and etc
for ma_count in range(self.buy_ma_count.value):
key = ma_count*self.buy_ma_gap.value
past_key = (ma_count-1)*self.buy_ma_gap.value
if past_key > 1 and key in dataframe.keys() and past_key in dataframe.keys():
conditions.append(dataframe[key] < dataframe[past_key])
for i in self.buy_ma_count.range:
if i > 1:
shift = self.buy_ma_shift.value
for shift in self.buy_ma_shift.range:
conditions.append(
dataframe[f"buy-ma-{i}"].shift(shift)
> dataframe[f"buy-ma-{i-1}"].shift(shift)
)
if conditions:
dataframe.loc[reduce(lambda x, y: x & y, conditions), "buy"] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
for i in self.sell_ma_count.range:
dataframe[f"sell-ma-{i+1}"] = ta.SMA(
dataframe, timeperiod=int((i + 1) * self.sell_ma_gap.value)
)
conditions = []
for i in self.sell_ma_count.range:
if i > 1:
shift = self.sell_ma_shift.value
for shift in self.sell_ma_shift.range:
conditions.append(
dataframe[f"sell-ma-{i}"].shift(shift)
< dataframe[f"sell-ma-{i-1}"].shift(shift)
)
for ma_count in range(self.sell_ma_count.value):
key = ma_count*self.sell_ma_gap.value
past_key = (ma_count-1)*self.sell_ma_gap.value
if past_key > 1 and key in dataframe.keys() and past_key in dataframe.keys():
conditions.append(dataframe[key] > dataframe[past_key])
if conditions:
dataframe.loc[reduce(lambda x, y: x & y, conditions), "sell"] = 1
dataframe.loc[reduce(lambda x, y: x | y, conditions), "sell"] = 1
return dataframe