def evaluate_model(model_path, code, output_dir, input_shape=[30, 61]): extract_from_file("dataset/%s.csv" % code, output_dir, code) train_set, test_set = read_feature(output_dir, input_shape, code) saved_wp = WindPuller(input_shape).load_model(model_path) scores = saved_wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('test accuracy:', scores[1]) pred = saved_wp.predict(test_set.images, 1024) [cr, cap] = calculate_cumulative_return(test_set.labels, pred) # Output to a csv file # Read in the date, close from original data file. days_for_test = 700 tmp = pd.read_csv('dataset/%s.csv' % code, delimiter='\t') # tmp.columns = ['date', 'open', 'high', 'low', 'close', 'volume'] date = tmp['date'][-days_for_test:] close = tmp['close'][-days_for_test:] output = pd.DataFrame( { 'Return': test_set.labels, 'Position': pred.reshape(-1), 'Capital': cap.reshape(-1), 'Close': close.values }, index=date, columns=['Close', 'Return', 'Position', 'Capital']) output.to_csv('output/%s.csv' % code)
def evaluate_model(model_path, code, input_shape=[30, 83]): extract_from_file("dataset/%s.csv" % code, code) train_set, test_set = read_feature(".", input_shape, code) saved_wp = WindPuller(input_shape).load_model(model_path) scores = saved_wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('test accuracy:', scores[1]) pred = saved_wp.predict(test_set.images, 1024) cr = calculate_cumulative_return(test_set.labels, pred) print("changeRate\tpositionAdvice\tprincipal\tcumulativeReturn") for i in range(len(test_set.labels)): print(str(test_set.labels[i]) + "\t" + str(pred[i]) + "\t" + str(cr[i] + 1.) + "\t" + str(cr[i]))
def paper_test(): ''' 逐个读取每一天的14:57的数据,与数据库中数据合并,生成新特征,读取训练好的模型, 预测出信号。 ''' merged_data_dir = './paper_merge' signal_dir = './paper_signals' date = get_date_list() files = os.listdir(tsl_data_dir) # 0. 加载模型 wp_buy = WindPuller(input_shape).load_model(model_path_buy) wp_sell = WindPuller(input_shape).load_model(model_path_sell) for (idx, d) in enumerate(date): print('当前处理日期\t%s' % d) for (idf, f) in enumerate(files): # 1. 读取新的数据 f_path1 = os.path.join(tsl_data_dir, f) df1 = pd.read_csv(f_path1) # 获取某一天的数据 df1 = df1[df1['date'] == d] df1['volume'] == df1['volume'] * 80 / 79 # 2. 读取原来的数据 f_path2 = os.path.join(data_dir, f) df2 = pd.read_csv(f_path2) # 3. 合并数据,删除原来数据多余部分,追加最新的一天的数据 df2 = df2.iloc[:int(np.flatnonzero(df2.date == d))] df3 = df2.append(df1, ignore_index=True) df3 = df3[df2.columns] # 4. 保存数据 f_path_merged = os.path.join(merged_data_dir, f) df3.to_csv(f_path_merged, index=False) # 5. 提取1个特征,存入相应文件夹 output_prefix = f.split('.')[0] extract_from_file(idx, f_path_merged, feature_dir, output_prefix, 1) # 6. 读取提取完的特征 test_set = read_features(feature_dir, input_shape, output_prefix) # 7. 训练模型 signal_buy = wp_buy.predict(test_set.images, 1024) signal_buy = float(signal_buy[-1]) signal_sell = wp_sell.predict(test_set.images, 1024) signal_sell = float(signal_sell[-1]) # 8. 保存结果 f_path_signal = os.path.join(signal_dir, f) if idx == 0: # 写入字段名 title = 'date,signal_buy,signal_sell' with open(f_path_signal, 'a') as file: file.write(title) write = '%s,%.2f,%.2f\n' % (d, signal_buy, signal_sell) with open(f_path_signal, 'a') as file: file.write(write) n_read = idx * len(files) + idf + 1 print('当前处理第%d个文件,剩余%d个文件,请耐心等待...' % (n_read, len(files) * len(date) - n_read)) print('-' * 50) print('\n全部处理完毕!') print('=' * 80)