iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 27
0
AI & Data

量化投資與機器學習研究系列 第 27

27.稍微認真做一個股市預測模型

  • 分享至 

  • xImage
  •  

不知道模型怎麼調,可以用暴力法try出來
使用GridSearchCV,就算你真的了解模型的原理也是一樣要用GridSearchCV,所以別怕不了解直接用GridSearchCV交叉驗證就不會用錯模型

昨天的變數只有用當天的資料,預測未來的股價
'成交股數', '成交金額', '開盤價', '最高價', '最低價', '收盤價', '漲跌價差', '成交筆數', '三大法人買賣超','外資自營商買賣超', '外資自營商買進', '外資自營商賣出', '外陸資買賣超', '外陸資買進', '外陸資賣出', '投信買賣超','投信買進', '投信賣出', '自營商買賣超', '自營商買賣超避險', '自營商買進', '自營商買進避險', '自營商賣出','自營商賣出避險', '融券今日餘額', '融券前日餘額', '融券現金償還', '融券買進', '融券賣出', '融券限額','融資今日餘額', '融資前日餘額', '融資現金償還', '融資買進', '融資賣出', '融資限額', '資券互抵', '借券今日可限額','借券前日餘額', '借券當日調整', '借券當日賣出', '借券當日還券', '借券當日餘額'

因該要計算幾天累積買的量,這邊設定累積4天的量

data['三大法人買賣超'].rolling(window=4).agg(['sum','std','mean','max','min','median','kurt','skew'])

上面的方法可以算過去3天加當天"三大法人買賣超"的總數、標準差、4天的最大值、4天的最小值、4天的中位數、峰度、偏度(還可以發揮創意繼續加上去,例如變化量的斜率)

for col in data.columns:
    data=pd.merge(data, data[col].rolling(window=4).agg(['sum','std','mean','max','min','median','kurt','skew']),on='date',suffixes=('','_'+col) )

兩行可以生出377個變數

