老师您好,我想请教一下这个指数特征抽取模块应该怎么用呢?指数特征抽取(index_feature_extract)使用错误

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

(woshisilvio) #1
克隆策略

    {"Description":"实验创建于2017/9/28","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-106:features","SourceOutputPortId":"-315:data"},{"DestinationInputPortId":"-115:features","SourceOutputPortId":"-315:data"},{"DestinationInputPortId":"-126:features","SourceOutputPortId":"-315:data"},{"DestinationInputPortId":"-140:features","SourceOutputPortId":"-315:data"},{"DestinationInputPortId":"-147:features","SourceOutputPortId":"-315:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data1","SourceOutputPortId":"-2125:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-126:training_ds","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"DestinationInputPortId":"-140:instruments","SourceOutputPortId":"-2639:data"},{"DestinationInputPortId":"-159:instruments","SourceOutputPortId":"-2639:data"},{"DestinationInputPortId":"-3423:input_1","SourceOutputPortId":"-2639:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:data","SourceOutputPortId":"-2669:data"},{"DestinationInputPortId":"-209:data1","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"DestinationInputPortId":"-2125:instruments","SourceOutputPortId":"-4129:data"},{"DestinationInputPortId":"-115:instruments","SourceOutputPortId":"-4129:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-106:data"},{"DestinationInputPortId":"-106:input_data","SourceOutputPortId":"-115:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:model","SourceOutputPortId":"-126:model"},{"DestinationInputPortId":"-147:input_data","SourceOutputPortId":"-140:data"},{"DestinationInputPortId":"-2669:input_data","SourceOutputPortId":"-147:data"},{"DestinationInputPortId":"-209:data2","SourceOutputPortId":"-3423:data_1"},{"DestinationInputPortId":"-3423:input_2","SourceOutputPortId":"-3850:data"},{"DestinationInputPortId":"-216:input_ds","SourceOutputPortId":"-209:data"},{"DestinationInputPortId":"-159:options_data","SourceOutputPortId":"-216:sorted_data"}],"ModuleNodes":[{"Id":"-315","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"return_5\nreturn_10\nreturn_20\nvolume_0/mean(close_0,3)\navg_amount_0/avg_amount_5 \navg_amount_5/avg_amount_10 \nsum(mf_net_amount_xl_0, 5) \nsum(mf_net_amount_l_0, 5)\nclose_0\nmarket_cap_float_0\ngroupby_calcu_relative_ret(close_0)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-315"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-315","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2125","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, -5) / 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":"-2125"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2125","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"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":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"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":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2639","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2016-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-01-22","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600000.SHA\n600010.SHA\n600015.SHA\n600016.SHA\n600018.SHA\n600028.SHA\n600029.SHA\n600030.SHA\n600036.SHA\n600048.SHA\n600050.SHA\n600089.SHA\n600100.SHA\n600104.SHA\n600109.SHA\n600111.SHA\n600150.SHA\n600196.SHA\n600256.SHA\n600332.SHA\n600340.SHA\n600372.SHA\n600406.SHA\n600485.SHA\n600518.SHA\n600519.SHA\n600547.SHA\n600583.SHA\n600585.SHA\n600606.SHA\n600637.SHA\n600690.SHA\n600703.SHA\n600795.SHA\n600832.SHA\n600837.SHA\n600887.SHA\n600893.SHA\n600919.SHA\n600958.SHA\n600999.SHA\n601006.SHA\n601088.SHA\n601118.SHA\n601166.SHA\n601169.SHA\n601186.SHA\n601198.SHA\n601211.SHA\n601229.SHA\n601288.SHA\n601299.SHA\n601318.SHA\n601328.SHA\n601336.SHA\n601377.SHA\n601390.SHA\n601398.SHA\n601601.SHA\n601628.SHA\n601668.SHA\n601669.SHA\n601688.SHA\n601727.SHA\n601766.SHA\n601788.SHA\n601800.SHA\n601818.SHA\n601857.SHA\n601881.SHA\n601901.SHA\n601919.SHA\n601985.SHA\n601988.SHA\n601989.SHA\n601998.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-2639"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2639","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2669","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-2669"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2669","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"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":14,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-4129","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2011-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2016-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600000.SHA\n600010.SHA\n600015.SHA\n600016.SHA\n600018.SHA\n600028.SHA\n600029.SHA\n600030.SHA\n600036.SHA\n600048.SHA\n600050.SHA\n600089.SHA\n600100.SHA\n600104.SHA\n600109.SHA\n600111.SHA\n600150.SHA\n600196.SHA\n600256.SHA\n600332.SHA\n600340.SHA\n600372.SHA\n600406.SHA\n600485.SHA\n600518.SHA\n600519.SHA\n600547.SHA\n600583.SHA\n600585.SHA\n600606.SHA\n600637.SHA\n600690.SHA\n600703.SHA\n600795.SHA\n600832.SHA\n600837.SHA\n600887.SHA\n600893.SHA\n600919.SHA\n600958.SHA\n600999.SHA\n601006.SHA\n601088.SHA\n601118.SHA\n601166.SHA\n601169.SHA\n601186.SHA\n601198.SHA\n601211.SHA\n601229.SHA\n601288.SHA\n601299.SHA\n601318.SHA\n601328.SHA\n601336.SHA\n601377.SHA\n601390.SHA\n601398.SHA\n601601.SHA\n601628.SHA\n601668.SHA\n601669.SHA\n601688.SHA\n601727.SHA\n601766.SHA\n601788.SHA\n601800.SHA\n601818.SHA\n601857.SHA\n601881.SHA\n601901.SHA\n601919.SHA\n601985.SHA\n601988.SHA\n601989.SHA\n601998.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-4129"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4129","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-106","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":"def calcu_relative_ret(df):\n start_date = min(m4.data.read_df().date).strftime('%Y-%m-%d')\n end_date = max(m4.data.read_df().date).strftime('%Y-%m-%d')\n hs800_df = D.history_data(\n '000016.SHA',\n start_date=(pd.to_datetime(start_date) - datetime.timedelta(days=10)).strftime('%Y-%m-%d'), # 多取几天的数据\n end_date=end_date)[['date', 'close']].rename(columns={'close': 'hs800_close'})\n df = df[['date', 'close_0']].reset_index().merge(hs800_df, on='date', how='left').set_index('index')\n return df['close_0'].pct_change() - df['hs800_close'].pct_change()\n\ndef groupby_calcu_relative_ret(df, close_0):\n return df.groupby('instrument', group_keys=False).apply(calcu_relative_ret)\n\nbigquant_run = {\n 'groupby_calcu_relative_ret': groupby_calcu_relative_ret\n}\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-106"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"在衍生特征里配置自定义函数就可以了,也可以覆盖同名的预定义函数","CommentCollapsed":true},{"Id":"-115","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-115"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-115","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":9,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-126","ModuleId":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v6","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":"data_row_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"ndcg_discount_base","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":"-126"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-126"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-126"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-126"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-126","OutputType":null},{"Name":"feature_gains","NodeId":"-126","OutputType":null},{"Name":"m_lazy_run","NodeId":"-126","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-140","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-140"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-140"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-140","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-147","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":"def calcu_relative_ret(df):\n start_date = min(m11.data.read_df().date).strftime('%Y-%m-%d')\n end_date = max(m11.data.read_df().date).strftime('%Y-%m-%d')\n \n sz50_df = D.history_data(\n '000016.SHA',\n start_date=(pd.to_datetime(start_date) - datetime.timedelta(days=10)).strftime('%Y-%m-%d'), # 多取几天的数据\n end_date=end_date)[['date', 'close']].rename(columns={'close': 'sz50_close'})\n df = df[['date', 'close_0']].reset_index().merge(sz50_df, on='date', how='left').set_index('index')\n return df['close_0'].pct_change() - df['sz50_close'].pct_change()\n\ndef groupby_calcu_relative_ret(df, close_0):\n return df.groupby('instrument', group_keys=False).apply(calcu_relative_ret)\n\nbigquant_run = {\n 'groupby_calcu_relative_ret': groupby_calcu_relative_ret\n}\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-147"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-147"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-147","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"在衍生特征里配置自定义函数就可以了,也可以覆盖同名的预定义函数","CommentCollapsed":true},{"Id":"-159","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 = 3\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.8\n context.options['hold_days'] = 7\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n if cond1 and cond2 and cond3:\n return 'Long'\n elif cond4 and cond5 and cond6 :\n return 'Short' \n \n \n \n time_select = context.options['time_select']\n today_direction = time_select.ix[data.current_dt.strftime('%Y-%m-%d')].ix[0]\n \n positions = {e.symbol: p.cost_basis for e, p in context.portfolio.positions.items()}\n \n if today_direction == 'Short':\n print('大盘择时应该空仓!')\n positions = {e.symbol: p.cost_basis for e, p in context.portfolio.positions.items()}\n stock_hold = positions.keys()\n for sid in stock_hold:\n context.order_target(context.symbol(sid), 0)\n \n \n \n \n \n \n \n \n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n \n \n \n \n \n \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 #--------------------这里是移动止损-------------------------\n \n #------------------------------------------止损模块START--------------------------------------------\n date = data.current_dt.strftime('%Y-%m-%d') \n equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}\n # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断\n current_stoploss_stock = [] \n if len(equities) > 0:\n for i in equities.keys():\n stock_market_price = data.current(context.symbol(i), 'price') # 最新市场价格\n last_sale_date = equities[i].last_sale_date # 上次交易日期\n delta_days = data.current_dt - last_sale_date \n hold_days = delta_days.days # 持仓天数\n # 建仓以来的最高价\n highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max()\n # 确定止损位置-----7%\n stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.07\n record('止损位置', stoploss_line)\n # 如果价格下穿止损位置\n if stock_market_price < stoploss_line:\n context.order_target_percent(context.symbol(i), 0) \n current_stoploss_stock.append(i)\n print('日期:', date , '股票:', i, '出现止损状况')\n #-------------------------------------------止损模块END-------------------","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass","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":"-159"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-159"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-159"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-159"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-159"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-159","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":20,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-3423","ModuleId":"BigQuantSpace.index_feature_extract.index_feature_extract-v3","ModuleParameters":[{"Name":"before_days","Value":100,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"index","Value":"000300.HIX","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-3423"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-3423"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-3423","OutputType":null},{"Name":"data_2","NodeId":"-3423","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-3850","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nbenchmark_df['ma_close_5'] = benchmark_df['close'].rolling(5).mean()\nbenchmark_df['ma_close_30'] = benchmark_df['close'].rolling(30).mean()\nbenchmark_df['ma_volume_5'] = benchmark_df['volume'].rolling(5).mean()\nbenchmark_df['ma_volume_30'] = benchmark_df['volume'].rolling(30).mean()\nbenchmark_df['close_1'] = benchmark_df['close'].shift(1)\nbenchmark_df['close_2'] = benchmark_df['close'].shift(2)\nbenchmark_df['open_1'] = benchmark_df['open'].shift(1)\nbenchmark_df['open_2'] = benchmark_df['open'].shift(2)\nbenchmark_df['highest'] = benchmark_df['high'].rolling(30).max()\n\ncond1 = benchmark_df['ma_close_5'] >= benchmark_df['ma_close_30']\ncond2 = benchmark_df['ma_volume_5'] >= benchmark_df['ma_volume_30']\ncond3 = benchmark_df['close'] >= benchmark_df['open'] and benchmark_df['close_1'] >= benchmark_df['open_1'] and benchmark_df['close_2'] >= benchmark_df['open_2'] \ncond4 = benchmark_df['ma_close_5'] < benchmark_df['ma_close_30']\ncond5 = benchmark_df['close'] < benchmark_df['open'] and benchmark_df['close_1'] < benchmark_df['open_1'] and benchmark_df['close_2'] < benchmark_df['open_2'] \ncond6 = benchmark_df['close'] / benchmark_df['highest'] - 1 <= -0.1\n \n ","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-3850"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-3850","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-209","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"left","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-209"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-209"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-209","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-216","ModuleId":"BigQuantSpace.sort.sort-v4","ModuleParameters":[{"Name":"sort_by","Value":"date,position","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"group_by","Value":"--","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"keep_columns","Value":"--","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"ascending","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-216"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"sort_by_ds","NodeId":"-216"}],"OutputPortsInternal":[{"Name":"sorted_data","NodeId":"-216","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"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='-315' Position='198,-141,200,200'/><NodePosition Node='-2125' Position='-399,59,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='-278,196,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='-88,284,200,200'/><NodePosition Node='-2639' Position='765,-66,200,200'/><NodePosition Node='-2669' Position='739,300,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='301,520,200,200'/><NodePosition Node='-4129' Position='-271,-146,200,200'/><NodePosition Node='-106' Position='-38,64,200,200'/><NodePosition Node='-115' Position='-39,-26,200,200'/><NodePosition Node='-126' Position='49,400,200,200'/><NodePosition Node='-140' Position='765,48,200,200'/><NodePosition Node='-147' Position='762,158,200,200'/><NodePosition Node='-159' Position='831,884,200,200'/><NodePosition Node='-3423' Position='963,437,200,200'/><NodePosition Node='-3850' Position='1166,262,200,200'/><NodePosition Node='-209' Position='801,614,200,200'/><NodePosition Node='-216' Position='826,730,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]:
    # 本代码由可视化策略环境自动生成 2020年3月18日 22:13
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def calcu_relative_ret(df):
        start_date = min(m11.data.read_df().date).strftime('%Y-%m-%d')
        end_date = max(m11.data.read_df().date).strftime('%Y-%m-%d')
       
        sz50_df = D.history_data(
        '000016.SHA',
        start_date=(pd.to_datetime(start_date) - datetime.timedelta(days=10)).strftime('%Y-%m-%d'),  # 多取几天的数据
        end_date=end_date)[['date', 'close']].rename(columns={'close': 'sz50_close'})
        df = df[['date', 'close_0']].reset_index().merge(sz50_df, on='date', how='left').set_index('index')
        return df['close_0'].pct_change() - df['sz50_close'].pct_change()
    
    def groupby_calcu_relative_ret(df, close_0):
        return df.groupby('instrument', group_keys=False).apply(calcu_relative_ret)
    
    m19_user_functions_bigquant_run = {
        'groupby_calcu_relative_ret':  groupby_calcu_relative_ret
    }
    
    
    def calcu_relative_ret(df):
        start_date = min(m4.data.read_df().date).strftime('%Y-%m-%d')
        end_date = max(m4.data.read_df().date).strftime('%Y-%m-%d')
        hs800_df = D.history_data(
        '000016.SHA',
        start_date=(pd.to_datetime(start_date) - datetime.timedelta(days=10)).strftime('%Y-%m-%d'),  # 多取几天的数据
        end_date=end_date)[['date', 'close']].rename(columns={'close': 'hs800_close'})
        df = df[['date', 'close_0']].reset_index().merge(hs800_df, on='date', how='left').set_index('index')
        return df['close_0'].pct_change() - df['hs800_close'].pct_change()
    
    def groupby_calcu_relative_ret(df, close_0):
        return df.groupby('instrument', group_keys=False).apply(calcu_relative_ret)
    
    m3_user_functions_bigquant_run = {
        'groupby_calcu_relative_ret':  groupby_calcu_relative_ret
    }
    
    
    # 回测引擎:初始化函数,只执行一次
    def m20_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 = 3
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[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.8
        context.options['hold_days'] = 7
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m20_handle_data_bigquant_run(context, data):
        if  cond1 and cond2 and cond3:
            return 'Long'
        elif cond4 and cond5 and cond6 :
            return 'Short'  
        
        
        
        time_select = context.options['time_select']
        today_direction = time_select.ix[data.current_dt.strftime('%Y-%m-%d')].ix[0]
       
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
          
        if today_direction == 'Short':
            print('大盘择时应该空仓!')
            positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
            stock_hold = positions.keys()
            for sid in stock_hold:
                context.order_target(context.symbol(sid), 0)
                
            
        
        
        
        
        
        
        # 按日期过滤得到今日的预测数据
        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)
        #--------------------这里是移动止损-------------------------
                  
         #------------------------------------------止损模块START--------------------------------------------
        date = data.current_dt.strftime('%Y-%m-%d')  
        equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}
        # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
        current_stoploss_stock = [] 
        if len(equities) > 0:
            for i in equities.keys():
                stock_market_price = data.current(context.symbol(i), 'price')  # 最新市场价格
                last_sale_date = equities[i].last_sale_date   # 上次交易日期
                delta_days = data.current_dt - last_sale_date  
                hold_days = delta_days.days # 持仓天数
                # 建仓以来的最高价
                highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max()
                # 确定止损位置-----7%
                stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.07
                record('止损位置', stoploss_line)
                # 如果价格下穿止损位置
                if stock_market_price < stoploss_line:
                    context.order_target_percent(context.symbol(i), 0)     
                    current_stoploss_stock.append(i)
                    print('日期:', date , '股票:', i, '出现止损状况')
        #-------------------------------------------止损模块END-------------------
    # 回测引擎:准备数据,只执行一次
    def m20_prepare_bigquant_run(context):
        pass
    
    m1 = M.input_features.v1(
        features="""return_5
    return_10
    return_20
    volume_0/mean(close_0,3)
    avg_amount_0/avg_amount_5  
    avg_amount_5/avg_amount_10  
    sum(mf_net_amount_xl_0, 5) 
    sum(mf_net_amount_l_0, 5)
    close_0
    market_cap_float_0
    groupby_calcu_relative_ret(close_0)
    """
    )
    
    m10 = M.instruments.v2(
        start_date='2016-01-01',
        end_date='2018-01-22',
        market='CN_STOCK_A',
        instrument_list="""600000.SHA
    600010.SHA
    600015.SHA
    600016.SHA
    600018.SHA
    600028.SHA
    600029.SHA
    600030.SHA
    600036.SHA
    600048.SHA
    600050.SHA
    600089.SHA
    600100.SHA
    600104.SHA
    600109.SHA
    600111.SHA
    600150.SHA
    600196.SHA
    600256.SHA
    600332.SHA
    600340.SHA
    600372.SHA
    600406.SHA
    600485.SHA
    600518.SHA
    600519.SHA
    600547.SHA
    600583.SHA
    600585.SHA
    600606.SHA
    600637.SHA
    600690.SHA
    600703.SHA
    600795.SHA
    600832.SHA
    600837.SHA
    600887.SHA
    600893.SHA
    600919.SHA
    600958.SHA
    600999.SHA
    601006.SHA
    601088.SHA
    601118.SHA
    601166.SHA
    601169.SHA
    601186.SHA
    601198.SHA
    601211.SHA
    601229.SHA
    601288.SHA
    601299.SHA
    601318.SHA
    601328.SHA
    601336.SHA
    601377.SHA
    601390.SHA
    601398.SHA
    601601.SHA
    601628.SHA
    601668.SHA
    601669.SHA
    601688.SHA
    601727.SHA
    601766.SHA
    601788.SHA
    601800.SHA
    601818.SHA
    601857.SHA
    601881.SHA
    601901.SHA
    601919.SHA
    601985.SHA
    601988.SHA
    601989.SHA
    601998.SHA""",
        max_count=0
    )
    
    m17 = M.general_feature_extractor.v7(
        instruments=m10.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m19 = M.derived_feature_extractor.v3(
        input_data=m17.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m19_user_functions_bigquant_run,
        m_cached=False
    )
    
    m13 = M.dropnan.v1(
        input_data=m19.data
    )
    
    m18 = M.instruments.v2(
        start_date='2011-01-01',
        end_date='2016-01-01',
        market='CN_STOCK_A',
        instrument_list="""600000.SHA
    600010.SHA
    600015.SHA
    600016.SHA
    600018.SHA
    600028.SHA
    600029.SHA
    600030.SHA
    600036.SHA
    600048.SHA
    600050.SHA
    600089.SHA
    600100.SHA
    600104.SHA
    600109.SHA
    600111.SHA
    600150.SHA
    600196.SHA
    600256.SHA
    600332.SHA
    600340.SHA
    600372.SHA
    600406.SHA
    600485.SHA
    600518.SHA
    600519.SHA
    600547.SHA
    600583.SHA
    600585.SHA
    600606.SHA
    600637.SHA
    600690.SHA
    600703.SHA
    600795.SHA
    600832.SHA
    600837.SHA
    600887.SHA
    600893.SHA
    600919.SHA
    600958.SHA
    600999.SHA
    601006.SHA
    601088.SHA
    601118.SHA
    601166.SHA
    601169.SHA
    601186.SHA
    601198.SHA
    601211.SHA
    601229.SHA
    601288.SHA
    601299.SHA
    601318.SHA
    601328.SHA
    601336.SHA
    601377.SHA
    601390.SHA
    601398.SHA
    601601.SHA
    601628.SHA
    601668.SHA
    601669.SHA
    601688.SHA
    601727.SHA
    601766.SHA
    601788.SHA
    601800.SHA
    601818.SHA
    601857.SHA
    601881.SHA
    601901.SHA
    601919.SHA
    601985.SHA
    601988.SHA
    601989.SHA
    601998.SHA""",
        max_count=0
    )
    
    m5 = M.advanced_auto_labeler.v2(
        instruments=m18.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, -5) / 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,
        user_functions={}
    )
    
    m9 = M.general_feature_extractor.v7(
        instruments=m18.data,
        features=m1.data,
        start_date='',
        end_date='',
        m_cached=False
    )
    
    m3 = M.derived_feature_extractor.v3(
        input_data=m9.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m3_user_functions_bigquant_run
    )
    
    m6 = M.join.v3(
        data1=m5.data,
        data2=m3.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m7 = M.dropnan.v1(
        input_data=m6.data
    )
    
    m16 = M.stock_ranker_train.v6(
        training_ds=m7.data,
        features=m1.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,
        data_row_fraction=1,
        ndcg_discount_base=1,
        m_lazy_run=False
    )
    
    m14 = M.stock_ranker_predict.v5(
        model=m16.model,
        data=m13.data,
        m_lazy_run=False
    )
    
    m4 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    benchmark_df['ma_close_5'] = benchmark_df['close'].rolling(5).mean()
    benchmark_df['ma_close_30'] = benchmark_df['close'].rolling(30).mean()
    benchmark_df['ma_volume_5'] = benchmark_df['volume'].rolling(5).mean()
    benchmark_df['ma_volume_30'] = benchmark_df['volume'].rolling(30).mean()
    benchmark_df['close_1'] = benchmark_df['close'].shift(1)
    benchmark_df['close_2'] = benchmark_df['close'].shift(2)
    benchmark_df['open_1'] = benchmark_df['open'].shift(1)
    benchmark_df['open_2'] = benchmark_df['open'].shift(2)
    benchmark_df['highest'] = benchmark_df['high'].rolling(30).max()
    
    cond1 = benchmark_df['ma_close_5'] >= benchmark_df['ma_close_30']
    cond2 = benchmark_df['ma_volume_5'] >= benchmark_df['ma_volume_30']
    cond3 = benchmark_df['close'] >= benchmark_df['open'] and benchmark_df['close_1'] >= benchmark_df['open_1'] and benchmark_df['close_2'] >= benchmark_df['open_2'] 
    cond4 = benchmark_df['ma_close_5'] < benchmark_df['ma_close_30']
    cond5 = benchmark_df['close'] < benchmark_df['open'] and benchmark_df['close_1']  < benchmark_df['open_1']  and benchmark_df['close_2']  < benchmark_df['open_2'] 
    cond6 = benchmark_df['close'] / benchmark_df['highest'] - 1 <= -0.1
        
      """
    )
    
    m2 = M.index_feature_extract.v3(
        input_1=m10.data,
        input_2=m4.data,
        before_days=100,
        index='000300.HIX'
    )
    
    m8 = M.join.v3(
        data1=m14.predictions,
        data2=m2.data_1,
        on='date',
        how='left',
        sort=False
    )
    
    m11 = M.sort.v4(
        input_ds=m8.data,
        sort_by='date,position',
        group_by='--',
        keep_columns='--',
        ascending=True
    )
    
    m20 = M.trade.v4(
        instruments=m10.data,
        options_data=m11.sorted_data,
        start_date='',
        end_date='',
        initialize=m20_initialize_bigquant_run,
        handle_data=m20_handle_data_bigquant_run,
        prepare=m20_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'
    )
    
    设置测试数据集,查看训练迭代过程的NDCG
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-8944d768da994b76b1dee6190d800a6a"}/bigcharts-data-end

    衍生特征抽取(derived_feature_extractor)使用错误,你可以:

    1.一键查看文档

    2.一键搜索答案

    指数特征抽取(index_feature_extract)使用错误,你可以:

    1.一键查看文档

    2.一键搜索答案

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-2-15d043c5ef8d> in <module>()
        465     input_2=m4.data,
        466     before_days=100,
    --> 467     index='000300.HIX'
        468 )
        469 
    
    AttributeError: 'Attribute' object has no attribute 'id'
    In [ ]:
     
    

    我想构造一个大盘风控,当大盘的均线死叉的时候就空仓,放量下跌就全仓卖出,缩量上涨卖出一半仓位

    (达达) #2

    大盘风控这个问题你提问了好多次了,我看了一下你的策略,很多时候是你思想想法的粗暴组合啊。总结一些,你通常想混合止赢止损、大盘风控和每日排序轮仓的逻辑。你可以仔细阅读一个超级综合的案例,思考一下每个部分的数据是怎么从外边的模块传进来的。还有就是逻辑堆叠的顺序,为什么我给你的策略修改后都是先大盘风控,然后止盈止损,最后轮仓?因为大盘风控后当日要直接return掉,不再执行后面的止盈止损。注意观察一下,为什么我止盈止损的时候,每个止损逻辑都要记录一下卖出过哪些股票,用哪个变量记录的?不同的止损逻辑之间是要防止重复下单的。最后轮仓的时候,也是要避免之前重复下单的。
    大盘的指标是在外部计算好的,通过数据连接与预测数据拼接后传到回测模块中。可以仔细研究一下,然后你不会做叠加做减法总是容易的吧。你试着逐步删减一些逻辑代码段,自己尝试一下不要让回测报错就能理解代码块到底怎么组合。
    请你仔细阅读(案例)[BigQuant A股股票选股模板策略介绍]