def get_prediction_data(klines, n): """获取用于随机森林的n个输入数据(n为数据长度): n天中每天的特征参数及其涨跌情况""" close_prices = klines.close[- 30 - n:] # 获取本交易日及以前的收盘价(此时在预定的收盘时间: 认为本交易日已收盘) # 计算所需指标 sma_data = sma(close_prices, 30, 0.02)[-n:] # SMA指标, 函数默认时间周期参数:30 wma_data = ema2(close_prices, 30)[-n:] # WMA指标 mom_data = trma(close_prices, 30)[-n:] # MOM指标 x_all = list(zip(sma_data, wma_data, mom_data)) # 样本特征组 y_all = list(klines.close.iloc[i] >= klines.close.iloc[i - 1] for i in list(reversed(range(-1, -n - 1, -1)))) # 样本标签组 # x_all: 大前天指标 前天指标 昨天指标 (今天指标) # y_all: (大前天) 前天 昨天 今天 -明天- # 准备算法需要用到的数据 x_train = x_all[: -1] # 训练数据: 特征 x_predict = x_all[-1] # 预测数据(用本交易日的指标预测下一交易日的涨跌) y_train = y_all[1:] # 训练数据: 标签 (去掉第一个数据后让其与指标隔一位对齐(例如: 昨天的特征 -> 对应预测今天的涨跌标签)) return x_train, y_train, x_predict
def ZJFZ_kdj(self, time_system='day'): ''' ZJFZ VAR2:=REF(LOW,1); VAR3:=SMA(ABS(LOW-VAR2),3,1)/SMA(MAX(LOW-VAR2,0),3,1)*100; VAR4:=EMA(VAR3*10,3); VAR5:=LLV(LOW,13); VAR6:=HHV(VAR4,13); VAR7:=EMA(IF(LOW<=VAR5,(VAR4+VAR6*2)/2,0),3)/618; VAR8:=IF(VAR7>500,500,VAR7); STICKLINE(VAR8>-120,0,VAR8,3,1),COLORGRAY; STICKLINE(VAR8>1 AND "KDJ.J">REF("KDJ.J",1) AND REF("KDJ.J",1)<REF("KDJ.J",2),0,VAR8,3,0),COLORRED; STICKLINE(VAR8>0.1 AND VAR8<1 AND "KDJ.J">REF("KDJ.J",1) AND REF("KDJ.J",1)<REF("KDJ.J",2),0,VAR8,3,0),COLOR00FFFF; ''' time1 = time.time() ans = [] if time_system == 'day': day = 240 elif time_system == 'week': day = 1200 for code in tqdm.tqdm(self.stock): count = 0 if code[0] == '3': continue data = self.getdata_day(code, 1200, 260) if len(data) < 60: continue if data['close'][0] < 6: continue var2 = tafunc.ref(data['low'], 1) var3 = tafunc.sma( tafunc.abs(data['low'] - var2), 3, 1) / tafunc.sma( tafunc.max(data['low'], var2), 3, 1) * 100 var4 = tafunc.ema(var3 * 10, 3) var5 = tafunc.llv(data['low'], 13) var6 = tafunc.hhv(var4, 13) for i in range(len(data['low'])): a = data['low'][i] b = var5[i] if not np.isnan(b) and a <= b: var4[i] = (var4[i] + var6[i] * 2) / 2 else: var4[i] = 0 var7 = tafunc.ema(var4, 3) / 618 kdj = ta.KDJ(data, 9, 3, 3) j = kdj['j'] gold = tafunc.crossup(kdj['k'], kdj['d']) j1 = tafunc.ref(j, 1) for i in range(len(j)): if np.isnan(j1[i]): var7[i] = 0 continue # if all((var7[i] > 0.01 , gold[i] > 0)) or all((var7[i] > 0.01 , j[i] > j1[i])): if all((var7[i] > 0.01, gold[i] > 0)): var7[i] = 1 else: var7[i] = 0 if np.sum(var7[-4:]) > 0: # print(code) ans.append(code) with open('ZJFZ_kdj.txt', "w") as f: for c in ans: f.write(c + '\n') time2 = time.time() print('ZJFZ_kdj' + '运行时间:', time2 - time1) return ans