如何合并两个可视化策略

新手专区
标签: #<Tag:0x00007fcbfd992f20>

(user341) #1

如题,我有两个策略,如何让每天运行符合策略1的股票选出来,或者 符合策略2 股票也选出来。两个策略建模思想不同所以没法合并,但是我想同时调用这两个策略 分别给与百分之50的仓位 ,这两个策略都是可视化AI策略。应该如何合并回测呢》?


(iQuant) #2

您好,稍等,稍后我们给您写一个样例。


(user341) #3

haode


(user341) #4

emmmmmmmmmmmmmmmm 弱弱的问一下 样例写好了么


(iQuant) #5

正在写哈,我再帮您催一下。


(user341) #6

好的 谢谢 麻烦了


(XiaoyuDu) #7

你好,就在昨天,也有一位客户想要用合并策略的思路,并进行滚动训练出现了bug在社区提问,我们帮他修复了策略并添加了滚动训练方法,帖子链接如下,你可以复制粘贴进去看:

把两个策略合并成一个出错,求助。

我们修复后的策略已经提供链接附在后面,以下是解释:

解决合并策略的问题,需要理解策略运行的路径和数据的传导,如上图所示是一个效果不错的合并策略,左半边部分是用Stockranker 模型,右半边采用随机森林模型;
这里面有两个很需要注意的地方:

1) 两个模型分别使用不同的特质数据后,预测集数据的抽取需要有这两个模型分别使用因子的并集,这样才可以保证一个数据表可以分别被放进两个模型中而不会出现特征缺失无法预测的情况,以下圈出部分主要就是实现这个,当然,你也可以分别构建预测集数据而不是使用一个共同的模块抽取,此时就不需要取因子的并集;

2) 两个模型预测后输出的结果是两个表,分别告诉Trade模块需要购买哪些股票,因此在模型中使用m25模块去做了一个表的合并以实现输入可交易股票的合并

3)采用滚动训练模块可以提高模型对市场大势的适应性;而至于是每个模型各给50%还是平均分摊在两个模型共同生成的预测股票中,这是灵活的,可以在模型1预测出来的股票表中生成一列t名ype,数值为1,模型2预测出来的该列数值为2,对于标签为1或为2的在交易模块里面分配不同的权重等。

以上就是合并策略的主要思路,在同预测集数据抽取模块的时候需要注意两边使用的特征可能不同,在输入的交易模块的时候取一个交集(或者取并集,看你的思路),加油!


