💬 Forum

BollingerBounceStrategy

uploads/BollingerBounceStrategy.py · uploaded by 🐋 Ron · first seen 2026-07-18

Basics mode: spot timeframe: 1h interface version: 3
Settings stoploss: -0.06 has minimal roi trailing process only new candles startup candle count: 50 hyperopt hyperopt params: 4
Indicators Bollinger_Bands RSI talib technical
Concepts mean_reversion 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
"""
Bollinger Bands Bounce Strategy

Buy when price touches/crosses below lower Bollinger Band then bounces back.
Sell when price touches/crosses above upper Bollinger Band.

Uses RSI as confirmation to avoid catching falling knives.

Timeframe: 1h
Pairs: BTC/USDT, ETH/USDT, SOL/USDT, BNB/USDT
"""
import numpy as np
import pandas as pd
from datetime import datetime, timedelta, timezone
from pandas import DataFrame
from typing import Optional, Union

from freqtrade.strategy import (
    IStrategy,
    Trade,
    Order,
    PairLocks,
    BooleanParameter,
    CategoricalParameter,
    DecimalParameter,
    IntParameter,
    RealParameter,
)

import talib.abstract as ta
from technical import qtpylib


class BollingerBounceStrategy(IStrategy):
    """
    Bollinger Bands Bounce strategy.
    Buy: Price closes below lower band and RSI confirms oversold.
    Sell: Price closes above upper band or RSI confirms overbought.
    """

    INTERFACE_VERSION = 3
    can_short: bool = False

    minimal_roi = {
        "0": 0.05,
        "60": 0.03,
        "120": 0.015,
        "240": 0.0,
    }

    stoploss = -0.06  # 6% stop loss

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

    timeframe = "1h"
    process_only_new_candles = True
    use_exit_signal = True
    exit_profit_only = False
    ignore_roi_if_entry_signal = False

    # Hyperoptable parameters
    buy_bb_window = IntParameter(15, 30, default=20, space="buy", optimize=True)
    buy_bb_stds = DecimalParameter(1.5, 3.0, default=2.0, decimals=1, space="buy", optimize=True)
    buy_rsi_limit = IntParameter(20, 45, default=35, space="buy", optimize=True)
    sell_rsi_limit = IntParameter(60, 85, default=75, space="sell", optimize=True)

    startup_candle_count: int = 50

    order_types = {
        "entry": "limit",
        "exit": "limit",
        "stoploss": "market",
        "stoploss_on_exchange": False,
    }

    order_time_in_force = {"entry": "GTC", "exit": "GTC"}

    plot_config = {
        "main_plot": {
            "bb_lowerband": {"color": "green"},
            "bb_middleband": {"color": "orange"},
            "bb_upperband": {"color": "red"},
        },
        "subplots": {
            "RSI": {
                "rsi": {"color": "blue"},
            },
        },
    }

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """Calculate Bollinger Bands and RSI."""
        # Bollinger Bands with multiple windows for hyperopt
        for window in range(15, 31):
            for std in [1.5, 2.0, 2.5, 3.0]:
                suffix = f"_{window}_{str(std).replace('.', '')}"
                bollinger = qtpylib.bollinger_bands(
                    qtpylib.typical_price(dataframe), window=window, stds=std
                )
                dataframe[f"bb_lowerband{suffix}"] = bollinger["lower"]
                dataframe[f"bb_middleband{suffix}"] = bollinger["mid"]
                dataframe[f"bb_upperband{suffix}"] = bollinger["upper"]

        # RSI for confirmation
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)

        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """Buy when price is below lower Bollinger Band and RSI confirms oversold."""
        std_str = str(self.buy_bb_stds.value).replace(".", "")
        suffix = f"_{self.buy_bb_window.value}_{std_str}"

        dataframe.loc[
            (
                (dataframe["close"] < dataframe[f"bb_lowerband{suffix}"])
                & (dataframe["rsi"] < self.buy_rsi_limit.value)
                & (dataframe["volume"] > 0)
            ),
            "enter_long",
        ] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """Sell when price is above upper Bollinger Band or RSI overbought."""
        std_str = str(self.buy_bb_stds.value).replace(".", "")
        suffix = f"_{self.buy_bb_window.value}_{std_str}"

        dataframe.loc[
            (
                (
                    (dataframe["close"] > dataframe[f"bb_upperband{suffix}"])
                    | (dataframe["rsi"] > self.sell_rsi_limit.value)
                )
                & (dataframe["volume"] > 0)
            ),
            "exit_long",
        ] = 1

        return dataframe