Skip to content

Commit

Permalink
normalized bollinger bands width implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
aticio committed Aug 17, 2023
1 parent b500ff0 commit 92a3dfc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
48 changes: 48 additions & 0 deletions legitindicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,3 +1125,51 @@ def momentum_normalized(data, length):
cube_anm = cube_transform(agc_norm_mom)

return cube_anm


def bollinger_bands_width_normalized(data, length, stdd):
"""Normalized Bollinger Bands Width indicator
:param data: list of price data
:type data: list
:param length: lookback period
:type length: int
:param stdd: standart deviation multiplier
:type stdd: float
:return: Normalized width of bollinger bands
:rtype: list
"""
dev = []
upper = []
lower = []
bbw = []
bbwn = []
basis = sma(data, length)

for i, _ in enumerate(data):
if i < length:
dev.append(0)
upper.append(0)
lower.append(0)
bbw.append(0)
else:
tmp = data[i - length:i]
dev.append(stdd * statistics.stdev(tmp))
upper.append(basis[i] + dev[i])
lower.append(basis[i] - dev[i])
bbw.append(((basis[i] + dev[i]) - (basis[i] - dev[i]))/basis[i])

for i, _ in enumerate(bbw):
if i < length:
bbwn.append(0)
else:
max_val = max(bbw[i - length:i + 1])
min_val = min(bbw[i - length:i + 1])

if max_val == 0 and min_val == 0:
bbwn.append(0)
else:
bbwn.append(round((bbw[i] - min_val) / (max_val - min_val), 3))

return bbwn

2 changes: 1 addition & 1 deletion tests/test_bollingerbandspb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

BINANCE_URL = "https://api.binance.com/api/v3/klines"
SYMBOL = "BTCUSDT"
INTERVAL = "1h"
INTERVAL = "1d"
PARAMS = {"symbol": SYMBOL, "interval": INTERVAL}


Expand Down
16 changes: 16 additions & 0 deletions tests/test_bollingerbandswidthnormalized.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests
from legitindicators import bollinger_bands_width_normalized

BINANCE_URL = "https://api.binance.com/api/v3/klines"
SYMBOL = "BTCUSDT"
INTERVAL = "1d"
PARAMS = {"symbol": SYMBOL, "interval": INTERVAL}


def test_bollinger_bands_width_normalized():
response = requests.get(url=BINANCE_URL, params=PARAMS)
data = response.json()
close = [float(d[4]) for d in data]
bbwn = bollinger_bands_width_normalized(close, 20, 2)
print(bbwn)
assert len(bbwn) == len(close)

0 comments on commit 92a3dfc

Please sign in to comment.