netflypsb commited on
Commit
5565a53
·
verified ·
1 Parent(s): 517c0ba

Create indicators/bollinger_bands.py

Browse files
Files changed (1) hide show
  1. indicators/bollinger_bands.py +30 -0
indicators/bollinger_bands.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+ def calculate_bollinger_bands(data, column='close', period=21, std_multiplier=1.7):
4
+ """
5
+ Calculates Bollinger Bands for a specified column in the data DataFrame.
6
+
7
+ Args:
8
+ data (pd.DataFrame): The input DataFrame containing the price data.
9
+ column (str): The name of the column to calculate the Bollinger Bands on. Default is 'close'.
10
+ period (int): The period over which to calculate the SMA and standard deviation. Default is 21.
11
+ std_multiplier (float): The multiplier for the standard deviation to define the width of the bands. Default is 1.7.
12
+
13
+ Returns:
14
+ pd.DataFrame: The input DataFrame augmented with Bollinger Bands ('BB_Middle', 'BB_Upper', 'BB_Lower').
15
+ """
16
+ # Calculate the SMA for the specified period
17
+ data['BB_Middle'] = data[column].rolling(window=period).mean()
18
+
19
+ # Calculate the standard deviation
20
+ std_dev = data[column].rolling(window=period).std()
21
+
22
+ # Calculate the upper and lower Bollinger Bands
23
+ data['BB_Upper'] = data['BB_Middle'] + (std_multiplier * std_dev)
24
+ data['BB_Lower'] = data['BB_Middle'] - (std_multiplier * std_dev)
25
+
26
+ return data
27
+
28
+ # Example usage:
29
+ # Assuming `data` is a pandas DataFrame with a 'close' column:
30
+ # data_with_bbands = calculate_bollinger_bands(data, column='close', period=21, std_multiplier=1.7)