成交股數, 成交金額, 開盤價, 最高價, 最低價, 收盤價, 漲跌價差, 成交筆數, 三大法人買賣超, 外資自營商買賣超, 外資自營商買進, 外資自營商賣出, 外陸資買賣超, 外陸資買進, 外陸資賣出, 投信買賣超, 投信買進, 投信賣出, 自營商買賣超, 自營商買賣超避險, 自營商買進, 自營商買進避險, 自營商賣出, 自營商賣出避險, 融券今日餘額, 融券前日餘額, 融券現金償還, 融券買進, 融券賣出, 融券限額, 融資今日餘額, 融資前日餘額, 融資現金償還, 融資買進, 融資賣出, 融資限額, 資券互抵, 借券今日可限額, 借券前日餘額, 借券當日調整, 借券當日賣出, 借券當日還券, 借券當日餘額, sum, std, mean, max, min, median, kurt, skew, sum_成交金額, std_成交金額, mean_成交金額, max_成交金額, min_成交金額, median_成交金額, kurt_成交金額, skew_成交金額, sum_開盤價, std_開盤價, mean_開盤價, max_開盤價, min_開盤價, median_開盤價, kurt_開盤價, skew_開盤價, sum_最高價, std_最高價, mean_最高價, max_最高價, min_最高價, median_最高價, kurt_最高價, skew_最高價, sum_最低價, std_最低價, mean_最低價, max_最低價, min_最低價, median_最低價, kurt_最低價, skew_最低價, sum_收盤價, std_收盤價, mean_收盤價, max_收盤價, min_收盤價, median_收盤價, kurt_收盤價, skew_收盤價, sum_漲跌價差, std_漲跌價差, mean_漲跌價差, max_漲跌價差, min_漲跌價差, median_漲跌價差, kurt_漲跌價差, skew_漲跌價差, sum_成交筆數, std_成交筆數, mean_成交筆數, max_成交筆數, min_成交筆數, median_成交筆數, kurt_成交筆數, skew_成交筆數, sum_三大法人買賣超, std_三大法人買賣超, mean_三大法人買賣超, max_三大法人買賣超, min_三大法人買賣超, median_三大法人買賣超, kurt_三大法人買賣超, skew_三大法人買賣超, sum_外資自營商買賣超, std_外資自營商買賣超, mean_外資自營商買賣超, max_外資自營商買賣超, min_外資自營商買賣超, median_外資自營商買賣超, sum_外資自營商買進, std_外資自營商買進, mean_外資自營商買進, max_外資自營商買進, min_外資自營商買進, median_外資自營商買進, sum_外資自營商賣出, std_外資自營商賣出, mean_外資自營商賣出, max_外資自營商賣出, min_外資自營商賣出, median_外資自營商賣出, sum_外陸資買賣超, std_外陸資買賣超, mean_外陸資買賣超, max_外陸資買賣超, min_外陸資買賣超, median_外陸資買賣超, kurt_外陸資買賣超, skew_外陸資買賣超, sum_外陸資買進, std_外陸資買進, mean_外陸資買進, max_外陸資買進, min_外陸資買進, median_外陸資買進, kurt_外陸資買進, skew_外陸資買進, sum_外陸資賣出, std_外陸資賣出, mean_外陸資賣出, max_外陸資賣出, min_外陸資賣出, median_外陸資賣出, kurt_外陸資賣出, skew_外陸資賣出, sum_投信買賣超, std_投信買賣超, mean_投信買賣超, max_投信買賣超, min_投信買賣超, median_投信買賣超, kurt_投信買賣超, skew_投信買賣超, sum_投信買進, std_投信買進, mean_投信買進, max_投信買進, min_投信買進, median_投信買進, kurt_投信買進, skew_投信買進, sum_投信賣出, std_投信賣出, mean_投信賣出, max_投信賣出, min_投信賣出, median_投信賣出, kurt_投信賣出, skew_投信賣出, sum_自營商買賣超, std_自營商買賣超, mean_自營商買賣超, max_自營商買賣超, min_自營商買賣超, median_自營商買賣超, kurt_自營商買賣超, skew_自營商買賣超, sum_自營商買賣超避險, std_自營商買賣超避險, mean_自營商買賣超避險, max_自營商買賣超避險, min_自營商買賣超避險, median_自營商買賣超避險, kurt_自營商買賣超避險, skew_自營商買賣超避險, sum_自營商買進, std_自營商買進, mean_自營商買進, max_自營商買進, min_自營商買進, median_自營商買進, kurt_自營商買進, skew_自營商買進, sum_自營商買進避險, std_自營商買進避險, mean_自營商買進避險, max_自營商買進避險, min_自營商買進避險, median_自營商買進避險, kurt_自營商買進避險, skew_自營商買進避險, sum_自營商賣出, std_自營商賣出, mean_自營商賣出, max_自營商賣出, min_自營商賣出, median_自營商賣出, kurt_自營商賣出, skew_自營商賣出, sum_自營商賣出避險, std_自營商賣出避險, mean_自營商賣出避險, max_自營商賣出避險, min_自營商賣出避險, median_自營商賣出避險, kurt_自營商賣出避險, skew_自營商賣出避險, sum_融券今日餘額, std_融券今日餘額, mean_融券今日餘額, max_融券今日餘額, min_融券今日餘額, median_融券今日餘額, kurt_融券今日餘額, skew_融券今日餘額, sum_融券前日餘額, std_融券前日餘額, mean_融券前日餘額, max_融券前日餘額, min_融券前日餘額, median_融券前日餘額, kurt_融券前日餘額, skew_融券前日餘額, sum_融券現金償還, std_融券現金償還, mean_融券現金償還, max_融券現金償還, min_融券現金償還, median_融券現金償還, kurt_融券現金償還, skew_融券現金償還, sum_融券買進, std_融券買進, mean_融券買進, max_融券買進, min_融券買進, median_融券買進, kurt_融券買進, skew_融券買進, sum_融券賣出, std_融券賣出, mean_融券賣出, max_融券賣出, min_融券賣出, median_融券賣出, kurt_融券賣出, skew_融券賣出, sum_融券限額, std_融券限額, mean_融券限額, max_融券限額, min_融券限額, median_融券限額, sum_融資今日餘額, std_融資今日餘額, mean_融資今日餘額, max_融資今日餘額, min_融資今日餘額, median_融資今日餘額, kurt_融資今日餘額, skew_融資今日餘額, sum_融資前日餘額, std_融資前日餘額, mean_融資前日餘額, max_融資前日餘額, min_融資前日餘額, median_融資前日餘額, kurt_融資前日餘額, skew_融資前日餘額, sum_融資現金償還, std_融資現金償還, mean_融資現金償還, max_融資現金償還, min_融資現金償還, median_融資現金償還, kurt_融資現金償還, skew_融資現金償還, sum_融資買進, std_融資買進, mean_融資買進, max_融資買進, min_融資買進, median_融資買進, kurt_融資買進, skew_融資買進, sum_融資賣出, std_融資賣出, mean_融資賣出, max_融資賣出, min_融資賣出, median_融資賣出, kurt_融資賣出, skew_融資賣出, sum_融資限額, std_融資限額, mean_融資限額, max_融資限額, min_融資限額, median_融資限額, sum_資券互抵, std_資券互抵, mean_資券互抵, max_資券互抵, min_資券互抵, median_資券互抵, kurt_資券互抵, skew_資券互抵, sum_借券今日可限額, std_借券今日可限額, mean_借券今日可限額, max_借券今日可限額, min_借券今日可限額, median_借券今日可限額, kurt_借券今日可限額, skew_借券今日可限額, sum_借券前日餘額, std_借券前日餘額, mean_借券前日餘額, max_借券前日餘額, min_借券前日餘額, median_借券前日餘額, kurt_借券前日餘額, skew_借券前日餘額, sum_借券當日調整, std_借券當日調整, mean_借券當日調整, max_借券當日調整, min_借券當日調整, median_借券當日調整, kurt_借券當日調整, skew_借券當日調整, sum_借券當日賣出, std_借券當日賣出, mean_借券當日賣出, max_借券當日賣出, min_借券當日賣出, median_借券當日賣出, kurt_借券當日賣出, skew_借券當日賣出, sum_借券當日還券, std_借券當日還券, mean_借券當日還券, max_借券當日還券, min_借券當日還券, median_借券當日還券, kurt_借券當日還券, skew_借券當日還券, sum_借券當日餘額, std_借券當日餘額, mean_借券當日餘額, max_借券當日餘額, min_借券當日餘額, median_借券當日餘額, kurt_借券當日餘額, skew_借券當日餘額

