'Ta-lib : Indicators work with stock but not with cryptocurrency
I have a problem when using the Ta-lib library. I'm trying to find the RSI and the Bollinger Bands of BTC-USD. The problem is that when I try to find them for any cryptocurrencies, it gives no value (NaN). But when I try to find them for any stocks it works. When I display the data (Open Close High Low Volume) it works all the time (for cryptocurrencies and stocks). It gives me correctly : picture_0
But when I calculate the RSI and the BBANDS for cryptocurrencies it doesn't work.
Here is my code :
def get_indicators(data):
# Get RSI
data["rsi"] = talib.RSI(data["Close"], timeperiod=7)
data["upper"], data["middle"], data["lower"] = talib.BBANDS(data["Close"], timeperiod=10, nbdevup=2, nbdevdn=2)
# pprint(upper)
# pprint(middle)
# pprint(lower)
return data
df2 = get_indicators(df)
df2
When I run that, it gives me that, when my df is the data frame of a stock: picture_1
When I run that, when my df is the data frame of cryptocurrency, it gives me : picture_2
In the beginning, I thought that the problem was with the indicator, so I tried to replace RSI with ROC to see if it changes something.
So, I replace data["rsi"] = talib.ROC(data["Close"], timeperiod=7) with data["roc"] = talib.ROC(data["Close"], timeperiod=7) and this time it calcultated the ROC correctly for both stocks and cryptocurrencies. It gave me:
I don't understand where the problem is, it is possible to calculate RSI and Bollinger Bands for stocks but not for cryptocurrencies. Can someone please help me?
Solution 1:[1]
The error is not with you but with the way that TAlib calculates the ROC. If you specify a time period of 10 then the first 10 results cannot be calculated because it doesn't have a value for 10 iterations back to compare. So it just fills them as NaN. Try changing the time period to test it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | buddemat |
