💬 Forum

MultiMA_TSL

Ysertysert2/freqtrade-strategies/strategies/MultiMA_TSL/MultiMA_TSL.py · ★3 · ⑂2 · first seen 2026-07-24 · repo updated 2025-09-23

Basics mode: spot timeframe: 5m interface version: 3 outdated
Settings stoploss: -0.15 has minimal roi trailing custom stoploss protections process only new candles startup candle count: 200 hyperopt hyperopt params: 26
Indicators EMA RSI talib technical
Concepts risk_management trailing
15 related strategies ( identical code, similar name)

Each tile is a different kind of check — from an instant code lint to full sandboxed backtests and forward tests on recent data. Not sure what a check actually proves? See the FAQ →

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy as np
import talib.abstract as ta
from freqtrade.strategy.interface import IStrategy
from freqtrade.strategy import (merge_informative_pair,
                                DecimalParameter, IntParameter, BooleanParameter, CategoricalParameter, stoploss_from_open)
from pandas import DataFrame
from functools import reduce
from freqtrade.persistence import Trade
from datetime import datetime, timedelta
from freqtrade.exchange import timeframe_to_prev_date
from technical.indicators import zema

###########################################################################################################
##    MultiMA_TSL, modded by stash86, based on SMAOffsetProtectOptV1 (modded by Perkmeister)             ##
##    Based on @Lamborghini Store's SMAOffsetProtect strat, heavily based on @tirail's original SMAOffset##
##                                                                                                       ##
##    Strategy for Freqtrade https://github.com/freqtrade/freqtrade                                      ##
##                                                                                                       ##
###########################################################################################################

# I hope you do enough testing before proceeding, either backtesting and/or dry run.
# Any profits and losses are all your responsibility