用交叉驗證跟GridSearch取出最佳參數,因為是時間序列所以要用TimeSeriesSplit

from sklearn import ensemble
params = {'n_estimators': [100,200,300], 'max_depth': [5,6,7], 'min_samples_split': [2],
          'learning_rate': [0.01], 'loss': ['ls']}

from sklearn.model_selection import TimeSeriesSplit, GridSearchCV
my_cv = TimeSeriesSplit(n_splits=3).split(X)
clf = GridSearchCV(ensemble.GradientBoostingRegressor(), params, cv=my_cv, n_jobs=1)
clf.fit(X_train, y_train)

clf.best_estimator_.get_params()

{'alpha': 0.9,
'criterion': 'friedman_mse',
'init': None,
'learning_rate': 0.01,
'loss': 'ls',
'max_depth': 7,
'max_features': None,
'max_leaf_nodes': None,
'min_impurity_decrease': 0.0,
'min_impurity_split': None,
'min_samples_leaf': 1,
'min_samples_split': 2,
'min_weight_fraction_leaf': 0.0,
'n_estimators': 200,
'presort': 'auto',
'random_state': None,
'subsample': 1.0,
'verbose': 0,
'warm_start': False}

Mean squared error: 343.01
Mean absolute error: 15.65

還是不好,未來要過濾一下變數或找其他變數,累積的天數要幾天也要調(也有暴力法可以用)
https://ithelp.ithome.com.tw/upload/images/20181110/20103419OiuSLmIEx1.png


上一篇
26.隨便做一個股市預測模型
下一篇
28.如何改善模型
系列文
量化投資與機器學習研究30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言