2.2 期现套利 • 通过股指期货的期现差与 ETF 对冲套利

来源:https://uqer.io/community/share/55aa5aa8f9f06c56de1b53bb

通过股指期货(IF1507)的期现差会围绕沪深300指数上下波动的原理,当期现差扩大到一定程度后,使用股指期货和ETF向对冲,以套取期现差的稳定利润。

只通过IF1507验证了一个基本可能性。还有很多细节需要完善。

还有,不知道如何通过order方法卖空股指期货和etf,了解的大神可以帮我解决一下~~

  1. import pandas as pd
  2. start = datetime(2015, 6, 1) # 回测起始时间
  3. end = datetime(2015, 7, 17) # 回测结束时间
  4. benchmark = 'HS300' # 策略参考标准
  5. universe = ['510300.XSHG','IF1507.CCFX'] # 股票池
  6. capital_base = 1000000 # 起始资金
  7. maxQxc = 0.0 #最大期现差
  8. isOrder = False #是否已经买入对冲
  9. direction = False # 买入方向
  10. buyPosition = 0 # 买入点位
  11. total = 0
  12. def initialize(account): # 初始化虚拟账户状态
  13. pass
  14. def handle_data(account): # 每个交易日的买入卖出指令
  15. # log.debug(account.current_date)
  16. # lowToNow = DataAPI.MktEqudAdjGet(secID = '510300', field = ['closePrice'],beginDate = lowDate, endDate = nowDate)
  17. global maxQxc,isOrder,direction,buyPosition,total
  18. # 获取股指期货的行情数据
  19. if1507 = DataAPI.MktFutdGet(ticker="IF1507",
  20. beginDate=account.current_date.strftime("%Y%m%d"),endDate=account.current_date.strftime("%Y%m%d"))
  21. # 获取etf基金的行情数据
  22. etf300 = DataAPI.MktFunddGet(ticker = '510300',
  23. beginDate=account.current_date.strftime("%Y%m%d"),endDate=account.current_date.strftime("%Y%m%d"))
  24. # 获取沪深300的行情数据
  25. hs300 = DataAPI.MktIdxdGet(ticker="000300",
  26. beginDate=account.current_date.strftime("%Y%m%d"),endDate=account.current_date.strftime("%Y%m%d"))
  27. # 计算期现差
  28. qxc = if1507['closePrice'].iloc[0] - hs300['closeIndex'].iloc[0]
  29. # log.debug((hs300['indexID']) + "/" + (if1507['secID']) + "/" + (etf300['secID']))
  30. # log.debug(str(qxc) + "/" + str(hs300['closeIndex'].iloc[0]) + "/" + str(if1507['closePrice'].iloc[0]) + "/" + str(etf300['closePrice'].iloc[0]))
  31. # 保存期现差最大值,为后面判断买点时用
  32. if(abs(qxc) > abs(maxQxc)):
  33. maxQxc = qxc
  34. # 判断期现差绝对值大于100,并开始从高点回落,同时账户是空仓,就买入
  35. if(abs(maxQxc) > 100 and abs(qxc) < abs(maxQxc) and not isOrder ):
  36. direction = qxc > 0
  37. buyPosition = qxc
  38. isOrder = True
  39. # if direction:
  40. # 正期现差时
  41. # order(account.universe[1],-1) # 卖空一手期指
  42. # order(account.universe[0],3000) # 买入30万基金
  43. # else:
  44. # 负期现差时
  45. # order(account.universe[1],1) # 卖空一手期指
  46. # order(account.universe[0],-3000) # 买入30万基金
  47. log.debug("买入点位 = "+ str(if1507['closePrice'].iloc[0]) + ",期现差 = " + str(qxc))
  48. # 判断已经持仓,并期现差已经反转,就卖出
  49. if(isOrder):
  50. if(direction):
  51. if(qxc < 0):
  52. earnings = buyPosition - qxc
  53. isOrder = False
  54. maxQxc = 0
  55. # order(account.universe[1],1) # 卖空一手期指
  56. # order(account.universe[0],-3000) # 买入30万基金
  57. account.cash = account.cash + abs(earnings)* 300
  58. log.debug("卖出盈利 = " + str(abs(earnings)* 300))
  59. else:
  60. if(qxc > 0):
  61. earnings = buyPosition - qxc
  62. isOrder = False
  63. maxQxc = 0
  64. # order(account.universe[1],-1) # 卖空一手期指
  65. # order(account.universe[0],3000) # 买入30万基金
  66. account.cash = account.cash + abs(earnings)* 300
  67. log.debug("卖出盈利 = " + str(abs(earnings) * 300 ) + ",卖出时期现差 = " + str(qxc))
  68. # for stock in account.universe:
  69. # log.debug(stock)
  70. # log.debug(account.cash)
  71. return

