def rsi(price, n=14):
    gain = price.diff().fillna(0) # fifference between day n and n-1, replace nan (first value) with 0
    
    def rsiCalc(p):
        # subfunction for calculating rsi for one lookback period
        avgGain = p[p>0].sum()/n
        avgLoss = -p[p<0].sum()/n 
        rs = avgGain/avgLoss
        return 100 - 100/(1+rs)
    
    # run for all periods with rolling_apply
    return pd.rolling_apply(gain,n,rsiCalc)
rsi indicator
dataset/ETHPy150Open sjev/trading-with-python/lib/indicators.py/rsi