5.3 SVR · 使用SVR预测股票开盘价 v1.0

来源:https://uqer.io/community/share/5646f635f9f06c4446b48126

一、策略概述

本策略主旨思想是利用SVR建立的模型对股票每日开盘价进行回归拟合,即把前一日的 ['openPrice','highestPrice','lowestPrice','closePrice','turnoverVol','turnoverValue']作为当日 'openPrice' 的自变量,当日 'openPrice' 作为因变量。SVR的实现使用第三方库scikit-learn。

二、SVR

SVR详情

SVR参考文献见下方

5.3 SVR · 使用SVR预测股票开盘价 v1.0 - 图1

SVM-Regression

The method of Support Vector Classification can be extended to solve regression problems. This method is called Support Vector Regression.

The model produced by support vector classification (as described above) depends only on a subset of the training data, because the cost function for building the model does not care about training points that lie beyond the margin. Analogously, the model produced by Support Vector Regression depends only on a subset of the training data, because the cost function for building the model ignores any training data close to the model prediction.

There are three different implementations of Support Vector Regression: SVR, NuSVR and LinearSVR. LinearSVR provides a faster implementation than SVR but only considers linear kernels, while NuSVR implements a slightly different formulation than SVR and LinearSVR.

As with classification classes, the fit method will take as argument vectors X, y, only that in this case y is expected to have floating point values instead of integer values:

  1. >>> from sklearn import svm
  2. >>> X = [[0, 0], [2, 2]]
  3. >>> y = [0.5, 2.5]
  4. >>> clf = svm.SVR()
  5. >>> clf.fit(X, y)
  6. SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
  7. kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)
  8. >>> clf.predict([[1, 1]])
  9. array([ 1.5])

Support Vector Regression (SVR) using linear and non-linear kernels:

  1. import numpy as np
  2. from sklearn.svm import SVR
  3. import matplotlib.pyplot as plt
  4. ###############################################################################
  5. # Generate sample data
  6. X = np.sort(5 * np.random.rand(40, 1), axis=0)
  7. y = np.sin(X).ravel()
  8. ###############################################################################
  9. # Add noise to targets
  10. y[::5] += 3 * (0.5 - np.random.rand(8))
  11. ###############################################################################
  12. # Fit regression model
  13. svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
  14. svr_lin = SVR(kernel='linear', C=1e3)
  15. svr_poly = SVR(kernel='poly', C=1e3, degree=2)
  16. y_rbf = svr_rbf.fit(X, y).predict(X)
  17. y_lin = svr_lin.fit(X, y).predict(X)
  18. y_poly = svr_poly.fit(X, y).predict(X)
  19. ###############################################################################
  20. # look at the results
  21. plt.scatter(X, y, c='k', label='data')
  22. plt.plot(X, y_rbf, c='g', label='RBF model')
  23. plt.plot(X, y_lin, c='r', label='Linear model')
  24. plt.plot(X, y_poly, c='b', label='Polynomial model')
  25. plt.xlabel('data')
  26. plt.ylabel('target')
  27. plt.title('Support Vector Regression')
  28. plt.legend()
  29. plt.show()

5.3 SVR · 使用SVR预测股票开盘价 v1.0 - 图2

三、PS

原本使用前一天数据预测当天的,但在 Quartz 中,交易策略被具体化为根据一定的规则,判断每个交易日以开盘价买入多少数量的何种股票。回测不影响,但在使模拟盘时无法获取当天的closePrice等,所以将程序改为用地n-2个交易日的数据作为自变量,第n个交易日的openPrice作为因变量。

股票筛选的方法还很欠缺,本程序只用了'去除流动性差的股票'和'净利润增长率大于1的前N支股票'分别进行股票筛选测试,个人感觉都不很理想,还希望大牛们能提供一些有效的筛选方法。

对于股票指数来说,大多数时候都无法对其进行精确的预测,本策略只做参考。

