💬 Forum

MultiTimeframeStrategy

smpsandro1239/Freqtrade/user_data/strategies/MultiTimeframeStrategy.py · ★1 · first seen 2026-07-16 · repo updated 2025-09-10 · ⬇ 1 download

Basics mode: spot timeframe: 1m interface version: 3 5m 15m 1h
Settings stoploss: -0.06 has minimal roi trailing custom stoploss protections hyperopt hyperopt params: 7
Indicators ATR EMA RSI SMA
Concepts mean_reversion 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 →

Source

Download Raw
  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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# -*- coding: utf-8 -*-
"""
MultiTimeframeStrategy - Análise Multi-Timeframe
Analisa 1m, 5m, 15m, 1h simultaneamente para confirmação de sinais
"""
import logging
from datetime import datetime

import numpy as np
import pandas as pd

from freqtrade.strategy import (CategoricalParameter, DecimalParameter,
                                IntParameter, IStrategy, informative)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class MultiTimeframeStrategy(IStrategy):
    """
    Estratégia Multi-Timeframe

    Timeframes analisados:
    - 1m: Entrada precisa
    - 5m: Confirmação de momentum
    - 15m: Tendência de médio prazo
    - 1h: Tendência de longo prazo

    Lógica:
    - Tendência 1h deve ser bullish
    - Confirmação 15m deve estar alinhada
    - Momentum 5m deve ser favorável
    - Entrada precisa no 1m
    """

    INTERFACE_VERSION = 3

    # Configurações básicas
    timeframe = '1m'  # Timeframe principal para entrada
    stoploss = -0.06
    minimal_roi = {
        "0": 0.06,
        "5": 0.04,
        "10": 0.03,
        "15": 0.02,
        "30": 0.01,
        "60": 0.005
    }

    # Trailing stop
    trailing_stop = True
    trailing_stop_positive = 0.02
    trailing_stop_positive_offset = 0.025
    trailing_only_offset_is_reached = True

    # Parâmetros otimizáveis
    rsi_oversold = IntParameter(20, 35, default=30, space='buy')
    rsi_overbought = IntParameter(65, 80, default=70, space='sell')

    # Parâmetros de confirmação multi-timeframe
    trend_strength_threshold = DecimalParameter(0.5, 0.9, default=0.7, space='buy')
    volume_threshold = DecimalParameter(1.0, 2.0, default=1.5, space='buy')

    # Proteções
    cooldown_lookback = IntParameter(2, 48, default=15, space="protection")
    stop_duration = IntParameter(12, 200, default=30, space="protection")
    use_stop_protection = CategoricalParameter([True, False], default=True, space="protection")

    def get_int_value(self, param):
        """Helper para converter parâmetros para int"""
        return int(param.value)

    @property
    def protections(self):
        if not self.use_stop_protection.value:
            return []
        return [
            {
                "method": "CooldownPeriod",
                "stop_duration_candles": self.get_int_value(self.stop_duration)
            },
            {
                "method": "StoplossGuard",
                "lookback_period_candles": self.get_int_value(self.cooldown_lookback),
                "trade_limit": 2,
                "stop_duration_candles": self.get_int_value(self.stop_duration),
                "only_per_pair": False
            }
        ]

    def rsi(self, series: pd.Series, period: int = 14) -> pd.Series:
        """Implementação nativa do RSI"""
        delta = series.diff()
        gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
        loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
        rs = gain / loss
        return 100 - (100 / (1 + rs))

    def ema(self, series: pd.Series, period: int) -> pd.Series:
        """Implementação nativa da EMA"""
        return series.ewm(span=period).mean()

    def sma(self, series: pd.Series, period: int) -> pd.Series:
        """Implementação nativa da SMA"""
        return series.rolling(window=period).mean()

    def atr(self, high: pd.Series, low: pd.Series, close: pd.Series, period: int = 14):
        """Implementação nativa do ATR"""
        tr1 = high - low
        tr2 = abs(high - close.shift())
        tr3 = abs(low - close.shift())
        tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1)
        return tr.rolling(window=period).mean()

    def calculate_trend_strength(self, dataframe: pd.DataFrame) -> pd.Series:
        """
        Calcular força da tendência baseada em múltiplos indicadores
        Retorna valor entre 0 (bearish) e 1 (bullish)
        """
        # EMA alignment
        ema_8 = self.ema(dataframe['close'], 8)
        ema_21 = self.ema(dataframe['close'], 21)
        ema_50 = self.ema(dataframe['close'], 50)

        ema_alignment = (
            (ema_8 > ema_21).astype(int) * 0.4 +
            (ema_21 > ema_50).astype(int) * 0.3 +
            (dataframe['close'] > ema_8).astype(int) * 0.3
        )

        # RSI momentum
        rsi = self.rsi(dataframe['close'])
        rsi_momentum = ((rsi - 50) / 50).clip(-1, 1) * 0.5 + 0.5

        # Price momentum
        price_change_5 = dataframe['close'].pct_change(5)
        price_momentum = (price_change_5 > 0).astype(int)

        # Combine all factors
        trend_strength = (
            ema_alignment * 0.5 +
            rsi_momentum * 0.3 +
            price_momentum * 0.2
        )

        return trend_strength.clip(0, 1)

    @informative('5m')
    def populate_indicators_5m(self, dataframe: pd.DataFrame, metadata: dict) -> pd.DataFrame:
        """Indicadores para timeframe 5m"""
        dataframe['rsi'] = self.rsi(dataframe['close'])
        dataframe['ema_8'] = self.ema(dataframe['close'], 8)
        dataframe['ema_21'] = self.ema(dataframe['close'], 21)
        dataframe['ema_50'] = self.ema(dataframe['close'], 50)
        dataframe['volume_sma'] = self.sma(dataframe['volume'], 20)
        dataframe['volume_ratio'] = dataframe['volume'] / dataframe['volume_sma']
        dataframe['trend_strength'] = self.calculate_trend_strength(dataframe)

        return dataframe

    @informative('15m')
    def populate_indicators_15m(self, dataframe: pd.DataFrame, metadata: dict) -> pd.DataFrame:
        """Indicadores para timeframe 15m"""
        dataframe['rsi'] = self.rsi(dataframe['close'])
        dataframe['ema_8'] = self.ema(dataframe['close'], 8)
        dataframe['ema_21'] = self.ema(dataframe['close'], 21)
        dataframe['ema_50'] = self.ema(dataframe['close'], 50)
        dataframe['sma_100'] = self.sma(dataframe['close'], 100)
        dataframe['trend_strength'] = self.calculate_trend_strength(dataframe)

        # Volatilidade
        dataframe['atr'] = self.atr(dataframe['high'], dataframe['low'], dataframe['close'])
        dataframe['volatility'] = dataframe['close'].rolling(20).std()

        return dataframe

    @informative('1h')
    def populate_indicators_1h(self, dataframe: pd.DataFrame, metadata: dict) -> pd.DataFrame:
        """Indicadores para timeframe 1h - Tendência de longo prazo"""
        dataframe['rsi'] = self.rsi(dataframe['close'])
        dataframe['ema_8'] = self.ema(dataframe['close'], 8)
        dataframe['ema_21'] = self.ema(dataframe['close'], 21)
        dataframe['ema_50'] = self.ema(dataframe['close'], 50)
        dataframe['sma_200'] = self.sma(dataframe['close'], 200)
        dataframe['trend_strength'] = self.calculate_trend_strength(dataframe)

        # Tendência de longo prazo
        dataframe['long_trend'] = (
            (dataframe['ema_8'] > dataframe['ema_21']) &
            (dataframe['ema_21'] > dataframe['ema_50']) &
            (dataframe['close'] > dataframe['sma_200'])
        ).astype(int)

        return dataframe

    def populate_indicators(self, dataframe, metadata):
        """
        Indicadores para timeframe principal (1m)
        """
        # Indicadores básicos 1m
        dataframe['rsi'] = self.rsi(dataframe['close'])
        dataframe['ema_8'] = self.ema(dataframe['close'], 8)
        dataframe['ema_21'] = self.ema(dataframe['close'], 21)
        dataframe['volume_sma'] = self.sma(dataframe['volume'], 20)
        dataframe['volume_ratio'] = dataframe['volume'] / dataframe['volume_sma']

        # ATR para stop loss dinâmico
        dataframe['atr'] = self.atr(dataframe['high'], dataframe['low'], dataframe['close'])

        # Momentum de curto prazo
        dataframe['price_change_3'] = dataframe['close'].pct_change(3)
        dataframe['price_change_5'] = dataframe['close'].pct_change(5)

        # Trend strength no timeframe principal
        dataframe['trend_strength'] = self.calculate_trend_strength(dataframe)

        return dataframe

    def populate_entry_trend(self, dataframe, metadata):
        """
        Sinais de entrada baseados em confirmação multi-timeframe
        """
        # Condições do timeframe 1h (tendência de longo prazo)
        long_trend_bullish = (
            (dataframe['long_trend_1h'] == 1) &
            (dataframe['trend_strength_1h'] > 0.6)
        )

        # Condições do timeframe 15m (tendência de médio prazo)
        medium_trend_bullish = (
            (dataframe['trend_strength_15m'] > self.trend_strength_threshold.value) &
            (dataframe['rsi_15m'] < 65) &
            (dataframe['ema_8_15m'] > dataframe['ema_21_15m'])
        )

        # Condições do timeframe 5m (momentum)
        momentum_favorable = (
            (dataframe['trend_strength_5m'] > 0.6) &
            (dataframe['volume_ratio_5m'] > 1.2) &
            (dataframe['rsi_5m'] < 60)
        )

        # Condições do timeframe 1m (entrada precisa)
        entry_precise = (
            (dataframe['rsi'] < self.rsi_oversold.value) &
            (dataframe['ema_8'] > dataframe['ema_21']) &
            (dataframe['volume_ratio'] > self.volume_threshold.value) &
            (dataframe['price_change_3'] > -0.005)  # Não em queda livre
        )

        # Confirmação adicional: preço não muito longe da EMA
        price_not_extended = (
            abs(dataframe['close'] - dataframe['ema_21']) / dataframe['ema_21'] < 0.02
        )

        # Sinal final: todas as condições devem ser verdadeiras
        entry_signal = (
            long_trend_bullish &
            medium_trend_bullish &
            momentum_favorable &
            entry_precise &
            price_not_extended
        )

        dataframe.loc[entry_signal, 'enter_long'] = 1
        dataframe.loc[entry_signal, 'enter_tag'] = 'multi_tf_confirmed'

        return dataframe

    def populate_exit_trend(self, dataframe, metadata):
        """
        Sinais de saída baseados em deterioração multi-timeframe
        """
        # Saída por RSI overbought no 1m
        rsi_exit = dataframe['rsi'] > self.rsi_overbought.value

        # Saída por deterioração da tendência 5m
        momentum_deterioration = (
            (dataframe['trend_strength_5m'] < 0.4) |
            (dataframe['rsi_5m'] > 75)
        )

        # Saída por deterioração da tendência 15m
        medium_trend_deterioration = (
            (dataframe['trend_strength_15m'] < 0.3) |
            (dataframe['ema_8_15m'] < dataframe['ema_21_15m'])
        )

        # Saída por reversão da tendência 1h
        long_trend_reversal = (
            (dataframe['long_trend_1h'] == 0) &
            (dataframe['trend_strength_1h'] < 0.4)
        )

        # Volume anômalo (possível dump)
        volume_spike = dataframe['volume_ratio'] > 3.0

        # Sinal de saída: qualquer condição crítica
        exit_signal = (
            rsi_exit |
            momentum_deterioration |
            medium_trend_deterioration |
            long_trend_reversal |
            volume_spike
        )

        dataframe.loc[exit_signal, 'exit_long'] = 1

        return dataframe

    def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
                       current_rate: float, current_profit: float, **kwargs) -> float:
        """
        Stop loss dinâmico baseado no ATR
        """
        dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
        last_candle = dataframe.iloc[-1].squeeze()

        if 'atr' in last_candle:
            # Stop loss baseado em 2x ATR
            atr_stop = 2.0 * last_candle['atr'] / current_rate

            # Mínimo de 3%, máximo de 8%
            dynamic_stop = max(0.03, min(0.08, atr_stop))

            return -dynamic_stop

        # Fallback para stop loss fixo
        return self.stoploss