【统计套利】配对交易

来源:https://uqer.io/community/share/559c85baf9f06cb5614f190d

策略思路

寻找走势相关且股价相近的一对股票,根据其价格变动买卖

策略实现

  • 首先,历史前五日的Pearson相关系数若大于给定的阈值
  • 如果两只股票走势趋同,则按上涨(下跌)趋势买入(卖出)股票
  • 如果两只股票走势背离,则买入下跌股票,卖出上涨股票 首先需要在A股中寻找走势相关性很大的股票,这是一项很繁复的工作。为简单起见,这里直接使用了一个现成的结果:000159和000967在2012年的走势十分相似,这一点可以通过复权收盘价曲线来验证
  1. <matplotlib.axes.AxesSubplot at 0x6415790>

【统计套利】配对交易 - 图1

接下来我们对2013年全年的这两只股票进行配对交易策略的回测:

  1. from scipy.stats.stats import pearsonr
  2. start = '2013-01-01'
  3. end = '2014-01-01'
  4. benchmark = 'HS300'
  5. universe = ['000159.XSHE', '000967.XSHE']
  6. capital_base = 1000000
  7. def initialize(account):
  8. account.cutoff = 0.9
  9. account.prev_prc1 = 0
  10. account.prev_prc2 = 0
  11. account.prev_prcb = 0
  12. def handle_data(account):
  13. if len(account.universe) < 2: return
  14. clsp = account.get_attribute_history('closePrice', 5)
  15. stk1, stk2 = universe
  16. px1, px2 = clsp[stk1], clsp[stk2]
  17. prc1, prc2 = px1[-1], px2[-1]
  18. prcb = account.get_symbol_history('benchmark', 1)['return'][0]
  19. if account.prev_prc1 == 0:
  20. account.prev_prc1 = prc1
  21. account.prev_prc2 = prc2
  22. account.prev_prcb = prcb
  23. return
  24. corval, pval = pearsonr(px1, px2)
  25. if abs(corval) < account.cutoff:
  26. return
  27. mov1, mov2 = adj(prc1, prc2, prcb, account.prev_prc1, account.prev_prc2, account.prev_prcb)
  28. amount = 100000 / prc2
  29. if mov1 > 0:
  30. order(stk2, amount)
  31. elif mov1 < 0:
  32. if account.valid_secpos.get(stk2, 0) > amount:
  33. order(stk2, -amount)
  34. else:
  35. order_to(stk2, 0)
  36. amount = 100000 / prc1
  37. if mov2 > 0:
  38. order(stk1, amount)
  39. elif mov2 < 0:
  40. if account.valid_secpos.get(stk1, 0) > amount:
  41. order(stk1, -amount)
  42. else:
  43. order_to(stk1, 0)
  44. account.prev_prc1 = prc1
  45. account.prev_prc2 = prc2
  46. account.prev_prcb = prcb
  47. def adj(x, y, base, prev_x, prev_y, prev_base):
  48. dhs = base / prev_base - 1
  49. dx = x / prev_x - 1 - dhs
  50. dy = y / prev_y - 1 - dhs
  51. return dx, dy

【统计套利】配对交易 - 图2

  1. bt
tradeDatecashsecurity_positionportfolio_valuebenchmark_returnblotter
02013-01-111000000.000000{}1000000.000000-0.018703[]
12013-01-14802627.092268{u'000159.XSHE': 16139, u'000967.XSHE': 21500}1008718.2982680.038051[Order(order_time: 2013-01-14, symbol: 000967….
22013-01-15802627.092268{u'000159.XSHE': 16139, u'000967.XSHE': 21500}1013493.7622680.007033[]
32013-01-16802627.092268{u'000159.XSHE': 16139, u'000967.XSHE': 21500}1013887.243268-0.007227[]
42013-01-17802627.092268{u'000159.XSHE': 16139, u'000967.XSHE': 21500}1017193.274268-0.009445[]
52013-01-18601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1019875.3341720.016719[Order(order_time: 2013-01-18, symbol: 000967….
62013-01-21601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1024320.3331720.005957[]
72013-01-22601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1022956.509172-0.005362[]
82013-01-23601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1020170.5131720.004066[]
92013-01-24601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1010615.301172-0.009473[]
102013-01-25601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1007214.174172-0.004290[]
112013-01-28601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1015317.8461720.031182[]
122013-01-29601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1032167.4661720.009050[]
132013-01-30601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1037040.1991720.004802[]
142013-01-31601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1037199.250172-0.000681[]
152013-02-01601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1044424.1731720.021006[]
162013-02-04601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1053929.9511720.001713[]
172013-02-05601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1042352.8101720.008606[]
182013-02-06601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1036748.2351720.001505[]
192013-02-07601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1035219.288172-0.005753[]
202013-02-08601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1038390.0291720.004294[]
212013-02-18601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1040366.776172-0.012357[]
222013-02-19601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1034837.773172-0.018948[]
232013-02-20601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1041832.3101720.006341[]
242013-02-21601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1030863.855172-0.034074[]
252013-02-22601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1031881.589172-0.005340[]
262013-02-25601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1039886.4081720.003220[]
272013-02-26601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1027334.880172-0.014346[]
282013-02-27601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1036669.4631720.010547[]
292013-02-28601751.321172{u'000159.XSHE': 31203, u'000967.XSHE': 41512}1049123.5181720.030312[]
2032013-11-204.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1213819.9010660.005261[]
2042013-11-214.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1207170.725066-0.006132[]
2052013-11-224.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1191941.099066-0.004988[]
2062013-11-254.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1185756.570066-0.003895[]
2072013-11-264.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1202201.685066-0.000507[]
2082013-11-274.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1247942.1070660.011339[]
2092013-11-284.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1254278.9370660.010375[]
2102013-11-294.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1254572.047066-0.000242[Order(order_time: 2013-11-29, symbol: 000967….
2112013-12-024.001066{u'000159.XSHE': 58955.0, u'000967.XSHE': 1104…1162773.529066-0.008266[Order(order_time: 2013-12-02, symbol: 000967….
2122013-12-03205057.003206{u'000159.XSHE': 46102.0, u'000967.XSHE': 9480…1203266.6192060.009922[Order(order_time: 2013-12-03, symbol: 000967….
2132013-12-047547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1223800.7966360.013243[Order(order_time: 2013-12-04, symbol: 000967….
2142013-12-057547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1211481.369636-0.002804[]
2152013-12-067547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1205769.229636-0.006446[]
2162013-12-097547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1212153.686636-0.000575[]
2172013-12-107547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1216462.6196360.001000[]
2182013-12-117547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1191498.173636-0.016533[]
2192013-12-127547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1187601.796636-0.001140[]
2202013-12-137547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1180581.953636-0.001402[]
2212013-12-167547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1160961.657636-0.016085[]
2222013-12-177547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1172930.532636-0.004878[]
2232013-12-187547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1175604.1346360.000361[]
2242013-12-197547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1176775.594636-0.010525[]
2252013-12-207547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1132028.254636-0.023272[]
2262013-12-237547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1147914.7716360.002840[]
2272013-12-247547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1159575.8886360.001593[]
2282013-12-257547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1178451.2876360.007372[]
2292013-12-267547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1158503.363636-0.017257[]
2302013-12-277547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1166499.8506360.016836[]
2312013-12-307547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1158181.598636-0.001745[]
2322013-12-317547.120636{u'000159.XSHE': 58573.0, u'000967.XSHE': 1098…1162504.5386360.013294[]
  1. 233 rows × 6 columns

根据bt的表现,在仓位控制和组合管理方面应该还有着不少进一步优化的空间