💬 Forum

BollingerBandDipStrategy

jimmson/freqtrader/user_data/strategies/BollingerBandDipStrategy.py · ★1 · first seen 2026-07-19 · repo updated 2025-10-13 · ⬇ 1 download

Basics mode: spot timeframe: 3m interface version: 3
Settings stoploss: -0.99 has minimal roi trailing protections hyperopt hyperopt params: 4
Indicators Bollinger_Bands DEMA EMA MACD RSI talib
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 →

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
from datetime import datetime
from typing import Optional
import freqtrade.vendor.qtpylib.indicators as qtpylib
from freqtrade.strategy import IStrategy, merge_informative_pair, IntParameter
from pandas import DataFrame
import talib.abstract as ta

bb_color = 'rgba(255,76,46,0.2)'


class BollingerBandDipStrategy(IStrategy):
    INTERFACE_VERSION = 3

    # Strategy parameters
    timeframe = '3m'
    informative_timeframe = '4h'

    buy_params = {
        "leverage_amount": 10,
        "percent_below_lowerband": 22,
        "rsi_down": 20,
    }

    sell_params = {
        "rsi_up": 70,
    }

    minimal_roi = {
        "0": 1.5,
    }

    # Stoploss:
    stoploss = -0.99 # value loaded from strategy
    use_custom_stoploss = False

    # Trailing stop:
    trailing_stop = True  # Enable trailing stop
    trailing_stop_positive = 0.99
    trailing_stop_positive_offset = 1.0
    trailing_only_offset_is_reached = True  # Ensure trailing stop only activates after offset is reached

    # Percentage below lowerband
    percent_below_lowerband = IntParameter(10, 20, default=16, space="buy")
    leverage_amount = IntParameter(1, 15, default=5, space="buy")
    rsi_down = IntParameter(15, 45, default=30, space="buy")
    rsi_up = IntParameter(65, 95, default=70, space="sell")

    # Bollinger Bands parameters
    bb_length = 10
    bb_stddev = 1.8

    plot_config = {
        'main_plot': {
            'bb_threshold': {'color': 'orange'},
            'bb_lowerband_4h': {'color': bb_color},
            'bb_upperband_4h': {
                'color': bb_color,
                'fill_to': 'bb_lowerband_4h',
                'fill_label': 'Bollinger Band 4h',
                'fill_color': bb_color,
            },
        },
        'subplots': {
            'RSI': {
                'rsi_mid': {'color': 'purple'},
                'rsi_up': {'color': 'green'},
                'rsi_down': {'color': 'red'},
                'rsi': {'color': 'blue'},
                'rsi_ema': {'color': 'grey'},
            },
        },
    }

    @property
    def protections(self):
        return [
            {
                "method": "StoplossGuard",
                "lookback_period_candles": 500,
                "trade_limit": 1,
                "stop_duration_candles": 1000,
                "only_per_pair": True,
                "only_per_side": False
            }
        ]

    def informative_pairs(self):
        pairs = self.dp.current_whitelist()
        informative_pairs = [(pair, self.informative_timeframe) for pair in pairs]
        return informative_pairs

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Populate indicators for Bollinger Bands strategy
        """
        if not self.dp:
            # Don't do anything if DataProvider is not available.
            return dataframe

        informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=self.informative_timeframe)

        bollinger_4h = ta.BBANDS(
            informative,
            timeperiod=self.bb_length,
            nbdevup=self.bb_stddev,
            nbdevdn=self.bb_stddev
        )

        # Add Bollinger Bands to the 4h informative dataframe
        informative['bb_lowerband'] = bollinger_4h['lowerband']
        informative['bb_middleband'] = bollinger_4h['middleband']
        informative['bb_upperband'] = bollinger_4h['upperband']

        for val in self.percent_below_lowerband.range:
            informative[f'bb_threshold_{val}'] = bollinger_4h['lowerband'].mul(
                (100 - self.percent_below_lowerband.value) / 100)

        dataframe = merge_informative_pair(dataframe, informative, self.timeframe, self.informative_timeframe,
                                           ffill=True)

        dataframe['dema'] = ta.DEMA(dataframe, timeperiod=3, price='low')

        dataframe['bb_threshold'] = dataframe[f'bb_threshold_{self.percent_below_lowerband.value}_4h']

        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['rsi_mid'] = 50
        dataframe['rsi_up'] = self.rsi_up.value
        dataframe['rsi_down'] = self.rsi_down.value
        dataframe['rsi_ema'] = ta.EMA(dataframe['rsi'], timeperiod=3)

            # Calculate MACD
        macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9)
        dataframe['macd'] = macd['macd']
        dataframe['macdsignal'] = macd['macdsignal']
        dataframe['macdhist'] = macd['macdhist']

        dataframe['dema_cross_above_bb_threshold'] = qtpylib.crossed_above(dataframe['dema'], dataframe['bb_threshold'])
        dataframe['rsi_ema_cross_above_down'] = qtpylib.crossed_above(dataframe['rsi_ema'], dataframe['rsi_down'])
        dataframe['rsi_ema_cross_above_up'] = qtpylib.crossed_above(dataframe['rsi_ema'], dataframe['rsi_up'])
        dataframe['rsi_cross_above_down'] = qtpylib.crossed_above(dataframe['rsi'], dataframe['rsi_down'])
        dataframe['rsi_cross_below_down'] = qtpylib.crossed_below(dataframe['rsi'], dataframe['rsi_down'])
        dataframe['rsi_cross_above_up'] = qtpylib.crossed_above(dataframe['rsi'], dataframe['rsi_up'])
        # dataframe['rsi_cross_below_up'] = qtpylib.crossed_below(dataframe['rsi'], dataframe['rsi_up'])

        return dataframe


    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Define entry conditions for long positions
        """
        dataframe.loc[
            (dataframe['rsi_cross_above_down'] == True) &
            (dataframe['dema'] < dataframe['bb_threshold']),
            'enter_long'] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Define exit conditions for long positions
        """
        dataframe.loc[
            dataframe['rsi_cross_above_up'] == True,
            'exit_long'] = 1

        return dataframe

    def leverage(
        self,
        pair: str,
        current_time: datetime,
        current_rate: float,
        proposed_leverage: float,
        max_leverage: float,
        entry_tag: Optional[str],
        side: str,
        **kwargs,
    ) -> float:
        """
        Calculate leverage based on the provided settings
        """
        return self.leverage_amount.value