• QQ空间
  • 回复
  • 收藏

【宽客策略源码】alpha对冲(股票+期货)

geek168 数据策略 2019-8-11 15:14 93604人围观

本策略是以沪深300为基准标的,通过期货和现货进行对冲,期货以沪深300股指期货为投资标的,现货沪深300成分股作为股票池,进行定数触发和调仓,以获取收益的同时控制风险。

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

上涨-黄金.jpg

路过

雷人

握手

鲜花

鸡蛋
原作者: abctrader
关注微信