Bollinger Bands
A case study of bollinger bands
In this chapter, we are going to test bollinger bands over a period of three years.
What Is a Bollinger Band?
Bollinger Bands were developed and copyrighted by famous technical trader John Bollinger, designed to discover opportunities that give investors a higher probability of properly identifying when an asset is oversold or overbought.
Key details
There are three lines that compose Bollinger Bands A simple moving average (i.e middle band) and an upper and lower band The upper and lower bands are typically 2 standard deviations +/- from a 20-day SMA (simple moving average)
How to use bollinger bands in trading
- If the price is near the upper Bollinger Band, it’s considered “expensive” because it is 2 standard deviation above the average (the 20-period moving average).
- If the is price near the lower Bollinger Band, it’s considered “cheap” because it’s 2 standard deviation below the average.
Let’s test the strategy by backtesting it using Quantplay
- Enter a trade at 9:20 AM (when the 9:15 5-minute candle ends)
- Exit the trade at 3:15 PM or whenever the stoploss triggers
- Trade INR 1,00,000 in the stock
- Sell the stock with 2% stoploss
Strategy code
from quantplay.strategy.base import QuantplayAlgorithm
from quantplay.utils.constant import TickInterval
from quantplay.service import market
import numpy as np
import pandas as pd
import ta
class BollingerBands(QuantplayAlgorithm):
def __init__(self):
self.interval = "5minute"
self.entry_time = "09:15"
self.exit_time = "15:15"
self.exchange_to_trade_on = "NSE"
self.stream_symbols_by_security_type = {"EQ": market.symbols(universe="FNO_STOCKS")}
self.strategy_type = "intraday"
self.strategy_tag = "bband"
super(BollingerBands, self).__init__()
def get_trades(self, market_data):
trades = market.get_trades(market_data, self.entry_time)
bbands = ta.volatility.BollingerBands(close=trades.close,
window=20,
window_dev=2)
trades["bb_high"] = bbands.bollinger_hband()
trades = trades[trades.close > trades.bb_high]
trades.loc[:, 'tradingsymbol'] = trades.symbol
trades.loc[:, "transaction_type"] = "SELL"
trades.loc[:, "stoploss"] = 0.02
trades.loc[:, "quantity"] = (100000/trades.close).astype(int)
return trades
strategy = BollingerBands()
strategy.validate()
res, trades = strategy.backtest()
balance progression image
profit by month image
We hope you enjoyed our article about bollinger bands. Using only the bands to trade is a risky strategy since the indicator focuses on price and volatility, while ignoring a lot of other relevant information.
In our upcoming blogs we are going to discuss the right market conditions to trade bands and improve the strategy results.
Follow us on your favorite social media to know when we post a better version of the strategy.