07指标RSI- 附代码

相对强弱指标RSI
计算公式:
<1> N日RSI =A/(A+B)×100
<2> A=N日内收盘涨幅之和的平均 A = (前一日A*(n-1) + 当日涨值)/n
<3>B=N日内收盘跌幅之和的平均 B = (前一日B*(n-1) + 当日跌值)/n (跌值取正值:如下跌-4.32,跌值=4.32)
<4> 0<=RSI<=100

import numpy as np

# 输入:
#     close_k: 收盘价list
#     periods:周期
def RSI(close_k,periods):
    length = len(close_k)
    ans = [np.nan]*length
    A = 0
    B = 0

    for j in range(1,length):
        up = 0
        down = 0
        if close_k[j]>=close_k[j-1]:
            up = close_k[j]-close_k[j-1]
            down = 0
        else:
            up=0
            down = close_k[j-1]-close_k[j]
        A = (A*(periods-1)+up)/periods
        B = (B*(periods-1)+down)/periods
        if A + B!=0:
            ans[j] = 100 * A / (A + B)
    return ans






if __name__==__main__:
    #000568收盘价,时间:2021-05-06  至 2021-08-18
    date2 = [239.88, 237.4, 226.03, 236.87, 243.3, 243.11, 247.8, 257.81, 257.9, 258.03, 259.5, 257.5, 264.77, 284.05, 275.89, 275.8, 274.49, 274.95, 277.71, 274.8, 276.99, 279.6, 283.22, 262.02, 262.2, 266.71, 258.25, 249.86, 242.58, 242.45, 232.8, 236.5, 232.02, 225.98, 231.0, 234.93, 235.5, 228.15, 235.94, 236.0, 226.6, 223.0, 225.51, 227.65, 220.7, 216.51, 223.8, 224.36, 225.62, 228.11, 222.2, 228.48, 226.04, 222.72, 216.55, 216.5, 199.68, 190.0, 191.29, 185.35, 171.1, 180.89, 187.85, 180.73, 176.62, 178.54, 179.7, 194.69, 190.2, 186.53, 188.9, 188.48, 179.38, 179.55]
    #301033, 2021.7.26 - 2021.8.18
    date1 = [71.03, 72.99, 75.0, 71.69, 62.69, 60.97, 62.45, 60.97, 67.38, 65.29, 69.72, 68.8, 69.08, 69.61, 70.25, 74.48, 70.57, 69.33]
    print(RSI(date1,6))
    print(RSI(date1,12))
    print(RSI(date1,24))

 

07指标RSI- 附代码

上一篇:antd在线换肤定制功能


下一篇:如何读取propertyGridControl1中各行的数据