本文使用keras搭建神经网络,实现基于深度学习算法的股票价格预测。
本文使用的数据来源为tushare,一个免费开源接口;且只取开票价进行预测。
import numpy as np
import tushare as ts
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM,Dropout, GRU, Bidirectional
import math
from sklearn.metrics import mean_squared_error
"""
获取历史数据
"""
mytoken = '8a32fbf66426b2a10f3299f46ec8428f4fbc12d0b2ec176dc1d6ff93'
ts.set_token(mytoken)
save_path3 = 'NormalData'
if not os.path.exists(os.path.join(save_path, save_path3)):
os.mkdir(os.path.join(save_path, save_path3))
def getNormalData():
pool = pro.stock_basic(exchange='',
list_status='L',
adj='qfq',
fields='ts_code,symbol,name,area,industry,fullname,list_date, market,exchange,is_hs')
pool = pool[pool['market'].isin(['主板', '中小板'])].reset_index()
print('获得上市股票总数:', len(pool)-1)
j = 1
for i in pool.ts_code:
print('正在获取第%d家,股票代码%s.' % (j, i))
path = os.path.join(save_path, save_path3, i + '.csv')
j += 1
df = pro.stk_limit(ts_code=i,
adj='qfq',
start_date=startdate,
end_date=enddate)
df = df.sort_values('trade_date', ascending=True)
df.to_csv(path, index=False)
dataset = pd.read_csv('/Users/Desktop/000001.SZ_NormalData.csv')
dataset.head()
trade_date | open | high | low | close | vol | ts_code | |
---|---|---|---|---|---|---|---|
0 | 20120104 | 5.0408 | 5.0408 | 4.8986 | 4.9018 | 147910.98 | 000001.SZ |
1 | 20120105 | 4.9018 | 5.0279 | 4.8921 | 4.9762 | 244080.05 | 000001.SZ |
2 | 20120106 | 4.9632 | 5.0150 | 4.8986 | 4.9632 | 133151.15 | 000001.SZ |
3 | 20120109 | 4.9665 | 5.1120 | 4.9309 | 5.1023 | 221138.66 | 000001.SZ |
4 | 20120120 | 5.4094 | 5.4773 | 5.2769 | 5.4256 | 586675.66 | 000001.SZ |
training_set = dataset[:1933].iloc[:,1:2].values
test_set = dataset[1934:].iloc[:,1:2].values
dataset["high"][:1933].plot(figsize=(16,4),legend=True)
dataset["high"][1934:].plot(figsize=(16,4),legend=True)
plt.legend(['Training set (before 2020)','Test set (2020 and beyond)'])
plt.title('stock price')
plt.show()
#Scaling the training set
sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled