克隆策略
In [95]:
m8.predictions.read().head()
Out[95]:
score date instrument position
0 1.206727 2014-10-29 600035.SHA 1
1 0.970216 2014-10-29 600004.SHA 2
2 0.948796 2014-10-29 600486.SHA 3
3 0.939215 2014-10-29 601555.SHA 4
4 0.938887 2014-10-29 002598.SZA 5
In [100]:
m12.predictions.read().head()
Out[100]:
classes_prob_0 classes_prob_1 pred_label date instrument
2 0.640431 0.359569 0 2014-10-30 000001.SZA
21 0.679217 0.320783 0 2014-11-26 000001.SZA
35 0.571991 0.428009 0 2014-12-16 000001.SZA
39 0.699138 0.300862 0 2014-12-22 000001.SZA
46 0.650829 0.349171 0 2014-12-31 000001.SZA
In [103]:
df1 = m8.predictions.read_df()
df1_filter=df1.groupby('date',group_keys=False).apply(lambda x:x.sort_values('position')[:2]).reset_index(drop=True)
df1.head()
Out[103]:
score date instrument position
0 1.206727 2014-10-29 600035.SHA 1
1 0.970216 2014-10-29 600004.SHA 2
2 0.948796 2014-10-29 600486.SHA 3
3 0.939215 2014-10-29 601555.SHA 4
4 0.938887 2014-10-29 002598.SZA 5
In [121]:
df2 = m12.predictions.read_df()

