Files
freqtrade-strategies/user_data/strategies/MultiMa.py
T

89 lines
3.0 KiB
Python
Raw Normal View History

2021-05-18 22:13:53 +04:30
# MultiMa Strategy
# 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
from pandas import DataFrame
2021-09-23 21:34:14 +10:00
2021-05-18 22:13:53 +04:30
# --------------------------------
# Add your lib to import here
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
from functools import reduce
class MultiMa(IStrategy):
2021-09-23 21:34:14 +10:00
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")
2021-05-18 22:13:53 +04:30
# buy_ma_rolling = IntParameter(0, 10, default=0, space='buy')
2021-09-23 21:34:14 +10:00
sell_ma_count = IntParameter(0, 10, default=10, space="sell")
sell_ma_gap = IntParameter(2, 10, default=2, space="sell")
2021-11-04 10:23:17 -05:00
sell_ma_shift = IntParameter(0, 10, default=0, space="sell")
2021-05-18 22:13:53 +04:30
# sell_ma_rolling = IntParameter(0, 10, default=0, space='sell')
# ROI table:
2021-09-23 21:34:14 +10:00
minimal_roi = {"0": 0.30873, "569": 0.16689, "3211": 0.06473, "7617": 0}
2021-05-18 22:13:53 +04:30
# Stoploss:
2021-09-23 21:34:14 +10:00
stoploss = -0.1
2021-05-18 22:13:53 +04:30
# Buy hypers
2021-09-23 21:34:14 +10:00
timeframe = "4h"
2021-05-18 22:13:53 +04:30
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# We will dinamicly generate the indicators
2021-05-19 10:40:00 +04:30
# 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
2021-05-19 10:22:41 +04:30
2021-05-18 22:13:53 +04:30
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
2021-05-19 10:47:58 +04:30
for i in self.buy_ma_count.range:
2021-09-23 21:34:14 +10:00
dataframe[f"buy-ma-{i+1}"] = ta.SMA(
dataframe, timeperiod=int((i + 1) * self.buy_ma_gap.value)
)
2021-05-18 22:13:53 +04:30
conditions = []
2021-05-19 10:47:58 +04:30
for i in self.buy_ma_count.range:
2021-05-18 22:13:53 +04:30
if i > 1:
shift = self.buy_ma_shift.value
2021-05-19 10:47:58 +04:30
for shift in self.buy_ma_shift.range:
2021-05-18 22:13:53 +04:30
conditions.append(
2021-09-23 21:34:14 +10:00
dataframe[f"buy-ma-{i}"].shift(shift)
> dataframe[f"buy-ma-{i-1}"].shift(shift)
2021-05-18 22:13:53 +04:30
)
if conditions:
2021-09-23 21:34:14 +10:00
dataframe.loc[reduce(lambda x, y: x & y, conditions), "buy"] = 1
2021-05-18 22:13:53 +04:30
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
2021-05-19 10:47:58 +04:30
for i in self.sell_ma_count.range:
2021-09-23 21:34:14 +10:00
dataframe[f"sell-ma-{i+1}"] = ta.SMA(
dataframe, timeperiod=int((i + 1) * self.sell_ma_gap.value)
)
2021-05-19 10:40:00 +04:30
2021-05-18 22:13:53 +04:30
conditions = []
2021-05-19 10:47:58 +04:30
for i in self.sell_ma_count.range:
2021-05-18 22:13:53 +04:30
if i > 1:
shift = self.sell_ma_shift.value
2021-05-19 10:47:58 +04:30
for shift in self.sell_ma_shift.range:
2021-05-18 22:13:53 +04:30
conditions.append(
2021-09-23 21:34:14 +10:00
dataframe[f"sell-ma-{i}"].shift(shift)
< dataframe[f"sell-ma-{i-1}"].shift(shift)
2021-05-18 22:13:53 +04:30
)
if conditions:
2021-09-23 21:34:14 +10:00
dataframe.loc[reduce(lambda x, y: x & y, conditions), "sell"] = 1
2021-05-18 22:13:53 +04:30
return dataframe