Bollinger Band Mean-Reversion Entry with a Tight Stop
Summary
This strategy defines a long entry when the close reaches or falls below 98% of the lower Bollinger Band. The bands use a 20-period window and two standard deviations; the strategy is configured for a one-minute timeframe. It also calculates MACD values, although those values are not used in the displayed entry condition.
The code sets a 1.5% stop loss and includes a minimal-return schedule, but the exit signal itself is empty, so the document does not specify an indicator-based exit. Its comments describe a trailing-stop exit, yet that behavior is not implemented in the shown exit rule. No backtest results, market selection, transaction-cost assumptions, or rationale for the parameters are supplied, so profitability and robustness cannot be assessed from this document.
Key ideas
- The long entry triggers when price is at or below 98% of the lower Bollinger Band.
- The Bollinger Bands use a 20-period window and two standard deviations.
- The strategy is configured for a one-minute timeframe and sets a 1.5% stop loss.
- MACD is calculated but does not affect the shown entry condition.
- The displayed exit rule is empty despite a comment referring to a trailing stop.
Tags
Full text
# Low_BB.py
```py
# --- Do not remove these libs ---
from freqtrade.strategy import IStrategy
from typing import Dict, List
from functools import reduce
from pandas import DataFrame
# --------------------------------
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
from typing import Dict, List
from functools import reduce
from pandas import DataFrame, DatetimeIndex, merge
# --------------------------------
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
# import numpy as np # noqa
class Low_BB(IStrategy):
"""
author@: Thorsten
works on new objectify branch!
idea:
buy after crossing .98 * lower_bb and sell if trailing stop loss is hit
"""
INTERFACE_VERSION: int = 3
# Minimal ROI designed for the strategy.
# This attribute will be overridden if the config file contains "minimal_roi"
minimal_roi = {
"0": 0.9,
"1": 0.05,
"10": 0.04,
"15": 0.5
}
# Optimal stoploss designed for the strategy
# This attribute will be overridden if the config file contains "stoploss"
stoploss = -0.015
# Optimal timeframe for the strategy
timeframe = '1m'
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
##################################################################################
# buy and sell indicators
bollinger = qtpylib.bollinger_bands(
qtpylib.typical_price(dataframe), window=20, stds=2
)
dataframe['bb_lowerband'] = bollinger['lower']
dataframe['bb_middleband'] = bollinger['mid']
dataframe['bb_upperband'] = bollinger['upper']
macd = ta.MACD(dataframe)
dataframe['macd'] = macd['macd']
dataframe['macdsignal'] = macd['macdsignal']
dataframe['macdhist'] = macd['macdhist']
# dataframe['cci'] = ta.CCI(dataframe)
# dataframe['mfi'] = ta.MFI(dataframe)
# dataframe['rsi'] = ta.RSI(dataframe, timeperiod=7)
# dataframe['canbuy'] = np.nan
# dataframe['canbuy2'] = np.nan
# dataframe.loc[dataframe.close.rolling(49).min() <= 1.1 * dataframe.close, 'canbuy'] == 1
# dataframe.loc[dataframe.close.rolling(600).max() < 1.2 * dataframe.close, 'canbuy'] = 1
# dataframe.loc[dataframe.close.rolling(600).max() * 0.8 > dataframe.close, 'canbuy2'] = 1
##################################################################################
# required for graphing
bollinger = qtpylib.bollinger_bands(dataframe['close'], window=20, stds=2)
dataframe['bb_lowerband'] = bollinger['lower']
dataframe['bb_upperband'] = bollinger['upper']
dataframe['bb_middleband'] = bollinger['mid']
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the buy signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
"""
dataframe.loc[
(
(dataframe['close'] <= 0.98 * dataframe['bb_lowerband'])
)
,
'enter_long'] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the sell signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
"""
dataframe.loc[
(),
'exit_long'] = 1
return dataframe
```Shown in full with attribution under the source's licence. Licence: GPL-3.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.