{"id":19966688,"url":"https://github.com/truthhun/industry-rotation","last_synced_at":"2025-10-07T14:33:52.883Z","repository":{"id":88501700,"uuid":"119641823","full_name":"TruthHun/industry-rotation","owner":"TruthHun","description":"行业轮动(股票)，基于沪深300的行业指数的行业轮动策略","archived":false,"fork":false,"pushed_at":"2018-01-31T06:02:31.000Z","size":69,"stargazers_count":15,"open_issues_count":0,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-04T00:32:09.735Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.myquant.cn/docs/python_strategyies/111","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TruthHun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-01-31T05:57:10.000Z","updated_at":"2024-06-01T10:14:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"1367aa01-dc70-45fb-a9c0-94c8075ff967","html_url":"https://github.com/TruthHun/industry-rotation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/TruthHun/industry-rotation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Findustry-rotation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Findustry-rotation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Findustry-rotation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Findustry-rotation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TruthHun","download_url":"https://codeload.github.com/TruthHun/industry-rotation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2Findustry-rotation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272116857,"owners_count":24876268,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-25T02:00:12.092Z","response_time":1107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T02:37:42.730Z","updated_at":"2025-10-07T14:33:52.783Z","avatar_url":"https://github.com/TruthHun.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 行业轮动(股票)\n基于沪深300的行业指数的行业轮动策略\n\n## 源码\n```python\n# coding=utf-8\nfrom __future__ import print_function, absolute_import, unicode_literals\nimport numpy as np\nfrom gm.api import *\n\n'''\n本策略每隔1个月定时触发计算SHSE.000910.SHSE.000909.SHSE.000911.SHSE.000912.SHSE.000913.SHSE.000914\n(300工业.300材料.300可选.300消费.300医药.300金融)这几个行业指数过去\n20个交易日的收益率,随后选取了收益率最高的指数的成份股中流通市值最大的5只股票\n对不在股票池的股票平仓并等权配置股票池的标的\n回测数据为:SHSE.000910.SHSE.000909.SHSE.000911.SHSE.000912.SHSE.000913.SHSE.000914和他们的成份股\n回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00\n'''\n\n\ndef init(context):\n    # 每月第一个交易日的09:40 定时执行algo任务\n    schedule(schedule_func=algo, date_rule='1m', time_rule='09:40:00')\n    # 用于筛选的行业指数\n    context.index = ['SHSE.000910', 'SHSE.000909', 'SHSE.000911', 'SHSE.000912', 'SHSE.000913', 'SHSE.000914']\n    # 用于统计数据的天数\n    context.count = 20\n    # 最大下单资金比例\n    context.ratio = 0.8\n\n\ndef algo(context):\n    # 获取当天的日期\n    today = context.now\n    # 获取上一个交易日\n    last_day = get_previous_trading_date(exchange='SHSE', date=today)\n    return_index = []\n    # 获取并计算行业指数收益率\n\n    for i in context.index:\n        return_index_his = history_n(symbol=i, frequency='1d', count=context.count, fields='close,bob',\n                                     fill_missing='Last', adjust=ADJUST_PREV, end_time=last_day, df=True)\n        return_index_his = return_index_his['close'].values\n        return_index.append(return_index_his[-1] / return_index_his[0] - 1)\n    # 获取指定数内收益率表现最好的行业\n    sector = context.index[np.argmax(return_index)]\n    print('最佳行业指数是: ', sector)\n    # 获取最佳行业指数成份股\n    symbols = get_history_constituents(index=sector, start_date=last_day, end_date=last_day)[0]['constituents'].keys()\n    # 获取当天有交易的股票\n    not_suspended_info = get_history_instruments(symbols=symbols, start_date=today, end_date=today)\n    not_suspended_symbols = [item['symbol'] for item in not_suspended_info if not item['is_suspended']]\n\n    # 获取最佳行业指数成份股的市值，从大到小排序并选取市值最大的5只股票\n    fin = get_fundamentals(table='trading_derivative_indicator', symbols=not_suspended_symbols, start_date=last_day,\n                           end_date=last_day, limit=5, fields='NEGOTIABLEMV', order_by='-NEGOTIABLEMV', df=True)\n    fin.index = fin['symbol']\n    # 计算权重\n    percent = 1.0 / len(fin.index) * context.ratio\n    # 获取当前所有仓位\n    positions = context.account().positions()\n    # 如标的池有仓位,平不在标的池的仓位\n    for position in positions:\n        symbol = position['symbol']\n        if symbol not in fin.index:\n            order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market,\n                                 position_side=PositionSide_Long)\n            print('市价单平不在标的池的', symbol)\n    # 对标的池进行操作\n    for symbol in fin.index:\n        order_target_percent(symbol=symbol, percent=percent, order_type=OrderType_Market,\n                             position_side=PositionSide_Long)\n        print(symbol, '以市价单调整至仓位', percent)\n\n\nif __name__ == '__main__':\n    '''\n    strategy_id策略ID,由系统生成\n    filename文件名,请与本文件名保持一致\n    mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST\n    token绑定计算机的ID,可在系统设置-密钥管理中生成\n    backtest_start_time回测开始时间\n    backtest_end_time回测结束时间\n    backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST\n    backtest_initial_cash回测初始资金\n    backtest_commission_ratio回测佣金比例\n    backtest_slippage_ratio回测滑点比例\n    '''\n    run(strategy_id='strategy_id',\n        filename='main.py',\n        mode=MODE_BACKTEST,\n        token='token_id',\n        backtest_start_time='2017-07-01 08:00:00',\n        backtest_end_time='2017-10-01 16:00:00',\n        backtest_adjust=ADJUST_PREV,\n        backtest_initial_cash=10000000,\n        backtest_commission_ratio=0.0001,\n        backtest_slippage_ratio=0.0001)\n```\n\n## 绩效\n![绩效](attach.png)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruthhun%2Findustry-rotation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftruthhun%2Findustry-rotation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruthhun%2Findustry-rotation/lists"}