【研报复现大赛】进阶篇(评审中)

策略分享
研报复现大赛
标签: #<Tag:0x00007f51fcf2c3f8> #<Tag:0x00007f51fcf2c2b8>

#1

一、 比赛时间安排:2019年6月24日-2019年7月22日

1. 研报复现期:6月24日-7月15日,参赛者可进行多次策略修改和优化
2. 策略提交期:6月24日-7月15日,将复现的策略分享至当前赛道的评论区,之后分享的策略不计入比赛
3. 策略评审期:7月15日-7月22日,策略展示获得获点赞和BigQuant专家评审
4. 排名公布期:7月23日公布排名榜单

二、 比赛简介:

精选3个研报进行复现,任务策略排名top5的参赛者将获得现金奖励。

三、比赛研报:

  1. 华泰单因子测试之动量类因子
  2. 华泰单因子测试之估值类因子
  3. 华泰单因子测试之财务质量因子

四、 策略编写规则

1. 参照模板策略构建策略:
    训练集时间范围:至少3年
    测试集时间范围:2017-01-01 - 2019-06-01
    标注及轮仓周期:自行选择
    算法: 自行选择

2.  策略构建要求:
    必须提交可视化策略;
    因子特征必须来自上述三篇研报;
    允许单因子/多因子组合;
    允许因子/标注的数据预处理: 缺失数据处理、中性化处理、去极值处理、标准化处理等;
    允许策略含止盈止损/大盘风控等功能;
    允许采用滚动训练。

五、 考核原则:

    每个用户只能通过本贴的回帖方式提交一次最满意的策略结果;
    总评分=策略点赞评分+专家评审评分+提交时间评分;
    专家评审评分原则:给定测试集时间范围的策略阶段收益率,夏普比率,1/最大回撤三个指标的综合排名
    策略排名top5的参赛者将获得现金奖励。

六、因子预处理链接:

(【宽客学院】因子预处理)


