Bollinger Bands

Introduction

Bollinger Bands are a popular technical analysis tool created by John Bollinger in the 1980s. They consist of a middle band (the moving average) and two outer bands (standard deviations above and below the moving average). Bollinger Bands are used to measure market volatility and help identify overbought and oversold conditions.

Calculation of Bollinger Bands

Bollinger Bands consist of three lines:

  1. Middle Band: This is typically a simple moving average (SMA) of the closing prices over a specified period.
  2. Upper Band: This band is calculated by adding a specified number of standard deviations (usually 2) to the middle band.
  3. Lower Band: This band is calculated by subtracting the same number of standard deviations from the middle band.

Formulas

  1. Middle Band (SMA):
mathematical expression or equation
  1. Upper Band:
mathematical expression or equation
  1. Lower Band:
mathematical expression or equation

Where:

  • ( n ) is the number of periods (e.g., 20 days).
  • ( k ) is the number of standard deviations (typically 2).

Example Calculation

Let’s consider a simple example using hypothetical closing prices for a stock over 5 days:

DayClosing Price
1$20
2$22
3$24
4$23
5$25
  1. Calculate the 5-Day SMA:
mathematical expression or equation
  1. Calculate the Standard Deviation:

    • First, calculate the variance.
    • Differences from the mean:
      • (20 - 22.8)² = 7.84
      • (22 - 22.8)² = 0.64
      • (24 - 22.8)² = 1.44
      • (23 - 22.8)² = 0.04
      • (25 - 22.8)² = 4.84
    • Sum of squares: ( 7.84 + 0.64 + 1.44 + 0.04 + 4.84 = 14.80 )
    • Variance: ( \frac{14.80}{5} = 2.96 )
    • Standard Deviation: ( \sqrt{2.96} \approx 1.72 )
  2. Calculate Upper and Lower Bands:

    • Upper Band:
    mathematical expression or equation
    • Lower Band:
    mathematical expression or equation

Final Bands

  • Middle Band: 22.8
  • Upper Band: 26.24
  • Lower Band: 19.36

Python Code

import pandas as pd

def calculate_bollinger_bands(data, window=20, num_of_std=2):
    """Calculate the Bollinger Bands for a given DataFrame."""
    # Calculate the Simple Moving Average (SMA)
    data['SMA'] = data['Close'].rolling(window=window).mean()
    
    # Calculate the Standard Deviation (SD)
    data['STD'] = data['Close'].rolling(window=window).std()

    # Calculate Upper and Lower Bollinger Bands
    data['Upper Band'] = data['SMA'] + (data['STD'] * num_of_std)
    data['Lower Band'] = data['SMA'] - (data['STD'] * num_of_std)

    return data

Interpretation of Bollinger Bands

Volatility

  • Narrow Bands: Indicate low volatility and potential price consolidation.
  • Widening Bands: Indicate high volatility and increased price movement.

Overbought and Oversold Conditions

  • Prices touching or exceeding the upper band may indicate overbought conditions (potential sell signal).
  • Prices touching or dropping below the lower band may indicate oversold conditions (potential buy signal).

Trend Confirmation

  • Prices consistently riding the upper band can indicate a strong uptrend.
  • Prices consistently riding the lower band can indicate a strong downtrend.

Analogy

Think of Bollinger Bands like a rubber band around prices. When prices are stable and the market is calm, the rubber band (the bands) is tight. When market activity increases and prices start to fluctuate significantly, the rubber band stretches, indicating higher volatility. Just as a rubber band may snap back to its original shape after being stretched, prices tend to revert to the middle band when they approach the upper or lower bands.

Conclusion

Bollinger Bands are a versatile tool that helps traders understand market volatility and identify potential buy or sell signals. By examining price movements relative to the bands, traders can make more informed decisions regarding their trades.


References