Skip to content

Commit

Permalink
Fixing bug: normalization of width bollinger band and adding percenta…
Browse files Browse the repository at this point in the history
…ge bollinger band. Based on this issue: #121
  • Loading branch information
bukosabino committed Mar 11, 2020
1 parent ac3f5f8 commit 36af41d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 46 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -132,8 +132,11 @@ df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator()
# Add Bollinger Band low indicator
df['bb_bbli'] = indicator_bb.bollinger_lband_indicator()

# Add width size Bollinger Bands
# Add Width Size Bollinger Bands
df['bb_bbw'] = indicator_bb.bollinger_wband()

# Add Percentage Bollinger Bands
df['bb_bbp'] = indicator_bb.bollinger_pband()
```


Expand Down
86 changes: 43 additions & 43 deletions ta/tests/data/cs-bbands.csv
@@ -1,43 +1,43 @@
Date,Close,MiddleBand,20-day Standard Deviation,HighBand,LowBand,WidthBand,CrossUp,CrossDown
1-May-09,86.155700,,,,,,,
4-May-09,89.086700,,,,,,,
5-May-09,88.782900,,,,,,,
6-May-09,90.322800,,,,,,,
7-May-09,89.067100,,,,,,,
8-May-09,91.145300,,,,,,,
11-May-09,89.439700,,,,,,,
12-May-09,89.175000,,,,,,,
13-May-09,86.930200,,,,,,,
14-May-09,87.675200,,,,,,,
15-May-09,86.959600,,,,,,,
18-May-09,89.429900,,,,,,,
19-May-09,89.322100,,,,,,,
20-May-09,88.724100,,,,,,,
21-May-09,87.449700,,,,,,,
22-May-09,87.263400,,,,,,,
26-May-09,89.498500,,,,,,,
27-May-09,87.900600,,,,,,,
28-May-09,89.126000,,,,,,,
29-May-09,90.704300,88.707940,1.291961,91.291862,86.124018,5.167845,0,0
1-Jun-09,92.900100,89.045160,1.452054,91.949268,86.141052,5.808215,1,0
2-Jun-09,92.978400,89.239745,1.686425,92.612595,85.866895,6.745700,1,0
3-Jun-09,91.802100,89.390705,1.771747,92.934200,85.847210,7.086989,0,0
4-Jun-09,92.664700,89.507800,1.902075,93.311950,85.703650,7.608300,0,0
5-Jun-09,92.684300,89.688660,2.019895,93.728450,85.648870,8.079580,0,0
8-Jun-09,92.302100,89.746500,2.076546,93.899592,85.593408,8.306184,0,0
9-Jun-09,92.772500,89.913140,2.176557,94.266255,85.560025,8.706230,0,0
10-Jun-09,92.537300,90.081255,2.241921,94.565096,85.597414,8.967682,0,0
11-Jun-09,92.949000,90.382195,2.202359,94.786912,85.977478,8.809435,0,0
12-Jun-09,93.203900,90.658630,2.192185,95.043001,86.274259,8.768742,0,0
15-Jun-09,91.066900,90.863995,2.021805,94.907605,86.820385,8.087220,0,0
16-Jun-09,89.831800,90.884090,2.009411,94.902912,86.865268,8.037643,0,0
17-Jun-09,89.743500,90.905160,1.995080,94.895320,86.915000,7.980320,0,0
18-Jun-09,90.399400,90.988925,1.936044,94.861013,87.116837,7.744176,0,0
19-Jun-09,90.738700,91.153375,1.760127,94.673629,87.633121,7.040508,0,0
22-Jun-09,88.017700,91.191090,1.682751,94.556593,87.825587,6.731006,0,0
23-Jun-09,88.086700,91.120500,1.779126,94.678752,87.562248,7.116503,0,0
24-Jun-09,86.843900,91.067665,1.886418,94.840502,87.294828,7.545674,0,1
25-Jun-09,90.778100,91.150270,1.835059,94.820387,87.480153,7.340234,0,0
26-Jun-09,90.541600,91.142135,1.837377,94.816889,87.467381,7.349507,0,0
29-Jun-09,91.389400,91.066600,1.794097,94.654793,87.478407,7.176387,0,0
30-Jun-09,90.650000,90.950180,1.741022,94.432224,87.468136,6.964089,0,0
Date,Close,MiddleBand,20-day Standard Deviation,HighBand,LowBand,WidthBand,CrossUp,CrossDown,PercentageBand
1-May-09,86.155700,,,,,,,,
4-May-09,89.086700,,,,,,,,
5-May-09,88.782900,,,,,,,,
6-May-09,90.322800,,,,,,,,
7-May-09,89.067100,,,,,,,,
8-May-09,91.145300,,,,,,,,
11-May-09,89.439700,,,,,,,,
12-May-09,89.175000,,,,,,,,
13-May-09,86.930200,,,,,,,,
14-May-09,87.675200,,,,,,,,
15-May-09,86.959600,,,,,,,,
18-May-09,89.429900,,,,,,,,
19-May-09,89.322100,,,,,,,,
20-May-09,88.724100,,,,,,,,
21-May-09,87.449700,,,,,,,,
22-May-09,87.263400,,,,,,,,
26-May-09,89.498500,,,,,,,,
27-May-09,87.900600,,,,,,,,
28-May-09,89.126000,,,,,,,,
29-May-09,90.704300,88.707940,1.291961,91.291862,86.124018,5.825685,0,0,0.886304
1-Jun-09,92.900100,89.045160,1.452054,91.949268,86.141052,6.522775,1,0,1.163705
2-Jun-09,92.978400,89.239745,1.686425,92.612595,85.866895,7.559076,1,0,1.054228
3-Jun-09,91.802100,89.390705,1.771747,92.934200,85.847210,7.928105,0,0,0.840257
4-Jun-09,92.664700,89.507800,1.902075,93.311950,85.703650,8.500153,0,0,0.914928
5-Jun-09,92.684300,89.688660,2.019895,93.728450,85.648870,9.008474,0,0,0.870767
8-Jun-09,92.302100,89.746500,2.076546,93.899592,85.593408,9.255162,0,0,0.807674
9-Jun-09,92.772500,89.913140,2.176557,94.266255,85.560025,9.682934,0,0,0.828427
10-Jun-09,92.537300,90.081255,2.241921,94.565096,85.597414,9.955104,0,0,0.773877
11-Jun-09,92.949000,90.382195,2.202359,94.786912,85.977478,9.746870,0,0,0.791370
12-Jun-09,93.203900,90.658630,2.192185,95.043001,86.274259,9.672264,0,0,0.790266
15-Jun-09,91.066900,90.863995,2.021805,94.907605,86.820385,8.900357,0,0,0.525090
16-Jun-09,89.831800,90.884090,2.009411,94.902912,86.865268,8.843839,0,0,0.369080
17-Jun-09,89.743500,90.905160,1.995080,94.895320,86.915000,8.778732,0,0,0.354434
18-Jun-09,90.399400,90.988925,1.936044,94.861013,87.116837,8.511119,0,0,0.423875
19-Jun-09,90.738700,91.153375,1.760127,94.673629,87.633121,7.723804,0,0,0.441102
22-Jun-09,88.017700,91.191090,1.682751,94.556593,87.825587,7.381210,0,0,0.028541
23-Jun-09,88.086700,91.120500,1.779126,94.678752,87.562248,7.809991,0,0,0.073695
24-Jun-09,86.843900,91.067665,1.886418,94.840502,87.294828,8.285788,0,1,-0.059760
25-Jun-09,90.778100,91.150270,1.835059,94.820387,87.480153,8.052894,0,0,0.449297
26-Jun-09,90.541600,91.142135,1.837377,94.816889,87.467381,8.063786,0,0,0.418289
29-Jun-09,91.389400,91.066600,1.794097,94.654793,87.478407,7.880372,0,0,0.544981
30-Jun-09,90.650000,90.950180,1.741022,94.432224,87.468136,7.657037,0,0,0.456896
5 changes: 5 additions & 0 deletions ta/tests/volatility.py
Expand Up @@ -61,6 +61,11 @@ def test_wband(self):
result = self._indicator.bollinger_wband()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_pband(self):
target = 'PercentageBand'
result = self._indicator.bollinger_pband()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)

def test_hband_indicator(self):
target = 'CrossUp'
result = self._indicator.bollinger_hband_indicator()
Expand Down
18 changes: 16 additions & 2 deletions ta/volatility.py
Expand Up @@ -108,15 +108,29 @@ def bollinger_lband(self) -> pd.Series:
return pd.Series(lband, name='lband')

def bollinger_wband(self) -> pd.Series:
"""Bollinger Channel Width Band
"""Bollinger Channel Band Width
From: https://school.stockcharts.com/doku.php?id=technical_indicators:bollinger_band_width
Returns:
pandas.Series: New feature generated.
"""
wband = self._hband - self._lband
wband = ((self._hband - self._lband) / self._mavg) * 100
wband = self._check_fillna(wband, value=0)
return pd.Series(wband, name='bbiwband')

def bollinger_pband(self) -> pd.Series:
"""Bollinger Channel Percentage Band
From: https://school.stockcharts.com/doku.php?id=technical_indicators:bollinger_band_perce
Returns:
pandas.Series: New feature generated.
"""
pband = (self._close - self._lband) / (self._hband - self._lband)
pband = self._check_fillna(pband, value=0)
return pd.Series(pband, name='bbipband')

def bollinger_hband_indicator(self) -> pd.Series:
"""Bollinger Channel Indicator Crossing High Band (binary).
Expand Down
1 change: 1 addition & 0 deletions ta/wrapper.py
Expand Up @@ -100,6 +100,7 @@ def add_volatility_ta(df: pd.DataFrame, high: str, low: str, close: str,
df[f'{colprefix}volatility_bbh'] = indicator_bb.bollinger_hband()
df[f'{colprefix}volatility_bbl'] = indicator_bb.bollinger_lband()
df[f'{colprefix}volatility_bbw'] = indicator_bb.bollinger_wband()
df[f'{colprefix}volatility_bbp'] = indicator_bb.bollinger_pband()
df[f'{colprefix}volatility_bbhi'] = indicator_bb.bollinger_hband_indicator()
df[f'{colprefix}volatility_bbli'] = indicator_bb.bollinger_lband_indicator()

Expand Down

0 comments on commit 36af41d

Please sign in to comment.