BigQuant研报复现PK赛(有奖竞赛)
(quanttt) #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":"-215: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":"-7117:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-7106:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-183:input_2","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-189:input_2","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-1228:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-7106:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"-7106:predict_ds","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"-183:input_1","SourceOutputPortId":"-222:data"},{"DestinationInputPortId":"-238:input_data","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-218:input_1","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"-1228:options_data","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-231:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-238:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-218:input_2","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-224:input_2","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-189:input_1","SourceOutputPortId":"-183:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-189:data"},{"DestinationInputPortId":"-224:input_1","SourceOutputPortId":"-218:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-224:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2014-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2016-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -22) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n#标准化\n(label - group_mean('',label))/group_std('',label)\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\n#where(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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#每日换手率作为权重对每日收益率求算术平均值\nsum(turn_0*return_0,22)/22\n\nfs_deducted_profit_ttm_0/market_cap_0\n(fs_bps_0*fs_paicl_up_capital_0)/market_cap_0\n\nfs_free_cash_flow_0/market_cap_0\nfs_roe_0\n\nfs_net_cash_flow_ttm_0/fs_total_profit_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-06-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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"120","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","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":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","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":"200","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","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":"-238"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1228","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n 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\n context.options['hold_days'] = 22\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n if context.trading_day_index%context.options['hold_days'] !=0:\n return\n \n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n layer = len(ranker_prediction)\n if layer==0:\n return\n ranker_prediction[\"组合\"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])\n ranker_prediction = ranker_prediction[ranker_prediction[\"组合\"]==1]\n stock_to_buy = list(ranker_prediction.instrument)\n \n # 定期轮仓卖出\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n stock_to_sell = [k for k in positions if k not in stock_to_buy]\n\n for instrument in stock_to_sell:\n context.order_target(context.symbol(instrument), 0)\n\n # 定期买入\n weight = [1/len(stock_to_buy) for k in stock_to_buy]\n #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])\n for i,instrument in enumerate(stock_to_buy):\n context.order_target_percent(context.symbol(instrument), weight[i])\n \n \n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1228"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1228","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"第一组股票轮仓回测结果","CommentCollapsed":false},{"Id":"-7106","ModuleId":"BigQuantSpace.stock_ranker.stock_ranker-v2","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":"slim_data","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"predict_ds","NodeId":"-7106"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-7106","OutputType":null},{"Name":"predictions","NodeId":"-7106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-7117","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nshift(close_0,-22)/close_0-1\nturn_0\n#close_0/shift(close_0, 22)-1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-7117"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-7117","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-183","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":3,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-183"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-183"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-183","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-189","ModuleId":"BigQuantSpace.standardlize.standardlize-v8","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-189"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-189"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-189","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-218","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":3,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-218"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-218"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-218","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-224","ModuleId":"BigQuantSpace.standardlize.standardlize-v8","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-224"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-224"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-224","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='247,83,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='48,342,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='725,-27,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='308,624,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='928,176,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='320,709,200,200'/><NodePosition Node='-86' Position='949,704,200,200'/><NodePosition Node='-215' Position='438,245,200,200'/><NodePosition Node='-222' Position='450,341,200,200'/><NodePosition Node='-231' Position='930,294,200,200'/><NodePosition Node='-238' Position='935,395,200,200'/><NodePosition Node='-1228' Position='958,947,200,200'/><NodePosition Node='-7106' Position='493,798,200,200'/><NodePosition Node='-7117' Position='557,91,200,200'/><NodePosition Node='-183' Position='462,427,200,200'/><NodePosition Node='-189' Position='460,516,200,200'/><NodePosition Node='-218' Position='941,504,200,200'/><NodePosition Node='-224' Position='945,603,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 [43]:
    # 本代码由可视化策略环境自动生成 2019年7月11日 09:29
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m23_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))
    
        context.options['hold_days'] = 22
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m23_handle_data_bigquant_run(context, data):
    
        if context.trading_day_index%context.options['hold_days'] !=0:
            return
        
        # 按日期过滤得到今日的预测数据
        ranker_prediction = context.ranker_prediction[
              context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
        layer = len(ranker_prediction)
        if layer==0:
            return
        ranker_prediction["组合"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])
        ranker_prediction = ranker_prediction[ranker_prediction["组合"]==1]
        stock_to_buy = list(ranker_prediction.instrument)
        
        # 定期轮仓卖出
        positions = {e.symbol: p.amount * p.last_sale_price
                     for e, p in context.portfolio.positions.items()}
       
        stock_to_sell = [k for k in positions if k not in stock_to_buy]
    
        for instrument in stock_to_sell:
            context.order_target(context.symbol(instrument), 0)
    
        # 定期买入
        weight = [1/len(stock_to_buy) for k in stock_to_buy]
        #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])
        for i,instrument in enumerate(stock_to_buy):
            context.order_target_percent(context.symbol(instrument), weight[i])
            
            
            
    
    # 回测引擎:准备数据,只执行一次
    def m23_prepare_bigquant_run(context):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2014-01-01',
        end_date='2016-12-31',
        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/develop/datasource/deprecated/history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
    
    # 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -22) / shift(open, -1)
    
    # 极值处理:用1%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    #标准化
    (label - group_mean('',label))/group_std('',label)
    # 将分数映射到分类,这里使用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="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    #每日换手率作为权重对每日收益率求算术平均值
    sum(turn_0*return_0,22)/22
    
    fs_deducted_profit_ttm_0/market_cap_0
    (fs_bps_0*fs_paicl_up_capital_0)/market_cap_0
    
    fs_free_cash_flow_0/market_cap_0
    fs_roe_0
    
    fs_net_cash_flow_ttm_0/fs_total_profit_0
    """
    )
    
    m6 = M.input_features.v1(
        features_ds=m3.data,
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    shift(close_0,-22)/close_0-1
    turn_0
    #close_0/shift(close_0, 22)-1"""
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m6.data,
        start_date='',
        end_date='',
        before_start_days=120
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m6.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m4 = M.winsorize.v6(
        input_1=m16.data,
        input_2=m3.data,
        columns_input=[],
        median_deviate=3
    )
    
    m11 = M.standardlize.v8(
        input_1=m4.data,
        input_2=m3.data,
        columns_input=[]
    )
    
    m7 = M.join.v3(
        data1=m2.data,
        data2=m11.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m13 = M.dropnan.v1(
        input_data=m7.data
    )
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2017-01-01'),
        end_date=T.live_run_param('trading_date', '2019-06-01'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m17 = M.general_feature_extractor.v7(
        instruments=m9.data,
        features=m6.data,
        start_date='',
        end_date='',
        before_start_days=200
    )
    
    m18 = M.derived_feature_extractor.v3(
        input_data=m17.data,
        features=m6.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m5 = M.winsorize.v6(
        input_1=m18.data,
        input_2=m6.data,
        columns_input=[],
        median_deviate=3
    )
    
    m8 = M.standardlize.v8(
        input_1=m5.data,
        input_2=m6.data,
        columns_input=[]
    )
    
    m14 = M.dropnan.v1(
        input_data=m8.data
    )
    
    m12 = M.stock_ranker.v2(
        training_ds=m13.data,
        features=m3.data,
        predict_ds=m14.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,
        slim_data=True
    )
    
    m23 = M.trade.v4(
        instruments=m9.data,
        options_data=m12.predictions,
        start_date='',
        end_date='',
        initialize=m23_initialize_bigquant_run,
        handle_data=m23_handle_data_bigquant_run,
        prepare=m23_prepare_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='close',
        order_price_field_sell='close',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000300.SHA'
    )
    
    设置测试数据集,查看训练迭代过程的NDCG
    bigcharts-data-start/{"__id":"bigchart-d207538981f84898a38562cdc4ef9dbe","__type":"tabs"}/bigcharts-data-end
    • 收益率-31.19%
    • 年化收益率-14.85%
    • 基准收益率9.66%
    • 阿尔法-0.19
    • 贝塔0.67
    • 夏普比率-0.96
    • 胜率0.43
    • 盈亏比0.61
    • 收益波动率18.17%
    • 信息比率-0.09
    • 最大回撤49.09%
    bigcharts-data-start/{"__id":"bigchart-7dd41fef0f964e848c731c25ea29279e","__type":"tabs"}/bigcharts-data-end

    (songqiang) #3
    克隆策略

      {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215: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":"-7117:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-7106:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-183:input_2","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-1228:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-7106:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"-7106:predict_ds","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"-183:input_1","SourceOutputPortId":"-222:data"},{"DestinationInputPortId":"-238:input_data","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-218:input_1","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"-1228:options_data","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-231:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-238:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-218:input_2","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-183:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-218:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -22) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n#标准化\n(label - group_mean('',label))/group_std('',label)\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\n#where(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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#每日换手率作为权重对每日收益率求算术平均值\nsum(turn_0*return_0,66)/66\nmarket_cap_0\nfs_roe_0\nfs_net_cash_flow_ttm_0/fs_total_profit_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-06-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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"120","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","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":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","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":"200","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","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":"-238"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1228","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n 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\n context.options['hold_days'] = 22\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n if context.trading_day_index%context.options['hold_days'] !=0:\n return\n \n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n layer = len(ranker_prediction)\n if layer==0:\n return\n ranker_prediction[\"组合\"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])\n ranker_prediction = ranker_prediction[ranker_prediction[\"组合\"]==1]\n stock_to_buy = list(ranker_prediction.instrument)\n \n # 定期轮仓卖出\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n stock_to_sell = [k for k in positions if k not in stock_to_buy]\n\n for instrument in stock_to_sell:\n context.order_target(context.symbol(instrument), 0)\n\n # 定期买入\n weight = [1/len(stock_to_buy) for k in stock_to_buy]\n #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])\n for i,instrument in enumerate(stock_to_buy):\n context.order_target_percent(context.symbol(instrument), weight[i])\n \n \n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1228"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1228","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"第一组股票轮仓回测结果","CommentCollapsed":false},{"Id":"-7106","ModuleId":"BigQuantSpace.stock_ranker.stock_ranker-v2","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":"slim_data","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"predict_ds","NodeId":"-7106"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-7106","OutputType":null},{"Name":"predictions","NodeId":"-7106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-7117","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nshift(close_0,-22)/close_0-1\nturn_0\n#close_0/shift(close_0, 22)-1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-7117"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-7117","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-183","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":3,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-183"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-183"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-183","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-218","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":"3","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-218"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-218"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-218","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='247,83,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='48,342,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='725,-27,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='248.46875,495.015625,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='983.810546875,107.787109375,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='486.19140625,584.9765625,200,200'/><NodePosition Node='-86' Position='863.4237670898438,563.853515625,200,200'/><NodePosition Node='-215' Position='399.552734375,179.26759338378906,200,200'/><NodePosition Node='-222' Position='432.63671875,275.267578125,200,200'/><NodePosition Node='-231' Position='897.75390625,218.34571838378906,200,200'/><NodePosition Node='-238' Position='886.630859375,323.06640625,200,200'/><NodePosition Node='-1228' Position='602.0526733398438,847.78125,200,200'/><NodePosition Node='-7106' Position='517.8046875,691.33984375,200,200'/><NodePosition Node='-7117' Position='557,91,200,200'/><NodePosition Node='-183' Position='466.9609375,348.86529541015625,200,200'/><NodePosition Node='-218' Position='937.279296875,425.8652038574219,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
      In [1]:
      # 本代码由可视化策略环境自动生成 2019年7月12日 18:25
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # 回测引擎:初始化函数,只执行一次
      def m23_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))
      
          context.options['hold_days'] = 22
      
      # 回测引擎:每日数据处理函数,每天执行一次
      def m23_handle_data_bigquant_run(context, data):
      
          if context.trading_day_index%context.options['hold_days'] !=0:
              return
          
          # 按日期过滤得到今日的预测数据
          ranker_prediction = context.ranker_prediction[
                context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
          layer = len(ranker_prediction)
          if layer==0:
              return
          ranker_prediction["组合"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])
          ranker_prediction = ranker_prediction[ranker_prediction["组合"]==1]
          stock_to_buy = list(ranker_prediction.instrument)
          
          # 定期轮仓卖出
          positions = {e.symbol: p.amount * p.last_sale_price
                       for e, p in context.portfolio.positions.items()}
         
          stock_to_sell = [k for k in positions if k not in stock_to_buy]
      
          for instrument in stock_to_sell:
              context.order_target(context.symbol(instrument), 0)
      
          # 定期买入
          weight = [1/len(stock_to_buy) for k in stock_to_buy]
          #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])
          for i,instrument in enumerate(stock_to_buy):
              context.order_target_percent(context.symbol(instrument), weight[i])
              
              
              
      
      # 回测引擎:准备数据,只执行一次
      def m23_prepare_bigquant_run(context):
          pass
      
      
      m1 = M.instruments.v2(
          start_date='2015-01-01',
          end_date='2015-12-31',
          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/develop/datasource/deprecated/history_data.html
      #   添加benchmark_前缀,可使用对应的benchmark数据
      # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
      
      # 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
      shift(close, -22) / shift(open, -1)
      
      # 极值处理:用1%和99%分位的值做clip
      clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
      
      #标准化
      (label - group_mean('',label))/group_std('',label)
      # 将分数映射到分类,这里使用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="""# #号开始的表示注释
      # 多个特征,每行一个,可以包含基础特征和衍生特征
      #每日换手率作为权重对每日收益率求算术平均值
      sum(turn_0*return_0,66)/66
      market_cap_0
      fs_roe_0
      fs_net_cash_flow_ttm_0/fs_total_profit_0
      """
      )
      
      m6 = M.input_features.v1(
          features_ds=m3.data,
          features="""
      # #号开始的表示注释,注释需单独一行
      # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
      shift(close_0,-22)/close_0-1
      turn_0
      #close_0/shift(close_0, 22)-1"""
      )
      
      m15 = M.general_feature_extractor.v7(
          instruments=m1.data,
          features=m6.data,
          start_date='',
          end_date='',
          before_start_days=120
      )
      
      m16 = M.derived_feature_extractor.v3(
          input_data=m15.data,
          features=m6.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=True,
          remove_extra_columns=False
      )
      
      m4 = M.winsorize.v6(
          input_1=m16.data,
          input_2=m3.data,
          columns_input=[],
          median_deviate=3
      )
      
      m7 = M.join.v3(
          data1=m2.data,
          data2=m4.data,
          on='date,instrument',
          how='inner',
          sort=False
      )
      
      m13 = M.dropnan.v1(
          input_data=m7.data
      )
      
      m9 = M.instruments.v2(
          start_date=T.live_run_param('trading_date', '2018-01-01'),
          end_date=T.live_run_param('trading_date', '2019-06-01'),
          market='CN_STOCK_A',
          instrument_list='',
          max_count=0
      )
      
      m17 = M.general_feature_extractor.v7(
          instruments=m9.data,
          features=m6.data,
          start_date='',
          end_date='',
          before_start_days=200
      )
      
      m18 = M.derived_feature_extractor.v3(
          input_data=m17.data,
          features=m6.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=True,
          remove_extra_columns=False
      )
      
      m5 = M.winsorize.v6(
          input_1=m18.data,
          input_2=m6.data,
          columns_input=[],
          median_deviate=3
      )
      
      m14 = M.dropnan.v1(
          input_data=m5.data
      )
      
      m12 = M.stock_ranker.v2(
          training_ds=m13.data,
          features=m3.data,
          predict_ds=m14.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,
          slim_data=True
      )
      
      m23 = M.trade.v4(
          instruments=m9.data,
          options_data=m12.predictions,
          start_date='',
          end_date='',
          initialize=m23_initialize_bigquant_run,
          handle_data=m23_handle_data_bigquant_run,
          prepare=m23_prepare_bigquant_run,
          volume_limit=0.025,
          order_price_field_buy='close',
          order_price_field_sell='close',
          capital_base=1000000,
          auto_cancel_non_tradable_orders=True,
          data_frequency='daily',
          price_type='后复权',
          product_type='股票',
          plot_charts=True,
          backtest_only=False,
          benchmark='000300.SHA'
      )
      
      设置测试数据集,查看训练迭代过程的NDCG
      bigcharts-data-start/{"__id":"bigchart-b5ee10382bab4969969eaf94470bd228","__type":"tabs"}/bigcharts-data-end
      设置测试数据集,查看训练迭代过程的NDCG
      bigcharts-data-start/{"__id":"bigchart-8288979fb40e4cc5aa303c8d5d488fae","__type":"tabs"}/bigcharts-data-end
      • 收益率2.31%
      • 年化收益率1.7%
      • 基准收益率-9.95%
      • 阿尔法0.05
      • 贝塔0.61
      • 夏普比率0.03
      • 胜率0.57
      • 盈亏比1.01
      • 收益波动率19.06%
      • 信息比率0.03
      • 最大回撤23.87%
      bigcharts-data-start/{"__id":"bigchart-1c6c3bb0937c4b788505cdaca0d105e0","__type":"tabs"}/bigcharts-data-end

      (safeness) #4
      克隆策略

        {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215: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":"-7117:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-7106:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-183:input_2","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-1228:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-7106:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"-7106:predict_ds","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"-183:input_1","SourceOutputPortId":"-222:data"},{"DestinationInputPortId":"-238:input_data","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-218:input_1","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"-1228:options_data","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-231:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-238:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-218:input_2","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-183:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-218:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2013-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -22) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n#标准化\n(label - group_mean('',label))/group_std('',label)\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\n#where(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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#每日换手率作为权重对每日收益率求算术平均值\nsum(turn_0*return_0,132)/132\n(fs_bps_0*fs_paicl_up_capital_0)/market_cap_0\nfs_net_cash_flow_ttm_0/market_cap_0\nfs_free_cash_flow_0/market_cap_0\nfs_roa_ttm_0\nfs_net_profit_margin_ttm_0\nfs_net_cash_flow_ttm_0/fs_total_profit_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-06-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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"120","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","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":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","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":"200","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","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":"-238"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1228","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n 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\n context.options['hold_days'] = 22\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n if context.trading_day_index%context.options['hold_days'] !=0:\n return\n \n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n layer = len(ranker_prediction)\n if layer==0:\n return\n ranker_prediction[\"组合\"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])\n ranker_prediction = ranker_prediction[ranker_prediction[\"组合\"]==1]\n stock_to_buy = list(ranker_prediction.instrument)\n \n # 定期轮仓卖出\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n stock_to_sell = [k for k in positions if k not in stock_to_buy]\n\n for instrument in stock_to_sell:\n context.order_target(context.symbol(instrument), 0)\n\n # 定期买入\n weight = [1/len(stock_to_buy) for k in stock_to_buy]\n #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])\n for i,instrument in enumerate(stock_to_buy):\n context.order_target_percent(context.symbol(instrument), weight[i])\n \n \n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1228"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1228","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"第一组股票轮仓回测结果","CommentCollapsed":false},{"Id":"-7106","ModuleId":"BigQuantSpace.stock_ranker.stock_ranker-v2","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":"slim_data","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"predict_ds","NodeId":"-7106"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-7106","OutputType":null},{"Name":"predictions","NodeId":"-7106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-7117","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nshift(close_0,-22)/close_0-1\nturn_0\n#close_0/shift(close_0, 22)-1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-7117"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-7117","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-183","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":3,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-183"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-183"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-183","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-218","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":3,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-218"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-218"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-218","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='217.84927368164062,120.72447967529297,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='48,342,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='725,-27,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='308,624,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='928,176,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='320,709,200,200'/><NodePosition Node='-86' Position='949,704,200,200'/><NodePosition Node='-215' Position='438,245,200,200'/><NodePosition Node='-222' Position='450,341,200,200'/><NodePosition Node='-231' Position='899.1345825195312,306.00323486328125,200,200'/><NodePosition Node='-238' Position='859.5510864257812,436.15399169921875,200,200'/><NodePosition Node='-1228' Position='760.8038940429688,898.987060546875,200,200'/><NodePosition Node='-7106' Position='493,798,200,200'/><NodePosition Node='-7117' Position='557,91,200,200'/><NodePosition Node='-183' Position='462,427,200,200'/><NodePosition Node='-218' Position='863.8363037109375,558.8719482421875,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 [ ]:
        # 本代码由可视化策略环境自动生成 2019年7月13日 09:22
        # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
        
        
        # 回测引擎:初始化函数,只执行一次
        def m23_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))
        
            context.options['hold_days'] = 22
        
        # 回测引擎:每日数据处理函数,每天执行一次
        def m23_handle_data_bigquant_run(context, data):
        
            if context.trading_day_index%context.options['hold_days'] !=0:
                return
            
            # 按日期过滤得到今日的预测数据
            ranker_prediction = context.ranker_prediction[
                  context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
            layer = len(ranker_prediction)
            if layer==0:
                return
            ranker_prediction["组合"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])
            ranker_prediction = ranker_prediction[ranker_prediction["组合"]==1]
            stock_to_buy = list(ranker_prediction.instrument)
            
            # 定期轮仓卖出
            positions = {e.symbol: p.amount * p.last_sale_price
                         for e, p in context.portfolio.positions.items()}
           
            stock_to_sell = [k for k in positions if k not in stock_to_buy]
        
            for instrument in stock_to_sell:
                context.order_target(context.symbol(instrument), 0)
        
            # 定期买入
            weight = [1/len(stock_to_buy) for k in stock_to_buy]
            #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])
            for i,instrument in enumerate(stock_to_buy):
                context.order_target_percent(context.symbol(instrument), weight[i])
                
                
                
        
        # 回测引擎:准备数据,只执行一次
        def m23_prepare_bigquant_run(context):
            pass
        
        
        m1 = M.instruments.v2(
            start_date='2013-01-01',
            end_date='2015-12-31',
            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/develop/datasource/deprecated/history_data.html
        #   添加benchmark_前缀,可使用对应的benchmark数据
        # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
        
        # 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
        shift(close, -22) / shift(open, -1)
        
        # 极值处理:用1%和99%分位的值做clip
        clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
        
        #标准化
        (label - group_mean('',label))/group_std('',label)
        # 将分数映射到分类,这里使用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="""# #号开始的表示注释
        # 多个特征,每行一个,可以包含基础特征和衍生特征
        #每日换手率作为权重对每日收益率求算术平均值
        sum(turn_0*return_0,132)/132
        (fs_bps_0*fs_paicl_up_capital_0)/market_cap_0
        fs_net_cash_flow_ttm_0/market_cap_0
        fs_free_cash_flow_0/market_cap_0
        fs_roa_ttm_0
        fs_net_profit_margin_ttm_0
        fs_net_cash_flow_ttm_0/fs_total_profit_0"""
        )
        
        m6 = M.input_features.v1(
            features_ds=m3.data,
            features="""
        # #号开始的表示注释,注释需单独一行
        # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
        shift(close_0,-22)/close_0-1
        turn_0
        #close_0/shift(close_0, 22)-1"""
        )
        
        m15 = M.general_feature_extractor.v7(
            instruments=m1.data,
            features=m6.data,
            start_date='',
            end_date='',
            before_start_days=120
        )
        
        m16 = M.derived_feature_extractor.v3(
            input_data=m15.data,
            features=m6.data,
            date_col='date',
            instrument_col='instrument',
            drop_na=True,
            remove_extra_columns=False
        )
        
        m4 = M.winsorize.v6(
            input_1=m16.data,
            input_2=m3.data,
            columns_input=[],
            median_deviate=3
        )
        
        m7 = M.join.v3(
            data1=m2.data,
            data2=m4.data,
            on='date,instrument',
            how='inner',
            sort=False
        )
        
        m13 = M.dropnan.v1(
            input_data=m7.data
        )
        
        m9 = M.instruments.v2(
            start_date=T.live_run_param('trading_date', '2017-01-01'),
            end_date=T.live_run_param('trading_date', '2019-06-01'),
            market='CN_STOCK_A',
            instrument_list='',
            max_count=0
        )
        
        m17 = M.general_feature_extractor.v7(
            instruments=m9.data,
            features=m6.data,
            start_date='',
            end_date='',
            before_start_days=200
        )
        
        m18 = M.derived_feature_extractor.v3(
            input_data=m17.data,
            features=m6.data,
            date_col='date',
            instrument_col='instrument',
            drop_na=True,
            remove_extra_columns=False
        )
        
        m5 = M.winsorize.v6(
            input_1=m18.data,
            input_2=m6.data,
            columns_input=[],
            median_deviate=3
        )
        
        m14 = M.dropnan.v1(
            input_data=m5.data
        )
        
        m12 = M.stock_ranker.v2(
            training_ds=m13.data,
            features=m3.data,
            predict_ds=m14.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,
            slim_data=True
        )
        
        m23 = M.trade.v4(
            instruments=m9.data,
            options_data=m12.predictions,
            start_date='',
            end_date='',
            initialize=m23_initialize_bigquant_run,
            handle_data=m23_handle_data_bigquant_run,
            prepare=m23_prepare_bigquant_run,
            volume_limit=0.025,
            order_price_field_buy='close',
            order_price_field_sell='close',
            capital_base=1000000,
            auto_cancel_non_tradable_orders=True,
            data_frequency='daily',
            price_type='后复权',
            product_type='股票',
            plot_charts=True,
            backtest_only=False,
            benchmark='000300.SHA'
        )
        
        设置测试数据集,查看训练迭代过程的NDCG
        bigcharts-data-start/{"__id":"bigchart-5a24d0582a8e4f93a69c925ad414e2fe","__type":"tabs"}/bigcharts-data-end
        设置测试数据集,查看训练迭代过程的NDCG
        bigcharts-data-start/{"__id":"bigchart-fe50d88fc6ed40158175a405a17b495b","__type":"tabs"}/bigcharts-data-end

        (mlzhang) #5
        克隆策略

          {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215: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":"-7117:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-7106:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-1228:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-2618:input_1","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-7106:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"-7106:predict_ds","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-2618:input_3","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-222:data"},{"DestinationInputPortId":"-238:input_data","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-143:input_1","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-143:data_1"},{"DestinationInputPortId":"-1228:options_data","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-2618:input_2","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-231:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-238:features","SourceOutputPortId":"-7117:data"}],"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":"2016-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -22) / 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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#每日换手率作为权重对每日收益率求算术平均值\nsum((close_0/shift(close_0,1)-1) * turn_0,22)/22\nreturn_5\nreturn_10\nreturn_20\n# avg_amount_0/avg_amount_5\n# avg_amount_5/avg_amount_20\n# rank_avg_amount_0/rank_avg_amount_5\n# rank_avg_amount_5/rank_avg_amount_10\n# rank_return_0\n# rank_return_5\nrank_return_10\n# rank_return_0/rank_return_5\n# rank_return_5/rank_return_10\n# pe_ttm_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-06-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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"120","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","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":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","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":"200","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","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":"-238"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-143","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, N):\n # 示例代码如下。在这里编写您的代码\n df=input_1.read_df().set_index('date')\n def cal(x):\n weights = np.array([ np.exp(-1*j/N/4) for j in range(N*22)])\n result = np.dot(x,weights)\n return result\n df1 = df.groupby('instrument').rolling(N*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(N)+'m'})\n df1 = df1.merge(df.reset_index(),on=['date','instrument'])\n data_1 = DataSource.write_df(df1)\n return Outputs(data_1=data_1)\n\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":"{\n 'N':1\n}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-143"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-143"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-143"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-143","OutputType":null},{"Name":"data_2","NodeId":"-143","OutputType":null},{"Name":"data_3","NodeId":"-143","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1228","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n 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\n context.options['hold_days'] = 22\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n if context.trading_day_index%context.options['hold_days'] !=0:\n return\n \n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n layer = len(ranker_prediction)\n if layer==0:\n return\n ranker_prediction[\"组合\"] = pd.cut(ranker_prediction['position'],bins=[0,layer/5,2*layer/5,3*layer/5,4*layer/5,layer],labels=[1,2,3,4,5])\n ranker_prediction = ranker_prediction[ranker_prediction[\"组合\"]==1]\n stock_to_buy = list(ranker_prediction.instrument)\n \n # 定期轮仓卖出\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n stock_to_sell = [k for k in positions if k not in stock_to_buy]\n\n for instrument in stock_to_sell:\n context.order_target(context.symbol(instrument), 0)\n\n # 定期买入\n weight = [1/len(stock_to_buy) for k in stock_to_buy]\n #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])\n for i,instrument in enumerate(stock_to_buy):\n context.order_target_percent(context.symbol(instrument), weight[i])\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1228"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1228","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"第一组股票轮仓回测结果","CommentCollapsed":false},{"Id":"-7106","ModuleId":"BigQuantSpace.stock_ranker.stock_ranker-v2","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":"slim_data","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"predict_ds","NodeId":"-7106"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-7106","OutputType":null},{"Name":"predictions","NodeId":"-7106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-7117","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nshift(close_0,-22)/close_0-1\n\n#估值因子\nfs_net_profit_ttm_0/market_cap_0 # EP\n# fs_deducted_profit_ttm_0/market_cap_0 # EPCUT\n# fs_net_cash_flow_ttm_0/market_cap_0 # NCFP\n# fs_net_cash_flow_0/market_cap_0 # OCFP\n# fs_free_cash_flow_0/market_cap_0 # FCFP","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-7117"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-7117","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2618","ModuleId":"BigQuantSpace.StockRanker_grouptest.StockRanker_grouptest-v3","ModuleParameters":[{"Name":"ret_column","Value":"shift(close_0,-22)/close_0-1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"N","Value":"22","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-2618"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-2618"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-2618"}],"OutputPortsInternal":[],"UsePreviousResults":false,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='246,85,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='48,342,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='723,-25,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='317,487,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1012,246,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='291,660,200,200'/><NodePosition Node='-86' Position='1023,748,200,200'/><NodePosition Node='-215' Position='438,245,200,200'/><NodePosition Node='-222' Position='450,341,200,200'/><NodePosition Node='-231' Position='1019,373,200,200'/><NodePosition Node='-238' Position='1019,482,200,200'/><NodePosition Node='-143' Position='1013,604,200,200'/><NodePosition Node='-1228' Position='949,979,200,200'/><NodePosition Node='-7106' Position='524,843,200,200'/><NodePosition Node='-7117' Position='555,95,200,200'/><NodePosition Node='-2618' Position='505,993,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年7月13日 09:53
          # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
          
          
          # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
          def m5_run_bigquant_run(input_1, N):
              # 示例代码如下。在这里编写您的代码
              df=input_1.read_df().set_index('date')
              def cal(x):
                  weights = np.array([ np.exp(-1*j/N/4) for j in range(N*22)])
                  result = np.dot(x,weights)
                  return result
              df1 = df.groupby('instrument').rolling(N*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(N)+'m'})
              df1 = df1.merge(df.reset_index(),on=['date','instrument'])
              data_1 = DataSource.write_df(df1)
              return Outputs(data_1=data_1)
          
          
          # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
          def m5_post_run_bigquant_run(outputs):
              return outputs
          
          # 回测引擎:初始化函数,只执行一次
          def m23_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))
          
              context.options['hold_days'] = 22
          
          # 回测引擎:每日数据处理函数,每天执行一次
          def m23_handle_data_bigquant_run(context, data):
          
              if context.trading_day_index%context.options['hold_days'] !=0:
                  return
              
              # 按日期过滤得到今日的预测数据
              ranker_prediction = context.ranker_prediction[
                    context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
              layer = len(ranker_prediction)
              if layer==0:
                  return
              ranker_prediction["组合"] = pd.cut(ranker_prediction['position'],bins=[0,layer/5,2*layer/5,3*layer/5,4*layer/5,layer],labels=[1,2,3,4,5])
              ranker_prediction = ranker_prediction[ranker_prediction["组合"]==1]
              stock_to_buy = list(ranker_prediction.instrument)
              
              # 定期轮仓卖出
              positions = {e.symbol: p.amount * p.last_sale_price
                           for e, p in context.portfolio.positions.items()}
             
              stock_to_sell = [k for k in positions if k not in stock_to_buy]
          
              for instrument in stock_to_sell:
                  context.order_target(context.symbol(instrument), 0)
          
              # 定期买入
              weight = [1/len(stock_to_buy) for k in stock_to_buy]
              #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])
              for i,instrument in enumerate(stock_to_buy):
                  context.order_target_percent(context.symbol(instrument), weight[i])
          
          # 回测引擎:准备数据,只执行一次
          def m23_prepare_bigquant_run(context):
              pass
          
          
          m1 = M.instruments.v2(
              start_date='2010-01-01',
              end_date='2016-12-31',
              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/develop/datasource/deprecated/history_data.html
          #   添加benchmark_前缀,可使用对应的benchmark数据
          # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
          
          # 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
          shift(close, -22) / 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="""# #号开始的表示注释
          # 多个特征,每行一个,可以包含基础特征和衍生特征
          #每日换手率作为权重对每日收益率求算术平均值
          sum((close_0/shift(close_0,1)-1) * turn_0,22)/22
          return_5
          return_10
          return_20
          # avg_amount_0/avg_amount_5
          # avg_amount_5/avg_amount_20
          # rank_avg_amount_0/rank_avg_amount_5
          # rank_avg_amount_5/rank_avg_amount_10
          # rank_return_0
          # rank_return_5
          rank_return_10
          # rank_return_0/rank_return_5
          # rank_return_5/rank_return_10
          # pe_ttm_0
          """
          )
          
          m6 = M.input_features.v1(
              features_ds=m3.data,
              features="""
          # #号开始的表示注释,注释需单独一行
          # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
          shift(close_0,-22)/close_0-1
          
          #估值因子
          fs_net_profit_ttm_0/market_cap_0  # EP
          # fs_deducted_profit_ttm_0/market_cap_0  # EPCUT
          # fs_net_cash_flow_ttm_0/market_cap_0  # NCFP
          # fs_net_cash_flow_0/market_cap_0  # OCFP
          # fs_free_cash_flow_0/market_cap_0  # FCFP"""
          )
          
          m15 = M.general_feature_extractor.v7(
              instruments=m1.data,
              features=m6.data,
              start_date='',
              end_date='',
              before_start_days=120
          )
          
          m16 = M.derived_feature_extractor.v3(
              input_data=m15.data,
              features=m6.data,
              date_col='date',
              instrument_col='instrument',
              drop_na=True,
              remove_extra_columns=False
          )
          
          m7 = M.join.v3(
              data1=m2.data,
              data2=m16.data,
              on='date,instrument',
              how='inner',
              sort=False
          )
          
          m13 = M.dropnan.v1(
              input_data=m7.data
          )
          
          m9 = M.instruments.v2(
              start_date=T.live_run_param('trading_date', '2017-01-01'),
              end_date=T.live_run_param('trading_date', '2019-06-01'),
              market='CN_STOCK_A',
              instrument_list='',
              max_count=0
          )
          
          m17 = M.general_feature_extractor.v7(
              instruments=m9.data,
              features=m6.data,
              start_date='',
              end_date='',
              before_start_days=200
          )
          
          m18 = M.derived_feature_extractor.v3(
              input_data=m17.data,
              features=m6.data,
              date_col='date',
              instrument_col='instrument',
              drop_na=True,
              remove_extra_columns=False
          )
          
          m5 = M.cached.v3(
              input_1=m18.data,
              run=m5_run_bigquant_run,
              post_run=m5_post_run_bigquant_run,
              input_ports='',
              params="""{
              'N':1
          }""",
              output_ports=''
          )
          
          m14 = M.dropnan.v1(
              input_data=m5.data_1
          )
          
          m12 = M.stock_ranker.v2(
              training_ds=m13.data,
              features=m3.data,
              predict_ds=m14.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,
              slim_data=True
          )
          
          m23 = M.trade.v4(
              instruments=m9.data,
              options_data=m12.predictions,
              start_date='',
              end_date='',
              initialize=m23_initialize_bigquant_run,
              handle_data=m23_handle_data_bigquant_run,
              prepare=m23_prepare_bigquant_run,
              volume_limit=0.025,
              order_price_field_buy='open',
              order_price_field_sell='close',
              capital_base=1000000,
              auto_cancel_non_tradable_orders=True,
              data_frequency='daily',
              price_type='后复权',
              product_type='股票',
              plot_charts=True,
              backtest_only=False,
              benchmark='000300.SHA'
          )
          
          m4 = M.StockRanker_grouptest.v3(
              input_1=m9.data,
              input_2=m12.predictions,
              input_3=m14.data,
              ret_column='shift(close_0,-22)/close_0-1',
              N=22,
              m_cached=False
          )
          
          设置测试数据集,查看训练迭代过程的NDCG
          bigcharts-data-start/{"__id":"bigchart-3e580eed09994abeb0df726e68d37ab3","__type":"tabs"}/bigcharts-data-end
          设置测试数据集,查看训练迭代过程的NDCG
          bigcharts-data-start/{"__id":"bigchart-f05034cf481b4115bff9b5d3b8d4cd10","__type":"tabs"}/bigcharts-data-end
          • 收益率-17.13%
          • 年化收益率-7.76%
          • 基准收益率9.66%
          • 阿尔法-0.11
          • 贝塔0.64
          • 夏普比率-0.58
          • 胜率0.45
          • 盈亏比1.14
          • 收益波动率16.73%
          • 信息比率-0.06
          • 最大回撤33.64%
          bigcharts-data-start/{"__id":"bigchart-4a698c1aa6f4434bbbfe252a930aef5f","__type":"tabs"}/bigcharts-data-end

          (yangziriver) #8
          克隆策略

          StockRanker多因子选股策略

            {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-274: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":"-274:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-281:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-288:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-295:features","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-6060:options_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"DestinationInputPortId":"-288:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-6060: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":"-281:input_data","SourceOutputPortId":"-274:data"},{"DestinationInputPortId":"-1793:input_1","SourceOutputPortId":"-274:data"},{"DestinationInputPortId":"-1802:data2","SourceOutputPortId":"-281:data"},{"DestinationInputPortId":"-295:input_data","SourceOutputPortId":"-288:data"},{"DestinationInputPortId":"-6621:input_1","SourceOutputPortId":"-288:data"},{"DestinationInputPortId":"-6919:data2","SourceOutputPortId":"-295:data"},{"DestinationInputPortId":"-1802:data1","SourceOutputPortId":"-1793:data_1"},{"DestinationInputPortId":"-6919:data1","SourceOutputPortId":"-6621:data_1"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-1802:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-6919:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:features","SourceOutputPortId":"-7220:data"}],"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":"2016-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-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, -26) / 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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#fs_roa_0\n#fs_bps_0\n#fs_roe_0\nlog(market_cap_0)\nshift(turn_0,22)/shift(turn_0,528)\nshift(return_0,22)\nsum(turn_0*return_0,22)/22\n(market_cap_0/fs_net_profit_ttm_0)/fs_net_profit_yoy_0\nreturn_0\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-06-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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-274","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":"760","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-274"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-274"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-274","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-281","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":"False","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":"-281"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-281"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-281","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-288","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":"760","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-288"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-288"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-288","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-295","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":"False","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":"-295"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-295"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-295","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-6060","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n 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 = 20\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 0.1\n context.options['hold_days'] = 26\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n\n # 1. 资金分配\n # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金\n # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)\n is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)\n cash_avg = context.portfolio.portfolio_value / context.options['hold_days']\n cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)\n cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.perf_tracker.position_tracker.positions.items()}\n\n # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰\n if not is_staging and cash_for_sell > 0:\n equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}\n instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(\n lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))\n # print('rank order for sell %s' % instruments)\n for instrument in instruments:\n context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n if cash_for_sell <= 0:\n break\n\n # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票\n buy_cash_weights = context.stock_weights\n buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n for i, instrument in enumerate(buy_instruments):\n cash = cash_for_buy * buy_cash_weights[i]\n if cash > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash = max_cash_per_instrument - positions.get(instrument, 0)\n if cash > 0:\n context.order_value(context.symbol(instrument), cash)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-6060"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-6060"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-6060"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-6060"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-6060"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-6060","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1793","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, N=3):\n # 示例代码如下。在这里编写您的代码\n df=input_1.read_df().set_index('date')\n def cal(x):\n weights = np.array([ np.exp(-1*j/3/4) for j in range(3*22)])\n result = np.dot(x,weights)\n return result\n df1 = df.groupby('instrument').rolling(3*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(3)+'m'})\n df1 = df1.merge(df.reset_index(),on=['date','instrument'])\n data_1 = DataSource.write_df(df1)\n return Outputs(data_1=data_1)\n\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":"{\n 'N':1\n}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-1793"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-1793"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-1793"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-1793","OutputType":null},{"Name":"data_2","NodeId":"-1793","OutputType":null},{"Name":"data_3","NodeId":"-1793","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"exp函数","CommentCollapsed":true},{"Id":"-6621","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, N=3):\n # 示例代码如下。在这里编写您的代码\n df=input_1.read_df().set_index('date')\n def cal(x):\n weights = np.array([ np.exp(-1*j/3/4) for j in range(3*22)])\n result = np.dot(x,weights)\n return result\n df1 = df.groupby('instrument').rolling(3*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(3)+'m'})\n df1 = df1.merge(df.reset_index(),on=['date','instrument'])\n data_1 = DataSource.write_df(df1)\n return Outputs(data_1=data_1)\n\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":"{\n 'N':1\n}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-6621"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-6621"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-6621"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-6621","OutputType":null},{"Name":"data_2","NodeId":"-6621","OutputType":null},{"Name":"data_3","NodeId":"-6621","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"IsPartOfPartialRun":null,"Comment":"exp函数","CommentCollapsed":true},{"Id":"-1802","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":"-1802"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-1802"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1802","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-6919","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":"-6919"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-6919"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-6919","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-7220","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"log(market_cap_0)\nshift(turn_0,22)/shift(turn_0,528)\nshift(return_0,22)\nsum(turn_0*return_0,22)/22\n(market_cap_0/fs_net_profit_ttm_0)/fs_net_profit_yoy_0\nexp_wgt_return_3m","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-7220"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-7220","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='211,64,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='70,185,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='766,21,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-43' Position='728,699,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='331,536,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='930,775,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1074,108,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='517,619,200,200'/><NodePosition Node='-86' Position='1059,560,200,200'/><NodePosition Node='-274' Position='381,188,200,200'/><NodePosition Node='-281' Position='385,280,200,200'/><NodePosition Node='-288' Position='1078,215,200,200'/><NodePosition Node='-295' Position='1128,306,200,200'/><NodePosition Node='-6060' Position='988,870,200,200'/><NodePosition Node='-1793' Position='333,359,200,200'/><NodePosition Node='-6621' Position='926,378,200,200'/><NodePosition Node='-1802' Position='477,445,200,200'/><NodePosition Node='-6919' Position='1067,474,200,200'/><NodePosition Node='-7220' Position='722,516,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":false}
            In [7]:
            # 本代码由可视化策略环境自动生成 2019年7月13日 22:26
            # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
            
            
            # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
            def m5_run_bigquant_run(input_1, N=3):
                # 示例代码如下。在这里编写您的代码
                df=input_1.read_df().set_index('date')
                def cal(x):
                    weights = np.array([ np.exp(-1*j/3/4) for j in range(3*22)])
                    result = np.dot(x,weights)
                    return result
                df1 = df.groupby('instrument').rolling(3*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(3)+'m'})
                df1 = df1.merge(df.reset_index(),on=['date','instrument'])
                data_1 = DataSource.write_df(df1)
                return Outputs(data_1=data_1)
            
            
            # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
            def m5_post_run_bigquant_run(outputs):
                return outputs
            
            # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
            def m10_run_bigquant_run(input_1, N=3):
                # 示例代码如下。在这里编写您的代码
                df=input_1.read_df().set_index('date')
                def cal(x):
                    weights = np.array([ np.exp(-1*j/3/4) for j in range(3*22)])
                    result = np.dot(x,weights)
                    return result
                df1 = df.groupby('instrument').rolling(3*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(3)+'m'})
                df1 = df1.merge(df.reset_index(),on=['date','instrument'])
                data_1 = DataSource.write_df(df1)
                return Outputs(data_1=data_1)
            
            
            # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
            def m10_post_run_bigquant_run(outputs):
                return outputs
            
            # 回测引擎:初始化函数,只执行一次
            def m4_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 = 20
                # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
                context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
                # 设置每只股票占用的最大资金比例
                context.max_cash_per_instrument = 0.1
                context.options['hold_days'] = 26
            
            # 回测引擎:每日数据处理函数,每天执行一次
            def m4_handle_data_bigquant_run(context, data):
                # 按日期过滤得到今日的预测数据
                ranker_prediction = context.ranker_prediction[
                    context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
            
                # 1. 资金分配
                # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
                # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
                is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
                cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
                cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
                cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
                positions = {e.symbol: p.amount * p.last_sale_price
                             for e, p in context.perf_tracker.position_tracker.positions.items()}
            
                # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰
                if not is_staging and cash_for_sell > 0:
                    equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
                    instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
                            lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
                    # print('rank order for sell %s' % instruments)
                    for instrument in instruments:
                        context.order_target(context.symbol(instrument), 0)
                        cash_for_sell -= positions[instrument]
                        if cash_for_sell <= 0:
                            break
            
                # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票
                buy_cash_weights = context.stock_weights
                buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
                max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
                for i, instrument in enumerate(buy_instruments):
                    cash = cash_for_buy * buy_cash_weights[i]
                    if cash > max_cash_per_instrument - positions.get(instrument, 0):
                        # 确保股票持仓量不会超过每次股票最大的占用资金量
                        cash = max_cash_per_instrument - positions.get(instrument, 0)
                    if cash > 0:
                        context.order_value(context.symbol(instrument), cash)
            
            # 回测引擎:准备数据,只执行一次
            def m4_prepare_bigquant_run(context):
                pass
            
            
            m1 = M.instruments.v2(
                start_date='2010-01-01',
                end_date='2016-12-31',
                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, -26) / 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="""# #号开始的表示注释
            # 多个特征,每行一个,可以包含基础特征和衍生特征
            #fs_roa_0
            #fs_bps_0
            #fs_roe_0
            log(market_cap_0)
            shift(turn_0,22)/shift(turn_0,528)
            shift(return_0,22)
            sum(turn_0*return_0,22)/22
            (market_cap_0/fs_net_profit_ttm_0)/fs_net_profit_yoy_0
            return_0
            
            """
            )
            
            m15 = M.general_feature_extractor.v7(
                instruments=m1.data,
                features=m3.data,
                start_date='',
                end_date='',
                before_start_days=760
            )
            
            m5 = M.cached.v3(
                input_1=m15.data,
                run=m5_run_bigquant_run,
                post_run=m5_post_run_bigquant_run,
                input_ports='',
                params="""{
                'N':1
            }""",
                output_ports=''
            )
            
            m16 = M.derived_feature_extractor.v3(
                input_data=m15.data,
                features=m3.data,
                date_col='date',
                instrument_col='instrument',
                drop_na=False,
                remove_extra_columns=False
            )
            
            m11 = M.join.v3(
                data1=m5.data_1,
                data2=m16.data,
                on='date,instrument',
                how='inner',
                sort=False
            )
            
            m7 = M.join.v3(
                data1=m2.data,
                data2=m11.data,
                on='date,instrument',
                how='inner',
                sort=False
            )
            
            m13 = M.dropnan.v1(
                input_data=m7.data
            )
            
            m9 = M.instruments.v2(
                start_date=T.live_run_param('trading_date', '2017-01-01'),
                end_date=T.live_run_param('trading_date', '2019-06-01'),
                market='CN_STOCK_A',
                instrument_list='',
                max_count=0
            )
            
            m17 = M.general_feature_extractor.v7(
                instruments=m9.data,
                features=m3.data,
                start_date='',
                end_date='',
                before_start_days=760
            )
            
            m18 = M.derived_feature_extractor.v3(
                input_data=m17.data,
                features=m3.data,
                date_col='date',
                instrument_col='instrument',
                drop_na=False,
                remove_extra_columns=False
            )
            
            m10 = M.cached.v3(
                input_1=m17.data,
                run=m10_run_bigquant_run,
                post_run=m10_post_run_bigquant_run,
                input_ports='',
                params="""{
                'N':1
            }""",
                output_ports=''
            )
            
            m12 = M.join.v3(
                data1=m10.data_1,
                data2=m18.data,
                on='date,instrument',
                how='inner',
                sort=False
            )
            
            m14 = M.dropnan.v1(
                input_data=m12.data
            )
            
            m19 = M.input_features.v1(
                features="""log(market_cap_0)
            shift(turn_0,22)/shift(turn_0,528)
            shift(return_0,22)
            sum(turn_0*return_0,22)/22
            (market_cap_0/fs_net_profit_ttm_0)/fs_net_profit_yoy_0
            exp_wgt_return_3m"""
            )
            
            m6 = M.stock_ranker_train.v5(
                training_ds=m13.data,
                features=m19.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
            )
            
            m8 = M.stock_ranker_predict.v5(
                model=m6.model,
                data=m14.data,
                m_lazy_run=False
            )
            
            m4 = M.trade.v4(
                instruments=m9.data,
                options_data=m8.predictions,
                start_date='',
                end_date='',
                initialize=m4_initialize_bigquant_run,
                handle_data=m4_handle_data_bigquant_run,
                prepare=m4_prepare_bigquant_run,
                volume_limit=0.025,
                order_price_field_buy='open',
                order_price_field_sell='close',
                capital_base=1000000,
                auto_cancel_non_tradable_orders=True,
                data_frequency='daily',
                price_type='后复权',
                product_type='股票',
                plot_charts=True,
                backtest_only=False,
                benchmark=''
            )
            
            设置测试数据集,查看训练迭代过程的NDCG
            bigcharts-data-start/{"__id":"bigchart-9a5b0302a17840b0b07fb16dabc35fae","__type":"tabs"}/bigcharts-data-end
            • 收益率-12.81%
            • 年化收益率-5.72%
            • 基准收益率9.66%
            • 阿尔法-0.07
            • 贝塔0.76
            • 夏普比率-0.19
            • 胜率0.54
            • 盈亏比0.91
            • 收益波动率26.81%
            • 信息比率-0.02
            • 最大回撤48.14%
            bigcharts-data-start/{"__id":"bigchart-c0f8b9f105424a93960a98e717220fb0","__type":"tabs"}/bigcharts-data-end
            In [ ]:
             
            

            (bd4c6a) #9
            克隆策略

              {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215: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":"-7117:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-7106:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-183:input_2","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-189:input_2","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-1228:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-7106:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"-7106:predict_ds","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"-183:input_1","SourceOutputPortId":"-222:data"},{"DestinationInputPortId":"-238:input_data","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-218:input_1","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"-1228:options_data","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-231:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-238:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-218:input_2","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-224:input_2","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-189:input_1","SourceOutputPortId":"-183:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-189:data"},{"DestinationInputPortId":"-224:input_1","SourceOutputPortId":"-218:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-224:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2014-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2016-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -22) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n#标准化\n(label - group_mean('',label))/group_std('',label)\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\n#where(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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#每日换手率作为权重对每日收益率求算术平均值\nsum(turn_0*return_0,264)/264\nfs_net_profit_ttm_0/market_cap_0\n(market_cap_0/fs_net_profit_ttm_0)/fs_net_profit_yoy_0\nfs_roe_0\nfs_roa_ttm_0\nfs_net_profit_margin_ttm_0\nfs_net_cash_flow_ttm_0/fs_total_profit_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-06-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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"120","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","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":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","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":"200","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","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":"-238"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1228","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n 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\n context.options['hold_days'] = 22\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n if context.trading_day_index%context.options['hold_days'] !=0:\n return\n \n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n layer = len(ranker_prediction)\n if layer==0:\n return\n ranker_prediction[\"组合\"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])\n ranker_prediction = ranker_prediction[ranker_prediction[\"组合\"]==1]\n stock_to_buy = list(ranker_prediction.instrument)\n \n # 定期轮仓卖出\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n stock_to_sell = [k for k in positions if k not in stock_to_buy]\n\n for instrument in stock_to_sell:\n context.order_target(context.symbol(instrument), 0)\n\n # 定期买入\n weight = [1/len(stock_to_buy) for k in stock_to_buy]\n #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])\n for i,instrument in enumerate(stock_to_buy):\n context.order_target_percent(context.symbol(instrument), weight[i])\n \n \n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1228"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1228","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"第一组股票轮仓回测结果","CommentCollapsed":false},{"Id":"-7106","ModuleId":"BigQuantSpace.stock_ranker.stock_ranker-v2","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":"slim_data","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"predict_ds","NodeId":"-7106"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-7106","OutputType":null},{"Name":"predictions","NodeId":"-7106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-7117","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nshift(close_0,-22)/close_0-1\nturn_0\n#close_0/shift(close_0, 22)-1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-7117"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-7117","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-183","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":3,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-183"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-183"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-183","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-189","ModuleId":"BigQuantSpace.standardlize.standardlize-v8","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-189"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-189"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-189","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-218","ModuleId":"BigQuantSpace.winsorize.winsorize-v6","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"median_deviate","Value":3,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-218"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-218"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-218","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-224","ModuleId":"BigQuantSpace.standardlize.standardlize-v8","ModuleParameters":[{"Name":"columns_input","Value":"[]","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-224"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-224"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-224","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='247,83,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='48,342,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='725,-27,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='308,624,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='928,176,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='320,709,200,200'/><NodePosition Node='-86' Position='949,704,200,200'/><NodePosition Node='-215' Position='422,212,200,200'/><NodePosition Node='-222' Position='462,323,200,200'/><NodePosition Node='-231' Position='930,294,200,200'/><NodePosition Node='-238' Position='935,395,200,200'/><NodePosition Node='-1228' Position='728,940,200,200'/><NodePosition Node='-7106' Position='493,798,200,200'/><NodePosition Node='-7117' Position='557,91,200,200'/><NodePosition Node='-183' Position='348,427,200,200'/><NodePosition Node='-189' Position='460,516,200,200'/><NodePosition Node='-218' Position='1026,493,200,200'/><NodePosition Node='-224' Position='945,604,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 [2]:
              # 本代码由可视化策略环境自动生成 2019年7月13日 17:11
              # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
              
              
              # 回测引擎:初始化函数,只执行一次
              def m23_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))
              
                  context.options['hold_days'] = 22
              
              # 回测引擎:每日数据处理函数,每天执行一次
              def m23_handle_data_bigquant_run(context, data):
              
                  if context.trading_day_index%context.options['hold_days'] !=0:
                      return
                  
                  # 按日期过滤得到今日的预测数据
                  ranker_prediction = context.ranker_prediction[
                        context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
                  layer = len(ranker_prediction)
                  if layer==0:
                      return
                  ranker_prediction["组合"] = pd.cut(ranker_prediction['position'],bins=[0,0.2*layer,2*layer/5,3*layer/5,0.8*layer,layer],labels=[1,2,3,4,5])
                  ranker_prediction = ranker_prediction[ranker_prediction["组合"]==1]
                  stock_to_buy = list(ranker_prediction.instrument)
                  
                  # 定期轮仓卖出
                  positions = {e.symbol: p.amount * p.last_sale_price
                               for e, p in context.portfolio.positions.items()}
                 
                  stock_to_sell = [k for k in positions if k not in stock_to_buy]
              
                  for instrument in stock_to_sell:
                      context.order_target(context.symbol(instrument), 0)
              
                  # 定期买入
                  weight = [1/len(stock_to_buy) for k in stock_to_buy]
                  #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])
                  for i,instrument in enumerate(stock_to_buy):
                      context.order_target_percent(context.symbol(instrument), weight[i])
                      
                      
                      
              
              # 回测引擎:准备数据,只执行一次
              def m23_prepare_bigquant_run(context):
                  pass
              
              
              m1 = M.instruments.v2(
                  start_date='2014-01-01',
                  end_date='2016-12-31',
                  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/develop/datasource/deprecated/history_data.html
              #   添加benchmark_前缀,可使用对应的benchmark数据
              # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
              
              # 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
              shift(close, -22) / shift(open, -1)
              
              # 极值处理:用1%和99%分位的值做clip
              clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
              
              #标准化
              (label - group_mean('',label))/group_std('',label)
              # 将分数映射到分类,这里使用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="""# #号开始的表示注释
              # 多个特征,每行一个,可以包含基础特征和衍生特征
              #每日换手率作为权重对每日收益率求算术平均值
              sum(turn_0*return_0,264)/264
              fs_net_profit_ttm_0/market_cap_0
              (market_cap_0/fs_net_profit_ttm_0)/fs_net_profit_yoy_0
              fs_roe_0
              fs_roa_ttm_0
              fs_net_profit_margin_ttm_0
              fs_net_cash_flow_ttm_0/fs_total_profit_0"""
              )
              
              m6 = M.input_features.v1(
                  features_ds=m3.data,
                  features="""
              # #号开始的表示注释,注释需单独一行
              # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
              shift(close_0,-22)/close_0-1
              turn_0
              #close_0/shift(close_0, 22)-1"""
              )
              
              m15 = M.general_feature_extractor.v7(
                  instruments=m1.data,
                  features=m6.data,
                  start_date='',
                  end_date='',
                  before_start_days=120
              )
              
              m16 = M.derived_feature_extractor.v3(
                  input_data=m15.data,
                  features=m6.data,
                  date_col='date',
                  instrument_col='instrument',
                  drop_na=True,
                  remove_extra_columns=False
              )
              
              m4 = M.winsorize.v6(
                  input_1=m16.data,
                  input_2=m3.data,
                  columns_input=[],
                  median_deviate=3
              )
              
              m11 = M.standardlize.v8(
                  input_1=m4.data,
                  input_2=m3.data,
                  columns_input=[]
              )
              
              m7 = M.join.v3(
                  data1=m2.data,
                  data2=m11.data,
                  on='date,instrument',
                  how='inner',
                  sort=False
              )
              
              m13 = M.dropnan.v1(
                  input_data=m7.data
              )
              
              m9 = M.instruments.v2(
                  start_date=T.live_run_param('trading_date', '2017-01-01'),
                  end_date=T.live_run_param('trading_date', '2019-06-01'),
                  market='CN_STOCK_A',
                  instrument_list='',
                  max_count=0
              )
              
              m17 = M.general_feature_extractor.v7(
                  instruments=m9.data,
                  features=m6.data,
                  start_date='',
                  end_date='',
                  before_start_days=200
              )
              
              m18 = M.derived_feature_extractor.v3(
                  input_data=m17.data,
                  features=m6.data,
                  date_col='date',
                  instrument_col='instrument',
                  drop_na=True,
                  remove_extra_columns=False
              )
              
              m5 = M.winsorize.v6(
                  input_1=m18.data,
                  input_2=m6.data,
                  columns_input=[],
                  median_deviate=3
              )
              
              m8 = M.standardlize.v8(
                  input_1=m5.data,
                  input_2=m6.data,
                  columns_input=[]
              )
              
              m14 = M.dropnan.v1(
                  input_data=m8.data
              )
              
              m12 = M.stock_ranker.v2(
                  training_ds=m13.data,
                  features=m3.data,
                  predict_ds=m14.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,
                  slim_data=True
              )
              
              m23 = M.trade.v4(
                  instruments=m9.data,
                  options_data=m12.predictions,
                  start_date='',
                  end_date='',
                  initialize=m23_initialize_bigquant_run,
                  handle_data=m23_handle_data_bigquant_run,
                  prepare=m23_prepare_bigquant_run,
                  volume_limit=0.025,
                  order_price_field_buy='close',
                  order_price_field_sell='close',
                  capital_base=1000000,
                  auto_cancel_non_tradable_orders=True,
                  data_frequency='daily',
                  price_type='后复权',
                  product_type='股票',
                  plot_charts=True,
                  backtest_only=False,
                  benchmark='000300.SHA'
              )
              
              设置测试数据集,查看训练迭代过程的NDCG
              bigcharts-data-start/{"__type":"tabs","__id":"bigchart-0c65f1364e824729a20eb1c82ecf7a46"}/bigcharts-data-end
              设置测试数据集,查看训练迭代过程的NDCG
              bigcharts-data-start/{"__type":"tabs","__id":"bigchart-41955f7241b140c39143285a7caf03fa"}/bigcharts-data-end
              • 收益率-19.7%
              • 年化收益率-9.0%
              • 基准收益率9.66%
              • 阿尔法-0.13
              • 贝塔0.71
              • 夏普比率-0.57
              • 胜率0.39
              • 盈亏比1.12
              • 收益波动率18.64%
              • 信息比率-0.06
              • 最大回撤43.56%
              bigcharts-data-start/{"__type":"tabs","__id":"bigchart-eaa6c41f113f437e9730f66bc3f23488"}/bigcharts-data-end

              (muhai123) #10
              克隆策略

                {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215: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":"-7117:features_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-7106:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-231:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-1228:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-2618:input_1","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-7106:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"-7106:predict_ds","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-2618:input_3","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-222:data"},{"DestinationInputPortId":"-238:input_data","SourceOutputPortId":"-231:data"},{"DestinationInputPortId":"-143:input_1","SourceOutputPortId":"-238:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-143:data_1"},{"DestinationInputPortId":"-1228:options_data","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-2618:input_2","SourceOutputPortId":"-7106:predictions"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-231:features","SourceOutputPortId":"-7117:data"},{"DestinationInputPortId":"-238:features","SourceOutputPortId":"-7117:data"}],"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":"2016-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -22) / 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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#每日换手率作为权重对每日收益率求算术平均值\nsum((close_0/shift(close_0,1)-1) * turn_0,22)/22\npe_ttm_0/market_cap_0\nclose_0/close_(77+1)\nreturn_20\nreturn_5\nsum((close_0/shift(close_0,1)-1) * turn_0,22)/22\nsum(turn_0*return_0,77)/77\n#OCFP\nfs_net_cash_flow_ttm_0/market_cap_0\n#SP\nfs_operating_revenue_ttm_0/market_cap_0\n#EP\nfs_net_profit_ttm_0/market_cap_0\n#EPcut\nfs_deducted_profit_ttm_0/market_cap_0\n#BP\nfs_bps_0/market_cap_0\n#FCFP\nfs_free_cash_flow_0/market_cap_0\n#PEG\npe_ttm_0/fs_net_profit_ttm_0\n#销售净利率\nfs_net_profit_margin_0\n#销售毛利率\nfs_gross_profit_margin_0\n#净利率\nfs_roe_0\nfs_deducted_profit_0/fs_net_income_0\nfs_net_cash_flow_0/fs_operating_revenue_0\nfs_current_liabilities_0/fs_total_liability_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2019-06-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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"360","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","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":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-231","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":"360","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-231"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-231"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-231","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-238","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":"-238"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-238"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-238","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-143","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, N):\n # 示例代码如下。在这里编写您的代码\n df=input_1.read_df().set_index('date')\n def cal(x):\n weights = np.array([ np.exp(-1*j/N/4) for j in range(N*22)])\n result = np.dot(x,weights)\n return result\n df1 = df.groupby('instrument').rolling(N*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(N)+'m'})\n df1 = df1.merge(df.reset_index(),on=['date','instrument'])\n data_1 = DataSource.write_df(df1)\n return Outputs(data_1=data_1)\n\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":"{\n 'N':1\n}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-143"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-143"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-143"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-143","OutputType":null},{"Name":"data_2","NodeId":"-143","OutputType":null},{"Name":"data_3","NodeId":"-143","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1228","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n 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\n context.options['hold_days'] = 22\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n if context.trading_day_index%context.options['hold_days'] !=0:\n return\n \n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n layer = len(ranker_prediction)\n if layer==0:\n return\n ranker_prediction[\"组合\"] = pd.cut(ranker_prediction['position'],bins=[0,layer/5,2*layer/5,3*layer/5,4*layer/5,layer],labels=[1,2,3,4,5])\n ranker_prediction = ranker_prediction[ranker_prediction[\"组合\"]==1]\n stock_to_buy = list(ranker_prediction.instrument)\n \n # 定期轮仓卖出\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n stock_to_sell = [k for k in positions if k not in stock_to_buy]\n\n for instrument in stock_to_sell:\n context.order_target(context.symbol(instrument), 0)\n\n # 定期买入\n weight = [1/len(stock_to_buy) for k in stock_to_buy]\n #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])\n for i,instrument in enumerate(stock_to_buy):\n context.order_target_percent(context.symbol(instrument), weight[i])\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1228"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1228"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1228","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"第一组股票轮仓回测结果","CommentCollapsed":false},{"Id":"-7106","ModuleId":"BigQuantSpace.stock_ranker.stock_ranker-v2","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":"slim_data","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_model","NodeId":"-7106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"predict_ds","NodeId":"-7106"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-7106","OutputType":null},{"Name":"predictions","NodeId":"-7106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-7117","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nshift(close_0,-22)/close_0-1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-7117"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-7117","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2618","ModuleId":"BigQuantSpace.StockRanker_grouptest.StockRanker_grouptest-v3","ModuleParameters":[{"Name":"ret_column","Value":"shift(close_0,-22)/close_0-1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"N","Value":"22","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-2618"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-2618"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-2618"}],"OutputPortsInternal":[],"UsePreviousResults":false,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='246,85,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='48,342,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='725,-26,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='317,487,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1012,246,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='291,660,200,200'/><NodePosition Node='-86' Position='1023,748,200,200'/><NodePosition Node='-215' Position='439,243,200,200'/><NodePosition Node='-222' Position='451,341,200,200'/><NodePosition Node='-231' Position='1019,373,200,200'/><NodePosition Node='-238' Position='1019,482,200,200'/><NodePosition Node='-143' Position='1013,604,200,200'/><NodePosition Node='-1228' Position='949,979,200,200'/><NodePosition Node='-7106' Position='524,843,200,200'/><NodePosition Node='-7117' Position='555,95,200,200'/><NodePosition Node='-2618' Position='505,993,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 [2]:
                # 本代码由可视化策略环境自动生成 2019年7月15日 19:59
                # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
                
                
                # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
                def m5_run_bigquant_run(input_1, N):
                    # 示例代码如下。在这里编写您的代码
                    df=input_1.read_df().set_index('date')
                    def cal(x):
                        weights = np.array([ np.exp(-1*j/N/4) for j in range(N*22)])
                        result = np.dot(x,weights)
                        return result
                    df1 = df.groupby('instrument').rolling(N*22)['turn_0'].apply(cal).reset_index().rename(columns={'turn_0':'exp_wgt_return_'+str(N)+'m'})
                    df1 = df1.merge(df.reset_index(),on=['date','instrument'])
                    data_1 = DataSource.write_df(df1)
                    return Outputs(data_1=data_1)
                
                
                # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
                def m5_post_run_bigquant_run(outputs):
                    return outputs
                
                # 回测引擎:初始化函数,只执行一次
                def m23_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))
                
                    context.options['hold_days'] = 22
                
                # 回测引擎:每日数据处理函数,每天执行一次
                def m23_handle_data_bigquant_run(context, data):
                
                    if context.trading_day_index%context.options['hold_days'] !=0:
                        return
                    
                    # 按日期过滤得到今日的预测数据
                    ranker_prediction = context.ranker_prediction[
                          context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
                    layer = len(ranker_prediction)
                    if layer==0:
                        return
                    ranker_prediction["组合"] = pd.cut(ranker_prediction['position'],bins=[0,layer/5,2*layer/5,3*layer/5,4*layer/5,layer],labels=[1,2,3,4,5])
                    ranker_prediction = ranker_prediction[ranker_prediction["组合"]==1]
                    stock_to_buy = list(ranker_prediction.instrument)
                    
                    # 定期轮仓卖出
                    positions = {e.symbol: p.amount * p.last_sale_price
                                 for e, p in context.portfolio.positions.items()}
                   
                    stock_to_sell = [k for k in positions if k not in stock_to_buy]
                
                    for instrument in stock_to_sell:
                        context.order_target(context.symbol(instrument), 0)
                
                    # 定期买入
                    weight = [1/len(stock_to_buy) for k in stock_to_buy]
                    #非等仓位权重可以设置 weight = T.norm([1 / math.log(i + 2) for i in range(len(stock_to_buy))])
                    for i,instrument in enumerate(stock_to_buy):
                        context.order_target_percent(context.symbol(instrument), weight[i])
                
                # 回测引擎:准备数据,只执行一次
                def m23_prepare_bigquant_run(context):
                    pass
                
                
                m1 = M.instruments.v2(
                    start_date='2010-01-01',
                    end_date='2016-12-31',
                    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/develop/datasource/deprecated/history_data.html
                #   添加benchmark_前缀,可使用对应的benchmark数据
                # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
                
                # 计算收益:22日前收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
                shift(close, -22) / 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="""# #号开始的表示注释
                # 多个特征,每行一个,可以包含基础特征和衍生特征
                #每日换手率作为权重对每日收益率求算术平均值
                sum((close_0/shift(close_0,1)-1) * turn_0,22)/22
                pe_ttm_0/market_cap_0
                close_0/close_(77+1)
                return_20
                return_5
                sum((close_0/shift(close_0,1)-1) * turn_0,22)/22
                sum(turn_0*return_0,77)/77
                #OCFP
                fs_net_cash_flow_ttm_0/market_cap_0
                #SP
                fs_operating_revenue_ttm_0/market_cap_0
                #EP
                fs_net_profit_ttm_0/market_cap_0
                #EPcut
                fs_deducted_profit_ttm_0/market_cap_0
                #BP
                fs_bps_0/market_cap_0
                #FCFP
                fs_free_cash_flow_0/market_cap_0
                #PEG
                pe_ttm_0/fs_net_profit_ttm_0
                #销售净利率
                fs_net_profit_margin_0
                #销售毛利率
                fs_gross_profit_margin_0
                #净利率
                fs_roe_0
                fs_deducted_profit_0/fs_net_income_0
                fs_net_cash_flow_0/fs_operating_revenue_0
                fs_current_liabilities_0/fs_total_liability_0
                """
                )
                
                m6 = M.input_features.v1(
                    features_ds=m3.data,
                    features="""
                # #号开始的表示注释,注释需单独一行
                # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
                shift(close_0,-22)/close_0-1"""
                )
                
                m15 = M.general_feature_extractor.v7(
                    instruments=m1.data,
                    features=m6.data,
                    start_date='',
                    end_date='',
                    before_start_days=360
                )
                
                m16 = M.derived_feature_extractor.v3(
                    input_data=m15.data,
                    features=m6.data,
                    date_col='date',
                    instrument_col='instrument',
                    drop_na=True,
                    remove_extra_columns=False
                )
                
                m7 = M.join.v3(
                    data1=m2.data,
                    data2=m16.data,
                    on='date,instrument',
                    how='inner',
                    sort=False
                )
                
                m13 = M.dropnan.v1(
                    input_data=m7.data
                )
                
                m9 = M.instruments.v2(
                    start_date=T.live_run_param('trading_date', '2017-01-01'),
                    end_date=T.live_run_param('trading_date', '2019-06-01'),
                    market='CN_STOCK_A',
                    instrument_list='',
                    max_count=0
                )
                
                m17 = M.general_feature_extractor.v7(
                    instruments=m9.data,
                    features=m6.data,
                    start_date='',
                    end_date='',
                    before_start_days=360
                )
                
                m18 = M.derived_feature_extractor.v3(
                    input_data=m17.data,
                    features=m6.data,
                    date_col='date',
                    instrument_col='instrument',
                    drop_na=True,
                    remove_extra_columns=False
                )
                
                m5 = M.cached.v3(
                    input_1=m18.data,
                    run=m5_run_bigquant_run,
                    post_run=m5_post_run_bigquant_run,
                    input_ports='',
                    params="""{
                    'N':1
                }""",
                    output_ports=''
                )
                
                m14 = M.dropnan.v1(
                    input_data=m5.data_1
                )
                
                m12 = M.stock_ranker.v2(
                    training_ds=m13.data,
                    features=m3.data,
                    predict_ds=m14.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,
                    slim_data=True
                )
                
                m23 = M.trade.v4(
                    instruments=m9.data,
                    options_data=m12.predictions,
                    start_date='',
                    end_date='',
                    initialize=m23_initialize_bigquant_run,
                    handle_data=m23_handle_data_bigquant_run,
                    prepare=m23_prepare_bigquant_run,
                    volume_limit=0.025,
                    order_price_field_buy='open',
                    order_price_field_sell='close',
                    capital_base=1000000,
                    auto_cancel_non_tradable_orders=True,
                    data_frequency='daily',
                    price_type='后复权',
                    product_type='股票',
                    plot_charts=True,
                    backtest_only=False,
                    benchmark='000300.SHA'
                )
                
                m4 = M.StockRanker_grouptest.v3(
                    input_1=m9.data,
                    input_2=m12.predictions,
                    input_3=m14.data,
                    ret_column='shift(close_0,-22)/close_0-1',
                    N=22,
                    m_cached=False
                )
                
                设置测试数据集,查看训练迭代过程的NDCG
                bigcharts-data-start/{"__type":"tabs","__id":"bigchart-b83b880dde584e2b8021a4ae951fa022"}/bigcharts-data-end
                • 收益率-13.05%
                • 年化收益率-5.84%
                • 基准收益率9.66%
                • 阿尔法-0.09
                • 贝塔0.51
                • 夏普比率-0.59
                • 胜率0.48
                • 盈亏比0.82
                • 收益波动率13.56%
                • 信息比率-0.05
                • 最大回撤30.78%
                bigcharts-data-start/{"__type":"tabs","__id":"bigchart-3f9f73ec528c4d778c64affc5835ea2b"}/bigcharts-data-end

                #12

                此篇加了不少因子预处理模块,例如去极值,标准化处理等。选手可以试试修改轮仓周期,试试日频交易。


                #13

                此篇策略回测收益曲线看起来不错,仔细一看原来回测时间修改了,违背规则啦!同样,换仓周期的频率可以试着高一点,因子预处理方面,标准化、行业中性化模块都可以拿来用用。


                #14

                在选取因子的时候,尽量选用研报内提及的因子!可以多多参考帮助中心里面的文档,其中有因子库。在构建因子的时候,基本都可以从因子库里面选取对应因子构建。因子预处理方面,标准化、缺失值填充、行业中性化、去极值都可以试着用一用。 算法方面也可以在模块库里面选择不同的算法来构建策略。


                #15

                选手可以再研究一下因子预处理,包括标准化、缺失值填充、行业中性化、去极值等。
                换仓周期也可以用更高频率,止盈止损风控等策略也可以考虑。
                m5模块选手是想构建一个动量因子,但可以用更简洁的办法:【学院教程】利用表达式引擎批量生成因子
                从特征重要性表中可以看出,shift(return_0,22)在这个组合里面并不占优势,可以选择其他因子试试。


                #16

                因子方面:同时用了不少相关性较强的因子,可以多多考虑不相关因子的组合。
                因子预处理:可以加入去极值、标准化、行业中性化、缺失值填充等模块。
                轮仓周期可用高频,策略算法在模块库中还有更多选择。