-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBBWidthRatio.py
32 lines (25 loc) · 1.12 KB
/
BBWidthRatio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pandas as pd
import talib
def bollinger_band_width_ratio(df, period=20, deviations=2.0):
"""
Calculates the Bollinger Bands width ratio indicator.
Args:
df : pandas.DataFrame
A DataFrame containing 'open', 'high', 'low', 'close', and 'volume' columns.
period : int
The number of periods to use when calculating the Bollinger Bands.
deviations : float
The number of standard deviations to use when calculating the Bollinger Bands width.
Returns:
pandas.Series
A series containing the Bollinger Bands width ratio indicator values.
"""
# Calculate Bollinger Bands
upperband, middleband, lowerband = talib.BBANDS(
df['close'], timeperiod=period, nbdevup=deviations, nbdevdn=deviations, matype=0)
# Calculate the width of the bands as a ratio to the middle band
bb_width_ratio = ((upperband - lowerband) / middleband) * 100
return bb_width_ratio
# Example usage:
# Assuming 'data' is a pandas DataFrame with the required columns
# data['BBWidthRatio'] = bollinger_band_width_ratio(data, period=20, deviations=2.0)