|
- 技術面 : 台股股價 daily、即時報價、歷史 tick、即時最佳五檔、PER、PBR、每5秒委託成交統計、加權指數、當日沖銷交易標的及成交量值。
- 基本面 : 綜合損益表、現金流量表、資產負債表、股利政策表、除權除息結果表、月營收。
- 籌碼面 : 外資持股、股權分散表、融資融券、三大法人買賣、借券成交明細。
- 消息面 : 台股相關新聞。
- 衍生性商品 : 期貨、選擇權 daily data、即時報價、交易明細,選擇權、期貨三大法人買賣,期貨各卷商每日交易、選擇權各卷商每日交易。
- 國際市場 : 美股股價 daily、minute、美國債券殖利率、貨幣發行量(美國)、黃金價格、原油價格、G8 央行利率、G8 匯率、
資料每天更新。你不需收集資料,就可進行分析。
首先先導入套件 接著我們可以使用FinMind的功能取得股價,這次我們嘗試的就是這個月股價不斷創高、台灣人稱外資控盤工具的 台積電(2330-TW), 選用的資料從2018-01-01開始
我們可以取得的資料有 - Trading_Volume(成交量)
- Trading_money(成交金額)
- Trading_turnover(周轉率):週轉率高代表股票交易越活絡
- close(收盤價)
- date(日期)
- max(當日最高價)
- min(當日最低價)
- open(開盤價)
- spread(震幅)
- stock_id(股票代碼)
Finmind,金融資料,股票,Python,LSTM
plt可以透過這個來進行視覺化
代碼 - import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- #2330:TSMC
- from FinMind.data import DataLoader
- dl=DataLoader()
- data = dl.taiwan_stock_daily_adj("2330", "2018-01-01", "2021-07-02")
- #print(data)
- # 繪製k線圖
- fig=plt.figure(figsize=(20,8))
- plt.xticks(rotation = 90)
- ax1 = fig.add_subplot(111)
- ax1.plot(data.close,color='red',label='close')
- ax1.plot(data.open,color='green',label='open')
- plt.legend()
- # twin 為共享x軸
- ax2= ax1.twinx()
- plt.bar(data.date,data.Trading_Volume.astype('int')//1000)
- ax3 = ax1.twinx()
- plt.savefig('2330_year.png')
- plt.show()
複製代碼
Finmind,金融資料,股票,Python,LSTM
訓練集資料有406筆,而我們只使用開盤價而已- #切分Test集
- test = data[data.date>'2019-09-01']
- train = data[:len(data)-len(test)]
- #只要open high
- train_set = train['open']
- test_set = test['open']
複製代碼
接著將數據做歸一化(Normalization)
- from sklearn.preprocessing import MinMaxScaler
- sc = MinMaxScaler(feature_range = (0, 1))
- #需將資料做reshape的動作,使其shape為(資料長度,1)
- train_set= train_set.values.reshape(-1,1)
- training_set_scaled = sc.fit_transform(train_set)
複製代碼
這裡需要特別解釋一下,for迴圈中的10代表著,我們將10天的資料視為一句話(一個序列的意思),因為序列型資料需要這樣子去呈現才行(X_train),而y_train則是用第i天前的資料來預測第i天的股價的意思。
- X_train = []
- y_train = []
- for i in range(10,len(train_set)):
- X_train.append(training_set_scaled[i-10:i-1, 0])
- y_train.append(training_set_scaled[i, 0])
- X_train, y_train = np.array(X_train), np.array(y_train)
- X_train = np.reshape(X_train,(X_train.shape[0], X_train.shape[1], 1))
- print("X_train.shape",X_train.shape)
複製代碼
Finmind,金融資料,股票,Python,LSTM
接著我們建置最簡單的LSTM模型
- import keras
- from keras.models import Sequential
- from keras.layers import Dense
- from keras.layers import LSTM
- from keras.layers import Dropout,BatchNormalization
- keras.backend.clear_session()
- regressor = Sequential()
- regressor.add(LSTM(units = 100, input_shape = (X_train.shape[1], 1)))
- regressor.add(Dense(units = 1))
- regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
複製代碼
Finmind,金融資料,股票,Python,LSTM
我們只使用一層LSTM層,訓練100個epoch,batch_size設為16,並劃出train loss
- history = regressor.fit(X_train, y_train, epochs = 100, batch_size = 16)
- plt.title('train_loss')
- plt.ylabel('loss')
- plt.xlabel('Epoch')
- plt.plot( history.history["loss"])
- plt.show()
複製代碼
Finmind,金融資料,股票,Python,LSTM
接著就到了開獎的時候了,但這裏我們需要稍微處理一下輸入的資料,一樣的邏輯,要預測第i天,就需要第i天前的資料。
- dataset_total = pd.concat((train['open'], test['open']), axis = 0)
- inputs = dataset_total[len(dataset_total) - len(test) - 10:].values
- inputs = inputs.reshape(-1,1)
- inputs = sc.transform(inputs)
- X_test = []
- for i in range(10, len(inputs)):
- X_test.append(inputs[i-10:i-1, 0])
- X_test = np.array(X_test)
- X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
- predicted_stock_price = regressor.predict(X_test)
- #使用sc的 inverse_transform將股價轉為歸一化前
- predicted_stock_price = sc.inverse_transform(predicted_stock_price)
複製代碼 劃出預測結果
- plt.plot(test['open'].values, color = 'black', label = 'Real TSMC Stock Price')
- plt.plot(predicted_stock_price, color = 'green', label = 'Predicted TSMC Stock Price')
- plt.title('TATA Stock Price Prediction')
- plt.xlabel('Time')
- plt.ylabel('Stock Price')
- plt.legend()
- plt.show()
複製代碼
可以看到我們的預測模型低估了台積電的實力了XD
Finmind,金融資料,股票,Python,LSTM
補充:其他的嘗試在我們的網路層再加一層後(中間用dropout,防止過擬合),再來看一次結果
Finmind,金融資料,股票,Python,LSTM
Finmind,金融資料,股票,Python,LSTM
XD結果看差多了
參考文章
https://awesomeopensource.com/project/FinMind/FinMind
https://wenwender.wordpress.com/ ... %E8%82%A1%E7%A5%A8/
|
|