Relative Strength Index in python pandas

前端 未结 12 1452
生来不讨喜
生来不讨喜 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 17:45

    dUp= delta[delta > 0]
    dDown= delta[delta < 0]
    

    also you need something like:

    RolUp = RolUp.reindex_like(delta, method='ffill')
    RolDown = RolDown.reindex_like(delta, method='ffill')
    

    otherwise RS = RolUp / RolDown will not do what you desire

    Edit: seems this is a more accurate way of RS calculation:

    # dUp= delta[delta > 0]
    # dDown= delta[delta < 0]
    
    # dUp = dUp.reindex_like(delta, fill_value=0)
    # dDown = dDown.reindex_like(delta, fill_value=0)
    
    dUp, dDown = delta.copy(), delta.copy()
    dUp[dUp < 0] = 0
    dDown[dDown > 0] = 0
    
    RolUp = pd.rolling_mean(dUp, n)
    RolDown = pd.rolling_mean(dDown, n).abs()
    
    RS = RolUp / RolDown
    

提交回复
热议问题