💬 Forum

BollingerBandStrategy

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

Basics mode: spot timeframe: 3m
Settings stoploss: -0.1 has minimal roi trailing
Indicators Bollinger_Bands talib
Concepts 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
from pandas import DataFrame
from functools import reduce
from freqtrade.strategy import IStrategy
from freqtrade.exchange import timeframe_to_minutes

import logging

import talib.abstract as ta

class BollingerBandStrategy(IStrategy):

    timeframe = "3m"
    timeframe_mins = timeframe_to_minutes(timeframe)

    # ROI table:
    minimal_roi = {
        "0": 0.242,
        str(timeframe_mins * 3): 0.01,  # 2% after 3 candles
        str(timeframe_mins * 6): 0.00  # 1% After 6 candles
    }
    # Stoploss:
    stoploss = -0.1

    # Trailing stop:
    trailing_stop = True
    trailing_stop_positive = 0.01
    trailing_stop_positive_offset = 0.05
    trailing_only_offset_is_reached = False


    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        logging.info(dataframe['date'])
        upperband, middleband, lowerband = ta.BBANDS(
            dataframe['close'],
            timeperiod=20
            )
        dataframe['upperband'] = upperband
        dataframe['middleband'] = middleband
        dataframe['lowerband'] = lowerband

        dataframe['iii'] = self.intraday_intensity_index(dataframe)
        dataframe['money_flow'] = dataframe['iii'].rolling(window=21).sum() / dataframe['close'].rolling(window=21).sum()
        return dataframe


    def intraday_intensity_index(self, dataframe):
        close = dataframe['close']
        high = dataframe['high']
        low = dataframe['low']
        volume = dataframe['volume']

        return ( (close * 2) - high - low ) / ( (high - low) ) * volume

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        conditions_long = []
        conditions_short = []

        conditions_long.append(
            dataframe['close'] < dataframe['lowerband']
        )
        conditions_long.append(
            (dataframe['volume'] > 0)
        )
        conditions_long.append(
            (dataframe['money_flow'] > 0)
        )
        conditions_long.append(
            (dataframe['iii'] > 0)
        )

        conditions_short.append(
                dataframe['close'] > dataframe['upperband']
            )
        conditions_short.append(
            (dataframe['money_flow'] < 0)
        )
        conditions_short.append(
            (dataframe['volume'] > 0)
        )
        conditions_short.append(
            (dataframe['iii'] < 0)
        )

        dataframe.loc[
            (
                reduce(lambda x, y: x & y, conditions_long)
            ),
            'enter_long'] = 1

        dataframe.loc[
            (
                reduce(lambda x, y: x & y, conditions_short)
            ),
            'enter_short'] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        return super().populate_exit_trend(dataframe, metadata)