python实现kdj、macd、rsi、bias、willr指标

#kdj指标
def myself_kdj(df):
    low_list = df['low'].rolling(9, min_periods=9).min()
    low_list.fillna(value=df['low'].expanding().min(), inplace=True)
    high_list = df['high'].rolling(9, min_periods=9).max()
    high_list.fillna(value = df['high'].expanding().max(), inplace=True)
    rsv = (df['close'] - low_list) / (high_list - low_list) * 100
    df['k'] = pd.DataFrame(rsv).ewm(com=2).mean()
    df['d'] = df['k'].ewm(com=2).mean()
    df['j'] = 3 * df['k'] - 2 * df['d']
    return df
#macd指标
def get_macd_data(data,short=0,long1=0,mid=0):
    if short==0:
        short=12
    if long1==0:
        long1=26
    if mid==0:
        mid=9
    data['sema']=pd.Series(data['close']).ewm(span=short).mean()
    data['lema']=pd.Series(data['close']).ewm(span=long1).mean()
    data.fillna(0,inplace=True)
    data['data_dif']=data['sema']-data['lema']
    data['data_dea']=pd.Series(data['data_dif']).ewm(span=mid).mean()
    data['data_macd']=2*(data['data_dif']-data['data_dea'])
    data.fillna(0,inplace=True)
    return data[['candle_begin_time_GMT8', 'data_dif','data_dea','data_macd']]
#rsi指标
#建议用talib库的RSI方法,亲测有用
df["rsi_6"] = ta.RSI(df['close'], timeperiod=6)
df["rsi_12"] = ta.RSI(df['close'], timeperiod=12)
df["rsi_24"] = ta.RSI(df['close'], timeperiod=24)
#计算方法:
#bias指标
#N期BIAS=(当日收盘价-N期平均收盘价)/N期平均收盘价*100%
df['bias_6'] = (df['close'] - df['close'].rolling(6, min_periods=1).mean())/ df['close'].rolling(6, min_periods=1).mean()*100
df['bias_12'] = (df['close'] - df['close'].rolling(12, min_periods=1).mean())/ df['close'].rolling(12, min_periods=1).mean()*100
df['bias_24'] = (df['close'] - df['close'].rolling(24, min_periods=1).mean())/ df['close'].rolling(24, min_periods=1).mean()*100
df['bias_6'] = round(df['bias_6'], 2)
df['bias_12'] = round(df['bias_12'], 2)
df['bias_24'] = round(df['bias_24'], 2)
#威廉指标
#建议用talib库的WILLR方法,亲测有用
df['willr'] = ta.WILLR(df['high'], df['low'], df['close'], timeperiod=14)

你可能感兴趣的:(python实现kdj、macd、rsi、bias、willr指标)