ETF基金轮动策略

基金
etf
标签: #<Tag:0x00007fcf62883900> #<Tag:0x00007fcf628837c0>

(eqsxin) #1

在BigQuant上待了一段时间了,今天分享一个策略,仅供大家交流~

在美国市场上,ETF轮动策略是一个广泛使用的策略,其中一个比较出名的article就是“SMART 4” Sector Rotation Strategy ,大家可以搜索一下。另外在quantopian上有较多相关的ETF轮动策略,比如ETF rotation strategy

今天我分享的策略就是国内A股ETF轮动策略。交易型开放式指数基金,通常又被称为交易所交易基金(Exchange Traded Funds,简称“ETF”),是一种在交易所上市交易的、基金份额可变的一种开放式基金。我们可以简单将其理解为股票,只是该股票背后其实是买入了一篮子股票。我们可以通过一行代码查看平台上有哪些ETF基金。

如果要查看该ETF基金的价格和名称,可以这样:
image

ETF基金轮动策略背后的逻辑是:动量,强者越强,弱者越弱。因此我们需要找出一个动量度量指标,这里我们参考了ETF rotation strategy,通过多个不同周期的收益率为每个基金打分,计算出一个score,然后买入score高的基金,每一个月调仓一次。

策略源代码

克隆策略

    {"Description":"实验创建于2018/3/22","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-87:instruments","SourceOutputPortId":"-78:data"}],"ModuleNodes":[{"Id":"-78","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2016-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-03-22","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_FUND","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-78"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-78","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-87","ModuleId":"BigQuantSpace.trade.trade-v3","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n date = data.current_dt.strftime('%Y-%m-%d')\n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions]\n\n# 下面注释代码为大盘择时,大家可以根据自己情况选择使用 \n# bench_mark_ma_short = D.history_data(['000300.SHA'],'2006-01-01', date, ['close'])['close'].tail(10).mean()\n# bench_mark_ma_long = D.history_data(['000300.SHA'],'2006-01-01', date, ['close'])['close'].tail(40).mean()\n# if bench_mark_ma_short < bench_mark_ma_long and len(stock_hold_now) > 0:\n# for j in stock_hold_now:\n# context.order_target_percent(context.symbol(j), 0)\n \n # 按月调仓\n if context.trading_day_index % 21 != 0:\n return \n # 根据日期获取调仓需要买入的股票的列表\n stock_to_buy = context.daily_buy_stock.ix[date]\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,因为\n # weight大于0,因此是等权重买入\n context.order_target_percent(context.symbol(stock), weight)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n instruments =context.instruments\n start_date = context.start_date \n end_date = context.end_date\n price_data = D.history_data(instruments, start_date, end_date, fields=['close'])\n ret_data = price_data.groupby('instrument').apply(calcu_ret)\n ret_data.reset_index(inplace=True, drop=True)\n ret_data['date'] = ret_data['date'].map(lambda x:x.strftime('%Y-%m-%d'))\n daily_buy_stock = ret_data.groupby('date').apply(seek_stock)\n context.daily_buy_stock = daily_buy_stock\n \n# 计算不同期限的收益率\ndef calcu_ret(df):\n df = df.sort_values('date')\n for i in [21,63,126,252]:\n df['ret_%s'%i] = df['close']/df['close'].shift(i)-1 \n return df\n\n# 计算出收益率得分\ndef seek_stock(df):\n for j in ['ret_21','ret_63','ret_126','ret_252']:\n df['%s'%j] = df['%s'%j].rank(ascending=True) \n df['score'] = 0.3*df['ret_21']+0.3*df['ret_63']+0.2*df['ret_126']+0.2*df['ret_252']\n result = df.sort_values('score', ascending=False)\n return list(result.instrument)[:20] ","ValueType":"Literal","LinkedGlobalParameter":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","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"amount_integer","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-87"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-87"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-87","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-78' Position='779.9218139648438,454.1680603027344,200,200'/><NodePosition Node='-87' Position='753.6383056640625,634.1087646484375,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [6]:
    # 本代码由可视化策略环境自动生成 2018年3月22日 18:09
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2016-01-01',
        end_date='2018-03-22',
        market='CN_FUND',
        instrument_list='',
        max_count=0
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m2_handle_data_bigquant_run(context, data):
        date = data.current_dt.strftime('%Y-%m-%d')
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
    
    # 下面注释代码为大盘择时,大家可以根据自己情况选择使用    
    #     bench_mark_ma_short = D.history_data(['000300.SHA'],'2006-01-01', date, ['close'])['close'].tail(10).mean()
    #     bench_mark_ma_long = D.history_data(['000300.SHA'],'2006-01-01', date, ['close'])['close'].tail(40).mean()
    #     if bench_mark_ma_short < bench_mark_ma_long and len(stock_hold_now) > 0:
    #         for j in stock_hold_now:
    #             context.order_target_percent(context.symbol(j), 0)
                
        # 按月调仓
        if context.trading_day_index % 21 != 0:
            return 
        # 根据日期获取调仓需要买入的股票的列表
        stock_to_buy = context.daily_buy_stock.ix[date]
        # 通过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 m2_prepare_bigquant_run(context):
        instruments =context.instruments
        start_date = context.start_date 
        end_date = context.end_date
        price_data = D.history_data(instruments, start_date, end_date, fields=['close'])
        ret_data = price_data.groupby('instrument').apply(calcu_ret)
        ret_data.reset_index(inplace=True, drop=True)
        ret_data['date'] = ret_data['date'].map(lambda x:x.strftime('%Y-%m-%d'))
        daily_buy_stock = ret_data.groupby('date').apply(seek_stock)
        context.daily_buy_stock = daily_buy_stock
        
    # 计算不同期限的收益率
    def calcu_ret(df):
        df = df.sort_values('date')
        for i in [21,63,126,252]:
            df['ret_%s'%i] = df['close']/df['close'].shift(i)-1 
        return df
    
    # 计算出收益率得分
    def seek_stock(df):
        for j in ['ret_21','ret_63','ret_126','ret_252']:
            df['%s'%j] = df['%s'%j].rank(ascending=True) 
        df['score'] = 0.3*df['ret_21']+0.3*df['ret_63']+0.2*df['ret_126']+0.2*df['ret_252']
        result = df.sort_values('score', ascending=False)
        return list(result.instrument)[:20] 
    # 回测引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):
        # 手续费设置
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) 
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m2_before_trading_start_bigquant_run(context, data):
        pass
    
    m2 = M.trade.v3(
        instruments=m1.data,
        start_date='',
        end_date='',
        handle_data=m2_handle_data_bigquant_run,
        prepare=m2_prepare_bigquant_run,
        initialize=m2_initialize_bigquant_run,
        before_trading_start=m2_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        benchmark='000300.SHA',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        plot_charts=True,
        backtest_only=False,
        amount_integer=False
    )
    
    [2018-03-22 17:52:59.648426] INFO: bigquant: instruments.v2 开始运行..
    [2018-03-22 17:52:59.652457] INFO: bigquant: 命中缓存
    [2018-03-22 17:52:59.653852] INFO: bigquant: instruments.v2 运行完成[0.005476s].
    [2018-03-22 17:52:59.707649] INFO: bigquant: backtest.v7 开始运行..
    [2018-03-22 17:53:08.998792] INFO: algo: set price type:backward_adjusted
    [2018-03-22 17:53:22.794980] INFO: Performance: Simulated 541 trading days out of 541.
    [2018-03-22 17:53:22.797176] INFO: Performance: first open: 2016-01-04 01:30:00+00:00
    [2018-03-22 17:53:22.798392] INFO: Performance: last close: 2018-03-22 07:00:00+00:00
    
    • 收益率7.5%
    • 年化收益率3.43%
    • 基准收益率7.76%
    • 阿尔法-0.01
    • 贝塔0.43
    • 夏普比率-0.12
    • 胜率0.557
    • 盈亏比1.062
    • 收益波动率8.7%
    • 信息比率-0.01
    • 最大回撤9.14%
    [2018-03-22 17:53:28.045433] INFO: bigquant: backtest.v7 运行完成[28.337734s].