0

My dataframe is of the form: ['SYMBOL', 'vDate', 'LTP']

I need to calculate a new column for RSI, using finta library. I am using the following code and getting Attribute error "'int' object has no attribute 'rename'"

import pandas as pd
from finta import TA

scrips = get_Data()

gp = scrips.groupby(['SYMBOL'])

scrips['RSI'] = gp['LTP'].apply(lambda s: TA.RSI(s, 14, "LTP") )
ewokx
  • 2,204
  • 3
  • 14
  • 27
Shudipto
  • 61
  • 1
  • 5

1 Answers1

0

You can use other python libraries as well which are widely used libraries such as pandas_ta , TA-lib.

df.tail()
            Symbol  Close
Date        
2022-06-24  INFY    1443.70
2022-06-27  INFY    1477.80
2022-06-28  INFY    1482.40
2022-06-29  INFY    1464.85
2022-06-30  INFY    1462.45

RSI using pandas_ta library-

df['rsi'] = df.ta.rsi(close=df['Close'], length=14, scalar=None, drift=None, offset=None)
df.tail()


           Symbol    Close   rsi
Date            
2022-06-24  INFY    1441.10 44.913298
2022-06-27  INFY    1474.60 50.446537
2022-06-28  INFY    1480.15 51.318957
2022-06-29  INFY    1463.25 48.517840
2022-06-30  INFY    1461.90 48.291087

Ref link- https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614

https://github.com/twopirllc/pandas-ta

If DataFrame contains multiple symbol like df_new contains "INFY","TCS","HDFCBANK" symbols,RSI can be calculated using for loop for single dataframe. Complete code below.

Code-

# symbol_lst = ["INFY","TCS","HDFCBANK"]
!pip install investpy
df = get_history(symbol="INFY",
                        start= date(2021,6,10),
                        end= date(2022,6,30),index=False,
                        futures=False
                        )
df1 = get_history(symbol="TCS",
                        start= date(2021,6,10),
                        end= date(2022,6,30),index=False,
                        futures=False
                        )
df2 = get_history(symbol="HDFCBANK",
                        start= date(2021,6,10),
                        end= date(2022,6,30),index=False,
                        futures=False
                        )
df_new = df.append([df1,df2]) #dataframe with multiple symbols
unique_symbol = df_new['Symbol'].unique()
unique_symbol.tolist()
new = [] #Created Empty lst
for item in unique_symbol:
    hi = df_new.loc[df_new['Symbol'] == item]
    hi["RSI"] = hi.ta.rsi(close=hi['Close'], length=14, scalar=None, drift=None, offset=None)
    new.append(hi)
df_rsi = pd.concat(new, axis=0)
df_rsi.tail()
Divyank
  • 811
  • 2
  • 10
  • 26
  • Hi , Thanks for your input, this is interesting. But how do I use this in a groupby scenario, with multiple Symbols in the dataframe. – Shudipto Aug 08 '22 at 12:56
  • updated my answer for `df_new` contains multiple `symbols` and calculate `RSI` for each symbol. – Divyank Aug 08 '22 at 14:23