PDF下载 下载

alpha对冲(股票+期货)

阅读 28457
  1. # coding=utf-8
  2. from __future__ import print_function, absolute_import, unicode_literals
  3. from gm.api import *
  4. '''
  5. 本策略每隔1个月定时触发计算SHSE.000300成份股的过去一天EV/EBITDA值并选取30只EV/EBITDA值最小且大于零的股票
  6. 对不在股票池的股票平仓并等权配置股票池的标的
  7. 并用相应的CFFEX.IF对应的真实合约等额对冲
  8. 回测数据为:SHSE.000300和他们的成份股和CFFEX.IF对应的真实合约
  9. 回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00
  10. 注意:本策略仅供参考,实际使用中要考虑到期货和股票处于两个不同的账户,需要人为的保证两个账户的资金相同。
  11. '''
  12. def init(context):
  13. # 每月第一个交易日09:40:00的定时执行algo任务
  14. schedule(schedule_func=algo, date_rule='1m', time_rule='09:40:00')
  15. # 设置开仓在股票和期货的资金百分比(期货在后面自动进行杠杆相关的调整)
  16. context.percentage_stock = 0.4
  17. context.percentage_futures = 0.4
  18. def algo(context):
  19. # 获取当前时刻
  20. now = context.now
  21. # 获取上一个交易日
  22. last_day = get_previous_trading_date(exchange='SHSE', date=now)
  23. # 获取沪深300成份股
  24. stock300 = get_history_constituents(index='SHSE.000300', start_date=last_day,
  25. end_date=last_day)[0]['constituents'].keys()
  26. # 获取上一个工作日的CFFEX.IF对应的合约
  27. index_futures = get_continuous_contracts(csymbol='CFFEX.IF', start_date=last_day, end_date=last_day)[-1]['symbol']
  28. # 获取当天有交易的股票
  29. not_suspended_info = get_history_instruments(symbols=stock300, start_date=now, end_date=now)
  30. not_suspended_symbols = [item['symbol'] for item in not_suspended_info if not item['is_suspended']]
  31. # 获取成份股EV/EBITDA大于0并为最小的30个
  32. fin = get_fundamentals(table='trading_derivative_indicator', symbols=not_suspended_symbols,
  33. start_date=now, end_date=now, fields='EVEBITDA',
  34. filter='EVEBITDA>0', order_by='EVEBITDA', limit=30, df=True)
  35. fin.index = fin.symbol
  36. # 获取当前仓位
  37. positions = context.account().positions()
  38. # 平不在标的池或不为当前股指期货主力合约对应真实合约的标的
  39. for position in positions:
  40. symbol = position['symbol']
  41. sec_type = get_instrumentinfos(symbols=symbol)[0]['sec_type']
  42. # 若类型为期货且不在标的池则平仓
  43. if sec_type == SEC_TYPE_FUTURE and symbol != index_futures:
  44. order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market,
  45. position_side=PositionSide_Short)
  46. print('市价单平不在标的池的', symbol)
  47. elif symbol not in fin.index:
  48. order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market,
  49. position_side=PositionSide_Long)
  50. print('市价单平不在标的池的', symbol)
  51. # 获取股票的权重
  52. percent = context.percentage_stock / len(fin.index)
  53. # 买在标的池中的股票
  54. for symbol in fin.index:
  55. order_target_percent(symbol=symbol, percent=percent, order_type=OrderType_Market,
  56. position_side=PositionSide_Long)
  57. print(symbol, '以市价单调多仓到仓位', percent)
  58. # 获取股指期货的保证金比率
  59. ratio = get_history_instruments(symbols=index_futures, start_date=last_day, end_date=last_day)[0]['margin_ratio']
  60. # 更新股指期货的权重
  61. percent = context.percentage_futures * ratio
  62. # 买入股指期货对冲
  63. # 注意:股指期货的percent参数是按照期货的保证金来算比例,不是按照合约价值, 比如说0.1就是用0.1的仓位的资金全部买入期货。
  64. order_target_percent(symbol=index_futures, percent=percent, order_type=OrderType_Market,
  65. position_side=PositionSide_Short)
  66. print(index_futures, '以市价单调空仓到仓位', percent)
  67. if __name__ == '__main__':
  68. '''
  69. strategy_id策略ID,由系统生成
  70. filename文件名,请与本文件名保持一致
  71. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
  72. token绑定计算机的ID,可在系统设置-密钥管理中生成
  73. backtest_start_time回测开始时间
  74. backtest_end_time回测结束时间
  75. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
  76. backtest_initial_cash回测初始资金
  77. backtest_commission_ratio回测佣金比例
  78. backtest_slippage_ratio回测滑点比例
  79. '''
  80. run(strategy_id='strategy_id',
  81. filename='main.py',
  82. mode=MODE_BACKTEST,
  83. token='token_id',
  84. backtest_start_time='2017-07-01 08:00:00',
  85. backtest_end_time='2017-10-01 16:00:00',
  86. backtest_adjust=ADJUST_PREV,
  87. backtest_initial_cash=10000000,
  88. backtest_commission_ratio=0.0001,
  89. backtest_slippage_ratio=0.0001)

0 篇笔记