0

I try to calculate the BBP ( Bollinger bands percent ) in python by this code. Howevery, my BBP function returns inf or -inf for bbp. Confusingly when I use some coin close price like ETH this function return the correct bbp number (not inf).

This is my python code:

import requests
import json 
import pandas as pd
import numpy as np
from talib import RSI, BBANDS

def BBP(price, close):
    up, mid, low = BBANDS(close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
    bbp = (price['close'] - low) / (up - low)
    print(up[-1])
    print(mid[-1])
    print(low[-1])
    print(bbp.iloc[-1])
    return bbp

r = requests.get('https://min-api.cryptocompare.com/data/histohour?fsym=SALT&tsym=BTC&limit=900&s=Binance&aggregate=5')
j = r.json()

price = pd.DataFrame(j['Data'])
price = price.sort_values(by='time', ascending=False)
price = price.iloc[::-1]
price = price.dropna()
close = price['close'].values

up, mid, low = BBANDS(close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)

rsi = RSI(close, timeperiod=14)
bbp = BBP(price, close)

price.insert(loc=0, column='RSI',value=rsi)
price.insert(loc=0, column='BBP',value=bbp)

print(price.head(30))

If I use ETH instead of SALT in the request API that code works correctly but in other small prices coins the BBP function returns inf for the BBP column in the price data frame.

This is a sample of the return value for SALT:

    BBP        RSI     close      high       low      open        time  \
0   NaN        NaN  0.000069  0.000071  0.000068  0.000068  1534626000   
1   NaN        NaN  0.000070  0.000070  0.000068  0.000069  1534644000   
2   NaN        NaN  0.000072  0.000072  0.000068  0.000070  1534662000   
3   NaN        NaN  0.000073  0.000073  0.000071  0.000072  1534680000   
4   NaN        NaN  0.000074  0.000074  0.000072  0.000073  1534698000   
5   NaN        NaN  0.000073  0.000074  0.000072  0.000074  1534716000   
6   NaN        NaN  0.000073  0.000074  0.000072  0.000073  1534734000   
7   NaN        NaN  0.000071  0.000073  0.000071  0.000073  1534752000   
8   NaN        NaN  0.000072  0.000074  0.000070  0.000071  1534770000   
9   NaN        NaN  0.000069  0.000072  0.000069  0.000072  1534788000   
10  NaN        NaN  0.000070  0.000071  0.000068  0.000069  1534806000   
11  NaN        NaN  0.000072  0.000072  0.000069  0.000070  1534824000   
12  NaN        NaN  0.000070  0.000072  0.000070  0.000072  1534842000   
13  NaN        NaN  0.000070  0.000070  0.000069  0.000070  1534860000   
14  NaN  56.138260  0.000071  0.000072  0.000069  0.000070  1534878000   
15  NaN  53.757682  0.000071  0.000073  0.000071  0.000071  1534896000   
16  NaN  56.547317  0.000072  0.000072  0.000070  0.000071  1534914000   
17  NaN  52.340624  0.000070  0.000072  0.000070  0.000072  1534932000   
18  NaN  42.426811  0.000067  0.000071  0.000067  0.000070  1534950000   
19 -inf  41.721667  0.000067  0.000067  0.000065  0.000067  1534968000   
20 -inf  41.087686  0.000066  0.000067  0.000066  0.000067  1534986000   
21 -inf  42.663976  0.000067  0.000067  0.000066  0.000066  1535004000   
22 -inf  46.241512  0.000068  0.000068  0.000066  0.000067  1535022000   
23 -inf  47.300220  0.000068  0.000069  0.000067  0.000068  1535040000   
24 -inf  47.984947  0.000068  0.000069  0.000067  0.000068  1535058000   
25 -inf  47.984947  0.000068  0.000069  0.000067  0.000068  1535076000   
26 -inf  50.590822  0.000069  0.000069  0.000068  0.000068  1535094000   
27  inf  56.805348  0.000071  0.000071  0.000068  0.000069  1535112000   
28  inf  57.658800  0.000071  0.000072  0.000069  0.000071  1535130000   
29  inf  63.418810  0.000073  0.000073  0.000070  0.000071  1535148000 

How can I fix this?

Thanks.

Arman Feyzi
  • 788
  • 2
  • 11
  • 28

1 Answers1

1

This is how I use BBANDS:

upperband, middleband, lowerband = talib.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=talib.MA_Type.T3)

I multiply close (and other prices) by 1000000 since I then rescale them. Maybe it is because the closing prices are too low.

iso_9001_
  • 2,655
  • 6
  • 31
  • 47