• QQ空间
  • 回复
  • 收藏

【宽客策略源码】行业轮动(股票)

geek168 数据策略 2019-8-11 16:06 97426人围观

本策略每隔1个月定时触发计算SHSE.000910.SHSE.000909.SHSE.000911.SHSE.000912.SHSE.000913.SHSE.000914
(300工业.300材料.300可选.300消费.300医药.300金融)这几个行业指数过去
20个交易日的收益率并选取了收益率最高的指数的成份股获取并获取了他们的市值数据
随后把仓位调整至市值最大的5只股票上





回测数据为:SHSE.000910.SHSE.000909.SHSE.000911.SHSE.000912.SHSE.000913.SHSE.000914和他们的成份股
回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00



  1. # coding=utf-8
  2. from __future__ import print_function, absolute_import, unicode_literals
  3. import numpy as np
  4. from gm.api import *

  5. def init(context):
  6.     # 每月第一个交易日的09:40 定时执行algo任务
  7.     schedule(schedule_func=algo, date_rule='1m', time_rule='09:40:00')
  8.     # 用于筛选的行业指数
  9.     context.index = ['SHSE.000910', 'SHSE.000909', 'SHSE.000911', 'SHSE.000912', 'SHSE.000913', 'SHSE.000914']
  10.     # 用于统计数据的天数
  11.     context.date = 20
  12.     # 最大下单资金比例
  13.     context.ratio = 0.8
  14. def algo(context):
  15.     # 获取当天的日期
  16.     today = context.now
  17.     # 获取上一个交易日
  18.     last_day = get_previous_trading_date(exchange='SHSE', date=today)
  19.     return_index = []
  20.     # 获取并计算行业指数收益率
  21.     for i in context.index:
  22.         return_index_his = history_n(symbol=i, frequency='1d', count=context.date, fields='close,bob',
  23.                                      fill_missing='Last', adjust=ADJUST_PREV, end_time=last_day, df=True)
  24.         return_index_his = return_index_his['close'].values
  25.         return_index.append(return_index_his[-1] / return_index_his[0] - 1)
  26.     # 获取指定数内收益率表现最好的行业
  27.     sector = context.index[np.argmax(return_index)]
  28.     print('最佳行业指数是: ', sector)
  29.     # 获取最佳行业指数成份股
  30.     symbols = get_history_constituents(index=sector, start_date=last_day, end_date=last_day)[0]['constituents'].keys()
  31.     # 获取当天有交易的股票
  32.     not_suspended_info = get_history_instruments(symbols=symbols, start_date=today, end_date=today)
  33.     not_suspended_symbols = [item['symbol'] for item in not_suspended_info if not item['is_suspended']]
  34.     # 获取最佳行业指数成份股的市值,从大到小排序并选取市值最大的5只股票
  35.     fin = get_fundamentals(table='tq_sk_finindic', symbols=not_suspended_symbols, start_date=last_day,
  36.                            end_date=last_day, limit=5, fields='NEGOTIABLEMV', order_by='-NEGOTIABLEMV', df=True)
  37.     fin.index = fin['symbol']
  38.     # 计算权重
  39.     percent = 1.0 / len(fin.index) * context.ratio
  40.     # 获取当前所有仓位
  41.     positions = context.account().positions()
  42.     # 如标的池有仓位,平不在标的池的仓位
  43.     for position in positions:
  44.         symbol = position['symbol']
  45.         if symbol not in fin.index:
  46.             order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market,
  47.                                  position_side=PositionSide_Long)
  48.             print('市价单平不在标的池的', symbol)
  49.     # 对标的池进行操作
  50.     for symbol in fin.index:
  51.         order_target_percent(symbol=symbol, percent=percent, order_type=OrderType_Market,
  52.                              position_side=PositionSide_Long)
  53.         print(symbol, '以市价单调整至仓位', percent)
  54. if __name__ == '__main__':
  55.     '''
  56.     strategy_id策略ID,由系统生成
  57.     filename文件名,请与本文件名保持一致
  58.     mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
  59.     token绑定计算机的ID,可在系统设置-密钥管理中生成
  60.     backtest_start_time回测开始时间
  61.     backtest_end_time回测结束时间
  62.     backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
  63.     backtest_initial_cash回测初始资金
  64.     backtest_commission_ratio回测佣金比例
  65.     backtest_slippage_ratio回测滑点比例
  66.     '''
  67.     run(strategy_id='strategy_id',
  68.         filename='main.py',
  69.         mode=MODE_BACKTEST,
  70.         token='token_id',
  71.         backtest_start_time='2017-07-01 08:00:00',
  72.         backtest_end_time='2017-10-01 16:00:00',
  73.         backtest_adjust=ADJUST_PREV,
  74.         backtest_initial_cash=10000000,
  75.         backtest_commission_ratio=0.0001,
  76.         backtest_slippage_ratio=0.0001)
复制代码


行业轮动.png


路过

雷人

握手

鲜花

鸡蛋
原作者: abctrader
关注微信