Ethereum RSI Calculation Question: Solution
The problem you experience is common when you work with Python and Numpy financial data. In this article, we go through what the error message tells us and offer an updated solution to calculate the relative strength index (RSI) using the simple moving average (SMA).
The problem: AttributeError: 'Numpy.float64' object has no attribute 'rolling'
‘
When importing the “Numpy” directory, it returns a series of numeric arrays as an object. In some cases, however
`Python
Import numpy as np
Create an array with a float64
ARR = np.float64 (10)
`
The error message indicates that numpy.float64
does not have a” rolling “attribute. The reason for this is that the “Rolling” method is actually part of the Pandas Library, which we will later import.
The solution: Pandas calculates RSI -T
To calculate RSI, use SMA and properly process the Pandas directory. Here’s an updated code detail showing RSI calculation:
`Python
Import pandas as PD
Import numpy as np
Function to calculate RSI with SMA
DEP calculation_rsi (data, short_window = 14, long_window = 26):
Calculate the simple moving average (SMA)
data [‘sm’] = data [‘closure’]. Rolling (window = short_window) .mean ()
Calculate the relative strength index (RSI)
Data [‘RSI’] = 100.0 – (100.0 / (1 + data [‘sma’]. PCT_change (). Dropna ()) ** Long_window)
return data
Load closing prices to Pandas Dataframe
df = pd.read_csv (‘stock_prices.csv’, index_col = ‘date’, parse_dates = [‘date’])
Calculate RSI with SMA
RSI_DF = Calculate_RSI (DF)
Print the first few lines of the calculated RSI
Print (RSI_DF.HEAD ())
`
Explanation
In this updated code, we determine the “Calculate_rsi" feature, which takes a Pandas Dataframe (
data) and two parameters for short and long moving averages (short_window
and long_window
). Then calculate SMA by “Rolling” and then calculate RSI by “PCT_change”. Finally, return the calculated RSI as a new column in Pandas Dataframe.
Example Use the case
If you use this feature with your own data, simply replace the ‘stock_prices.csv’`’ and ‘Date’ ‘positioners with the path and date range of the file you want. You can then use “Calculate_RSI” to calculate RSI for stock prices.
Hope this helps! Let me know if you have any further questions or questions.