使用机器学习预测大盘
使用机器学习预测大盘
标签(空格分隔): 未分类
前言:金融数据具有多种特征,但是大盘每天的涨跌只有两种情况,要么涨要么跌(横盘的情况几乎不存在,因为参与交易的人太多了,即使资金强大的机构也不能完全控制市场,只能适当的引导市场的方向),所以金融时间数据具有天然的可分类的特点,而且金融时间数据量大且获取方便,是学习机器学习的一个好的数据来源
本文将使用KNN,SVM等方法来通过金融时间数据来预测大盘涨跌,并得到每种预测方法的准确度情况
1.数据准备
- 获得金融时间序列:
通过ABusymbolPd获取大盘两年的时间金融序列,并将金融时间序列存储到文件中,方便后面重复使用
from abupy import ABusymbolPd
import pandas as pd
kl_df = ABuSymbolPd.make_kl_df('000001',n_folds=2) kl_df.to_csv('000001.csv')
- 准备特征数据和label标签
import talib
kl_df = pd.read_csv('000001.csv')
kl_df = kl_df.set(['Unnamed: 0'])
trade_days = kl_df.index.tolist()
start_date = '2016-07-27' start_date_index = trade_days.index(start_date)
end_date = '2018-06-13' end_date_index = trade_days.index(end_date)
x_all = []
y_all = []
for index in range(start_date_index,end_date_index):
stock_data = kl_df[index-30:index]
close_prices = stock_data['close'].values
sma_data = talib.SMA(close_prices)[-1]
wma_data = talib.WMA(close_prices)[-1]
mom_data = talib.MOM(close_prices)[-1]
features = []
features.append(sma_data)
features.append(wma_data)
features.append(mom_data)
label = False
if close_prices[-1] > close_prices[-2]:
label = True
x_all.append(features)
y_all.append(label)
2.算法准备
- KNN算法
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
def knn_start(x_train,y_train,x_test,y_test):
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(x_train,y_train)
pred_on_test = knn.predict(x_test)
accuracy = metrics.accuracy_score(pred_on_test,y_test)
return accuracy
- SVM算法
from sklearn import svm
from sklearn import metrics
def svm_start(x_train,y_train,x_test,y_test):
clf = svm.SVC()
clf.fit(x_train,y_train)
pred_on_test = clf.predict(x_test)
accuracy = metric.accuracy_score(pred_on_test,y_test)
return accuracy
- 绘图函数
def plot(func):
x_data = []
y_data = []
for train_len in range(450):
x_data.append(train_len)
x_train = x_all[0:train_len]
y_train = y_all[0:train_len]
x_test = x_all[train_len:]
y_test = y_all[train_len:]
accuracy = func(x_train,y_train,x_test,y_test)
y_data.append(accuracy)
plt.plot(x_data,y_data,color='red')
3.结果评价
-
KNN算法
-
SVM算法