<1>计算RSV-未成熟随机值:RSV=(当日收盘价 - 9日内最低价)/ (9日内最高价 - 9日内最低价) × 100 , 0<=RSV<=100
<2>当日K值=2/3×前一日K值+1/3×当日RSV
<3>当日D值=2/3×前一日D值+1/3×当日K值
<4>J=3K - 2D
<5>新股第一天:初始化 D=K=RSV
import pandas as pd dt = {‘date‘:[‘2021-07-26‘, ‘2021-07-27‘, ‘2021-07-28‘, ‘2021-07-29‘, ‘2021-07-30‘, ‘2021-08-02‘, ‘2021-08-03‘, ‘2021-08-04‘, ‘2021-08-05‘, ‘2021-08-06‘, ‘2021-08-09‘, ‘2021-08-10‘, ‘2021-08-11‘] ,‘close‘:[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] ,‘high‘:[85.57, 79.39, 78.43, 74.0, 72.2, 62.44, 64.5, 61.71, 68.06, 66.67, 72.35, 71.5, 70.7] ,‘low‘:[70.52, 72.82, 69.66, 70.03, 62.48, 58.88, 60.06, 59.79, 61.0, 63.72, 65.2, 67.6, 67.51]} df = pd.DataFrame(dt) #整理9日内最大值 df[‘high-9‘]=df[‘high‘].rolling(9,min_periods=1).max() #整理9日内最小值 df[‘low-9‘]=df[‘low‘].rolling(9,min_periods=1).min() #计算RSV=(当日收盘价 - 9日内最低价)/ (9日内最高价 - 9日内最低价) × 100 df[‘RSV‘] = (df[‘close‘]-df[‘low-9‘])/(df[‘high-9‘]-df[‘low-9‘]) *100 # 计算K,D # 当日K值=2/3×前一日K值+1/3×当日RSV # 当日D值=2/3×前一日D值+1/3×当日K值 for index in df.index: if 0==index: df.loc[index,‘K‘] = df[‘RSV‘][index] df.loc[index,‘D‘] = df[‘RSV‘][index] else: df.loc[index, ‘K‘] = df[‘K‘][index-1]*2/3+df[‘RSV‘][index]*1/3 df.loc[index, ‘D‘] = df[‘D‘][index-1]*2/3+df[‘K‘][index]*1/3 # 计算J=3K—2D df[‘J‘] = df[‘K‘]*3 - df[‘D‘]*2 print(df)