克隆策略

    {"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},{"Id":"-666","ModuleId":"BigQuantSpace.hyper_rolling_train.hyper_rolling_train-v1","ModuleParameters":[{"Name":"run","Value":"def bigquant_run(\n bq_graph,\n inputs,\n trading_days_market='CN', # 使用那个市场的交易日历, TODO\n train_instruments_mid1='m1', # 训练数据 证券代码列表 模块id\n train_instruments_mid2='m5', # 训练数据 证券代码列表 模块id\n test_instruments_mid='m9', # 测试数据 证券代码列表 模块id\n predict_mid='m25', # 预测 模块id\n trade_mid='m19', # 回测 模块id\n start_date='2015-01-01', # 数据开始日期\n end_date=T.live_run_param('trading_date', '2019-01-01'), # 数据结束日期\n train_update_days=250, # 更新周期,按交易日计算,每多少天更新一次\n train_update_days_for_live=None, #模拟实盘模式下的更新周期,按交易日计算,每多少天更新一次。如果需要在模拟实盘阶段使用不同的模型更新周期,可以设置这个参数\n train_data_min_days=250, # 最小数据天数,按交易日计算,所以第一个滚动的结束日期是 从开始日期到开始日期+最小数据天数\n train_data_max_days=250, # 最大数据天数,按交易日计算,0,表示没有限制,否则每一个滚动的开始日期=max(此滚动的结束日期-最大数据天数, 开始日期\n rolling_count_for_live=1, #实盘模式下滚动次数,模拟实盘模式下,取最后多少次滚动。一般在模拟实盘模式下,只用到最后一次滚动训练的模型,这里可以设置为1;如果你的滚动训练数据时间段很短,以至于期间可能没有训练数据,这里可以设置大一点。0表示没有限制\n):\n def merge_datasources(input_1):\n df_list = [ds.read_df() for ds in input_1]\n df = pd.concat(df_list)\n instrument_data = {\n 'start_date': df['date'].min().strftime('%Y-%m-%d'),\n 'end_date': df['date'].max().strftime('%Y-%m-%d'),\n 'instruments': list(set(df['instrument'])),\n }\n return Outputs(data=DataSource.write_df(df), instrument_data=DataSource.write_pickle(instrument_data))\n\n def gen_rolling_dates(trading_days_market, start_date, end_date, train_update_days, train_update_days_for_live, train_data_min_days, train_data_max_days, rolling_count_for_live):\n # 是否实盘模式\n tdays = list(D.trading_days(market=trading_days_market, start_date=start_date, end_date=end_date)['date'])\n is_live_run = T.live_run_param('trading_date', None) is not None\n\n if is_live_run and train_update_days_for_live:\n train_update_days = train_update_days_for_live\n\n rollings = []\n train_end_date = train_data_min_days\n while train_end_date < len(tdays):\n if train_data_max_days is not None:\n train_start_date = max(train_end_date - train_data_max_days, 0)\n else:\n train_start_date = start_date\n rollings.append({\n 'train_start_date': tdays[train_start_date].strftime('%Y-%m-%d'),\n 'train_end_date': tdays[train_end_date - 1].strftime('%Y-%m-%d'),\n 'test_start_date': tdays[train_end_date].strftime('%Y-%m-%d'),\n 'test_end_date': tdays[min(train_end_date + train_update_days, len(tdays)) - 1].strftime('%Y-%m-%d'),\n })\n train_end_date += train_update_days\n\n if not rollings:\n raise Exception('没有滚动需要执行,请检查配置')\n\n if is_live_run and rolling_count_for_live:\n rollings = rollings[-rolling_count_for_live:]\n\n return rollings\n\n g = bq_graph\n\n rolling_dates = gen_rolling_dates(\n trading_days_market, start_date, end_date, train_update_days, train_update_days_for_live, train_data_min_days, train_data_max_days, rolling_count_for_live)\n\n # 训练和预测\n results = []\n for rolling in rolling_dates:\n parameters = {}\n # 先禁用回测\n parameters[trade_mid + '.__enabled__'] = False\n parameters[train_instruments_mid1 + '.start_date'] = rolling['train_start_date']\n parameters[train_instruments_mid1 + '.end_date'] = rolling['train_end_date']\n parameters[train_instruments_mid2 + '.start_date'] = rolling['train_start_date']\n parameters[train_instruments_mid2 + '.end_date'] = rolling['train_end_date']\n parameters[test_instruments_mid + '.start_date'] = rolling['test_start_date']\n parameters[test_instruments_mid + '.end_date'] = rolling['test_end_date']\n print('------ rolling_train:', parameters)\n results.append(g.run(parameters))\n\n # 合并预测结果并回测\n mx = M.cached.v3(run=merge_datasources, input_1=[result[predict_mid].data_1 for result in results])\n parameters = {}\n parameters['*.__enabled__'] = False\n parameters[trade_mid + '.__enabled__'] = True\n parameters[trade_mid + '.instruments'] = mx.instrument_data\n parameters[trade_mid + '.options_data'] = mx.data\n\n trade = g.run(parameters)\n\n return {'rollings': results, 'trade': trade}\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"run_now","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bq_graph","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"bq_graph_port","NodeId":"-666"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-666"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-666"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-666"}],"OutputPortsInternal":[{"Name":"result","NodeId":"-666","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"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,-179,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='572,296,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='92,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='805.017822265625,417.2759704589844,200,200'/><NodePosition Node='-372' Position='833.4302978515625,522.6809692382812,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='729.1972045898438,-30.767051696777344,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,111,200,200'/><NodePosition Node='-9453' Position='919.9066162109375,638.835205078125,200,200'/><NodePosition Node='-175' Position='387,405,200,200'/><NodePosition Node='-9916' Position='1462,272,200,200'/><NodePosition Node='-9924' Position='973.0220947265625,-32.784889221191406,200,200'/><NodePosition Node='-9932' Position='865.1826171875,83.74186706542969,200,200'/><NodePosition Node='-10806' Position='789.5519409179688,200.97482299804688,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,977,200,200'/><NodePosition Node='-666' Position='1711,866,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 [4]:
    # 本代码由可视化策略环境自动生成 2019年1月30日 15:16
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 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)]
    
    
    g = T.Graph({
    
        'm1': 'M.instruments.v2',
        'm1.start_date': '2010-01-01',
        'm1.end_date': '2015-01-01',
        'm1.market': 'CN_STOCK_A',
        'm1.instrument_list': '',
        'm1.max_count': 0,
    
        'm2': 'M.advanced_auto_labeler.v2',
        'm2.instruments': T.Graph.OutputPort('m1.data'),
        'm2.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)
    """,
        'm2.start_date': '',
        'm2.end_date': '',
        'm2.benchmark': '000300.SHA',
        'm2.drop_na_label': True,
        'm2.cast_label_int': True,
    
        'm3': 'M.input_features.v1',
        'm3.features': """# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    return_0
    return_4
    return_5""",
    
        'm26': 'M.input_features.v1',
        'm26.features_ds': T.Graph.OutputPort('m3.data'),
        'm26.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',
        'm15.instruments': T.Graph.OutputPort('m1.data'),
        'm15.features': T.Graph.OutputPort('m26.data'),
        'm15.start_date': '',
        'm15.end_date': '',
    
        'm32': 'M.input_features.v1',
        'm32.features_ds': T.Graph.OutputPort('m3.data'),
        'm32.features': """
    # 输入参与训练的自定义列
    
    ret1
    ret2
    ret3
    """,
    
        'm9': 'M.instruments.v2',
        'm9.start_date': T.live_run_param('trading_date', '2015-01-01'),
        'm9.end_date': T.live_run_param('trading_date', '2019-01-01'),
        'm9.market': 'CN_STOCK_A',
        'm9.instrument_list': '',
        'm9.max_count': 0,
    
        'm4': 'M.input_features.v1',
        'm4.features': """
    # #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    return_0
    return_4
    return_5""",
    
        'm27': 'M.input_features.v1',
        'm27.features_ds': T.Graph.OutputPort('m4.data'),
        'm27.features': """
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    st_status_0
    return_90
    return_10
    return_80
    high_0
    low_0
    close_0
    open_0""",
    
        'm11': 'M.cached.v3',
        'm11.input_1': T.Graph.OutputPort('m26.data'),
        'm11.input_2': T.Graph.OutputPort('m27.data'),
        'm11.run': m11_run_bigquant_run,
        'm11.post_run': m11_post_run_bigquant_run,
        'm11.input_ports': '',
        'm11.params': '{}',
        'm11.output_ports': '',
    
        'm17': 'M.general_feature_extractor.v7',
        'm17.instruments': T.Graph.OutputPort('m9.data'),
        'm17.features': T.Graph.OutputPort('m11.data_1'),
        'm17.start_date': '',
        'm17.end_date': '',
    
        'm38': 'M.input_features.v1',
        'm38.features_ds': T.Graph.OutputPort('m4.data'),
        'm38.features': """
    # 输入参与训练的自定义列
    
    ret1
    ret2
    ret3
    """,
    
        'm5': 'M.instruments.v2',
        'm5.start_date': '2010-01-01',
        'm5.end_date': '2015-01-01',
        'm5.market': 'CN_STOCK_A',
        'm5.instrument_list': '',
        'm5.max_count': 0,
    
        'm10': 'M.advanced_auto_labeler.v2',
        'm10.instruments': T.Graph.OutputPort('m5.data'),
        'm10.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)
    """,
        'm10.start_date': '',
        'm10.end_date': '',
        'm10.benchmark': '000300.SHA',
        'm10.drop_na_label': True,
        'm10.cast_label_int': True,
        'm10.user_functions': {},
    
        'm42': 'M.general_feature_extractor.v7',
        'm42.instruments': T.Graph.OutputPort('m5.data'),
        'm42.features': T.Graph.OutputPort('m27.data'),
        'm42.start_date': '',
        'm42.end_date': '',
    
        'm28': 'M.input_features.v1',
        'm28.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',
        'm16.input_data': T.Graph.OutputPort('m15.data'),
        'm16.features': T.Graph.OutputPort('m28.data'),
        'm16.date_col': 'date',
        'm16.instrument_col': 'instrument',
        'm16.drop_na': True,
        'm16.remove_extra_columns': False,
    
        'm30': 'M.input_features.v1',
        'm30.features': """
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    cond2=(close_0>atr+mid) & (shift(close_0, 1) < shift(atr+mid, 1))
    """,
    
        'm29': 'M.derived_feature_extractor.v3',
        'm29.input_data': T.Graph.OutputPort('m16.data'),
        'm29.features': T.Graph.OutputPort('m30.data'),
        'm29.date_col': 'date',
        'm29.instrument_col': 'instrument',
        'm29.drop_na': True,
        'm29.remove_extra_columns': False,
    
        'm7': 'M.join.v3',
        'm7.data1': T.Graph.OutputPort('m2.data'),
        'm7.data2': T.Graph.OutputPort('m29.data'),
        'm7.on': 'date,instrument',
        'm7.how': 'inner',
        'm7.sort': False,
    
        'm39': 'M.filter.v3',
        'm39.input_data': T.Graph.OutputPort('m7.data'),
        'm39.expr': 'st_status_0 == 0 and cond2>0',
        'm39.output_left_data': False,
    
        'm13': 'M.dropnan.v1',
        'm13.input_data': T.Graph.OutputPort('m39.data'),
    
        'm6': 'M.stock_ranker_train.v5',
        'm6.training_ds': T.Graph.OutputPort('m13.data'),
        'm6.features': T.Graph.OutputPort('m32.data'),
        'm6.learning_algorithm': '排序',
        'm6.number_of_leaves': 30,
        'm6.minimum_docs_per_leaf': 1000,
        'm6.number_of_trees': 20,
        'm6.learning_rate': 0.1,
        'm6.max_bins': 1023,
        'm6.feature_fraction': 1,
        'm6.m_lazy_run': False,
    
        'm34': 'M.input_features.v1',
        'm34.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',
        'm20.input_data': T.Graph.OutputPort('m42.data'),
        'm20.features': T.Graph.OutputPort('m34.data'),
        'm20.date_col': 'date',
        'm20.instrument_col': 'instrument',
        'm20.drop_na': True,
        'm20.remove_extra_columns': False,
        'm20.user_functions': {},
    
        'm35': 'M.cached.v3',
        'm35.input_1': T.Graph.OutputPort('m28.data'),
        'm35.input_2': T.Graph.OutputPort('m34.data'),
        'm35.run': m35_run_bigquant_run,
        'm35.post_run': m35_post_run_bigquant_run,
        'm35.input_ports': '',
        'm35.params': '{}',
        'm35.output_ports': '',
    
        'm18': 'M.derived_feature_extractor.v3',
        'm18.input_data': T.Graph.OutputPort('m17.data'),
        'm18.features': T.Graph.OutputPort('m35.data_1'),
        'm18.date_col': 'date',
        'm18.instrument_col': 'instrument',
        'm18.drop_na': True,
        'm18.remove_extra_columns': False,
    
        'm37': 'M.input_features.v1',
        'm37.features': """
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    cond2=(close_0>atr+mid) & (shift(close_0, 1) < shift(atr+mid, 1))
    
    
    """,
    
        'm36': 'M.cached.v3',
        'm36.input_1': T.Graph.OutputPort('m30.data'),
        'm36.input_2': T.Graph.OutputPort('m37.data'),
        'm36.run': m36_run_bigquant_run,
        'm36.post_run': m36_post_run_bigquant_run,
        'm36.input_ports': '',
        'm36.params': '{}',
        'm36.output_ports': '',
    
        'm31': 'M.derived_feature_extractor.v3',
        'm31.input_data': T.Graph.OutputPort('m18.data'),
        'm31.features': T.Graph.OutputPort('m36.data_1'),
        'm31.date_col': 'date',
        'm31.instrument_col': 'instrument',
        'm31.drop_na': True,
        'm31.remove_extra_columns': False,
    
        'm41': 'M.filter.v3',
        'm41.input_data': T.Graph.OutputPort('m31.data'),
        'm41.expr': 'st_status_0 == 0 and cond2>0',
        'm41.output_left_data': False,
    
        'm14': 'M.dropnan.v1',
        'm14.input_data': T.Graph.OutputPort('m41.data'),
    
        'm8': 'M.stock_ranker_predict.v5',
        'm8.model': T.Graph.OutputPort('m6.model'),
        'm8.data': T.Graph.OutputPort('m14.data'),
        'm8.m_lazy_run': False,
    
        'm33': 'M.derived_feature_extractor.v3',
        'm33.input_data': T.Graph.OutputPort('m20.data'),
        'm33.features': T.Graph.OutputPort('m37.data'),
        'm33.date_col': 'date',
        'm33.instrument_col': 'instrument',
        'm33.drop_na': True,
        'm33.remove_extra_columns': False,
    
        'm21': 'M.join.v3',
        'm21.data1': T.Graph.OutputPort('m33.data'),
        'm21.data2': T.Graph.OutputPort('m10.data'),
        'm21.on': 'date,instrument',
        'm21.how': 'inner',
        'm21.sort': False,
    
        'm40': 'M.filter.v3',
        'm40.input_data': T.Graph.OutputPort('m21.data'),
        'm40.expr': 'st_status_0 == 0 and cond2>0',
        'm40.output_left_data': False,
    
        'm22': 'M.dropnan.v1',
        'm22.input_data': T.Graph.OutputPort('m40.data'),
    
        'm12': 'M.random_forest_classifier.v1',
        'm12.training_ds': T.Graph.OutputPort('m22.data'),
        'm12.features': T.Graph.OutputPort('m38.data'),
        'm12.predict_ds': T.Graph.OutputPort('m14.data'),
        'm12.iterations': 10,
        'm12.feature_fraction': 1,
        'm12.max_depth': 30,
        'm12.min_samples_per_leaf': 200,
        'm12.key_cols': 'date,instrument',
        'm12.workers': 2,
        'm12.other_train_parameters': {},
    
        'm25': 'M.cached.v3',
        'm25.input_1': T.Graph.OutputPort('m8.predictions'),
        'm25.input_2': T.Graph.OutputPort('m12.predictions'),
        'm25.run': m25_run_bigquant_run,
        'm25.post_run': m25_post_run_bigquant_run,
        'm25.input_ports': '',
        'm25.params': '{}',
        'm25.output_ports': '',
    
        'm19': 'M.trade.v4',
        'm19.instruments': T.Graph.OutputPort('m9.data'),
        'm19.options_data': T.Graph.OutputPort('m25.data_1'),
        'm19.start_date': '',
        'm19.end_date': '',
        'm19.handle_data': m19_handle_data_bigquant_run,
        'm19.prepare': m19_prepare_bigquant_run,
        'm19.initialize': m19_initialize_bigquant_run,
        'm19.volume_limit': 0.025,
        'm19.order_price_field_buy': 'open',
        'm19.order_price_field_sell': 'close',
        'm19.capital_base': 1000000,
        'm19.auto_cancel_non_tradable_orders': True,
        'm19.data_frequency': 'daily',
        'm19.price_type': '后复权',
        'm19.product_type': '股票',
        'm19.plot_charts': True,
        'm19.backtest_only': False,
        'm19.benchmark': '',
    })
    
    # g.run({})
    
    
    def m23_run_bigquant_run(
        bq_graph,
        inputs,
        trading_days_market='CN', # 使用那个市场的交易日历, TODO
        train_instruments_mid1='m1', # 训练数据 证券代码列表 模块id
        train_instruments_mid2='m5', # 训练数据 证券代码列表 模块id
        test_instruments_mid='m9', # 测试数据 证券代码列表 模块id
        predict_mid='m25', # 预测 模块id
        trade_mid='m19', # 回测 模块id
        start_date='2015-01-01', # 数据开始日期
        end_date=T.live_run_param('trading_date', '2019-01-01'), # 数据结束日期
        train_update_days=250, # 更新周期,按交易日计算,每多少天更新一次
        train_update_days_for_live=None, #模拟实盘模式下的更新周期,按交易日计算,每多少天更新一次。如果需要在模拟实盘阶段使用不同的模型更新周期,可以设置这个参数
        train_data_min_days=250, # 最小数据天数,按交易日计算,所以第一个滚动的结束日期是 从开始日期到开始日期+最小数据天数
        train_data_max_days=250, # 最大数据天数,按交易日计算,0,表示没有限制,否则每一个滚动的开始日期=max(此滚动的结束日期-最大数据天数, 开始日期
        rolling_count_for_live=1, #实盘模式下滚动次数,模拟实盘模式下,取最后多少次滚动。一般在模拟实盘模式下,只用到最后一次滚动训练的模型,这里可以设置为1;如果你的滚动训练数据时间段很短,以至于期间可能没有训练数据,这里可以设置大一点。0表示没有限制
    ):
        def merge_datasources(input_1):
            df_list = [ds.read_df() for ds in input_1]
            df = pd.concat(df_list)
            instrument_data = {
                'start_date': df['date'].min().strftime('%Y-%m-%d'),
                'end_date': df['date'].max().strftime('%Y-%m-%d'),
                'instruments': list(set(df['instrument'])),
            }
            return Outputs(data=DataSource.write_df(df), instrument_data=DataSource.write_pickle(instrument_data))
    
        def gen_rolling_dates(trading_days_market, start_date, end_date, train_update_days, train_update_days_for_live, train_data_min_days, train_data_max_days, rolling_count_for_live):
            # 是否实盘模式
            tdays = list(D.trading_days(market=trading_days_market, start_date=start_date, end_date=end_date)['date'])
            is_live_run = T.live_run_param('trading_date', None) is not None
    
            if is_live_run and train_update_days_for_live:
                train_update_days = train_update_days_for_live
    
            rollings = []
            train_end_date = train_data_min_days
            while train_end_date < len(tdays):
                if train_data_max_days is not None:
                    train_start_date = max(train_end_date - train_data_max_days, 0)
                else:
                    train_start_date = start_date
                rollings.append({
                    'train_start_date': tdays[train_start_date].strftime('%Y-%m-%d'),
                    'train_end_date': tdays[train_end_date - 1].strftime('%Y-%m-%d'),
                    'test_start_date': tdays[train_end_date].strftime('%Y-%m-%d'),
                    'test_end_date': tdays[min(train_end_date + train_update_days, len(tdays)) - 1].strftime('%Y-%m-%d'),
                })
                train_end_date += train_update_days
    
            if not rollings:
                raise Exception('没有滚动需要执行,请检查配置')
    
            if is_live_run and rolling_count_for_live:
                rollings = rollings[-rolling_count_for_live:]
    
            return rollings
    
        g = bq_graph
    
        rolling_dates = gen_rolling_dates(
            trading_days_market, start_date, end_date, train_update_days, train_update_days_for_live, train_data_min_days, train_data_max_days, rolling_count_for_live)
    
        # 训练和预测
        results = []
        for rolling in rolling_dates:
            parameters = {}
            # 先禁用回测
            parameters[trade_mid + '.__enabled__'] = False
            parameters[train_instruments_mid1 + '.start_date'] = rolling['train_start_date']
            parameters[train_instruments_mid1 + '.end_date'] = rolling['train_end_date']
            parameters[train_instruments_mid2 + '.start_date'] = rolling['train_start_date']
            parameters[train_instruments_mid2 + '.end_date'] = rolling['train_end_date']
            parameters[test_instruments_mid + '.start_date'] = rolling['test_start_date']
            parameters[test_instruments_mid + '.end_date'] = rolling['test_end_date']
            print('------ rolling_train:', parameters)
            results.append(g.run(parameters))
    
        # 合并预测结果并回测
        mx = M.cached.v3(run=merge_datasources, input_1=[result[predict_mid].data_1 for result in results])
        parameters = {}
        parameters['*.__enabled__'] = False
        parameters[trade_mid + '.__enabled__'] = True
        parameters[trade_mid + '.instruments'] = mx.instrument_data
        parameters[trade_mid + '.options_data'] = mx.data
    
        trade = g.run(parameters)
    
        return {'rollings': results, 'trade': trade}
    
    
    m23 = M.hyper_rolling_train.v1(
        run=m23_run_bigquant_run,
        run_now=True,
        bq_graph=g
    )
    
    [2019-01-30 14:37:06.496595] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-30 14:37:06.501728] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.502634] INFO: bigquant: instruments.v2 运行完成[0.006043s].
    [2019-01-30 14:37:06.505171] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
    [2019-01-30 14:37:06.509049] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.509910] INFO: bigquant: advanced_auto_labeler.v2 运行完成[0.004741s].
    [2019-01-30 14:37:06.511837] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.515974] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.516897] INFO: bigquant: input_features.v1 运行完成[0.005045s].
    [2019-01-30 14:37:06.519096] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.523082] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.523784] INFO: bigquant: input_features.v1 运行完成[0.004702s].
    [2019-01-30 14:37:06.529444] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-30 14:37:06.539348] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.540488] INFO: bigquant: general_feature_extractor.v7 运行完成[0.011038s].
    [2019-01-30 14:37:06.542593] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.547840] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.548713] INFO: bigquant: input_features.v1 运行完成[0.006111s].
    [2019-01-30 14:37:06.551365] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-30 14:37:06.556236] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.557387] INFO: bigquant: instruments.v2 运行完成[0.006013s].
    [2019-01-30 14:37:06.560620] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.565162] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.566290] INFO: bigquant: input_features.v1 运行完成[0.005678s].
    [2019-01-30 14:37:06.568873] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.572985] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.573819] INFO: bigquant: input_features.v1 运行完成[0.004938s].
    [2019-01-30 14:37:06.577546] INFO: bigquant: cached.v3 开始运行..
    [2019-01-30 14:37:06.581710] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.582506] INFO: bigquant: cached.v3 运行完成[0.004957s].
    [2019-01-30 14:37:06.588324] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-30 14:37:06.592663] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.593494] INFO: bigquant: general_feature_extractor.v7 运行完成[0.005177s].
    [2019-01-30 14:37:06.595798] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.599693] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.600441] INFO: bigquant: input_features.v1 运行完成[0.004642s].
    [2019-01-30 14:37:06.602858] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-30 14:37:06.606447] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.607187] INFO: bigquant: instruments.v2 运行完成[0.004326s].
    [2019-01-30 14:37:06.609622] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
    [2019-01-30 14:37:06.613586] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.614730] INFO: bigquant: advanced_auto_labeler.v2 运行完成[0.005101s].
    [2019-01-30 14:37:06.619498] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-30 14:37:06.623268] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.623971] INFO: bigquant: general_feature_extractor.v7 运行完成[0.004469s].
    [2019-01-30 14:37:06.625734] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.629329] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.630072] INFO: bigquant: input_features.v1 运行完成[0.004332s].
    [2019-01-30 14:37:06.632285] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-30 14:37:06.635611] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.636356] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.004066s].
    [2019-01-30 14:37:06.638379] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.642164] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.643114] INFO: bigquant: input_features.v1 运行完成[0.004729s].
    [2019-01-30 14:37:06.645768] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-30 14:37:06.650682] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.651732] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.005988s].
    [2019-01-30 14:37:06.654217] INFO: bigquant: join.v3 开始运行..
    [2019-01-30 14:37:06.658492] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.659331] INFO: bigquant: join.v3 运行完成[0.005101s].
    [2019-01-30 14:37:06.661510] INFO: bigquant: filter.v3 开始运行..
    [2019-01-30 14:37:06.665743] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.666588] INFO: bigquant: filter.v3 运行完成[0.005077s].
    [2019-01-30 14:37:06.668511] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-30 14:37:06.671914] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.672575] INFO: bigquant: dropnan.v1 运行完成[0.004064s].
    [2019-01-30 14:37:06.674839] INFO: bigquant: stock_ranker_train.v5 开始运行..
    [2019-01-30 14:37:06.680410] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.681425] INFO: bigquant: stock_ranker_train.v5 运行完成[0.006584s].
    [2019-01-30 14:37:06.683216] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.687005] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.687755] INFO: bigquant: input_features.v1 运行完成[0.004539s].
    [2019-01-30 14:37:06.689867] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-30 14:37:06.695188] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.695956] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.006079s].
    [2019-01-30 14:37:06.698955] INFO: bigquant: cached.v3 开始运行..
    [2019-01-30 14:37:06.702914] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.703601] INFO: bigquant: cached.v3 运行完成[0.004647s].
    [2019-01-30 14:37:06.705773] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-30 14:37:06.709372] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.710347] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.004573s].
    [2019-01-30 14:37:06.712043] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-30 14:37:06.715602] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.716351] INFO: bigquant: input_features.v1 运行完成[0.004314s].
    [2019-01-30 14:37:06.719314] INFO: bigquant: cached.v3 开始运行..
    [2019-01-30 14:37:06.723142] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.724036] INFO: bigquant: cached.v3 运行完成[0.004712s].
    [2019-01-30 14:37:06.725891] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-30 14:37:06.729440] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.730194] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.004298s].
    [2019-01-30 14:37:06.731883] INFO: bigquant: filter.v3 开始运行..
    [2019-01-30 14:37:06.735743] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.736557] INFO: bigquant: filter.v3 运行完成[0.00467s].
    [2019-01-30 14:37:06.738358] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-30 14:37:06.742206] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.743180] INFO: bigquant: dropnan.v1 运行完成[0.004797s].
    [2019-01-30 14:37:06.745777] INFO: bigquant: stock_ranker_predict.v5 开始运行..
    [2019-01-30 14:37:06.754446] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.755802] INFO: bigquant: stock_ranker_predict.v5 运行完成[0.010011s].
    [2019-01-30 14:37:06.757959] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-30 14:37:06.763252] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.764005] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.006074s].
    [2019-01-30 14:37:06.766203] INFO: bigquant: join.v3 开始运行..
    [2019-01-30 14:37:06.843971] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.845039] INFO: bigquant: join.v3 运行完成[0.07879s].
    [2019-01-30 14:37:06.847990] INFO: bigquant: filter.v3 开始运行..
    [2019-01-30 14:37:06.853440] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.855155] INFO: bigquant: filter.v3 运行完成[0.00715s].
    [2019-01-30 14:37:06.857520] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-30 14:37:06.862188] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.863044] INFO: bigquant: dropnan.v1 运行完成[0.005509s].
    [2019-01-30 14:37:06.865176] INFO: bigquant: random_forest_classifier.v1 开始运行..
    [2019-01-30 14:37:06.871757] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.872753] INFO: bigquant: random_forest_classifier.v1 运行完成[0.007574s].
    [2019-01-30 14:37:06.876356] INFO: bigquant: cached.v3 开始运行..
    [2019-01-30 14:37:06.881176] INFO: bigquant: 命中缓存
    [2019-01-30 14:37:06.881883] INFO: bigquant: cached.v3 运行完成[0.005533s].
    [2019-01-30 14:37:06.895383] INFO: bigquant: backtest.v8 开始运行..
    [2019-01-30 14:37:06.902176] INFO: bigquant: 命中缓存
    
    • 收益率143.65%
    • 年化收益率25.88%
    • 基准收益率-14.8%
    • 阿尔法0.22
    • 贝塔0.21
    • 夏普比率1.35
    • 胜率0.58
    • 盈亏比1.12
    • 收益波动率15.81%
    • 信息比率0.06
    • 最大回撤21.77%
    [2019-01-30 14:37:08.027473] INFO: bigquant: backtest.v8 运行完成[1.132063s].
    

    (user341) #8

    哇 谢谢作者大大,辛苦了 谢谢 真的感谢


    (user341) #9

    如果我没有自定义的因子 是不是直接删掉就好了? 我两个模型 特征也一样 算法参数不一样 该怎么简化呢?


    (XiaoyuDu) #10

    如果两个模型特征一样,那么你用一个输入特征列表代替他的多个输入特征列表即可,中间的那些用于连接不同模块特征的自定义模块也可以删除;
    当然,最好还是先看看他自定义模块里面的代码时是做什么的;


    (user341) #11

    好的 谢谢


    (tkyz) #12

    两个训练模型特征输入一样,用两个特征输入列表,共用一个预测模型会不会有什么影响或问题,是不是跟用一个输入特征列表一样?


    (XiaoyuDu) #13

    新年快乐,假如模型是一棵决策树,即使输入的特征一样,如果数据是不一样的,那么这两颗决策树也会不同,我们用已经训练好的树去做预测,这两颗树因为训练的数据不一样是不一样的树,怎么能只用一棵树去做预测呢?
    我们可以像你之前的那样,将两棵树的预测结果做一个交集或者并集或者别的处理。
    模型是一个泛指,指的是一种函数或者映射关系,而同种模型经过不同的数据训练后参数可能完全不同,所以在预测的时候怎么共用呢


    (tkyz) #14

    意思就是即便输入特征一样,也要用两个预测树?


    (XiaoyuDu) #15

    输入的特征一样但是数据不同就得用两棵呀,就是,一个用18年数据训练出来得模型,一个用17年数据训练出来得模型,参数不同。 预测得时候看你选择哪颗树了


    (tkyz) #16

    我输入的特征和数据都一样,就是训练的机器学习算法不一样,我试过用二个预测树和共用一个预测树,结果回测结果一样


    (XiaoyuDu) #17

    我好像之前搞错了你的意思,你是共用了一个回测引擎吗?怎么做到共用一个预测模块的? 有图或者示例吗?


    (tkyz) #18

    就是这个帖子的模板啊,共用一个回测,然后共用一个预测集,当然训练集数据就是机器学习算法不一样,其他训练特征和训练数据都一样。