def get_vol_kdj_values(cls, base_df, dt, ktype="60"): base_df = base_df[base_df.index <= dt].tail(cls.get_max_30k(ktype)) data_df = gkd.Generator.generate_k_data(base_df, ktype=ktype) # 忽略当前volume data_df = data_df[:-1] data_df['v5'] = data_df['volume'].rolling(center=False, min_periods=1, window=5).sum() data_df['v15'] = data_df['volume'].rolling(center=False, min_periods=1, window=15).sum() zb_df = ZB.kdj(data_df) zb_df['direction'] = 1 # 向下的趋势 xd1 = (zb_df['kdj_d'] < zb_df['kdj_d'].shift(1)) | ( zb_df['kdj_k'] < zb_df['kdj_k'].shift(1)) zb_df.ix[xd1, 'direction'] = -1 zb_df['energy_d'] = zb_df['direction'] * zb_df['kdj_d'] data_df['direction'] = zb_df['direction'] data_df['energy_kdj_d'] = zb_df['energy_d'] data_df['energy_vol_kdj_d'] = data_df['energy_kdj_d'] * ( data_df['v5'] / data_df['v15']) cns = ['direction', 'energy_kdj_d', 'energy_vol_kdj_d'] return cls._extract(data_df, cns)
def select_x005(cls, st_df): """ kd """ name = "selected" kdj_df = ZB.kdj(st_df) x1 = kdj_df['kdj_k'] > kdj_df['kdj_k'].shift() kdj_df[name] = False kdj_df.ix[x1, name] = True return kdj_df.ix[:, [name]]
def select_x005(cls, st_df): """ kd """ name = "s005" kdj_df = ZB.kdj(st_df) x1 = kdj_df["kdj_k"] > kdj_df["kdj_k"].shift(1) # x2 = kdj_df["kdj_k"] <= 80 kdj_df[name] = False kdj_df.ix[x1, name] = True return kdj_df.ix[:, [name]]
def select_x004(cls, st_df): """ k60 """ name = "s004" kdj_df = ZB.kdj(st_df) x1 = kdj_df["kdj_k"] > kdj_df["kdj_k"].shift(1) x2 = kdj_df["kdj_k"] <= 50 kdj_df[name] = False kdj_df.ix[x1 & x2, name] = True return kdj_df.ix[:, [name]]
def get_kdj_values(cls, base_df, dt, ktype="60"): base_df = base_df[base_df.index <= dt].tail(cls.get_max_30k(ktype)) data_df = gkd.Generator.generate_k_data(base_df, ktype=ktype) zb_df = ZB.kdj(data_df) cur_d = zb_df['kdj_d'] max_d = zb_df['kdj_d'].rolling(center=False, min_periods=1, window=20).max() min_d = zb_df['kdj_d'].rolling(center=False, min_periods=1, window=20).max() zb_df['up_ratio'] = (cur_d - min_d) / cur_d zb_df['down_ratio'] = (max_d - cur_d) / max_d cns = ['kdj_k', 'kdj_d', 'up_ratio', 'down_ratio'] return cls._extract(zb_df, cns)
def select_x003(cls, st_df): """ k30 """ name = "s003" kdj_df = ZB.kdj(st_df) x1 = ZB.cross(kdj_df["kdj_k"], kdj_df["kdj_d"]) x2 = kdj_df["kdj_k"] <= 60 t1 = kdj_df.index.hour >= 11 kdj_df[name] = False kdj_df.ix[t1 & x1 & x2, name] = True return kdj_df.ix[:, [name]]
def select_x003(cls, st_df): """ """ name = "s004" kdj_df = ZB.kdj(st_df) x1 = kdj_df["kdj_k"] < kdj_df['kdj_d'] x2 = kdj_df["kdj_k"] >= kdj_df["kdj_k"].shift(1) x3 = ZB.cross(kdj_df["kdj_k"], kdj_df["kdj_d"]) t1 = kdj_df.index.hour >= 13 kdj_df[name] = False kdj_df.ix[t1 & x1 & x2, name] = True kdj_df.ix[t1 & x3, name] = True return kdj_df.ix[:, [name]]
def select_x005(cls, st_df): """ k60 """ name = "s005" kdj_df = ZB.kdj(st_df) diff = kdj_df['kdj_k'] - kdj_df['kdj_d'] kdj_df["diff"] = diff.ewm(com=2).mean() x10 = kdj_df["diff"] > kdj_df["diff"].shift(1) x11 = (kdj_df["kdj_k"] > kdj_df["kdj_k"].shift(1)) & ( kdj_df["kdj_k"] > kdj_df["kdj_k"].shift(2)) x2 = kdj_df["diff"] <= 0 kdj_df[name] = False kdj_df.ix[x2 & x10 & x11, name] = True return kdj_df.ix[:, [name]]
def select_x004(cls, st_df): """ k60 """ name = "selected" mdi_df = ZB.mdi(st_df, d=6) kdj_df = ZB.kdj(st_df) x1 = mdi_df["DIFF"] < mdi_df["DIFF"].shift(1) k1 = kdj_df["kdj_d"] < kdj_df["kdj_d"].shift(1) max_close = st_df['high'].rolling(center=False, min_periods=1, window=6).max() x2 = st_df['close'] <= (max_close - (max_close * 0.03)) mdi_df[name] = False mdi_df.ix[(x1 & k1) | x2, name] = True return mdi_df.ix[:, [name]]
def select_x004(cls, st_df): """ """ name = "s004" kdj_df = ZB.kdj(st_df) diff = kdj_df['kdj_k'] - kdj_df['kdj_d'] kdj_df["diff"] = diff.ewm(com=1).mean() # 高位, 确认趋势变坏 x1 = kdj_df["diff"] > 0 x2 = kdj_df["diff"] < kdj_df["diff"].shift(1) x3 = kdj_df["diff"].shift(1) <= kdj_df["diff"].shift(2) c1 = ZB.cross(kdj_df['kdj_d'], kdj_df['kdj_k']) c2 = kdj_df['kdj_k'] >= 30 kdj_df[name] = False kdj_df.ix[x1 & x2 & x3, name] = True kdj_df.ix[c1 & c2, name] = True return kdj_df.ix[:, [name]]
# coding=utf-8 import pandas as pd import matplotlib.pyplot as plt import gen_k_data as gkd from zibiao import ZB if __name__ == '__main2__': pd.set_option("display.max_columns", 500) df = gkd.read_5min_data('files/SH#600280.txt') df = df.tail(2000) df = df.head(1000) df_d = gkd.generate_k_data(df, ktype="30") df_kdj = ZB.kdj(df_d) df_mdi = ZB.mdi(df_d) xx_df = pd.concat([df_kdj, df_mdi], axis=1) xx_df = xx_df.reset_index(drop=True) diff1 = xx_df['kdj_k'] - xx_df['kdj_d'] xx_df["diff1"] = diff1.ewm(com=3).mean() diff2 = xx_df['PDI'] - xx_df['MDI'] xx_df["diff2"] = diff2.ewm(com=3).mean() del xx_df["PDI"] del xx_df["MDI"] del xx_df["ADX"]