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

    行业轮动(股票)

    原文: https://www.myquant.cn/docs/python_strategyies/111