克隆策略

    {"Description":"实验创建于2019/4/24","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-64:input_ds","SourceOutputPortId":"-6:data"},{"DestinationInputPortId":"-6:instruments","SourceOutputPortId":"-21:data"},{"DestinationInputPortId":"-43:instruments","SourceOutputPortId":"-21:data"},{"DestinationInputPortId":"-43:options_data","SourceOutputPortId":"-30:data"},{"DestinationInputPortId":"-6:features","SourceOutputPortId":"-35:data"},{"DestinationInputPortId":"-30:input_data","SourceOutputPortId":"-64:data_1"}],"ModuleNodes":[{"Id":"-6","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-6"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-6"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-6","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-21","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-04-23","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-21"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-21","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-30","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"pb_lf_0 < 1.5 & pe_ttm_0 < 15 & amount_0 > 0 & pb_lf_0 > 0 & pe_ttm_0 > 0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_left_data","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-30"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-30","OutputType":null},{"Name":"left_data","NodeId":"-30","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-35","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"pb_lf_0\npe_ttm_0\namount_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-35"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-35","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-43","ModuleId":"BigQuantSpace.trade.trade-v4","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 \n # 1. 按每个K线递增,记录策略运行天数\n context.extension['index'] += 1\n \n # 2. 每隔22个交易日进行换仓\n if context.extension['index'] % context.rebalance_days != 1:\n return \n \n # 3. 买入股票列表\n stock_to_buy = context.daily_stock_buy.ix[ata.current_dt.strftime('%Y-%m-%d')]\n \n # 4. 当前持仓列表 \n stock_hold_now = [equity.symbol for equity in context.portfolio.positions]\n\n # 5. 卖出股票列表 \n stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]\n \n # 6. 执行卖出\n for stock in stock_to_sell:\n if data.can_trade(context.symbol(stock)):\n context.order_target_percent(context.symbol(stock), 0)\n \n # 7. 执行买入\n if len(stock_to_buy)>0: \n weight = 1 / len(stock_to_buy) # 等权重\n for instrument in stock_to_buy:\n if data.can_trade(context.symbol(instrument)):\n context.order_target_percent(context.symbol(instrument), weight)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载股票指标数据,数据继承自m4模块\n context.indicator_data = context.options['data'].read_df()\n\n # 设置股票数量\n context.stock_num = 30\n \n def open_pos_con(df):\n return list(df.instrument)[:context.stock_num]\n \n # 计算每日股票买入列表\n context.daily_stock_buy = context.indicator_data.groupby('date').apply(open_pos_con)\n \n # 调仓天数,22个交易日大概就是一个月。可以理解为一个月换仓一次\n context.rebalance_days = 22\n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n \n # 设置交易费用,买入是万三,卖出是千分之1.3,如果不足5元按5元算\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n \n # 如果策略运行中,需要将数据(比如运行天数)进行保存,可以借用extension这个对象,类型为dict\n if 'index' not in context.extension:\n context.extension['index'] = 0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","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":"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":"product_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":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-43"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-43","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-64","ModuleId":"BigQuantSpace.sort.sort-v4","ModuleParameters":[{"Name":"sort_by","Value":"pe_ttm_0,pb_lf_0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"group_by","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"keep_columns","Value":"--","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"ascending","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-64"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"sort_by_ds","NodeId":"-64"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-64","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"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='-6' Position='197.74703979492188,108.72286987304688,200,200'/><NodePosition Node='-21' Position='7.024139404296875,22.698776245117188,200,200'/><NodePosition Node='-30' Position='202.10845947265625,251.40960693359375,200,200'/><NodePosition Node='-35' Position='329.6506652832031,19.144546508789062,200,200'/><NodePosition Node='-43' Position='118.91571044921875,334.2048034667969,200,200'/><NodePosition Node='-64' Position='197.65066528320312,177.7108154296875,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 [3]:
    # 本代码由可视化策略环境自动生成 2020年9月23日 14:48
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m2_handle_data_bigquant_run(context, data):
        
        # 1. 按每个K线递增,记录策略运行天数
        context.extension['index']  += 1
        
        # 2. 每隔22个交易日进行换仓
        if context.extension['index'] % context.rebalance_days != 1:
            return 
        
        # 3. 买入股票列表
        stock_to_buy = context.daily_stock_buy.ix[ata.current_dt.strftime('%Y-%m-%d')]
        
        # 4. 当前持仓列表    
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
    
        # 5. 卖出股票列表 
        stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
        
        # 6. 执行卖出
        for stock in stock_to_sell:
            if data.can_trade(context.symbol(stock)):
                context.order_target_percent(context.symbol(stock), 0)
                
        # 7. 执行买入
        if len(stock_to_buy)>0: 
            weight = 1 / len(stock_to_buy) # 等权重
            for  instrument in stock_to_buy:
                if data.can_trade(context.symbol(instrument)):
                    context.order_target_percent(context.symbol(instrument), weight)
    # 回测引擎:准备数据,只执行一次
    def m2_prepare_bigquant_run(context):
        # 加载股票指标数据,数据继承自m4模块
        context.indicator_data = context.options['data'].read_df()
    
         # 设置股票数量
        context.stock_num = 30
        
        def open_pos_con(df):
            return list(df.instrument)[:context.stock_num]
        
        # 计算每日股票买入列表
        context.daily_stock_buy = context.indicator_data.groupby('date').apply(open_pos_con)
        
        # 调仓天数,22个交易日大概就是一个月。可以理解为一个月换仓一次
        context.rebalance_days = 22
        
    
    # 回测引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):
        
        # 设置交易费用,买入是万三,卖出是千分之1.3,如果不足5元按5元算
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        
        # 如果策略运行中,需要将数据(比如运行天数)进行保存,可以借用extension这个对象,类型为dict
        if 'index' not in context.extension:
            context.extension['index'] = 0
    
    m3 = M.instruments.v2(
        start_date='2015-01-01',
        end_date='2019-04-23',
        market='CN_STOCK_A',
        instrument_list=''
    )
    
    m5 = M.input_features.v1(
        features="""pb_lf_0
    pe_ttm_0
    amount_0"""
    )
    
    m1 = M.general_feature_extractor.v7(
        instruments=m3.data,
        features=m5.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m6 = M.sort.v4(
        input_ds=m1.data,
        sort_by='pe_ttm_0,pb_lf_0',
        group_by='date',
        keep_columns='--',
        ascending=True
    )
    
    m4 = M.filter.v3(
        input_data=m6.data_1,
        expr='pb_lf_0 < 1.5 & pe_ttm_0 < 15 & amount_0 > 0 & pb_lf_0 > 0 & pe_ttm_0 > 0',
        output_left_data=False
    )
    
    m2 = M.trade.v4(
        instruments=m3.data,
        options_data=m4.data,
        start_date='',
        end_date='',
        handle_data=m2_handle_data_bigquant_run,
        prepare=m2_prepare_bigquant_run,
        initialize=m2_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    • 收益率86.33%
    • 年化收益率16.13%
    • 基准收益率13.73%
    • 阿尔法0.12
    • 贝塔0.84
    • 夏普比率0.6
    • 胜率0.45
    • 盈亏比2.44
    • 收益波动率25.28%
    • 信息比率0.05
    • 最大回撤30.73%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-5f61ccbd3ede4c17bafaa4c9263c96b1"}/bigcharts-data-end