服务器故障提交【2】

策略分享
标签: #<Tag:0x00007f69169f9058>

(Hi) #1

先说明一下问题,同样的策略这两天回测发现交易数据乱套了,甚至出现了持仓股票为负数,然后隔天买入再补上。
另外,希望尽快处理前天提交的问题 最近服务器数据有问题,之前的训练模型都变了 ,已经很多人出现这种情况了。
希望火力全开抓紧修复一下~
交易正常的策略【12月份运行缓存图】




2017-06-13买入300650.SZA 5200股 ,2017-06-14卖出100股,2017-06-15卖出5100股


最近交易不正常的策略【2020.02.03再次运行】

每日持仓和收益:先多卖出5200然后再买入5200抵消



下面是策略代码,9%收益是12月份的,21%收益的是刚刚回测的

克隆策略

传统量化小市值策略

策略回测阶段:2013-01-01 到 2015-06-01

每日按市值因子从小到大排序,每天买入前5名

阶段收益率:867.73% , 最大回撤:15.6%

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-215:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-250:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-230:input_ds","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"-1791:input_data","SourceOutputPortId":"-230:sorted_data"},{"DestinationInputPortId":"-250:options_data","SourceOutputPortId":"-1791:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-12-01","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":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"market_cap_float_0\namount_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","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":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-250","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n \n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n context.stock_count = 5\n\n # 加载历史排序数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n df = context.options['data'].read_df()\n \n # 定义函数获取小市值股票列表前几名\n def get_stock_list(df):\n return list(df.instrument)[:context.stock_count]\n \n # 计算需要买入的股票\n context.stock_list = df.groupby('date').apply(get_stock_list)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n \n # 按日期过滤得到今日的排序列表作为买入清单\n buy_list = context.stock_list.ix[data.current_dt.strftime('%Y-%m-%d')]\n \n # 获取当前持仓\n hold_stocks = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}\n \n # 计算需要卖出的股票\n stock_to_sell = [ k for k in hold_stocks if k not in buy_list ]\n \n # 循环卖出股票\n for instrument in stock_to_sell:\n sid = context.symbol(instrument)\n if data.can_trade(sid):\n context.order_target_percent(sid, 0) \n \n # 循环买入股票\n for instrument in buy_list:\n sid = context.symbol(instrument)\n if data.can_trade(sid):\n context.order_target_percent(sid, 1/len(buy_list))","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","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":"close","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":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-250"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-250","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-230","ModuleId":"BigQuantSpace.sort.sort-v4","ModuleParameters":[{"Name":"sort_by","Value":"market_cap_float_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":"-230"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"sort_by_ds","NodeId":"-230"}],"OutputPortsInternal":[{"Name":"sorted_data","NodeId":"-230","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1791","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"amount_0 > 10000","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":"-1791"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1791","OutputType":null},{"Name":"left_data","NodeId":"-1791","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"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='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='195,87,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='547,87,200,200'/><NodePosition Node='-215' Position='381,188,200,200'/><NodePosition Node='-250' Position='387,405,200,200'/><NodePosition Node='-230' Position='395,301,200,200'/><NodePosition Node='-1791' Position='79,249,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 [1]:
    # 本代码由可视化策略环境自动生成 2020年2月3日 20:49
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m19_initialize_bigquant_run(context):
        
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        context.stock_count = 5
    
        # 加载历史排序数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        df = context.options['data'].read_df()
        
        # 定义函数获取小市值股票列表前几名
        def get_stock_list(df):
            return list(df.instrument)[:context.stock_count]
        
        # 计算需要买入的股票
        context.stock_list = df.groupby('date').apply(get_stock_list)
    # 回测引擎:每日数据处理函数,每天执行一次
    def m19_handle_data_bigquant_run(context, data):
        
        # 按日期过滤得到今日的排序列表作为买入清单
        buy_list = context.stock_list.ix[data.current_dt.strftime('%Y-%m-%d')]
        
        # 获取当前持仓
        hold_stocks = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}
        
        # 计算需要卖出的股票
        stock_to_sell = [ k for k in hold_stocks if k not in buy_list ]
        
        # 循环卖出股票
        for instrument in stock_to_sell:
            sid = context.symbol(instrument)
            if data.can_trade(sid):
                context.order_target_percent(sid, 0)    
        
        # 循环买入股票
        for instrument in buy_list:
            sid = context.symbol(instrument)
            if data.can_trade(sid):
                context.order_target_percent(sid, 1/len(buy_list))
    # 回测引擎:准备数据,只执行一次
    def m19_prepare_bigquant_run(context):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2017-01-01',
        end_date='2019-12-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.input_features.v1(
        features="""market_cap_float_0
    amount_0"""
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m4 = M.sort.v4(
        input_ds=m15.data,
        sort_by='market_cap_float_0',
        group_by='date',
        keep_columns='--',
        ascending=True
    )
    
    m2 = M.filter.v3(
        input_data=m4.sorted_data,
        expr='amount_0 > 10000',
        output_left_data=False
    )
    
    m19 = M.trade.v4(
        instruments=m1.data,
        options_data=m2.data,
        start_date='',
        end_date='',
        initialize=m19_initialize_bigquant_run,
        handle_data=m19_handle_data_bigquant_run,
        prepare=m19_prepare_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000300.SHA'
    )
    
    • 收益率21.39%
    • 年化收益率7.13%
    • 基准收益率15.67%
    • 阿尔法0.04
    • 贝塔0.45
    • 夏普比率0.29
    • 胜率0.47
    • 盈亏比0.79
    • 收益波动率20.76%
    • 信息比率0.01
    • 最大回撤27.81%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-229b5a4e7c924e6dbdc307a818d3b2db"}/bigcharts-data-end



    克隆策略

    传统量化小市值策略

    策略回测阶段:2013-01-01 到 2015-06-01

    每日按市值因子从小到大排序,每天买入前5名

    阶段收益率:867.73% , 最大回撤:15.6%

      {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-215:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-250:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-230:input_ds","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"-1791:input_data","SourceOutputPortId":"-230:sorted_data"},{"DestinationInputPortId":"-250:options_data","SourceOutputPortId":"-1791:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-12-01","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":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"market_cap_float_0\namount_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","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":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-250","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n \n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n context.stock_count = 5\n\n # 加载历史排序数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n df = context.options['data'].read_df()\n \n # 定义函数获取小市值股票列表前几名\n def get_stock_list(df):\n return list(df.instrument)[:context.stock_count]\n \n # 计算需要买入的股票\n context.stock_list = df.groupby('date').apply(get_stock_list)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n \n # 按日期过滤得到今日的排序列表作为买入清单\n buy_list = context.stock_list.ix[data.current_dt.strftime('%Y-%m-%d')]\n \n # 获取当前持仓\n hold_stocks = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}\n \n # 计算需要卖出的股票\n stock_to_sell = [ k for k in hold_stocks if k not in buy_list ]\n \n # 循环卖出股票\n for instrument in stock_to_sell:\n sid = context.symbol(instrument)\n if data.can_trade(sid):\n context.order_target_percent(sid, 0) \n \n # 循环买入股票\n for instrument in buy_list:\n sid = context.symbol(instrument)\n if data.can_trade(sid):\n context.order_target_percent(sid, 1/len(buy_list))","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","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":"close","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":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-250"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-250","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-230","ModuleId":"BigQuantSpace.sort.sort-v4","ModuleParameters":[{"Name":"sort_by","Value":"market_cap_float_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":"-230"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"sort_by_ds","NodeId":"-230"}],"OutputPortsInternal":[{"Name":"sorted_data","NodeId":"-230","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1791","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"amount_0 > 10000","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":"-1791"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1791","OutputType":null},{"Name":"left_data","NodeId":"-1791","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"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='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='195,87,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='547,87,200,200'/><NodePosition Node='-215' Position='381,188,200,200'/><NodePosition Node='-250' Position='387,405,200,200'/><NodePosition Node='-230' Position='395,301,200,200'/><NodePosition Node='-1791' Position='79,249,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 [9]:
      # 本代码由可视化策略环境自动生成 2020年2月3日 21:01
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # 回测引擎:初始化函数,只执行一次
      def m19_initialize_bigquant_run(context):
          
          # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
          context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
      
          # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
          context.stock_count = 5
      
          # 加载历史排序数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
          df = context.options['data'].read_df()
          
          # 定义函数获取小市值股票列表前几名
          def get_stock_list(df):
              return list(df.instrument)[:context.stock_count]
          
          # 计算需要买入的股票
          context.stock_list = df.groupby('date').apply(get_stock_list)
      # 回测引擎:每日数据处理函数,每天执行一次
      def m19_handle_data_bigquant_run(context, data):
          
          # 按日期过滤得到今日的排序列表作为买入清单
          buy_list = context.stock_list.ix[data.current_dt.strftime('%Y-%m-%d')]
          
          # 获取当前持仓
          hold_stocks = {e.symbol: p.amount for e, p in context.portfolio.positions.items()}
          
          # 计算需要卖出的股票
          stock_to_sell = [ k for k in hold_stocks if k not in buy_list ]
          
          # 循环卖出股票
          for instrument in stock_to_sell:
              sid = context.symbol(instrument)
              if data.can_trade(sid):
                  context.order_target_percent(sid, 0)    
          
          # 循环买入股票
          for instrument in buy_list:
              sid = context.symbol(instrument)
              if data.can_trade(sid):
                  context.order_target_percent(sid, 1/len(buy_list))
      # 回测引擎:准备数据,只执行一次
      def m19_prepare_bigquant_run(context):
          pass
      
      
      m1 = M.instruments.v2(
          start_date='2017-01-01',
          end_date='2019-12-01',
          market='CN_STOCK_A',
          instrument_list='',
          max_count=0
      )
      
      m3 = M.input_features.v1(
          features="""market_cap_float_0
      amount_0"""
      )
      
      m15 = M.general_feature_extractor.v7(
          instruments=m1.data,
          features=m3.data,
          start_date='',
          end_date='',
          before_start_days=0
      )
      
      m4 = M.sort.v4(
          input_ds=m15.data,
          sort_by='market_cap_float_0',
          group_by='date',
          keep_columns='--',
          ascending=True
      )
      
      m2 = M.filter.v3(
          input_data=m4.sorted_data,
          expr='amount_0 > 10000',
          output_left_data=False
      )
      
      m19 = M.trade.v4(
          instruments=m1.data,
          options_data=m2.data,
          start_date='',
          end_date='',
          initialize=m19_initialize_bigquant_run,
          handle_data=m19_handle_data_bigquant_run,
          prepare=m19_prepare_bigquant_run,
          volume_limit=0.025,
          order_price_field_buy='open',
          order_price_field_sell='close',
          capital_base=1000000,
          auto_cancel_non_tradable_orders=True,
          data_frequency='daily',
          price_type='真实价格',
          product_type='股票',
          plot_charts=True,
          backtest_only=False,
          benchmark='000300.SHA'
      )
      
      • 收益率9.17%
      • 年化收益率3.17%
      • 基准收益率15.67%
      • 阿尔法0.0
      • 贝塔0.5
      • 夏普比率0.11
      • 胜率0.58
      • 盈亏比0.76
      • 收益波动率20.44%
      • 信息比率-0.0
      • 最大回撤28.13%
      bigcharts-data-start/{"__id":"bigchart-94dfd795d5724370ae4240e11cd4347b","__type":"tabs"}/bigcharts-data-end

      (iQuant) #2

      您好,收到您的提问,这个问题我们正在查看处理,稍后给您回复。


      (达达) #3

      已经修复~ 点击关闭开发环境刷新浏览器