Relative Strength Index in python pandas

前端 未结 12 1482
生来不讨喜
生来不讨喜 2020-12-07 17:29

I am new to pandas. What is the best way to calculate the relative strength part in the RSI indicator in pandas? So far I got the following:

from pylab impor         


        
12条回答
  •  孤街浪徒
    2020-12-07 18:05

    # Relative Strength Index
    # Avg(PriceUp)/(Avg(PriceUP)+Avg(PriceDown)*100
    # Where: PriceUp(t)=1*(Price(t)-Price(t-1)){Price(t)- Price(t-1)>0};
    #        PriceDown(t)=-1*(Price(t)-Price(t-1)){Price(t)- Price(t-1)<0};
    # Change the formula for your own requirement
    def rsi(values):
        up = values[values>0].mean()
        down = -1*values[values<0].mean()
        return 100 * up / (up + down)
    
    stock['RSI_6D'] = stock['Momentum_1D'].rolling(center=False,window=6).apply(rsi)
    stock['RSI_12D'] = stock['Momentum_1D'].rolling(center=False,window=12).apply(rsi)
    

    Momentum_1D = Pt - P(t-1) where P is closing price and t is date

提交回复
热议问题