class MultiMA_TSL(IStrategy):
    INTERFACE_VERSION = 3

    buy_params = {
        "base_nb_candles_buy_ema": 50,
        "low_offset_ema": 1.061,

        "base_nb_candles_buy_zema": 30,
        "low_offset_zema": 0.963,
        "rsi_buy_zema": 50,

        "base_nb_candles_buy_trima": 14,
        "low_offset_trima": 0.963,
        "rsi_buy_trima": 50,

        "buy_roc_max": 45,

        "buy_condition_trima_enable": True,
        "buy_condition_zema_enable": True,
    }

    sell_params = {
        "base_nb_candles_sell": 32,
        "high_offset_ema": 1.002,

        "base_nb_candles_sell_trima": 48,
        "high_offset_trima": 1.085,
    }

    # ROI table:
    minimal_roi = {
        "0": 100
    }

    stoploss = -0.15

    # Multi Offset
    base_nb_candles_sell = IntParameter(5, 80, default=20, space='sell', optimize=False)
    base_nb_candles_sell_trima = IntParameter(5, 80, default=20, space='sell', optimize=False)
    high_offset_trima = DecimalParameter(0.99, 1.1, default=1.012, space='sell', optimize=False)

    base_nb_candles_buy_ema = IntParameter(5, 80, default=20, space='buy', optimize=False)
    low_offset_ema = DecimalParameter(0.9, 1.1, default=0.958, space='buy', optimize=False)
    high_offset_ema = DecimalParameter(0.99, 1.1, default=1.012, space='sell', optimize=False)
    rsi_buy_ema = IntParameter(30, 70, default=61, space='buy', optimize=False)

    base_nb_candles_buy_trima = IntParameter(5, 80, default=20, space='buy', optimize=False)
    low_offset_trima = DecimalParameter(0.9, 0.99, default=0.958, space='buy', optimize=False)
    rsi_buy_trima = IntParameter(30, 70, default=61, space='buy', optimize=False)

    base_nb_candles_buy_zema = IntParameter(5, 80, default=20, space='buy', optimize=False)
    low_offset_zema = DecimalParameter(0.9, 0.99, default=0.958, space='buy', optimize=False)
    rsi_buy_zema = IntParameter(30, 70, default=61, space='buy', optimize=False)

    buy_condition_enable_optimize = True
    buy_condition_trima_enable = BooleanParameter(default=True, space='buy', optimize=buy_condition_enable_optimize)
    buy_condition_zema_enable = BooleanParameter(default=True, space='buy', optimize=buy_condition_enable_optimize)

    # Protection1
    ewo_low = DecimalParameter(-20.0, -8.0, default=-20.0, space='buy', optimize=False)
    ewo_high = DecimalParameter(2.0, 12.0, default=6.0, space='buy', optimize=False)
    fast_ewo = IntParameter(10, 50, default=50, space='buy', optimize=False)
    slow_ewo = IntParameter(100, 200, default=200, space='buy', optimize=False)
    buy_roc_max = DecimalParameter(20, 70, default=55, space='buy', optimize=False)
    buy_peak_max = DecimalParameter(1, 1.1, default=1.03, decimals=3, space='buy', optimize=False)
    buy_rsi_fast = IntParameter(0, 50, default=35, space='buy', optimize=False)

    # Trailing stoploss (not used)
    trailing_stop = False
    trailing_only_offset_is_reached = True
    trailing_stop_positive = 0.01
    trailing_stop_positive_offset = 0.018

    use_custom_stoploss = True

    # Protection hyperspace params:
    protection_params = {
        "low_profit_lookback": 60,
        "low_profit_min_req": 0.03,
        "low_profit_stop_duration": 29,

        "cooldown_lookback": 2,  # value loaded from strategy
        "stoploss_lookback": 72,  # value loaded from strategy
        "stoploss_stop_duration": 20,  # value loaded from strategy
    }

    cooldown_lookback = IntParameter(2, 48, default=2, space="protection", optimize=False)

    low_profit_lookback = IntParameter(2, 60, default=20, space="protection", optimize=False)
    low_profit_stop_duration = IntParameter(12, 200, default=20, space="protection", optimize=False)
    low_profit_min_req = DecimalParameter(-0.05, 0.05, default=-0.05, space="protection", decimals=2, optimize=False)

    @property
    def protections(self):
        prot = []

        prot.append({
            "method": "CooldownPeriod",
            "stop_duration_candles": self.cooldown_lookback.value
        })
        prot.append({
            "method": "LowProfitPairs",
            "lookback_period_candles": self.low_profit_lookback.value,
            "trade_limit": 1,
            "stop_duration": int(self.low_profit_stop_duration.value),
            "required_profit": self.low_profit_min_req.value
        })

        return prot

    # Optimal timeframe for the strategy.
    timeframe = '5m'

    # Run "populate_indicators()" only for new candle.
    process_only_new_candles = True

    # These values can be overridden in the "ask_strategy" section in the config.
    use_sell_signal = True
    sell_profit_only = False
    ignore_roi_if_buy_signal = False

    # Number of candles the strategy requires before producing valid signals
    startup_candle_count: int = 200

    #credit to Perkmeister for this custom stoploss to help the strategy ride a green candle when the sell signal triggered
    def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
                        current_rate: float, current_profit: float, **kwargs) -> float:
        sl_new = 1

        if not self.config['runmode'].value in ('backtest', 'hyperopt'):
            dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
            if(len(dataframe) >= 1):
                last_candle = dataframe.iloc[-1]
                if((last_candle['sell_copy'] == 1) & (last_candle['buy_copy'] == 0)):
                    sl_new = 0.001

        return sl_new

    def get_ticker_indicator(self):
        return int(self.timeframe[:-1])

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # EWO
        dataframe['ewo'] = EWO(dataframe, self.fast_ewo.value, self.slow_ewo.value)

        # RSI
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4)

        dataframe["roc_max"] = dataframe["close"].pct_change(48).rolling(12).max() * 100
        
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []

        dataframe['ema_offset_buy'] = ta.EMA(dataframe, int(self.base_nb_candles_buy_ema.value)) *self.low_offset_ema.value
        dataframe['zema_offset_buy'] = zema(dataframe, int(self.base_nb_candles_buy_zema.value)) *self.low_offset_zema.value
        dataframe['trima_offset_buy'] = ta.TRIMA(dataframe, int(self.base_nb_candles_buy_trima.value)) *self.low_offset_trima.value
        
        dataframe.loc[:, 'buy_tag'] = ''
        dataframe.loc[:, 'buy_copy'] = 0
        dataframe.loc[:, 'buy'] = 0

        buy_offset_trima = (
            self.buy_condition_trima_enable.value &
            (dataframe['close'] < dataframe['trima_offset_buy']) &
            (
                (dataframe['ewo'] < self.ewo_low.value)
                |
                (
                    (dataframe['ewo'] > self.ewo_high.value)
                    &
                    (dataframe['rsi'] < self.rsi_buy_trima.value)
                )
            )
        )
        dataframe.loc[buy_offset_trima, 'buy_tag'] += 'trima '
        conditions.append(buy_offset_trima)

        buy_offset_zema = (
            self.buy_condition_zema_enable.value &
            (dataframe['close'] < dataframe['zema_offset_buy']) &
            (
                (dataframe['ewo'] < self.ewo_low.value)
                |
                (
                    (dataframe['ewo'] > self.ewo_high.value)
                    &
                    (dataframe['rsi'] < self.rsi_buy_zema.value)
                )
            )
        )
        dataframe.loc[buy_offset_zema, 'buy_tag'] += 'zema '
        conditions.append(buy_offset_zema)

        add_check = (
            (dataframe['rsi_fast'] < self.buy_rsi_fast.value)
            &
            (dataframe['close'] < dataframe['ema_offset_buy'])
            &
            (dataframe['volume'] > 0)
        )
        
        if conditions:
            dataframe.loc[
                (add_check & reduce(lambda x, y: x | y, conditions)),
                ['buy_copy','buy']
            ]=(1,1)

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[:, 'sell_copy'] = 0

        dataframe['ema_offset_sell'] = ta.EMA(dataframe, int(self.base_nb_candles_sell.value)) *self.high_offset_ema.value
        dataframe['trima_offset_sell'] = ta.TRIMA(dataframe, int(self.base_nb_candles_sell_trima.value)) *self.high_offset_trima.value

        conditions = []

        conditions.append(
            (
                (dataframe['close'] > dataframe['ema_offset_sell']) &
                (dataframe['volume'] > 0)
            )
        )

        conditions.append(
            (
                (dataframe['close'] > dataframe['trima_offset_sell']) &
                (dataframe['volume'] > 0)
            )
        )

        if conditions:
            dataframe.loc[
                reduce(lambda x, y: x | y, conditions),
                ['sell_copy', 'sell']
            ]=(1,1)

        if not self.config['runmode'].value in ('backtest', 'hyperopt'):
            dataframe.loc[:, 'sell'] = 0

        return dataframe


# Elliot Wave Oscillator
def EWO(dataframe, sma1_length=5, sma2_length=35):
    df = dataframe.copy()
    sma1 = ta.EMA(df, timeperiod=sma1_length)
    sma2 = ta.EMA(df, timeperiod=sma2_length)
    smadif = (sma1 - sma2) / df['close'] * 100
    return smadif