复制链接
克隆策略

行业轮动策略

版本 v1.0

目录

行业轮动策略的交易规则

策略构建步骤

策略的实现

正文

一、行业轮动策略的交易规则

  • 选出动量得分前3的行业;
  • 选出每个行业myrank因子排名前10的股票;
  • 买入股票持仓20日开始换仓。

二、策略构建步骤

1、确定股票池和回测时间

  • 通过证券代码列表输入回测的起止日期

2、确定买卖条件信号

  • 在输入特征列表中通过表达式引擎定义myrank=rank_fs_roe_ttm_0+rank_fs_net_profit_qoq_0-rank_pb_lf_0这个因子 。
  • 计算出每个行业42,84,126天动量值,并按0.4,0.3,0.3的权重求出加权动量值,选出加权动量值排名前3的行业。
  • 在每个行业选出myrank值排名前10的股票。

3、确定买卖原则

  • 选出的30支股票为买入股票列表,以20个交易日开始换仓。
  • 已有持仓中满足卖出条件的股票为卖出股票列表,需执行卖出操作
  • 满足买入条件且没有持仓的股票为买入股票列表,需执行买入操作
  • 满足买入条件且已有持仓的股票为调仓股票列表,需执行调整仓位操作
  • 本策略中将买入股票列表和调仓股票列表中的所有股票统一调整为等资金比例仓位。

4、回测

  • 通过 trade 模块中的初始化函数定义交易手续费和滑点;
  • 通过 trade 模块中的主函数(handle函数)查看每日的买卖交易信号,按照买卖原则执行相应的买入/卖出/调仓操作。

三、策略的实现

