crypto_signal / indicators /bollinger_bands.py
netflypsb's picture
Create indicators/bollinger_bands.py
5565a53 verified
import pandas as pd
def calculate_bollinger_bands(data, column='close', period=21, std_multiplier=1.7):
"""
Calculates Bollinger Bands for a specified column in the data DataFrame.
Args:
data (pd.DataFrame): The input DataFrame containing the price data.
column (str): The name of the column to calculate the Bollinger Bands on. Default is 'close'.
period (int): The period over which to calculate the SMA and standard deviation. Default is 21.
std_multiplier (float): The multiplier for the standard deviation to define the width of the bands. Default is 1.7.
Returns:
pd.DataFrame: The input DataFrame augmented with Bollinger Bands ('BB_Middle', 'BB_Upper', 'BB_Lower').
"""
# Calculate the SMA for the specified period
data['BB_Middle'] = data[column].rolling(window=period).mean()
# Calculate the standard deviation
std_dev = data[column].rolling(window=period).std()
# Calculate the upper and lower Bollinger Bands
data['BB_Upper'] = data['BB_Middle'] + (std_multiplier * std_dev)
data['BB_Lower'] = data['BB_Middle'] - (std_multiplier * std_dev)
return data
# Example usage:
# Assuming `data` is a pandas DataFrame with a 'close' column:
# data_with_bbands = calculate_bollinger_bands(data, column='close', period=21, std_multiplier=1.7)