2.2 期现套利 • 通过股指期货的期现差与 ETF 对冲套利 - 图1

  1. 2015-06-01 [DEBUG] 1000000.0
  2. 2015-06-02 [DEBUG] 买入点位 = 5269.6,期现差 = 107.73
  3. 2015-06-02 [DEBUG] 1000000.0
  4. 2015-06-03 [DEBUG] 1000000.0
  5. 2015-06-04 [DEBUG] 1000000.0
  6. 2015-06-05 [DEBUG] 1000000.0
  7. 2015-06-08 [DEBUG] 卖出盈利 = 38004.0
  8. 2015-06-08 [DEBUG] 1038004.0
  9. 2015-06-09 [DEBUG] 1000000.0
  10. 2015-06-10 [DEBUG] 1000000.0
  11. 2015-06-11 [DEBUG] 1000000.0
  12. 2015-06-12 [DEBUG] 1000000.0
  13. 2015-06-15 [DEBUG] 1000000.0
  14. 2015-06-16 [DEBUG] 1000000.0
  15. 2015-06-17 [DEBUG] 1000000.0
  16. 2015-06-18 [DEBUG] 1000000.0
  17. 2015-06-19 [DEBUG] 1000000.0
  18. 2015-06-23 [DEBUG] 1000000.0
  19. 2015-06-24 [DEBUG] 1000000.0
  20. 2015-06-25 [DEBUG] 1000000.0
  21. 2015-06-26 [DEBUG] 1000000.0
  22. 2015-06-29 [DEBUG] 1000000.0
  23. 2015-06-30 [DEBUG] 买入点位 = 4381.4,期现差 = -91.598
  24. 2015-06-30 [DEBUG] 1000000.0
  25. 2015-07-01 [DEBUG] 1000000.0
  26. 2015-07-02 [DEBUG] 卖出盈利 = 34080.6,卖出时期现差 = 22.004
  27. 2015-07-02 [DEBUG] 1034080.6
  28. 2015-07-03 [DEBUG] 1000000.0
  29. 2015-07-06 [DEBUG] 1000000.0
  30. 2015-07-07 [DEBUG] 1000000.0
  31. 2015-07-08 [DEBUG] 买入点位 = 3463.4,期现差 = -199.638
  32. 2015-07-08 [DEBUG] 1000000.0
  33. 2015-07-09 [DEBUG] 1000000.0
  34. 2015-07-10 [DEBUG] 卖出盈利 = 77904.6,卖出时期现差 = 60.044
  35. 2015-07-10 [DEBUG] 1077904.6
  36. 2015-07-13 [DEBUG] 1000000.0
  37. 2015-07-14 [DEBUG] 买入点位 = 4001.6,期现差 = -110.549
  38. 2015-07-14 [DEBUG] 1000000.0
  39. 2015-07-15 [DEBUG] 1000000.0
  40. 2015-07-16 [DEBUG] 卖出盈利 = 36357.9,卖出时期现差 = 10.644
  41. 2015-07-16 [DEBUG] 1036357.9
  42. 2015-07-17 [DEBUG] 1000000.0