期间发现通过 get_attribute_history 与 DataAPI.MktEqudGet 获取的数据中,有些股票的数据存在一些差异。

关于止损,同样的止损策略,在其他平台可以明显看到,但在Uqer感觉并不起作用,不知是不是代码编写存在错误?还望大牛指正。

程序写的有点乱七八糟的,还望大家见谅,多有不足还望指导!

References:

A Tutorial on Support Vector Regression” Alex J. Smola, Bernhard Schölkopf -Statistics and Computing archive Volume 14 Issue 3, August 2004, p. 199-222

  1. # 定义SVR预测函数
  2. def svr_predict(tickerlist,strattime_trainX,endtime_trainX,strattime_trainY,endtime_trainY,time_testX):
  3. from sklearn import svm
  4. # Get train data
  5. Per_Train_X = DataAPI.MktEqudGet(secID=tickerlist,beginDate=strattime_trainX,endDate=endtime_trainX,field=['openPrice','highestPrice','lowestPrice','closePrice','turnoverVol','turnoverValue'],pandas="1")
  6. Train_X = []
  7. for i in xrange(len(Per_Train_X)):
  8. Train_X.append(list(Per_Train_X.iloc[i]))
  9. # Get train label
  10. Train_label = DataAPI.MktEqudGet(secID=tickerlist,beginDate=strattime_trainY,endDate=endtime_trainY,field='openPrice',pandas="1")
  11. Train_label = list(Train_label['openPrice'])
  12. # Get test data
  13. if len(Train_X) == len(Train_label):
  14. Per_Test_X = DataAPI.MktEqudGet(secID=tickerlist,tradeDate=time_testX,field=['openPrice','highestPrice','lowestPrice','closePrice','turnoverVol','turnoverValue'],pandas="1")
  15. Test_X= []
  16. for i in xrange(len(Per_Test_X)):
  17. Test_X.append(list(Per_Test_X.iloc[i]))
  18. # Fit regression model
  19. clf = svm.SVR()
  20. clf.fit(Train_X, Train_label)
  21. # print clf.fit(Train_X, Train_label)
  22. PRY = clf.predict(Test_X)
  23. return '%.2f' %PRY[0]
  24. # retunr rount(PRY[0],2)
  25. else:
  26. pass
  1. from CAL.PyCAL import *
  2. from heapq import nsmallest
  3. import pandas as pd
  4. start = '2013-05-01' # 回测起始时间
  5. end = '2015-10-01' # 回测结束时间
  6. benchmark = 'HS300' # 策略参考标准
  7. universe = set_universe('ZZ500') #+ set_universe('SH180') + set_universe('HS300') # 证券池,支持股票和基金
  8. # universe = StockScreener(Factor('LCAP').nsmall(300)) #先用筛选器选择出市值最小的N只股票
  9. capital_base = 1000000 # 起始资金
  10. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  11. refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  12. commission = Commission(buycost=0.0008, sellcost=0.0018) # 佣金万八
  13. cal = Calendar('China.SSE')
  14. stocknum = 50
  15. def initialize(account): # 初始化虚拟账户状态
  16. pass
  17. def handle_data(account): # 每个交易日的买入卖出指令
  18. global stocknum
  19. # 获得日期
  20. today = Date.fromDateTime(account.current_date).strftime('%Y%m%d') # 当天日期
  21. strattime_trainY = cal.advanceDate(today,'-100B',BizDayConvention.Preceding).strftime('%Y%m%d')
  22. endtime_trainY = time_testX = cal.advanceDate(today,'-1B',BizDayConvention.Preceding).strftime('%Y%m%d')
  23. strattime_trainX = cal.advanceDate(strattime_trainY,'-2B',BizDayConvention.Preceding).strftime('%Y%m%d')
  24. endtime_trainX = cal.advanceDate(endtime_trainY,'-2B',BizDayConvention.Preceding).strftime('%Y%m%d')
  25. history_start_time = cal.advanceDate(today,'-2B',BizDayConvention.Preceding).strftime('%Y%m%d')
  26. history_end_time = cal.advanceDate(today,'-1B',BizDayConvention.Preceding).strftime('%Y%m%d')
  27. #######################################################################
  28. # # 获取当日净利润增长率大于1的前N支股票,由于API的读取数量限制,分批运行API。
  29. # getData_today = pd.DataFrame()
  30. # for i in xrange(300,len(account.universe),300):
  31. # tmp = DataAPI.MktStockFactorsOneDayGet(secID=account.universe[i-300:i],tradeDate=today,field=['secID','MA5','MA10','NetProfitGrowRate'],pandas="1")
  32. # getData_today = pd.concat([getData_today,tmp],axis = 0)
  33. # i = (len(account.universe) / 300)*300
  34. # tmp = DataAPI.MktStockFactorsOneDayGet(secID=account.universe[i:],tradeDate=today,field=['secID','NetProfitGrowRate'],pandas="1")
  35. # getData_today = pd.concat([getData_today,tmp],axis = 0)
  36. # getData_today=getData_today[getData_today.NetProfitGrowRate>=1.0].dropna()
  37. # getData_today=getData_today.sort(columns='NetProfitGrowRate',ascending=False)
  38. # getData_today=getData_today.head(100)
  39. # buylist = list(getData_today['secID'])
  40. #######################################################################
  41. # 去除流动性差的股票
  42. tv = account.get_attribute_history('turnoverValue', 20)
  43. mtv = {sec: sum(tvs)/20. for sec,tvs in tv.items()}
  44. per_butylist = [s for s in account.universe if mtv.get(s, 0) >= 10**7]
  45. bucket = {}
  46. for stock in per_butylist:
  47. bucket[stock] = account.referencePrice[stock]
  48. buylist = nsmallest(stocknum, bucket, key=bucket.get)
  49. #########################################################################
  50. history = pd.DataFrame()
  51. for i in xrange(300,len(account.universe),300):
  52. tmp = DataAPI.MktEqudGet(secID=account.universe[i-300:i],beginDate=history_start_time,endDate=history_end_time,field=u"secID,closePrice",pandas="1")
  53. history = pd.concat([history,tmp],axis = 0)
  54. i = (len(account.universe) / 300)*300
  55. tmp = DataAPI.MktEqudGet(secID=account.universe[i:],beginDate=history_start_time,endDate=history_end_time,field=u"secID,closePrice",pandas="1")
  56. history = pd.concat([history,tmp],axis = 0)
  57. # history = account.get_attribute_history('closePrice', 2)
  58. # history = DataAPI.MktEqudGet(secID=account.universe,beginDate=history_start_time,endDate=history_end_time,field=u"secID,closePrice",pandas="1")
  59. history.columns = ['secID','closePrice']
  60. keys = list(history['secID'])
  61. history.set_index('secID',inplace=True)
  62. ########################################################################
  63. # Sell&止损
  64. for stock in account.valid_secpos:
  65. if stock in keys:
  66. PRY = svr_predict(stock,strattime_trainX,endtime_trainX,strattime_trainY,endtime_trainY,time_testX)
  67. if (PRY < (list(history['closePrice'][stock])[-1])) or (((list(history['closePrice'][stock])[-1]/list(history['closePrice'][stock])[0])-1) <= -0.05):
  68. order_to(stock, 0)
  69. # Buy
  70. for stock in buylist:
  71. N = stocknum - len(account.valid_secpos)
  72. if (stock in keys) and (N > 0):
  73. if stock not in account.valid_secpos:
  74. PRY = svr_predict(stock,strattime_trainX,endtime_trainX,strattime_trainY,endtime_trainY,time_testX)
  75. if (PRY > list(history['closePrice'][stock])[-1]):
  76. amount = (account.cash/N)/account.referencePrice[stock]
  77. order(stock, amount)

5.3 SVR · 使用SVR预测股票开盘价 v1.0 - 图3