def cal_rank_now(self, bias_now=None): """ 给定一个值,计算相对bias :param bias_now: :return: """ if bias_now is None: bias_now = self.cal_rt_bias() if bias_now >= 0: return relative_rank(self.bias_dict['bias_p'], bias_now) else: return relative_rank(self.bias_dict['bias_n'], bias_now) - 100
def cal_close_rank(df_): """ 计算排名子函数 :param df_: :return: """ close_history = df_['close'] close_now = close_history.values[-1] return relative_rank(close_history, close_now)
def predict_tomorrow(stk_code, label, N_STEPS=N_STEPS, feature_cols=feature_cols, HIDDEN_SIZE=HIDDEN_SIZE, NUM_LAYERS=NUM_LAYERS): """ :param stk_code: 例子 '300508' :param label: 例子 'high' :param N_STEPS: :param feature_cols: :param HIDDEN_SIZE: :param NUM_LAYERS: :return: """ """ ---------------------- 读取json中存储的极值 ---------------------- """ with open(rootPath + '\Function\LSTM\AboutLSTM\stk_max_min.json', 'r') as f: max_min_info = json.load(f) """ ---------------------- 获取实时数据 ---------------------- """ data_now = ts.get_k_data(stk_code)[-(N_STEPS + 30):] # 增加M9 Rank data_now['m9'] = data_now['close'].rolling(window=9).mean() data_now['diff_m9'] = data_now.apply(lambda x: (x['close'] - x['m9']) / x['close'], axis=1) data_now['rank'] = data_now.apply(lambda x: 100 - relative_rank( max_min_info[stk_code]['m9_history'], x['diff_m9']), axis=1) # rootPath = 'C:/Users\paul\Desktop\软件代码\Git-Clone' for c in ['close', 'high', 'low', 'open']: data_now[c] = (data_now[c].values - max_min_info[stk_code]['p_min'] ) / (max_min_info[stk_code]['p_max'] - max_min_info[stk_code]['p_min']) data_now['volume'] = ( data_now['volume'].values - max_min_info[stk_code]['v_min']) / ( max_min_info[stk_code]['v_max'] - max_min_info[stk_code]['v_min']) # 进行归一化 input_normal = data_now.loc[:, feature_cols].tail(20).values tf.reset_default_graph() """ ---------------------- 创建模型 ---------------------- """ predictions, loss, train_op, X, y = lstm_model(n_steps=N_STEPS, n_inputs=len(feature_cols), HIDDEN_SIZE=HIDDEN_SIZE, NUM_LAYERS=NUM_LAYERS) # 创建保存器用于模型 saver = tf.train.Saver() # 初始化 sess = tf.Session() model_name = stk_code + '_' + label model_dir = rootPath + 'Function\LSTM\AboutLSTM\modelDir/' if os.path.exists(model_dir + model_name + '/' + model_name + '.ckpt.meta'): saver = tf.train.import_meta_graph(model_dir + model_name + '/' + model_name + '.ckpt.meta') saver.restore(sess, tf.train.latest_checkpoint(model_dir + model_name + '/')) # graph = tf.get_default_graph() # 防报错 tf.reset_default_graph() r_rela = sess.run([predictions], feed_dict={X: [input_normal]})[0][0][0] return max_min_info[stk_code]['p_min'] + ( max_min_info[stk_code]['p_max'] - max_min_info[stk_code]['p_min']) * r_rela else: print('加载模型' + model_name + '失败!') return -1
# encoding=utf-8
def cal_rank_sig(sig, total): return relative_rank(total, sig)