df2_filter=df2.groupby('date',group_keys=False).apply(lambda x:x.sort_values('classes_prob_0')[:2]).reset_index(drop=True)
df2_filter.head()
Out[121]:
classes_prob_0 classes_prob_1 pred_label date instrument
0 0.522840 0.477160 0 2014-10-29 600834.SHA
1 0.526359 0.473641 0 2014-10-29 002682.SZA
2 0.549988 0.450012 0 2014-10-30 000657.SZA
3 0.566365 0.433635 0 2014-10-30 300093.SZA
4 0.563091 0.436909 0 2014-10-31 600259.SHA
In [125]:
df=pd.merge(df1_filter,df2_filter,left_on=['date','instrument'],right_on=['date','instrument']).sort_values(['date','position'])
df.head()
Out[125]:
score date instrument position classes_prob_0 classes_prob_1 pred_label
0 1.155616 2014-10-31 600259.SHA 1 0.563091 0.436909 0
1 0.787467 2014-11-04 600359.SHA 1 0.543182 0.456818 0
2 1.261694 2014-11-07 600446.SHA 1 0.519923 0.480077 0
3 1.308929 2014-11-13 002274.SZA 2 0.551363 0.448637 0
4 1.252863 2014-11-14 600100.SHA 2 0.533990 0.466010 0

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-351:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data1","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:data"},{"DestinationInputPortId":"-171:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-175:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:model","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:model"},{"DestinationInputPortId":"-1348:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-595:input_1","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"DestinationInputPortId":"-365:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-382:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:data","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-459:predict_ds","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-358:input_data","SourceOutputPortId":"-351:data"},{"DestinationInputPortId":"-9001:input_data","SourceOutputPortId":"-358:data"},{"DestinationInputPortId":"-372:input_data","SourceOutputPortId":"-365:data"},{"DestinationInputPortId":"-9453:input_data","SourceOutputPortId":"-372:data"},{"DestinationInputPortId":"-8132:features_ds","SourceOutputPortId":"-506:data"},{"DestinationInputPortId":"-11798:features_ds","SourceOutputPortId":"-506:data"},{"DestinationInputPortId":"-555:data2","SourceOutputPortId":"-518:data"},{"DestinationInputPortId":"-365:features","SourceOutputPortId":"-532:data_1"},{"DestinationInputPortId":"-9916:input_data","SourceOutputPortId":"-548:data"},{"DestinationInputPortId":"-12301:input_data","SourceOutputPortId":"-555:data"},{"DestinationInputPortId":"-459:training_ds","SourceOutputPortId":"-561:data"},{"DestinationInputPortId":"-382:options_data","SourceOutputPortId":"-595:data_1"},{"DestinationInputPortId":"-518:instruments","SourceOutputPortId":"-510:data"},{"DestinationInputPortId":"-13369:instruments","SourceOutputPortId":"-510:data"},{"DestinationInputPortId":"-532:input_1","SourceOutputPortId":"-171:data"},{"DestinationInputPortId":"-351:features","SourceOutputPortId":"-171:data"},{"DestinationInputPortId":"-532:input_2","SourceOutputPortId":"-8132:data"},{"DestinationInputPortId":"-13369:features","SourceOutputPortId":"-8132:data"},{"DestinationInputPortId":"-9932:input_1","SourceOutputPortId":"-45:data"},{"DestinationInputPortId":"-358:features","SourceOutputPortId":"-45:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-9001:data"},{"DestinationInputPortId":"-9001:features","SourceOutputPortId":"-32:data"},{"DestinationInputPortId":"-10806:input_1","SourceOutputPortId":"-32:data"},{"DestinationInputPortId":"-12892:input_data","SourceOutputPortId":"-9453:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:features","SourceOutputPortId":"-175:data"},{"DestinationInputPortId":"-555:data1","SourceOutputPortId":"-9916:data"},{"DestinationInputPortId":"-548:features","SourceOutputPortId":"-9924:data"},{"DestinationInputPortId":"-9932:input_2","SourceOutputPortId":"-9924:data"},{"DestinationInputPortId":"-372:features","SourceOutputPortId":"-9932:data_1"},{"DestinationInputPortId":"-9453:features","SourceOutputPortId":"-10806:data_1"},{"DestinationInputPortId":"-10806:input_2","SourceOutputPortId":"-10814:data"},{"DestinationInputPortId":"-9916:features","SourceOutputPortId":"-10814:data"},{"DestinationInputPortId":"-459:features","SourceOutputPortId":"-11798:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"-1348:data"},{"DestinationInputPortId":"-561:input_data","SourceOutputPortId":"-12301:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-12892:data"},{"DestinationInputPortId":"-548:input_data","SourceOutputPortId":"-13369:data"},{"DestinationInputPortId":"-595:input_2","SourceOutputPortId":"-459:predictions"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2010-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-01-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,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/data_history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -2) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na_label","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nreturn_0\nreturn_4\nreturn_5","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,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","ModuleId":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v5","ModuleParameters":[{"Name":"learning_algorithm","Value":"排序","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_leaves","Value":30,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"minimum_docs_per_leaf","Value":1000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_trees","Value":20,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"learning_rate","Value":0.1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_bins","Value":1023,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"}],"OutputPortsInternal":[{"Name":"model","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","OutputType":null},{"Name":"feature_gains","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","OutputType":null},{"Name":"m_lazy_run","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","ModuleId":"BigQuantSpace.stock_ranker_predict.stock_ranker_predict-v5","ModuleParameters":[{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"model","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"}],"OutputPortsInternal":[{"Name":"predictions","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","OutputType":null},{"Name":"m_lazy_run","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"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-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"Comment":"","CommentCollapsed":true},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":14,"Comment":"","CommentCollapsed":true},{"Id":"-351","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":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-351"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-351"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-351","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"Comment":"","CommentCollapsed":true},{"Id":"-358","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-358"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-358"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-358","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"Comment":"","CommentCollapsed":true},{"Id":"-365","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":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-365"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-365"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-365","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"Comment":"","CommentCollapsed":true},{"Id":"-372","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-372"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-372"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-372","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"Comment":"","CommentCollapsed":true},{"Id":"-382","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 # 获取今日的日期\n today = data.current_dt.strftime('%Y-%m-%d') \n \n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions ] \n \n # 按日期过滤得到今日的预测数据,和买入备选股票列表\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n stock_to_buy = list(ranker_prediction.instrument)\n \n # 需要卖出的股票:已有持仓中不在买入列表的股票\n stock_to_sell = [ i for i in stock_hold_now if i not in stock_to_buy]\n \n # 生成卖出订单:\n if len(stock_to_sell)>0:\n for instrument in stock_to_sell:\n sid = context.symbol(instrument) # 将标的转化为equity格式\n cur_position = context.portfolio.positions[sid].amount # 持仓\n if cur_position > 0 and data.can_trade(sid):\n context.order_target_percent(sid, 0) # 全部卖出 \n \n \n # 生成买入订单:买入每天两个策略前两名的股票\n if len(stock_to_buy)>0:\n if len(stock_to_buy)>0:\n weight = 1/4 # 每只股票的比重为等资金比例持有1/4仓位\n for instrument in stock_to_buy:\n sid = context.symbol(instrument) # 将标的转化为equity格式\n if data.can_trade(sid):\n context.order_target_percent(sid, weight) # 买入\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n context.ranker_prediction = context.options['data'].read_df()\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n stock_count = 4\n # 每只的股票等资金分配\n context.stock_weights = [1/stock_count for i in range(4)]\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":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-382"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-382"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-382"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-382"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-382"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-382","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":19,"Comment":"","CommentCollapsed":true},{"Id":"-506","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nreturn_0\nreturn_4\nreturn_5","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-506"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-506","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-518","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nwhere(shift(close, -2) / shift(open, -1)-1>0.01, 1, 0)\n\n# 极值处理:用1%和99%分位的值做clip\n#clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\n#all_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na_label","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-518"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-518","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"","CommentCollapsed":true},{"Id":"-532","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n f1 = input_1.read_pickle()\n f2 = input_2.read_pickle()\n factors = [ k for k in set(f1 + f2)]\n data_1 = DataSource.write_pickle(factors)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-532"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-532"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-532"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-532","OutputType":null},{"Name":"data_2","NodeId":"-532","OutputType":null},{"Name":"data_3","NodeId":"-532","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"Comment":"","CommentCollapsed":true},{"Id":"-548","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-548"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-548"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-548","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":20,"Comment":"","CommentCollapsed":true},{"Id":"-555","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-555"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-555"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-555","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":21,"Comment":"","CommentCollapsed":true},{"Id":"-561","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-561"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-561","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":22,"Comment":"","CommentCollapsed":true},{"Id":"-595","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n df1=input_1.read_df()\n df1_filter=df1.groupby('date',group_keys=False).apply(lambda x:x.sort_values('position')[:2]).reset_index(drop=True)\n df2=input_2.read_df()\n df2_filter=df2.groupby('date',group_keys=False).apply(lambda x:x.sort_values('classes_prob_0')[:2]).reset_index(drop=True)\n \n df=pd.merge(df1_filter,df2_filter,left_on=['date','instrument'],right_on=['date','instrument']).sort_values(['date','position'])\n \n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-595"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-595"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-595"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-595","OutputType":null},{"Name":"data_2","NodeId":"-595","OutputType":null},{"Name":"data_3","NodeId":"-595","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":25,"Comment":"","CommentCollapsed":true},{"Id":"-510","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2010-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-01-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":"-510"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-510","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-171","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nst_status_0\nreturn_90\nreturn_10\nreturn_80\nreturn_0\nhigh_0\nlow_0\nclose_0\nopen_0\n\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-171"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-171","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":26,"Comment":"辅助计算的因子,不参与训练","CommentCollapsed":false},{"Id":"-8132","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nst_status_0\nreturn_90\nreturn_10\nreturn_80\nhigh_0\nlow_0\nclose_0\nopen_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-8132"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-8132","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":27,"Comment":"辅助计算的因子,不参与训练","CommentCollapsed":false},{"Id":"-45","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nmid=mean(close_0,14)\natr=ta_atr(high_0, low_0, close_0, 14)\nret1=return_90/return_10\nret2=return_80/return_10\nret3=return_5/return_0\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-45"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-45","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":28,"Comment":"自己定义的因子","CommentCollapsed":false},{"Id":"-9001","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-9001"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-9001"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-9001","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":29,"Comment":"","CommentCollapsed":true},{"Id":"-32","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\ncond2=(close_0>atr+mid) & (shift(close_0, 1) < shift(atr+mid, 1))\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-32"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-32","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":30,"Comment":"自己定义的因子","CommentCollapsed":false},{"Id":"-9453","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-9453"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-9453"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-9453","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":31,"Comment":"","CommentCollapsed":true},{"Id":"-175","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# 输入参与训练的自定义列\n\nret1\nret2\nret3\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-175"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-175","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":32,"Comment":"自定义的因子,也参与模型训练","CommentCollapsed":true},{"Id":"-9916","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-9916"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-9916"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-9916","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":33,"Comment":"","CommentCollapsed":true},{"Id":"-9924","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nmid=mean(close_0,14)\natr=ta_atr(high_0, low_0, close_0, 14)\nret1=return_90/return_10\nret2=return_80/return_10\nret3=return_5/return_0\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-9924"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-9924","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":34,"Comment":"自己定义的因子","CommentCollapsed":false},{"Id":"-9932","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n f1 = input_1.read_pickle()\n f2 = input_2.read_pickle()\n factors = [ k for k in set(f1 + f2)]\n data_1 = DataSource.write_pickle(factors)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-9932"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-9932"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-9932"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-9932","OutputType":null},{"Name":"data_2","NodeId":"-9932","OutputType":null},{"Name":"data_3","NodeId":"-9932","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":35,"Comment":"","CommentCollapsed":true},{"Id":"-10806","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n f1 = input_1.read_pickle()\n f2 = input_2.read_pickle()\n factors = [ k for k in set(f1 + f2)]\n data_1 = DataSource.write_pickle(factors)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-10806"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-10806"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-10806"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-10806","OutputType":null},{"Name":"data_2","NodeId":"-10806","OutputType":null},{"Name":"data_3","NodeId":"-10806","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":36,"Comment":"","CommentCollapsed":true},{"Id":"-10814","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\ncond2=(close_0>atr+mid) & (shift(close_0, 1) < shift(atr+mid, 1))\n\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-10814"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-10814","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":37,"Comment":"自己定义的因子","CommentCollapsed":false},{"Id":"-11798","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# 输入参与训练的自定义列\n\nret1\nret2\nret3\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-11798"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-11798","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":38,"Comment":"自定义的因子,也参与模型训练","CommentCollapsed":true},{"Id":"-1348","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"st_status_0 == 0 and cond2>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":"-1348"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1348","OutputType":null},{"Name":"left_data","NodeId":"-1348","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":39,"Comment":"过滤上市天数不足120天以及ST的股票","CommentCollapsed":true},{"Id":"-12301","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"st_status_0 == 0 and cond2>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":"-12301"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-12301","OutputType":null},{"Name":"left_data","NodeId":"-12301","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":40,"Comment":"过滤上市天数不足120天以及ST的股票","CommentCollapsed":true},{"Id":"-12892","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"st_status_0 == 0 and cond2>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":"-12892"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-12892","OutputType":null},{"Name":"left_data","NodeId":"-12892","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":41,"Comment":"过滤上市天数不足120天以及ST的股票","CommentCollapsed":true},{"Id":"-13369","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":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-13369"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-13369"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-13369","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":42,"Comment":"","CommentCollapsed":true},{"Id":"-459","ModuleId":"BigQuantSpace.random_forest_classifier.random_forest_classifier-v1","ModuleParameters":[{"Name":"iterations","Value":10,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_depth","Value":30,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"min_samples_per_leaf","Value":200,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"key_cols","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"workers","Value":"2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"other_train_parameters","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-459"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-459"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"model","NodeId":"-459"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"predict_ds","NodeId":"-459"}],"OutputPortsInternal":[{"Name":"output_model","NodeId":"-459","OutputType":null},{"Name":"predictions","NodeId":"-459","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"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='43,-185,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='38,-93,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='287,-313,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-43' Position='227,681,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='95,366,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='395,801,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='571.1720581054688,296.2581481933594,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='95,548,200,200'/><NodePosition Node='-86' Position='841,876,200,200'/><NodePosition Node='-351' Position='409,-99,200,200'/><NodePosition Node='-358' Position='100,155,200,200'/><NodePosition Node='-365' Position='776,412,200,200'/><NodePosition Node='-372' Position='873,524,200,200'/><NodePosition Node='-382' Position='766,1171,200,200'/><NodePosition Node='-506' Position='1311,-320,200,200'/><NodePosition Node='-518' Position='1545,-102,200,200'/><NodePosition Node='-532' Position='778,-40,200,200'/><NodePosition Node='-548' Position='1454,163,200,200'/><NodePosition Node='-555' Position='1463,374,200,200'/><NodePosition Node='-561' Position='1466,568,200,200'/><NodePosition Node='-595' Position='851,1058,200,200'/><NodePosition Node='-510' Position='1543,-193,200,200'/><NodePosition Node='-171' Position='546,-217,200,200'/><NodePosition Node='-8132' Position='1056,-219,200,200'/><NodePosition Node='-45' Position='391,-9,200,200'/><NodePosition Node='-9001' Position='107,260,200,200'/><NodePosition Node='-32' Position='414,114,200,200'/><NodePosition Node='-9453' Position='850,685,200,200'/><NodePosition Node='-175' Position='387,405,200,200'/><NodePosition Node='-9916' Position='1462,272,200,200'/><NodePosition Node='-9924' Position='1159,-13,200,200'/><NodePosition Node='-9932' Position='790.827880859375,60.91395568847656,200,200'/><NodePosition Node='-10806' Position='779,168,200,200'/><NodePosition Node='-10814' Position='1157,108,200,200'/><NodePosition Node='-11798' Position='1158,425,200,200'/><NodePosition Node='-1348' Position='99,455,200,200'/><NodePosition Node='-12301' Position='1458,472,200,200'/><NodePosition Node='-12892' Position='820,772,200,200'/><NodePosition Node='-13369' Position='1157,-99,200,200'/><NodePosition Node='-459' Position='1327.1290283203125,977.5015869140625,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 [132]:
    # 本代码由可视化策略环境自动生成 2019年1月29日 17:15
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m11_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        f1 = input_1.read_pickle()
        f2 = input_2.read_pickle()
        factors = [ k for k in set(f1 + f2)]
        data_1 = DataSource.write_pickle(factors)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m11_post_run_bigquant_run(outputs):
        return outputs
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m35_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        f1 = input_1.read_pickle()
        f2 = input_2.read_pickle()
        factors = [ k for k in set(f1 + f2)]
        data_1 = DataSource.write_pickle(factors)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m35_post_run_bigquant_run(outputs):
        return outputs
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m36_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        f1 = input_1.read_pickle()
        f2 = input_2.read_pickle()
        factors = [ k for k in set(f1 + f2)]
        data_1 = DataSource.write_pickle(factors)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m36_post_run_bigquant_run(outputs):
        return outputs
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m25_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df1=input_1.read_df()
        df1_filter=df1.groupby('date',group_keys=False).apply(lambda x:x.sort_values('position')[:2]).reset_index(drop=True)
        df2=input_2.read_df()
        df2_filter=df2.groupby('date',group_keys=False).apply(lambda x:x.sort_values('classes_prob_0')[:2]).reset_index(drop=True)
        
        df=pd.merge(df1_filter,df2_filter,left_on=['date','instrument'],right_on=['date','instrument']).sort_values(['date','position'])
        
        data_1 = DataSource.write_df(df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m25_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m19_handle_data_bigquant_run(context, data):
    
        # 获取今日的日期
        today = data.current_dt.strftime('%Y-%m-%d')  
        
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = [equity.symbol for equity in context.portfolio.positions ]      
        
        # 按日期过滤得到今日的预测数据,和买入备选股票列表
        ranker_prediction = context.ranker_prediction[
            context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
        stock_to_buy = list(ranker_prediction.instrument)
        
        # 需要卖出的股票:已有持仓中不在买入列表的股票
        stock_to_sell = [ i for i in stock_hold_now if i not in stock_to_buy]
        
        # 生成卖出订单:
        if len(stock_to_sell)>0:
            for instrument in stock_to_sell:
                sid = context.symbol(instrument) # 将标的转化为equity格式
                cur_position = context.portfolio.positions[sid].amount # 持仓
                if cur_position > 0 and data.can_trade(sid):
                    context.order_target_percent(sid, 0) # 全部卖出   
        
        
        # 生成买入订单:买入每天两个策略前两名的股票
        if len(stock_to_buy)>0:
            if len(stock_to_buy)>0:
                weight = 1/4 # 每只股票的比重为等资金比例持有1/4仓位
                for instrument in stock_to_buy:
                    sid = context.symbol(instrument) # 将标的转化为equity格式
                    if  data.can_trade(sid):
                        context.order_target_percent(sid, weight) # 买入
    
    # 回测引擎:准备数据,只执行一次
    def m19_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m19_initialize_bigquant_run(context):
        # 加载预测数据
        context.ranker_prediction = context.options['data'].read_df()
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        stock_count = 4
        # 每只的股票等资金分配
        context.stock_weights = [1/stock_count for i in range(4)]
    
    
    m1 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2015-01-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.advanced_auto_labeler.v2(
        instruments=m1.data,
        label_expr="""# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/data_history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -2) / shift(open, -1)
    
    # 极值处理:用1%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True
    )
    
    m3 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    return_0
    return_4
    return_5"""
    )
    
    m26 = M.input_features.v1(
        features_ds=m3.data,
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    st_status_0
    return_90
    return_10
    return_80
    return_0
    high_0
    low_0
    close_0
    open_0
    
    
    """
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m26.data,
        start_date='',
        end_date=''
    )
    
    m32 = M.input_features.v1(
        features_ds=m3.data,
        features="""
    # 输入参与训练的自定义列
    
    ret1
    ret2
    ret3
    """
    )
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2015-01-01'),
        end_date=T.live_run_param('trading_date', '2019-01-01'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m4 = M.input_features.v1(
        features="""
    # #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    return_0
    return_4
    return_5"""
    )
    
    m27 = M.input_features.v1(
        features_ds=m4.data,
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    st_status_0
    return_90
    return_10
    return_80
    high_0
    low_0
    close_0
    open_0"""
    )
    
    m11 = M.cached.v3(
        input_1=m26.data,
        input_2=m27.data,
        run=m11_run_bigquant_run,
        post_run=m11_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m17 = M.general_feature_extractor.v7(
        instruments=m9.data,
        features=m11.data_1,
        start_date='',
        end_date=''
    )
    
    m38 = M.input_features.v1(
        features_ds=m4.data,
        features="""
    # 输入参与训练的自定义列
    
    ret1
    ret2
    ret3
    """
    )
    
    m5 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2015-01-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m10 = M.advanced_auto_labeler.v2(
        instruments=m5.data,
        label_expr="""
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    where(shift(close, -2) / shift(open, -1)-1>0.01, 1, 0)
    
    # 极值处理:用1%和99%分位的值做clip
    #clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    #all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True,
        user_functions={}
    )
    
    m42 = M.general_feature_extractor.v7(
        instruments=m5.data,
        features=m27.data,
        start_date='',
        end_date=''
    )
    
    m28 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    mid=mean(close_0,14)
    atr=ta_atr(high_0, low_0, close_0, 14)
    ret1=return_90/return_10
    ret2=return_80/return_10
    ret3=return_5/return_0
    
    """
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m28.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m30 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    cond2=(close_0>atr+mid) & (shift(close_0, 1) < shift(atr+mid, 1))
    """
    )
    
    m29 = M.derived_feature_extractor.v3(
        input_data=m16.data,
        features=m30.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m7 = M.join.v3(
        data1=m2.data,
        data2=m29.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m39 = M.filter.v3(
        input_data=m7.data,
        expr='st_status_0 == 0 and cond2>0',
        output_left_data=False
    )
    
    m13 = M.dropnan.v1(
        input_data=m39.data
    )
    
    m6 = M.stock_ranker_train.v5(
        training_ds=m13.data,
        features=m32.data,
        learning_algorithm='排序',
        number_of_leaves=30,
        minimum_docs_per_leaf=1000,
        number_of_trees=20,
        learning_rate=0.1,
        max_bins=1023,
        feature_fraction=1,
        m_lazy_run=False
    )
    
    m34 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    mid=mean(close_0,14)
    atr=ta_atr(high_0, low_0, close_0, 14)
    ret1=return_90/return_10
    ret2=return_80/return_10
    ret3=return_5/return_0
    
    """
    )
    
    m20 = M.derived_feature_extractor.v3(
        input_data=m42.data,
        features=m34.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m35 = M.cached.v3(
        input_1=m28.data,
        input_2=m34.data,
        run=m35_run_bigquant_run,
        post_run=m35_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m18 = M.derived_feature_extractor.v3(
        input_data=m17.data,
        features=m35.data_1,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m37 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    cond2=(close_0>atr+mid) & (shift(close_0, 1) < shift(atr+mid, 1))
    
    
    """
    )
    
    m36 = M.cached.v3(
        input_1=m30.data,
        input_2=m37.data,
        run=m36_run_bigquant_run,
        post_run=m36_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m31 = M.derived_feature_extractor.v3(
        input_data=m18.data,
        features=m36.data_1,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m41 = M.filter.v3(
        input_data=m31.data,
        expr='st_status_0 == 0 and cond2>0',
        output_left_data=False
    )
    
    m14 = M.dropnan.v1(
        input_data=m41.data
    )
    
    m8 = M.stock_ranker_predict.v5(
        model=m6.model,
        data=m14.data,
        m_lazy_run=False
    )
    
    m33 = M.derived_feature_extractor.v3(
        input_data=m20.data,
        features=m37.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m21 = M.join.v3(
        data1=m33.data,
        data2=m10.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m40 = M.filter.v3(
        input_data=m21.data,
        expr='st_status_0 == 0 and cond2>0',
        output_left_data=False
    )
    
    m22 = M.dropnan.v1(
        input_data=m40.data
    )
    
    m12 = M.random_forest_classifier.v1(
        training_ds=m22.data,
        features=m38.data,
        predict_ds=m14.data,
        iterations=10,
        feature_fraction=1,
        max_depth=30,
        min_samples_per_leaf=200,
        key_cols='date,instrument',
        workers=2,
        other_train_parameters={}
    )
    
    m25 = M.cached.v3(
        input_1=m8.predictions,
        input_2=m12.predictions,
        run=m25_run_bigquant_run,
        post_run=m25_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m19 = M.trade.v4(
        instruments=m9.data,
        options_data=m25.data_1,
        start_date='',
        end_date='',
        handle_data=m19_handle_data_bigquant_run,
        prepare=m19_prepare_bigquant_run,
        initialize=m19_initialize_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=''
    )
    
    [2019-01-29 17:08:05.456702] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-29 17:08:05.463487] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.465099] INFO: bigquant: instruments.v2 运行完成[0.008419s].
    [2019-01-29 17:08:05.468339] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
    [2019-01-29 17:08:05.473832] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.475361] INFO: bigquant: advanced_auto_labeler.v2 运行完成[0.007017s].
    [2019-01-29 17:08:05.478080] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:08:05.483138] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.484320] INFO: bigquant: input_features.v1 运行完成[0.006275s].
    [2019-01-29 17:08:05.486660] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:08:05.491766] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.492765] INFO: bigquant: input_features.v1 运行完成[0.006078s].
    [2019-01-29 17:08:05.500425] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-29 17:08:05.505565] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.506614] INFO: bigquant: general_feature_extractor.v7 运行完成[0.006162s].
    [2019-01-29 17:08:05.509472] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:08:05.514366] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.515247] INFO: bigquant: input_features.v1 运行完成[0.005764s].
    [2019-01-29 17:08:05.518065] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-29 17:08:05.523972] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.524952] INFO: bigquant: instruments.v2 运行完成[0.006908s].
    [2019-01-29 17:08:05.527103] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:08:05.531198] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.531925] INFO: bigquant: input_features.v1 运行完成[0.004821s].
    [2019-01-29 17:08:05.534311] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:08:05.538189] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.538919] INFO: bigquant: input_features.v1 运行完成[0.004604s].
    [2019-01-29 17:08:05.543024] INFO: bigquant: cached.v3 开始运行..
    [2019-01-29 17:08:05.549438] INFO: bigquant: 命中缓存
    [2019-01-29 17:08:05.551093] INFO: bigquant: cached.v3 运行完成[0.00808s].
    [2019-01-29 17:08:05.557968] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-29 17:08:52.258840] INFO: 基础特征抽取: 年份 2014, 特征行数=141569
    [2019-01-29 17:09:35.642926] INFO: 基础特征抽取: 年份 2015, 特征行数=569698
    [2019-01-29 17:10:13.367252] INFO: 基础特征抽取: 年份 2016, 特征行数=641546
    [2019-01-29 17:11:32.565460] INFO: 基础特征抽取: 年份 2017, 特征行数=743233
    [2019-01-29 17:11:44.714231] INFO: 基础特征抽取: 年份 2018, 特征行数=816977
    [2019-01-29 17:11:47.276421] INFO: 基础特征抽取: 年份 2019, 特征行数=0
    [2019-01-29 17:11:47.305215] INFO: 基础特征抽取: 总行数: 2913023
    [2019-01-29 17:11:47.308502] INFO: bigquant: general_feature_extractor.v7 运行完成[221.750514s].
    [2019-01-29 17:11:47.312266] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:11:47.325354] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.326730] INFO: bigquant: input_features.v1 运行完成[0.014477s].
    [2019-01-29 17:11:47.329545] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-29 17:11:47.334760] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.335963] INFO: bigquant: instruments.v2 运行完成[0.006419s].
    [2019-01-29 17:11:47.338829] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
    [2019-01-29 17:11:47.345261] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.346888] INFO: bigquant: advanced_auto_labeler.v2 运行完成[0.008043s].
    [2019-01-29 17:11:47.358525] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-29 17:11:47.365145] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.366228] INFO: bigquant: general_feature_extractor.v7 运行完成[0.007696s].
    [2019-01-29 17:11:47.369365] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:11:47.378227] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.379417] INFO: bigquant: input_features.v1 运行完成[0.01004s].
    [2019-01-29 17:11:47.383103] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-29 17:11:47.388892] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.389915] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.006859s].
    [2019-01-29 17:11:47.392943] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:11:47.399194] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.400186] INFO: bigquant: input_features.v1 运行完成[0.007238s].
    [2019-01-29 17:11:47.403454] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-29 17:11:47.408570] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.409510] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.006062s].
    [2019-01-29 17:11:47.412413] INFO: bigquant: join.v3 开始运行..
    [2019-01-29 17:11:47.429635] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.430752] INFO: bigquant: join.v3 运行完成[0.018311s].
    [2019-01-29 17:11:47.433516] INFO: bigquant: filter.v3 开始运行..
    [2019-01-29 17:11:47.528756] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.529836] INFO: bigquant: filter.v3 运行完成[0.096307s].
    [2019-01-29 17:11:47.532978] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-29 17:11:47.538862] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.539921] INFO: bigquant: dropnan.v1 运行完成[0.006915s].
    [2019-01-29 17:11:47.549994] INFO: bigquant: stock_ranker_train.v5 开始运行..
    [2019-01-29 17:11:47.645773] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.648050] INFO: bigquant: stock_ranker_train.v5 运行完成[0.098022s].
    [2019-01-29 17:11:47.651050] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:11:47.658371] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.659557] INFO: bigquant: input_features.v1 运行完成[0.00853s].
    [2019-01-29 17:11:47.662536] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-29 17:11:47.668840] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.670248] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.00771s].
    [2019-01-29 17:11:47.674402] INFO: bigquant: cached.v3 开始运行..
    [2019-01-29 17:11:47.731124] INFO: bigquant: 命中缓存
    [2019-01-29 17:11:47.732752] INFO: bigquant: cached.v3 运行完成[0.058339s].
    [2019-01-29 17:11:47.735513] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-29 17:11:50.038413] INFO: derived_feature_extractor: 提取完成 ret1=return_90/return_10, 0.021s
    [2019-01-29 17:11:50.061002] INFO: derived_feature_extractor: 提取完成 ret2=return_80/return_10, 0.021s
    [2019-01-29 17:11:50.074692] INFO: derived_feature_extractor: 提取完成 ret3=return_5/return_0, 0.012s
    [2019-01-29 17:11:54.244030] INFO: derived_feature_extractor: 提取完成 mid=mean(close_0,14), 4.168s
    [2019-01-29 17:12:01.152997] INFO: derived_feature_extractor: 提取完成 atr=ta_atr(high_0, low_0, close_0, 14), 6.906s
    [2019-01-29 17:12:02.297576] INFO: derived_feature_extractor: /y_2014, 141569
    [2019-01-29 17:12:02.574686] INFO: derived_feature_extractor: /y_2015, 569698
    [2019-01-29 17:12:03.463373] INFO: derived_feature_extractor: /y_2016, 641546
    [2019-01-29 17:12:04.362990] INFO: derived_feature_extractor: /y_2017, 743233
    [2019-01-29 17:12:05.877658] INFO: derived_feature_extractor: /y_2018, 816977
    [2019-01-29 17:12:07.412928] INFO: bigquant: derived_feature_extractor.v3 运行完成[19.677385s].
    [2019-01-29 17:12:07.416398] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-29 17:12:07.422133] INFO: bigquant: 命中缓存
    [2019-01-29 17:12:07.423153] INFO: bigquant: input_features.v1 运行完成[0.006755s].
    [2019-01-29 17:12:07.427898] INFO: bigquant: cached.v3 开始运行..
    [2019-01-29 17:12:07.434072] INFO: bigquant: 命中缓存
    [2019-01-29 17:12:07.434968] INFO: bigquant: cached.v3 运行完成[0.007081s].
    [2019-01-29 17:12:07.438233] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-29 17:12:09.874992] INFO: derived_feature_extractor: 提取完成 cond2=(close_0>atr+mid) & (shift(close_0, 1) < shift(atr+mid, 1)), 1.286s
    [2019-01-29 17:12:09.953130] INFO: derived_feature_extractor: /y_2014, 104572
    [2019-01-29 17:12:10.424702] INFO: derived_feature_extractor: /y_2015, 547984
    [2019-01-29 17:12:11.318960] INFO: derived_feature_extractor: /y_2016, 625475
    [2019-01-29 17:12:12.269310] INFO: derived_feature_extractor: /y_2017, 702247
    [2019-01-29 17:12:13.792519] INFO: derived_feature_extractor: /y_2018, 802730
    [2019-01-29 17:12:15.355325] INFO: bigquant: derived_feature_extractor.v3 运行完成[7.917064s].
    [2019-01-29 17:12:15.357993] INFO: bigquant: filter.v3 开始运行..
    [2019-01-29 17:12:15.365446] INFO: filter: 使用表达式 st_status_0 == 0 and cond2>0 过滤
    [2019-01-29 17:12:15.505925] INFO: filter: 过滤 /y_2014, 6130/0/104572
    [2019-01-29 17:12:15.725592] INFO: filter: 过滤 /y_2015, 36187/0/547984
    [2019-01-29 17:12:16.011539] INFO: filter: 过滤 /y_2016, 34463/0/625475
    [2019-01-29 17:12:16.334279] INFO: filter: 过滤 /y_2017, 33863/0/702247
    [2019-01-29 17:12:16.785000] INFO: filter: 过滤 /y_2018, 34973/0/802730
    [2019-01-29 17:12:16.864148] INFO: bigquant: filter.v3 运行完成[1.506103s].
    [2019-01-29 17:12:16.867888] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-29 17:12:16.995810] INFO: dropnan: /y_2014, 6130/6130
    [2019-01-29 17:12:17.074006] INFO: dropnan: /y_2015, 36187/36187
    [2019-01-29 17:12:17.144360] INFO: dropnan: /y_2016, 34463/34463
    [2019-01-29 17:12:17.231165] INFO: dropnan: /y_2017, 33863/33863
    [2019-01-29 17:12:17.340479] INFO: dropnan: /y_2018, 34973/34973
    [2019-01-29 17:12:17.364744] INFO: dropnan: 行数: 145616/145616
    [2019-01-29 17:12:17.368467] INFO: bigquant: dropnan.v1 运行完成[0.500596s].
    [2019-01-29 17:12:17.371480] INFO: bigquant: stock_ranker_predict.v5 开始运行..
    [2019-01-29 17:12:17.771399] INFO: StockRanker: prepare data: prediction ..
    [2019-01-29 17:12:21.054196] INFO: stock_ranker_predict: 准备预测: 145616 行
    [2019-01-29 17:12:21.055395] INFO: stock_ranker_predict: 正在预测 ..
    [2019-01-29 17:12:31.237077] INFO: bigquant: stock_ranker_predict.v5 运行完成[13.865554s].
    [2019-01-29 17:12:31.239465] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-29 17:12:31.245102] INFO: bigquant: 命中缓存
    [2019-01-29 17:12:31.246445] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.006961s].
    [2019-01-29 17:12:31.249115] INFO: bigquant: join.v3 开始运行..
    [2019-01-29 17:12:31.254464] INFO: bigquant: 命中缓存
    [2019-01-29 17:12:31.255420] INFO: bigquant: join.v3 运行完成[0.006295s].
    [2019-01-29 17:12:31.258163] INFO: bigquant: filter.v3 开始运行..
    [2019-01-29 17:12:31.262993] INFO: bigquant: 命中缓存
    [2019-01-29 17:12:31.263968] INFO: bigquant: filter.v3 运行完成[0.005798s].
    [2019-01-29 17:12:31.266204] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-29 17:12:31.270962] INFO: bigquant: 命中缓存
    [2019-01-29 17:12:31.271786] INFO: bigquant: dropnan.v1 运行完成[0.005569s].
    [2019-01-29 17:12:31.274915] INFO: bigquant: random_forest_classifier.v1 开始运行..
    [2019-01-29 17:12:32.009287] INFO: bigquant: random_forest_classifier.v1 运行完成[0.734349s].
    [2019-01-29 17:12:32.013537] INFO: bigquant: cached.v3 开始运行..
    [2019-01-29 17:12:34.034605] INFO: bigquant: cached.v3 运行完成[2.021031s].
    [2019-01-29 17:12:34.052695] INFO: bigquant: backtest.v8 开始运行..
    [2019-01-29 17:12:34.055187] INFO: bigquant: biglearning backtest:V8.1.8
    [2019-01-29 17:12:34.056415] INFO: bigquant: product_type:stock by specified
    [2019-01-29 17:12:52.103332] INFO: bigquant: 读取股票行情完成:3646127
    [2019-01-29 17:13:32.329825] INFO: algo: TradingAlgorithm V1.4.5
    [2019-01-29 17:13:45.321664] INFO: algo: trading transform...
    [2019-01-29 17:13:49.432240] INFO: Performance: Simulated 975 trading days out of 975.
    [2019-01-29 17:13:49.433796] INFO: Performance: first open: 2015-01-05 09:30:00+00:00
    [2019-01-29 17:13:49.434680] INFO: Performance: last close: 2018-12-28 15:00:00+00:00
    
    • 收益率143.65%
    • 年化收益率25.88%
    • 基准收益率-14.8%
    • 阿尔法0.22
    • 贝塔0.21
    • 夏普比率1.35
    • 胜率0.58
    • 盈亏比1.12
    • 收益波动率15.81%
    • 信息比率0.06
    • 最大回撤21.77%
    [2019-01-29 17:13:53.885310] INFO: bigquant: backtest.v8 运行完成[79.832587s].