Relative Strength Index in python pandas

前端 未结 12 1415
生来不讨喜
生来不讨喜 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:02

    It is not really necessary to calculate the mean, because after they are divided, you only need to calculate the sum, so we can use Series.cumsum ...

    def rsi(serie, n):
    
        diff_serie = close.diff()
        cumsum_incr = diff_serie.where(lambda x: x.gt(0), 0).cumsum()
        cumsum_decr = diff_serie.where(lambda x: x.lt(0), 0).abs().cumsum()
        rs_serie = cumsum_incr.div(cumsum_decr)
        rsi = rs_serie.mul(100).div(rs_serie.add(1)).fillna(0)
    
        return rsi
    

提交回复
热议问题