第三部分在这里~~~~https://bbs.pinggu.org/thread-3906762-1-1.html
ricequant,致力于打造亚太最出色的量化交易平台,www.ricequant.com
量化策略交流群:
Ricequant 量化策略社区 429907369
Ricequant高校量化交流群 484490463
策略的研究与执行策略研究
我们首先用Python来选择适合交易的股票对。 用于选取的股票池为:
600815 厦工股份 机械行业
600841 上柴股份 机械行业
600855 航天长峰 机械行业
600860 京城股份 机械行业
600984 *ST建机 机械行业
601038 一拖股份 机械行业
601002 晋亿实业 机械行业
601100 恒立油缸 机械行业
601106 中国一重 机械行业
601177 XD杭齿前 机械行业
计算所用历史数据为2012年全年的日线数据。
- import operator
- import numpy as np
- import statsmodels.tsa.stattools as sts
- import matplotlib.pyplot as plt
- import tushare as ts
- import pandas as pd
- from datetime import datetime
- from scipy.stats.stats import pearsonr
- > sector = pd.read_csv('sector.csv', index_col=0)
- sector_code = sector['code'][100:110]
- resectorcode = sector_code.reset_index(drop=True)
- stockPool = []
- rank = {}
- Rank = {}
- for i in range(10):
- stockPool.append(str(resectorcode[i])
- for i in range(10):
- for j in range(i+1,10):
- if i != j:
- # get the price of stock from TuShare
- price_of_i = ts.get_hist_data(stockPool[i], start='2012-01-01', end='2013-01-01')
- price_of_j = ts.get_hist_data(stockPool[j], start='2012-01-01', end='2013-01-01')
- # combine the close price of the two stocks and drop the NaN
- closePrice_of_ij = pd.concat([price_of_i['close'], price_of_j['close']], axis = 1)
- closePrice_of_ij = closePrice_of_ij.dropna()
- # change the column name in the dataFrame
- closePrice_of_ij.columns = ['close_i', 'close_j']
- # calculate the daily return and drop the return of first day cause it is NaN.
- ret_of_i = ((closePrice_of_ij['close_i'] - closePrice_of_ij['close_i'].shift())/closePrice_of_ij['close_i'].shift()).dropna()
- ret_of_j = ((closePrice_of_ij['close_j'] - closePrice_of_ij['close_j'].shift())/closePrice_of_ij['close_j'].shift()).dropna()
- # calculate the correlation and store them in rank1
- if len(ret_of_i) == len(ret_of_j):
- correlation = np.corrcoef(ret_of_i.tolist(), ret_of_j.tolist())
- m = stockPool[i] + '+' + stockPool[j]
- rank[m] = correlation[0,1]
- rank1 = sorted(rank.items(), key=operator.itemgetter(1))
- potentialPair = [list(map(int, item[0].split('+'))) for item in rank1]
- potentialPair = potentialPair[-5:]
-
- # 选出的相关系数最高的五对股票。 比如 ('600815+601177', 0.59753123459010704),600815+601177为两只股票的代码, 0.59753123459010704为它们之间的相关系数。
- [('600815+601177', 0.59753123459010704), ('601100+601106', 0.60006268751560954), ('601106+601177', 0.66441434941650324), ('600815+601100', 0.6792572923561927), ('600815+601106', 0.76303679456471019)]
以上为策略研究部分的第二部分代码。我们从股票池中选取两只股票,计算它们的回报然后算出它们之间的相关系数,最后取相关系数最高的五对股票来进行下一步的协整检验。
- for i in range(len(potentialPair)):
- m = str(potentialPair[i][0])
- n = str(potentialPair[i][1])
- price_of_1 = ts.get_hist_data(m, start='2012-01-01', end='2013-01-01')
- price_of_2 = ts.get_hist_data(n, start='2012-01-01', end='2013-01-01')
- closeprice_of_1 = price_of_1['close']
- closeprice_of_2 = price_of_2['close']
- if len(closeprice_of_1) != 0 and len(closeprice_of_2) != 0:
- model = pd.ols(y=closeprice_of_2, x=closeprice_of_1, intercept=True) # perform ols on these two stocks
- spread = closeprice_of_2 - closeprice_of_1*model.beta['x']
- spread = spread.dropna()
- sta = sts.adfuller(spread, 1)
- pair = m + '+' + n
- Rank[pair] = sta[0]
- rank2 = sorted(Rank.items(), key=operator.itemgetter(1))
以上为策略研究部分的第三部分代码。我们对选取出来的相关系数高的股票对进行协整检验,即检验它们的价差是否为稳定序列。 比如的对于股票对600815和601002,我们进行Augumented Dickey-Fuller test 得到结果如下
- (-3.34830942527566, 0.0128523914172048, 0, 115, {'5%': -2.8870195216569412, '1%': -3.4885349695076844, '10%': -2.5803597920604915}, -11.392077815567461)
现在来解释一下几个比较重要的结果。第一个值-3.34830942527566为T-统计量,第二个值0.0128523914172048为p-value。字典里面包含的内容为置信度为5%,1%和10%时的T-统计量的值。比如对于我们所选择的股票对600815和601002, T-统计量为-3.34830942527566,小于5%所对应的-2.8870195216569412,那么很大可能我们发现了一个平稳的时间序列。
通过以上策略研究部分,我们发现最适合做配对交易的股票对为厦工股份(600815), 晋亿实业(601002).接下来我们用RiceQuant量化交易平台来执行我们的策略,回测时间为2014年全年,初始资金为100000.0。在计算价差时,我们对价差时间序列进行了归一化处理,处理后的价差用zScore来表示,具体计算方式如下:
zScore=spread−spreadmeanspreadvariance
其他【量化策略帖子】:【内部数据】超级干净准确的历史股票数据
有人听说过【Jython】吗?Python&Java在Ricequant的完美结合
【量化投资】不懂量化策略就不要炒股了!看了量化这个你就懂了!
【原创】【量化策略】海龟交易体系的小白构建(一)之交易法则
【原创】【量化策略】海龟交易体系的小白构建(二)之交易实现
【原创】【量化策略】海龟交易体系的小白构建(三)之完全体系构建
【量化策略】【原创】如何使用策略研究在股灾中扭亏为盈,净赚500w(一)
【量化策略】【原创】如何使用策略研究在股灾中扭亏为盈,净赚500w(二) [推广有奖]


雷达卡








京公网安备 11010802022788号







