{"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data1","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-11425:features_ds","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:model","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:model"},{"to_node_id":"-11430:input_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"to_node_id":"-189:data1","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"to_node_id":"-1918:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-231:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-224:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:training_ds","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:data","from_node_id":"-86:data"},{"to_node_id":"-222:input_data","from_node_id":"-215:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","from_node_id":"-222:data"},{"to_node_id":"-238:input_data","from_node_id":"-231:data"},{"to_node_id":"-3779:input_data","from_node_id":"-238:data"},{"to_node_id":"-215:features","from_node_id":"-11425:data"},{"to_node_id":"-222:features","from_node_id":"-11425:data"},{"to_node_id":"-238:features","from_node_id":"-11425:data"},{"to_node_id":"-231:features","from_node_id":"-11425:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","from_node_id":"-11430:data"},{"to_node_id":"-86:input_data","from_node_id":"-3779:data"},{"to_node_id":"-215:instruments","from_node_id":"-184:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","from_node_id":"-184:data"},{"to_node_id":"-224:features","from_node_id":"-148:data"},{"to_node_id":"-189:data2","from_node_id":"-224:data"},{"to_node_id":"-1918:options_data","from_node_id":"-189:data"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15","module_id":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","parameters":[{"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# 计算收益: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","type":"Literal","bound_global_parameter":null},{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.HIX","type":"Literal","bound_global_parameter":null},{"name":"drop_na_label","value":"True","type":"Literal","bound_global_parameter":null},{"name":"cast_label_int","value":"True","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nvolume_0\n# 过去15个交易日的平均换手率/当天换手率\navg_turn_15/turn_0\n# 当天净主动买入额\nmf_net_amount_xl_0\n# 当天收盘价*当天的平均换手率+过去1个交易日的收盘价*过去1个交易日的平均换手率+过去2个交易日的收盘价*过去2个交易日的平均换手率\nalpha4=close_0*avg_turn_0+close_1*avg_turn_1+close_2*avg_turn_2\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","module_id":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v5","parameters":[{"name":"learning_algorithm","value":"排序","type":"Literal","bound_global_parameter":null},{"name":"number_of_leaves","value":30,"type":"Literal","bound_global_parameter":null},{"name":"minimum_docs_per_leaf","value":1000,"type":"Literal","bound_global_parameter":null},{"name":"number_of_trees","value":20,"type":"Literal","bound_global_parameter":null},{"name":"learning_rate","value":0.1,"type":"Literal","bound_global_parameter":null},{"name":"max_bins","value":1023,"type":"Literal","bound_global_parameter":null},{"name":"feature_fraction","value":1,"type":"Literal","bound_global_parameter":null},{"name":"m_lazy_run","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"training_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"features","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"test_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"base_model","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"}],"output_ports":[{"name":"model","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"feature_gains","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"m_lazy_run","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","module_id":"BigQuantSpace.join.join-v3","parameters":[{"name":"on","value":"date,instrument","type":"Literal","bound_global_parameter":null},{"name":"how","value":"inner","type":"Literal","bound_global_parameter":null},{"name":"sort","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"data1","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"},{"name":"data2","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","module_id":"BigQuantSpace.stock_ranker_predict.stock_ranker_predict-v5","parameters":[{"name":"m_lazy_run","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"model","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"},{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"}],"output_ports":[{"name":"predictions","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"},{"name":"m_lazy_run","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2018-01-01","type":"Literal","bound_global_parameter":"交易日期"},{"name":"end_date","value":"2021-12-26","type":"Literal","bound_global_parameter":"交易日期"},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"300782.SZA\n605358.SHA\n603290.SHA\n603392.SHA\n601865.SHA\n300759.SZA\n300750.SZA\n300677.SZA\n002607.SZA\n603259.SHA\n300751.SZA\n603613.SHA\n601100.SHA\n300763.SZA\n002568.SZA\n300724.SZA\n603345.SHA\n600763.SHA\n603713.SHA\n300595.SZA\n300014.SZA\n603712.SHA\n300760.SZA\n603317.SHA\n002791.SZA\n601066.SHA\n002985.SZA\n300661.SZA\n300347.SZA\n300777.SZA\n603129.SHA\n300454.SZA\n601888.SHA\n605111.SHA\n603638.SHA\n300850.SZA\n600809.SHA\n002414.SZA\n603893.SHA\n002967.SZA\n600132.SHA\n603605.SHA\n300015.SZA\n603267.SHA\n300012.SZA\n600882.SHA\n300684.SZA\n300390.SZA\n300769.SZA\n\t\n300748.SZA\n000799.SZA\n300767.SZA\n300775.SZA\n603737.SHA\n300601.SZA\n601698.SHA\n300841.SZA\n002975.SZA\n603501.SHA\n300122.SZA\n300677.SZA\n603392.SHA\n002791.SZA\n601865.SHA\n300759.SZA\n002568.SZA\n603613.SHA\n300014.SZA\n601100.SHA\n300763.SZA\n300274.SZA\n601633.SHA\n603501.SHA\n002709.SZA\n603317.SHA\n300661.SZA\n002985.SZA\n600882.SHA\n300598.SZA\n300777.SZA\n300552.SZA\n300346.SZA\n002475.SZA\n605111.SHA\n300850.SZA\n300526.SZA\n601012.SHA\n603893.SHA\n002967.SZA\n000858.SZA\n603267.SHA\n000568.SZA\n603638.SHA\n000708.SZA\n603456.SHA\n000995.SZA\n600399.SHA\n300767.SZA\n300595.SZA\n300347.SZA\n600763.SHA\n300751.SZA\n600316.SHA\n300775.SZA\n603208.SHA\n600862.SHA\n002241.SZA\n002706.SZA\n300390.SZA\n601698.SHA\n002541.SZA\n002607.SZA\n000733.SZA\n000596.SZA\n603345.SHA\n300151.SZA\n300496.SZA\n002705.SZA\n002756.SZA\n603185.SHA\n002850.SZA\n000661.SZA\n002600.SZA\n300724.SZA\n600584.SHA\n002414.SZA\n300223.SZA\n002920.SZA\n603906.SHA\n002714.SZA\n600966.SHA\n300083.SZA\n300601.SZA\n600438.SHA\n002812.SZA\n002459.SZA\n603027.SHA\n300015.SZA\n300763.SZA\n002709.SZA\n688202.SHA\n000422.SZA\n300769.SZA\n300751.SZA\n300343.SZA\n605117.SHA\n300827.SZA\n601633.SHA\n603026.SHA\n002240.SZA\n002326.SZA\n002487.SZA\n000762.SZA\n300432.SZA\n603396.SHA\n300363.SZA\n603985.SHA\n000155.SZA\n002594.SZA\n600399.SHA\n600702.SHA\n300171.SZA\n002176.SZA\n000733.SZA\n300750.SZA\n601127.SHA\n002812.SZA\n603260.SHA\n600610.SHA\n601012.SHA\n003022.SZA\n603127.SHA\n000301.SZA\n002585.SZA\n688198.SHA\n002245.SZA\n300693.SZA\n600096.SHA\n300568.SZA\n300382.SZA\n300443.SZA\n003031.SZA\n605376.SHA\n603806.SHA\n603223.SHA\n688116.SHA\n002529.SZA\n600141.SHA\n600956.SHA\n300035.SZA\n300316.SZA\n002460.SZA\n600110.SHA\n300671.SZA\n002407.SZA\n600532.SHA\n688599.SHA\n002472.SZA\n600499.SHA\n600111.SHA\n600884.SHA\n300696.SZA\n603267.SHA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"cacheable":true,"seq_num":9,"comment":"预测数据,用于回测和模拟","comment_collapsed":false},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84","module_id":"BigQuantSpace.dropnan.dropnan-v1","parameters":[],"input_ports":[{"name":"input_data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84"}],"cacheable":true,"seq_num":13,"comment":"","comment_collapsed":true},{"node_id":"-86","module_id":"BigQuantSpace.dropnan.dropnan-v1","parameters":[],"input_ports":[{"name":"input_data","node_id":"-86"}],"output_ports":[{"name":"data","node_id":"-86"}],"cacheable":true,"seq_num":14,"comment":"","comment_collapsed":true},{"node_id":"-215","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-215"},{"name":"features","node_id":"-215"}],"output_ports":[{"name":"data","node_id":"-215"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-222","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-222"},{"name":"features","node_id":"-222"}],"output_ports":[{"name":"data","node_id":"-222"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true},{"node_id":"-231","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-231"},{"name":"features","node_id":"-231"}],"output_ports":[{"name":"data","node_id":"-231"}],"cacheable":true,"seq_num":17,"comment":"","comment_collapsed":true},{"node_id":"-238","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-238"},{"name":"features","node_id":"-238"}],"output_ports":[{"name":"data","node_id":"-238"}],"cacheable":true,"seq_num":18,"comment":"","comment_collapsed":true},{"node_id":"-11425","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#周线金叉,择时思路\n# macd中的dif上穿dea,金叉,买入\ncond1=sum(ta_macd_dif(close_0,2,4,4),5)>sum(ta_macd_dea(close_0,2,4,4),5)\n# 当天收盘价大于过去25天收盘价的均值\ncond2=(close_0>ta_sma_10_0)&(close_0>ta_sma_20_0)&(close_0>ta_sma_30_0)&(close_0>ta_sma_60_0)\n# 当天股价在收盘时的涨跌停状态(1跌停,2未涨跌停,3涨停)\nprice_limit_status_0\n# 过滤st股票(0:正常股票,1:st,2:*st,11:暂停上市)\ncond4=st_status_0<1","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-11425"}],"output_ports":[{"name":"data","node_id":"-11425"}],"cacheable":true,"seq_num":4,"comment":"择时特征","comment_collapsed":true},{"node_id":"-11430","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"cond2&cond4","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-11430"}],"output_ports":[{"name":"data","node_id":"-11430"},{"name":"left_data","node_id":"-11430"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-3779","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"cond2&cond4","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-3779"}],"output_ports":[{"name":"data","node_id":"-3779"},{"name":"left_data","node_id":"-3779"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-1918","module_id":"BigQuantSpace.trade.trade-v4","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":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 = 2\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.5\n context.options['hold_days'] = 1\n \n # 这一段感觉是在为盘前准备函数写的,当order.amount>0时,认为是买入,<0卖出。\n from zipline.finance.slippage import SlippageModel\n class FixedPriceSlippage(SlippageModel):\n def process_order(self, data, order, bar_volume=0, trigger_check_price=0):\n if order.limit is None:\n price_field = self._price_field_buy if order.amount > 0 else self._price_field_sell\n price = data.current(order.asset, price_field)\n else:\n price = data.current(order.asset, self._price_field_buy)\n # 返回希望成交的价格和数量\n return (price, order.amount)\n # 设置price_field,默认是开盘买入,收盘卖出\n context.fix_slippage = FixedPriceSlippage(price_field_buy='open', price_field_sell='close')\n context.set_slippage(us_equities=context.fix_slippage)","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 获取当前持仓\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n today = data.current_dt.strftime('%Y-%m-%d')\n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == today]\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 \n \n # 2. 根据需要加入移动止赢止损模块、固定天数卖出模块、ST或退市股卖出模块\n stock_sold = [] # 记录卖出的股票,防止多次卖出出现空单\n \n #--------------------------START:持有固定天数卖出(不含建仓期)-----------\n current_stopdays_stock = []\n positions_lastdate = {e.symbol:p.last_sale_date for e,p in context.portfolio.positions.items()}\n # 不是建仓期(在前hold_days属于建仓期)\n if not is_staging:\n for instrument in positions.keys():\n #如果上面的止盈止损已经卖出过了,就不要重复卖出以防止产生空单\n if instrument in stock_sold:\n continue\n # 今天和上次交易的时间相隔hold_days就全部卖出 datetime.timedelta(context.options['hold_days'])也可以换成自己需要的天数,比如datetime.timedelta(5)\n if data.current_dt - positions_lastdate[instrument]>=datetime.timedelta(1) and data.can_trade(context.symbol(instrument)):\n context.order_target_percent(context.symbol(instrument), 0)\n current_stopdays_stock.append(instrument)\n cash_for_sell -= positions[instrument]\n if len(current_stopdays_stock)>0: \n stock_sold += current_stopdays_stock\n #------------------------- END:持有固定天数卖出-----------------------\n \n #-------------------------- START: ST和退市股卖出 --------------------- \n st_stock_list = []\n for instrument in positions.keys():\n try:\n instrument_name = ranker_prediction[ranker_prediction.instrument==instrument].name.values[0]\n # 如果股票状态变为了st或者退市 则卖出\n if 'ST' in instrument_name or '退' in instrument_name:\n if instrument in stock_sold:\n continue\n if data.can_trade(context.symbol(instrument)):\n context.order_target(context.symbol(instrument), 0)\n st_stock_list.append(instrument)\n cash_for_sell -= positions[instrument]\n except:\n continue\n if st_stock_list!=[]: \n stock_sold += st_stock_list\n\n #-------------------------- END: ST和退市股卖出 --------------------- \n \n \n # 3. 生成轮仓卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰\n if not is_staging and cash_for_sell > 0:\n instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(\n lambda x: x in positions)])))\n for instrument in instruments:\n if instrument in stock_sold:\n continue\n context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n stock_sold.append(instrument)\n\n # 4. 生成轮仓买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票\n # 计算今日跌停的股票\n #dt_list = list(ranker_prediction[ranker_prediction.price_limit_status_0==1].instrument)\n # 计算今日ST/退市的股票\n #st_list = list(ranker_prediction[ranker_prediction.name.str.contains('ST')|ranker_prediction.name.str.contains('退')].instrument)\n # 计算所有禁止买入的股票池\n banned_list = stock_sold\n buy_cash_weights = context.stock_weights\n buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n #buy_instruments=[k for k in list(ranker_prediction.instrument) if k not in banned_list][: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 cond1 = data.current(context.symbol(instrument), 'cond1')\n if cond1:\n current_price = data.current(context.symbol(instrument), 'price')\n amount = math.floor(cash / current_price - cash / current_price % 100)\n context.order(context.symbol(instrument), amount)\n\n\n","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n context.status_df = D.features(instruments =context.instruments,start_date = context.start_date, end_date = context.end_date, \n fields=['st_status_0','price_limit_status_0','price_limit_status_1'])\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"def bigquant_run(context, data):\n # 获取涨跌停状态数据\n df_price_limit_status = context.ranker_prediction.set_index('date')\n today=data.current_dt.strftime('%Y-%m-%d')\n # 得到当前未完成订单\n for orders in get_open_orders().values():\n # 循环,撤销订单\n for _order in orders:\n ins=str(_order.sid.symbol)\n try:\n #判断一下如果当日涨停(3),则取消卖单\n if df_price_limit_status[df_price_limit_status.instrument==ins].price_limit_status_0.ix[today]>2 and _order.amount<0:\n cancel_order(_order)\n print(today,'尾盘涨停取消卖单',ins) \n except:\n continue","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":0.025,"type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"open","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"close","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":"1000001","type":"Literal","bound_global_parameter":null},{"name":"auto_cancel_non_tradable_orders","value":"True","type":"Literal","bound_global_parameter":null},{"name":"data_frequency","value":"daily","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"后复权","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"股票","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-1918"},{"name":"options_data","node_id":"-1918"},{"name":"history_ds","node_id":"-1918"},{"name":"benchmark_ds","node_id":"-1918"},{"name":"trading_calendar","node_id":"-1918"}],"output_ports":[{"name":"raw_perf","node_id":"-1918"}],"cacheable":false,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"-184","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2010-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2018-01-01","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"300782.SZA\n605358.SHA\n603290.SHA\n603392.SHA\n601865.SHA\n300759.SZA\n300750.SZA\n300677.SZA\n002607.SZA\n603259.SHA\n300751.SZA\n603613.SHA\n601100.SHA\n300763.SZA\n002568.SZA\n300724.SZA\n603345.SHA\n600763.SHA\n603713.SHA\n300595.SZA\n300014.SZA\n603712.SHA\n300760.SZA\n603317.SHA\n002791.SZA\n601066.SHA\n002985.SZA\n300661.SZA\n300347.SZA\n300777.SZA\n603129.SHA\n300454.SZA\n601888.SHA\n605111.SHA\n603638.SHA\n300850.SZA\n600809.SHA\n002414.SZA\n603893.SHA\n002967.SZA\n600132.SHA\n603605.SHA\n300015.SZA\n603267.SHA\n300012.SZA\n600882.SHA\n300684.SZA\n300390.SZA\n300769.SZA\n\t\n300748.SZA\n000799.SZA\n300767.SZA\n300775.SZA\n603737.SHA\n300601.SZA\n601698.SHA\n300841.SZA\n002975.SZA\n603501.SHA\n300122.SZA\n300677.SZA\n603392.SHA\n002791.SZA\n601865.SHA\n300759.SZA\n002568.SZA\n603613.SHA\n300014.SZA\n601100.SHA\n300763.SZA\n300274.SZA\n601633.SHA\n603501.SHA\n002709.SZA\n603317.SHA\n300661.SZA\n002985.SZA\n600882.SHA\n300598.SZA\n300777.SZA\n300552.SZA\n300346.SZA\n002475.SZA\n605111.SHA\n300850.SZA\n300526.SZA\n601012.SHA\n603893.SHA\n002967.SZA\n000858.SZA\n603267.SHA\n000568.SZA\n603638.SHA\n000708.SZA\n603456.SHA\n000995.SZA\n600399.SHA\n300767.SZA\n300595.SZA\n300347.SZA\n600763.SHA\n300751.SZA\n600316.SHA\n300775.SZA\n603208.SHA\n600862.SHA\n002241.SZA\n002706.SZA\n300390.SZA\n601698.SHA\n002541.SZA\n002607.SZA\n000733.SZA\n000596.SZA\n603345.SHA\n300151.SZA\n300496.SZA\n002705.SZA\n002756.SZA\n603185.SHA\n002850.SZA\n000661.SZA\n002600.SZA\n300724.SZA\n600584.SHA\n002414.SZA\n300223.SZA\n002920.SZA\n603906.SHA\n002714.SZA\n600966.SHA\n300083.SZA\n300601.SZA\n600438.SHA\n002812.SZA\n002459.SZA\n603027.SHA\n300015.SZA\n300763.SZA\n002709.SZA\n688202.SHA\n000422.SZA\n300769.SZA\n300751.SZA\n300343.SZA\n605117.SHA\n300827.SZA\n601633.SHA\n603026.SHA\n002240.SZA\n002326.SZA\n002487.SZA\n000762.SZA\n300432.SZA\n603396.SHA\n300363.SZA\n603985.SHA\n000155.SZA\n002594.SZA\n600399.SHA\n600702.SHA\n300171.SZA\n002176.SZA\n000733.SZA\n300750.SZA\n601127.SHA\n002812.SZA\n603260.SHA\n600610.SHA\n601012.SHA\n003022.SZA\n603127.SHA\n000301.SZA\n002585.SZA\n688198.SHA\n002245.SZA\n300693.SZA\n600096.SHA\n300568.SZA\n300382.SZA\n300443.SZA\n003031.SZA\n605376.SHA\n603806.SHA\n603223.SHA\n688116.SHA\n002529.SZA\n600141.SHA\n600956.SHA\n300035.SZA\n300316.SZA\n002460.SZA\n600110.SHA\n300671.SZA\n002407.SZA\n600532.SHA\n688599.SHA\n002472.SZA\n600499.SHA\n600111.SHA\n600884.SHA\n300696.SZA\n603267.SHA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-184"}],"output_ports":[{"name":"data","node_id":"-184"}],"cacheable":true,"seq_num":27,"comment":"","comment_collapsed":true},{"node_id":"-148","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nname\ninstrument","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-148"}],"output_ports":[{"name":"data","node_id":"-148"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-224","module_id":"BigQuantSpace.use_datasource.use_datasource-v1","parameters":[{"name":"datasource_id","value":"instruments_CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-224"},{"name":"features","node_id":"-224"}],"output_ports":[{"name":"data","node_id":"-224"}],"cacheable":true,"seq_num":12,"comment":"","comment_collapsed":true},{"node_id":"-189","module_id":"BigQuantSpace.join.join-v3","parameters":[{"name":"on","value":"date","type":"Literal","bound_global_parameter":null},{"name":"how","value":"left","type":"Literal","bound_global_parameter":null},{"name":"sort","value":"True","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"data1","node_id":"-189"},{"name":"data2","node_id":"-189"}],"output_ports":[{"name":"data","node_id":"-189"}],"cacheable":true,"seq_num":19,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='70,183,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='743,-98,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-43' Position='661,607,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='249,375,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='853,683,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1092,-4,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='424,523,200,200'/><node_position Node='-86' Position='1064,575,200,200'/><node_position Node='-215' Position='399,162,200,200'/><node_position Node='-222' Position='385,280,200,200'/><node_position Node='-231' Position='1086,184,200,200'/><node_position Node='-238' Position='1078,303,200,200'/><node_position Node='-11425' Position='633,40,200,200'/><node_position Node='-11430' Position='378,455,200,200'/><node_position Node='-3779' Position='1064,439,200,200'/><node_position Node='-1918' Position='944,917,200,200'/><node_position Node='-184' Position='168,23,200,200'/><node_position Node='-148' Position='1473,362,200,200'/><node_position Node='-224' Position='1442,466,200,200'/><node_position Node='-189' Position='1058,817,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
[2021-12-27 16:51:58.914943] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-12-27 16:51:58.927205] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:58.929434] INFO: moduleinvoker: input_features.v1 运行完成[0.014523s].
[2021-12-27 16:51:58.933607] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-12-27 16:51:58.940382] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:58.941794] INFO: moduleinvoker: input_features.v1 运行完成[0.008188s].
[2021-12-27 16:51:58.947219] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-27 16:51:58.956249] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:58.957806] INFO: moduleinvoker: instruments.v2 运行完成[0.010595s].
[2021-12-27 16:51:58.974689] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2021-12-27 16:51:58.982940] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:58.984396] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.009731s].
[2021-12-27 16:51:58.992068] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-12-27 16:51:58.998476] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.000348] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.008284s].
[2021-12-27 16:51:59.009150] INFO: moduleinvoker: filter.v3 开始运行..
[2021-12-27 16:51:59.019417] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.021182] INFO: moduleinvoker: filter.v3 运行完成[0.012035s].
[2021-12-27 16:51:59.029554] INFO: moduleinvoker: dropnan.v1 开始运行..
[2021-12-27 16:51:59.037583] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.039282] INFO: moduleinvoker: dropnan.v1 运行完成[0.009729s].
[2021-12-27 16:51:59.044461] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-27 16:51:59.050917] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.052636] INFO: moduleinvoker: instruments.v2 运行完成[0.008184s].
[2021-12-27 16:51:59.066049] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2021-12-27 16:51:59.073715] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.075321] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.009297s].
[2021-12-27 16:51:59.082093] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-12-27 16:51:59.089458] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.091254] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.009157s].
[2021-12-27 16:51:59.100571] INFO: moduleinvoker: advanced_auto_labeler.v2 开始运行..
[2021-12-27 16:51:59.107149] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.108864] INFO: moduleinvoker: advanced_auto_labeler.v2 运行完成[0.008299s].
[2021-12-27 16:51:59.118685] INFO: moduleinvoker: join.v3 开始运行..
[2021-12-27 16:51:59.125376] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.127185] INFO: moduleinvoker: join.v3 运行完成[0.008505s].
[2021-12-27 16:51:59.137085] INFO: moduleinvoker: filter.v3 开始运行..
[2021-12-27 16:51:59.144017] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.145638] INFO: moduleinvoker: filter.v3 运行完成[0.008555s].
[2021-12-27 16:51:59.152835] INFO: moduleinvoker: dropnan.v1 开始运行..
[2021-12-27 16:51:59.159783] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.161271] INFO: moduleinvoker: dropnan.v1 运行完成[0.008436s].
[2021-12-27 16:51:59.168702] INFO: moduleinvoker: stock_ranker_train.v5 开始运行..
[2021-12-27 16:51:59.177620] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.291848] INFO: moduleinvoker: stock_ranker_train.v5 运行完成[0.123138s].
[2021-12-27 16:51:59.301910] INFO: moduleinvoker: stock_ranker_predict.v5 开始运行..
[2021-12-27 16:51:59.308909] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.310703] INFO: moduleinvoker: stock_ranker_predict.v5 运行完成[0.008786s].
[2021-12-27 16:51:59.315114] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-12-27 16:51:59.321876] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.324165] INFO: moduleinvoker: input_features.v1 运行完成[0.009057s].
[2021-12-27 16:51:59.329680] INFO: moduleinvoker: use_datasource.v1 开始运行..
[2021-12-27 16:51:59.336593] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.338416] INFO: moduleinvoker: use_datasource.v1 运行完成[0.008748s].
[2021-12-27 16:51:59.347644] INFO: moduleinvoker: join.v3 开始运行..
[2021-12-27 16:51:59.354306] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:51:59.356049] INFO: moduleinvoker: join.v3 运行完成[0.008416s].
[2021-12-27 16:51:59.441516] INFO: moduleinvoker: backtest.v8 开始运行..
[2021-12-27 16:51:59.447660] INFO: backtest: biglearning backtest:V8.6.1
[2021-12-27 16:53:08.864100] INFO: backtest: product_type:stock by specified
[2021-12-27 16:53:09.044520] INFO: moduleinvoker: cached.v2 开始运行..
[2021-12-27 16:53:09.057906] INFO: moduleinvoker: 命中缓存
[2021-12-27 16:53:09.061666] INFO: moduleinvoker: cached.v2 运行完成[0.017165s].
[2021-12-27 16:53:09.736233] INFO: algo: TradingAlgorithm V1.8.6
[2021-12-27 16:53:13.292440] INFO: algo: trading transform...
[2021-12-27 16:53:14.497988] ERROR: moduleinvoker: module name: backtest, module version: v8, trackeback: AttributeError: 'DataFrame' object has no attribute 'instrument'
[2021-12-27 16:53:14.508069] ERROR: moduleinvoker: module name: trade, module version: v4, trackeback: AttributeError: 'DataFrame' object has no attribute 'instrument'
bigcharts-data-start/{"__type":"tabs","__id":"bigchart-f31fd28353b64a34899d2963d1d217c1"}/bigcharts-data-end
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-29-9c19775615ea> in <module>
735 )
736
--> 737 m11 = M.trade.v4(
738 instruments=m9.data,
739 options_data=m19.data,
<ipython-input-29-9c19775615ea> in m11_handle_data_bigquant_run(context, data)
114 banned_list = stock_sold
115 buy_cash_weights = context.stock_weights
--> 116 buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
117 #buy_instruments=[k for k in list(ranker_prediction.instrument) if k not in banned_list][:len(buy_cash_weights)]
118 max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
AttributeError: 'DataFrame' object has no attribute 'instrument'