摘要
股票市场的发展对于市场经济具有重要的意义,许多研究者也对股票市场的走势进行预测研究。通过一些技术来预测股票未来的发展趋势,预测其后面涨跌。股票指数是预判经济的一个重要指标,股票指数表示的就是股票市场中总体的发展水平和走向。但由于影响股票指数波动的因素较多。随着金融市场的建立,股票价格的分析和预测问题越来越困难
一、实验数据准备
股票数据数据准备,通过网易api接口获取历史股票数据,核心代码如下:
# 从网易接口获取数据api_adr = 'http://quotes.money.163.com/service/chddata.html'fields = "TOPEN;TCLOSE;HIGH;LOW;VOTURNOVER"# 注意:获取上海证券与深圳证券股票的数据,需要构造不同的URLtag = "0" # 上海证券if sticker_code in ['000063','000066','000768','000651']:tag = "1" # 深圳证券params = {'code': tag + sticker_code, 'start': start_date, 'end': end_date, 'fields': fields}r = requests.get(api_adr, params=params)print(r.url)# txt_list = r.text.split('\n') # r.content二进制数据 r.text 文本数据txt_list = r.content.decode('gbk').split('\n') # r.content二进制数据 r.text 文本数据txt_list.reverse()txt_list[0] = txt_list[-1] # 列名替换开头的空字符col_name = "Date,Code,Name,Open,Close,High,Low,Volume\n"txt_list[0] = col_nametxt_list.pop(-1)root = os.path.dirname(os.path.dirname(__file__))dir_path = os.path.join(root,"data")filename = sticker_code + ".csv"print(os.path.join(dir_path,filename))with open(os.path.join(dir_path,filename), "w+",encoding='gbk') as f:for line in txt_list:if line.split(',')[3] != '0.0': # 去除无效数据f.write(line)def get_all_last_data(start_date): # 得到从start_date至今日 所有最新数据root = os.path.dirname(os.path.dirname(__file__))config_path = os.path.join(root,"config.json")configs = json.load(open(config_path, 'r'))companies = configs['companies']# start_date = '2010-06-21' # 只能按整年获取至今日数据cur = datetime.now()year = timedelta(days=365)cur = cur + year # 在当前日期上加一年end_date = cur.strftime("%Y-%m-%d") # 获取今年最新数据for code, company_name in companies.items():get_domestic_stock(code, start_date, end_date)
在config设置要下载的最新股票数据集:
"companies":{"600718":"drjt","000651":"gldq","600839":"scch","600519":"zhzg","601988":"zgyh","000066": "zgcc","601766":"zgzc","601390":"zgzt","000768":"zhfj","000063":"zxtx"}
单独运行股票文件,就可以获取了:
二、数据预处理
数据预处理就是把数据归一化,方便模型训练收敛,核心代码如下:
def normalise_windows(self, window_data, single_window=False):# 归一化窗口,基值为0normalised_data = []window_data = [window_data] if single_window else window_datafor window in window_data:normalised_window = []for col_i in range(window.shape[1]):normalised_col = [((float(p) / float(window[0, col_i])) - 1) for p in window[:, col_i]]normalised_window.append(normalised_col)normalised_window = np.array(normalised_window).Tnormalised_data.append(normalised_window)return np.array(normalised_data)
三、构建模型
3.1构建网络模型
构建3维数组,核心代码如下:
# 定义模型结构
class DataLoader():def __init__(self, filename, split, cols,isTest=False):dataframe = pd.read_csv(filename,encoding='gbk')i_split = int(len(dataframe) * split)self.data_train = dataframe.get(cols).values[:i_split]if isTest:self.data_test = dataframe.get(cols).values[0:]else:self.data_test = dataframe.get(cols).values[i_split:]self.len_train = len(self.data_train)self.len_test = len(self.data_test)print(self.len_train,self.len_test)self.len_train_windows = None
3.2定义LSTM模型
"layers": [{"type": "lstm","neurons": 100,"input_timesteps": 49,"input_dim": 1,"return_seq": true},{"type": "dropout","rate": 0.2},{"type": "lstm","neurons": 100,"return_seq": true},{"type": "lstm","neurons": 100,"return_seq": false},{"type": "dropout","rate": 0.2},{"type": "dense","neurons": 1,"activation": "linear"}]
四、输出和预测
#训练模型def train(self, x, y, epochs, batch_size, save_dir):timer = Timer()timer.start()print('[Model] Training Started')print('[Model] %s epochs, %s batch size' % (epochs, batch_size))save_fname = os.path.join(save_dir, '%s-e%s.h5' % (dt.datetime.now().strftime('%d%m%Y-%H%M%S'), str(epochs)))callbacks = [EarlyStopping(monitor='val_loss', patience=2),ModelCheckpoint(filepath=save_fname, monitor='val_loss', save_best_only=True)]self.model.fit(x,y,epochs=epochs,batch_size=batch_size,callbacks=callbacks)history = self.model.fit(x,y,epochs=epochs)plt.plot(history.history['loss'])plt.xlabel('epochs')plt.ylabel('loss')plt.show()#模型保存self.model.save(save_fname)print('[Model] Training Completed. Model saved as %s' % save_fname)timer.stop()
五、系统测试
逐点预测,通过前面50个值,预测第51个值,比较简单,拟合比较好
全序列预测:通过拿前面50个值,预测后面第51-100个值,可以分析出大致的走势
总结
在股市的预测当中,通过技术指标和股票的交易数据来进行股市的预测,本文利用了LSTM神经网络来构建股票的预测模型。该模型可以有效的应对时间序列的预测,解决了传统RNN梯度消失的问题,但本文提出的方法容易出现过离合的现象,如果结合基本面的股票信息,可能会改善本文模型的预测效果,将政策因素和市场环境作为股票的预测模型输入变量,可以优化本文预测模型的预测准确性,投资者可以根据本文的股票预测模型来结合自己的经验进行合理的投资。
可以进来一起学习python,欢迎进来。