可视化策略实现如下:

    {"description":"实验创建于3/23/2018","graph":{"edges":[{"to_node_id":"-298:options_data","from_node_id":"-440:data_1"},{"to_node_id":"-895:input_data","from_node_id":"-586:data"},{"to_node_id":"-586:features","from_node_id":"-592:data"},{"to_node_id":"-895:features","from_node_id":"-592:data"},{"to_node_id":"-586:instruments","from_node_id":"-596:data"},{"to_node_id":"-298:instruments","from_node_id":"-596:data"},{"to_node_id":"-1057:input_data","from_node_id":"-895:data"},{"to_node_id":"-440:input_1","from_node_id":"-1057:data"}],"nodes":[{"node_id":"-298","module_id":"BigQuantSpace.trade.trade-v4","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 手续费设置\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) \n context.mydata = context.options[\"data\"].read()","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按月调仓\n if context.trading_day_index % 20 != 0:\n return \n \n date = data.current_dt.strftime('%Y-%m-%d') # 日期\n # 整理出当天要买入的股票\n stock_to_buy = context.mydata[context.mydata.date==date][0].tolist()[0]\n \n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions]\n # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有\n no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]\n # 需要卖出的股票\n stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]\n \n # 卖出\n for stock in stock_to_sell:\n # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式\n if data.can_trade(context.symbol(stock)):\n # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,\n # 即卖出全部股票,可参考回测文档\n context.order_target_percent(context.symbol(stock), 0)\n \n # 如果当天没有买入的股票,就返回\n if len(stock_to_buy) == 0:\n return\n\n # 等权重买入 \n weight = 1 / len(stock_to_buy)\n \n # 买入\n for stock in stock_to_buy:\n if data.can_trade(context.symbol(stock)):\n # 下单使得某只股票的持仓权重达到weight,因为weight大于0,因此是等权重买入\n context.order_target_percent(context.symbol(stock), weight)","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":0.025,"type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"open","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"close","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":"10000000","type":"Literal","bound_global_parameter":null},{"name":"auto_cancel_non_tradable_orders","value":"True","type":"Literal","bound_global_parameter":null},{"name":"data_frequency","value":"daily","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"真实价格","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"股票","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-298"},{"name":"options_data","node_id":"-298"},{"name":"history_ds","node_id":"-298"},{"name":"benchmark_ds","node_id":"-298"},{"name":"trading_calendar","node_id":"-298"}],"output_ports":[{"name":"raw_perf","node_id":"-298"}],"cacheable":false,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-440","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n # 计算行业行业收益\n industry_df = DataSource(\"basic_info_index_CN_STOCK_A\").read()\n\n SW_industry_list = industry_df[industry_df.instrument.str.startswith('SW')].instrument.tolist()\n\n SW_industry_df = DataSource(\"bar1d_index_CN_STOCK_A\").read(SW_industry_list)\n \n SW_industry_ret = SW_industry_df.groupby('instrument').apply(calcu_ret)\n \n SW_industry_ret.reset_index(inplace=True, drop=True)\n SW_industry_ret['date'] = SW_industry_ret['date'].apply(lambda x:x.strftime('%Y-%m-%d'))\n \n daily_buy_industry_dict = {dt:seek_head_industry(SW_industry_ret.set_index('date').loc[dt]) for dt in list(set(SW_industry_ret.date))}\n \n \n daily_stock = input_1.read()\n daily_stock['industry_sw_level1_0'] = daily_stock['industry_sw_level1_0'].apply(lambda x:'SW'+str(x)+'.HIX')\n \n daily_stock = daily_stock.groupby(['date', 'industry_sw_level1_0']).apply(seek_head_stock).reset_index()\n \n buy_df = pd.DataFrame()\n for k,v in daily_buy_industry_dict.items():\n buy_df = buy_df.append(daily_stock[(daily_stock.date==k)&(daily_stock.industry_sw_level1_0.apply(lambda x: x in v))])\n \n buy_df = buy_df.groupby('date').apply(lambda x:x[0].values.tolist()).reset_index()\n buy_df[0] = buy_df[0].apply(lambda x:x[0]+x[1]+x[2])\n \n \n data_1 = DataSource.write_df(buy_df)\n \n \n return Outputs(data_1=data_1, data_2=None, data_3=None)\n\n# 计算不同周期的动量\ndef calcu_ret(df):\n df = df.sort_values('date')\n for i in [42, 84, 126]: # 分别代表2月、4月、半年的动量\n df['ret_%s'%i] = df['close']/df['close'].shift(i)-1 \n return df\n\n# 计算出得分\ndef seek_head_industry(df):\n for j in ['ret_42','ret_84','ret_126']:\n df['%s'%j] = df['%s'%j].rank(ascending=True) \n df['score'] = 0.4*df['ret_42']+0.3*df['ret_84']+0.3*df['ret_126'] # 得分的权重分别为0.4、0.3、0.3\n result = df.sort_values('score', ascending=False)\n return list(result.instrument)[:3] # 前3个行业\n\n# 选出特定行业优质股票\ndef seek_head_stock(df):\n result = df.sort_values(['myrank'], ascending=False)\n return list(result.instrument[:10]) # 每个行业选10只股票","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"data_1","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-440"},{"name":"input_2","node_id":"-440"},{"name":"input_3","node_id":"-440"}],"output_ports":[{"name":"data_1","node_id":"-440"},{"name":"data_2","node_id":"-440"},{"name":"data_3","node_id":"-440"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-586","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-586"},{"name":"features","node_id":"-586"}],"output_ports":[{"name":"data","node_id":"-586"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-592","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"industry_sw_level1_0\nmyrank=rank_fs_roe_ttm_0+rank_fs_net_profit_qoq_0-rank_pb_lf_0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-592"}],"output_ports":[{"name":"data","node_id":"-592"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"-596","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2021-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-01-10","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-596"}],"output_ports":[{"name":"data","node_id":"-596"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-895","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"{}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-895"},{"name":"features","node_id":"-895"}],"output_ports":[{"name":"data","node_id":"-895"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-1057","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-1057"},{"name":"features","node_id":"-1057"}],"output_ports":[{"name":"data","node_id":"-1057"}],"cacheable":true,"seq_num":9,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-298' Position='168,359,200,200'/><node_position Node='-440' Position='412,253,200,200'/><node_position Node='-586' Position='414,80,200,200'/><node_position Node='-592' Position='565.4124755859375,-4,200,200'/><node_position Node='-596' Position='45,-25,200,200'/><node_position Node='-895' Position='410,138,200,200'/><node_position Node='-1057' Position='409,195.58750915527344,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [3]:
    # 本代码由可视化策略环境自动生成 2022年1月14日 15:48
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m2_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        # 计算行业行业收益
        industry_df = DataSource("basic_info_index_CN_STOCK_A").read()
    
        SW_industry_list = industry_df[industry_df.instrument.str.startswith('SW')].instrument.tolist()
    
        SW_industry_df = DataSource("bar1d_index_CN_STOCK_A").read(SW_industry_list)
        
        SW_industry_ret = SW_industry_df.groupby('instrument').apply(calcu_ret)
        
        SW_industry_ret.reset_index(inplace=True, drop=True)
        SW_industry_ret['date'] = SW_industry_ret['date'].apply(lambda x:x.strftime('%Y-%m-%d'))
        
        daily_buy_industry_dict = {dt:seek_head_industry(SW_industry_ret.set_index('date').loc[dt]) for dt in list(set(SW_industry_ret.date))}
        
        
        daily_stock = input_1.read()
        daily_stock['industry_sw_level1_0'] = daily_stock['industry_sw_level1_0'].apply(lambda x:'SW'+str(x)+'.HIX')
        
        daily_stock = daily_stock.groupby(['date', 'industry_sw_level1_0']).apply(seek_head_stock).reset_index()
        
        buy_df = pd.DataFrame()
        for k,v in daily_buy_industry_dict.items():
            buy_df = buy_df.append(daily_stock[(daily_stock.date==k)&(daily_stock.industry_sw_level1_0.apply(lambda x: x in v))])
        
        buy_df = buy_df.groupby('date').apply(lambda x:x[0].values.tolist()).reset_index()
        buy_df[0] = buy_df[0].apply(lambda x:x[0]+x[1]+x[2])
        
        
        data_1 = DataSource.write_df(buy_df)
        
        
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 计算不同周期的动量
    def calcu_ret(df):
        df = df.sort_values('date')
        for i in [42, 84, 126]: # 分别代表2月、4月、半年的动量
            df['ret_%s'%i] = df['close']/df['close'].shift(i)-1 
        return df
    
    # 计算出得分
    def seek_head_industry(df):
        for j in ['ret_42','ret_84','ret_126']:
            df['%s'%j] = df['%s'%j].rank(ascending=True) 
        df['score'] = 0.4*df['ret_42']+0.3*df['ret_84']+0.3*df['ret_126']  # 得分的权重分别为0.4、0.3、0.3
        result = df.sort_values('score', ascending=False)
        return list(result.instrument)[:3]  # 前3个行业
    
    # 选出特定行业优质股票
    def seek_head_stock(df):
        result = df.sort_values(['myrank'], ascending=False)
        return list(result.instrument[:10]) # 每个行业选10只股票
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m2_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    def m4_initialize_bigquant_run(context):
        # 手续费设置
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) 
        context.mydata = context.options["data"].read()
    # 回测引擎:每日数据处理函数,每天执行一次
    def m4_handle_data_bigquant_run(context, data):
        # 按月调仓
        if context.trading_day_index % 20 != 0:
            return 
        
        date = data.current_dt.strftime('%Y-%m-%d') # 日期
        # 整理出当天要买入的股票
        stock_to_buy = context.mydata[context.mydata.date==date][0].tolist()[0]
        
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
        # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
        no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]
        # 需要卖出的股票
        stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
      
        # 卖出
        for stock in stock_to_sell:
            # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
            # 如果返回真值,则可以正常下单,否则会出错
            # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
            if data.can_trade(context.symbol(stock)):
                # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,
                #   即卖出全部股票,可参考回测文档
                context.order_target_percent(context.symbol(stock), 0)
        
        # 如果当天没有买入的股票,就返回
        if len(stock_to_buy) == 0:
            return
    
        # 等权重买入 
        weight =  1 / len(stock_to_buy)
        
        # 买入
        for stock in stock_to_buy:
            if data.can_trade(context.symbol(stock)):
                # 下单使得某只股票的持仓权重达到weight,因为weight大于0,因此是等权重买入
                context.order_target_percent(context.symbol(stock), weight)
    # 回测引擎:准备数据,只执行一次
    def m4_prepare_bigquant_run(context):
        pass
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m4_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m7 = M.input_features.v1(
        features="""industry_sw_level1_0
    myrank=rank_fs_roe_ttm_0+rank_fs_net_profit_qoq_0-rank_pb_lf_0"""
    )
    
    m5 = M.instruments.v2(
        start_date='2021-01-01',
        end_date='2022-01-10',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m6 = M.general_feature_extractor.v7(
        instruments=m5.data,
        features=m7.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m8 = M.derived_feature_extractor.v3(
        input_data=m6.data,
        features=m7.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m9 = M.dropnan.v2(
        input_data=m8.data
    )
    
    m2 = M.cached.v3(
        input_1=m9.data,
        run=m2_run_bigquant_run,
        post_run=m2_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='data_1'
    )
    
    m4 = M.trade.v4(
        instruments=m5.data,
        options_data=m2.data_1,
        start_date='',
        end_date='',
        initialize=m4_initialize_bigquant_run,
        handle_data=m4_handle_data_bigquant_run,
        prepare=m4_prepare_bigquant_run,
        before_trading_start=m4_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=10000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    • 收益率4.47%
    • 年化收益率4.54%
    • 基准收益率-7.05%
    • 阿尔法0.09
    • 贝塔0.3
    • 夏普比率0.2
    • 胜率0.48
    • 盈亏比1.03
    • 收益波动率29.6%
    • 信息比率0.03
    • 最大回撤30.77%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-9e0f5b429b0647d4b32a3201a555799f"}/bigcharts-data-end