{"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"to_node_id":"-274:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"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":"-134: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":"-2486:input_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"to_node_id":"-6060:options_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"to_node_id":"-288:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-6060: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":"-281:input_data","from_node_id":"-274:data"},{"to_node_id":"-166:input_1","from_node_id":"-281:data"},{"to_node_id":"-295:input_data","from_node_id":"-288:data"},{"to_node_id":"-126:input_1","from_node_id":"-295:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","from_node_id":"-116:data_1"},{"to_node_id":"-2480:input_data","from_node_id":"-126:data_1"},{"to_node_id":"-274:features","from_node_id":"-134:data"},{"to_node_id":"-281:features","from_node_id":"-134:data"},{"to_node_id":"-288:features","from_node_id":"-134:data"},{"to_node_id":"-295:features","from_node_id":"-134:data"},{"to_node_id":"-86:input_data","from_node_id":"-154:data_1"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","from_node_id":"-166:data_1"},{"to_node_id":"-154:input_1","from_node_id":"-2480:data"},{"to_node_id":"-116:input_1","from_node_id":"-2486:data"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2014-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2018-12-30","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","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-8"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"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/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","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.SHA","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":"return_5\nreturn_10\nreturn_20\nreturn_3\navg_amount_10\navg_amount_20\navg_turn_3\npe_ttm_0\navg_turn_1\navg_mf_net_amount_10\nmarket_cap_float_0\nbeta_csi500_5_0\n\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-v6","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":"data_row_fraction","value":1,"type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"ndcg_discount_base","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":"2019-01-01","type":"Literal","bound_global_parameter":"交易日期"},{"name":"end_date","value":"2023-04-21","type":"Literal","bound_global_parameter":"交易日期"},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","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":"-274","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":"300","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-274"},{"name":"features","node_id":"-274"}],"output_ports":[{"name":"data","node_id":"-274"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-281","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":"-281"},{"name":"features","node_id":"-281"}],"output_ports":[{"name":"data","node_id":"-281"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true},{"node_id":"-288","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":"300","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-288"},{"name":"features","node_id":"-288"}],"output_ports":[{"name":"data","node_id":"-288"}],"cacheable":true,"seq_num":17,"comment":"","comment_collapsed":true},{"node_id":"-295","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":"-295"},{"name":"features","node_id":"-295"}],"output_ports":[{"name":"data","node_id":"-295"}],"cacheable":true,"seq_num":18,"comment":"","comment_collapsed":true},{"node_id":"-6060","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 = 50\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = [1/stock_count for i in range(stock_count)]\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 1/10\n context.options['hold_days'] = 3\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n #-------------------大盘风控代码---------------------------#\n #获取当日日期\n today = data.current_dt.strftime('%Y-%m-%d')\n benckmark_risk=context.benckmark_risk.loc[today]\n benckmark_buys=context.benckmark_buys.loc[today]\n benckmark_buys2=context.benckmark_buys2.loc[today]\n benckmark_risk2=context.benckmark_risk2.loc[today]\n benckmark_risk4=context.benckmark_risk4.loc[today]\n benckmark_risk5=context.benckmark_risk5.loc[today]\n benckmark_zd1=context.benckmark_zd1.loc[today]\n benckmark_risk6=context.benckmark_risk6.loc[today]\n benckmark_ret120=context.benckmark_ret120.loc[today]\n \n print(today,benckmark_ret120)\n #------------------------------------------止损模块START--------------------------------------------\n equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}\n \n # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断\n stoploss_stock = []\n stopwin_stock = []\n add_stock = []\n if len(equities) > 0:\n for i in equities.keys():\n #print(equities)\n stock_cost = equities[i].cost_basis\n v1=equities[i].available_close_amount\n #print(equities)\n stock_market_price = data.current(context.symbol(i), 'price')\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 highest_price_since_10days = data.history(context.symbol(i), 'high', 10, '1d').max()\n # 确定止损位置\n stock_p_ma = data.history(context.symbol(i),'price',30,'1d').mean()\n \n stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.2\n #if (stock_p_ma5>stock_p_ma10) and (stock_p_ma5_1<stock_p_ma10_1):\n #if stock_market_price*v1<context.portfolio.cash*0.2:\n #context.order_target(context.symbol(i),v1*2)\n #if (benckmark_buys2==1)and(stock_market_price > stock_p_ma5):\n #if stock_market_price*v1<context.portfolio.cash*0.2:\n #context.order_target(context.symbol(i),v1*2)\n #context.portfolio.cash-=v1*2*stock_market_price\n #stoploss_stock.append(i)\n if i not in stoploss_stock:\n if (stock_market_price < stoploss_line)|(stock_market_price<stock_p_ma):\n context.order_target_percent(context.symbol(i), 0) \n stoploss_stock.append(i)\n #if stock_market_price<highest_price_since_10days*0.9:\n \n #context.order_target(context.symbol(i),0)\n #stoploss_stock.append(i)\n if i not in stopwin_stock:\n if (benckmark_zd1==0)and(benckmark_buys==0)and((stock_market_price - stock_cost ) / stock_cost>= 0.2): \n context.order_target_percent(context.symbol(i),0)\n stopwin_stock.append(i)\n if (benckmark_zd1==0)and(benckmark_risk4==1)and((stock_market_price - stock_cost ) / stock_cost>= 0.15): \n context.order_target_percent(context.symbol(i),0)\n stopwin_stock.append(i)\n if (benckmark_buys==1)and((stock_market_price - stock_cost ) / stock_cost>= 0.4): \n context.order_target(context.symbol(i),v1/2)\n stopwin_stock.append(i)\n \n \n if (benckmark_zd1==0)and(benckmark_buys2==1)and(stock_market_price<highest_price_since_buy*0.95): \n context.order_target_percent(context.symbol(i),0)\n stopwin_stock.append(i)\n #print('日期:',today,'股票:',stopwin_stock,'出现下跌市场止盈10状况')\n \n #if benckmark_zd1 == 1 :\n \n #context.order_target(context.symbol(i), v1*2)\n #stoploss_stock.append(i)\n #if len(stoploss_stock)>0:\n #print('日期:', today, '股票:', stoploss_stock, '出现跟踪止损状况')\n #if len(stopwin_stock)>0: \n #print('日期:', today, '股票:', stopwin_stock, '出现跟踪止yin状况')\n #if len(add_stock)>0:\n #print('日期:', today, '股票:', add_stock, 'add')\n \n #-------------------------------------------止损模块END--------------------------------------------- \n stock_hold_now = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0} \n #stock_hold_now = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0} \n #print(stock_hold_now)\n #print(ss)\n #v2=context.portfolio.positions[context.symbol(instrument)].available_close_amount\n #equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}\n #当risk为1时,市场有风险,全部平仓,不再执行其它操作\n #if benckmark_zd1 == 1 :\n #return\n #for instrument in stock_hold_now:\n \n #v3=context.portfolio.positions[context.symbol(instrument)].available_close_amount\n #pp = context.portfolio.positions[context.symbol(instrument)].last_sale_price\n \n # addam = np.where((v3*pp*2>context.portfolio.cash*0.2),(v3*pp*2),(context.portfolio.cash*0.2))\n \n #if context.portfolio.cash > addam:\n #context.order_value(symbol(instrument),addam )\n #context.portfolio.cash -= addam\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 # 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: p for e, p in context.portfolio.positions.items() if p.amount>0}\n positions_lastdate = {e.symbol:p.last_sale_date 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(instruments)\n #price_limit_status = context.price_limit_status\n #status_today = price_limit_status[price_limit_status.date==today]\n for instrument in equities:\n sid = equities[instrument].sid\n cur_position = context.portfolio.positions[sid].amount\n v2=context.portfolio.positions[context.symbol(instrument)].available_close_amount\n # 如果已经移动止损卖出过则不再轮仓卖出,以防止出现空头持仓\n if instrument in stoploss_stock:\n continue\n #if instrument in stopwin_stock:\n #continue\n \n \n \n if cur_position > 0 and benckmark_buys==0 and data.current_dt - equities[instrument].last_sale_date>=datetime.timedelta(2) and data.can_trade(context.symbol(instrument)):\n context.order_target_percent(sid, 0)\n #context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n if cur_position > 0 and benckmark_buys==1 and data.current_dt - equities[instrument].last_sale_date>=datetime.timedelta(1) and data.can_trade(context.symbol(instrument)):\n context.order_target_percent(sid, 0)\n #context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n # 记录轮仓卖出的股票\n #sell_stock.append(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 #print(buy_instruments)\n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n #print(buy_instruments)\n \n \n \n if benckmark_zd1==1:\n \n for i, instrument in enumerate(buy_instruments):\n \n cash = cash_for_buy * buy_cash_weights[i]\n cash1 = cash/3\n if cash1 > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash1 = max_cash_per_instrument - positions.get(instrument, 0)\n #cash == cash/2\n if cash1 > 0:\n price = data.current(context.symbol(instrument), 'price') # 最新价格\n stock_num = np.floor(cash1/price/100)*100\n if instrument in stoploss_stock:\n continue\n if instrument in add_stock:\n continue\n context.order(context.symbol(instrument), stock_num)\n if benckmark_buys2 == 1 and benckmark_zd1 == 0:\n #print(today,benckmark_buys2,cash_for_buy)\n \n for i, instrument in enumerate(buy_instruments):\n #cash == cash/2\n cash = cash_for_buy * buy_cash_weights[i]\n cash == cash\n if cash > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash = max_cash_per_instrument - positions.get(instrument, 0)\n \n if cash > 0:\n price = data.current(context.symbol(instrument), 'price') # 最新价格\n stock_num = np.floor(cash/price/100)*100\n if instrument in stoploss_stock:\n continue\n if instrument in add_stock:\n continue\n context.order(context.symbol(instrument), stock_num)\n # print(today,cash)\n if benckmark_buys2 ==0 and benckmark_zd1 ==0:\n for i, instrument in enumerate(buy_instruments):\n #cash == cash/2\n cash = cash_for_buy * buy_cash_weights[i]\n cash== cash/2\n if cash > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash = max_cash_per_instrument - positions.get(instrument, 0)\n \n if cash > 0:\n price = data.current(context.symbol(instrument), 'price') # 最新价格\n stock_num = np.floor(cash/price/100)*100\n if instrument in stoploss_stock:\n continue\n if instrument in add_stock:\n continue\n context.order(context.symbol(instrument), stock_num)","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 获取涨跌停状态\n #context.price_limit_status = DataSource('stock_status_CN_STOCK_A').read(fields=['price_limit_status'])\n #在数据准备函数中一次性计算每日的大盘风控条件相比于在handle中每日计算风控条件可以提高回测速度\n # 多取50天的数据便于计算均值(保证回测的第一天均值不为Nan值),\n # 其中context.start_date和context.end_date是回测指定的起始时间和终止时间\n start_date= (pd.to_datetime(context.start_date) - datetime.timedelta(days=60)).strftime('%Y-%m-%d') \n \n df=DataSource('bar1d_index_CN_STOCK_A').read(start_date=start_date,end_date=context.end_date,fields=['close'])\n\n #这里以上证指数000001.HIX为例\n benckmark_data=df[df.instrument=='000905.HIX']\n #计算上证指数5日涨幅\n #print(benckmark_data)\n benckmark_data['ret5']=talib.MA(benckmark_data['close'], timeperiod=5)\n benckmark_data['ret10']=talib.MA(benckmark_data['close'], timeperiod=10)\n benckmark_data['ret20']=talib.MA(benckmark_data['close'], timeperiod=20)\n benckmark_data['ret40']=talib.MA(benckmark_data['close'], timeperiod=40)\n benckmark_data['ret1']=benckmark_data['close']/benckmark_data['close'].shift(3)-1\n benckmark_data['ret120']=talib.MA(benckmark_data['close'], timeperiod=120)\n #print()\n #计算大盘风控条件,如果5日涨幅小于-10%则设置风险状态risk为1,否则为0\n benckmark_data['risk1'] = np.where(((benckmark_data['close']<benckmark_data['ret20'])&(benckmark_data['close'].shift(3)<benckmark_data['ret20'].shift(3))),1,0)\n benckmark_data['risk2'] = np.where(benckmark_data['ret1']<-0.03,1,0)\n benckmark_data['risk3'] = np.where(((benckmark_data['close']<benckmark_data['ret20'])&(benckmark_data['ret10']<benckmark_data['ret20'])&(benckmark_data['ret20']<benckmark_data['ret40'])),1,0)\n benckmark_data['risk4'] = np.where(((benckmark_data['close']<benckmark_data['ret5'])&(benckmark_data['ret5']<benckmark_data['ret10'])&(benckmark_data['ret10']<benckmark_data['ret20'])&(benckmark_data['ret20']<benckmark_data['ret40'])),1,0)\n benckmark_data['risk5'] = np.where(((benckmark_data['close']<benckmark_data['close'].shift(3))&(benckmark_data['close']<benckmark_data['ret40']*0.96)),1,0)\n benckmark_data['risk6'] = np.where((benckmark_data['risk3']==1)&(benckmark_data['close']<benckmark_data['close'].shift(3))&(benckmark_data['close']<benckmark_data['ret120']*0.93),1,0)\n benckmark_data['buy1'] = np.where(((benckmark_data['close']>benckmark_data['close'].shift(1))&(benckmark_data['close']>benckmark_data['ret20'])&(benckmark_data['ret10']>benckmark_data['ret20'])&(benckmark_data['ret20']>benckmark_data['ret40'])),1,0)\n benckmark_data['buy2'] = np.where(((benckmark_data['close']>benckmark_data['ret5'])&(benckmark_data['close']>benckmark_data['close'].shift(1))&(benckmark_data['close']>benckmark_data['ret20'])&(benckmark_data['ret10']>benckmark_data['ret20'])&(benckmark_data['ret20']>benckmark_data['ret40'])),1,0)\n #震荡区间\n benckmark_data['zd1'] = np.where((abs(benckmark_data['ret40']-benckmark_data['ret20'])<10),1,0)\n \n \n benckmark_data.set_index('date',inplace=True)\n #把风控序列输出给全局变量context.benckmark_risk\n context.benckmark_risk=benckmark_data['risk4']+benckmark_data['risk5']\n context.benckmark_buys=benckmark_data['buy1']\n context.benckmark_buys2=benckmark_data['buy2']\n context.benckmark_risk2=benckmark_data['risk2']\n context.benckmark_risk4=benckmark_data['risk4']\n context.benckmark_risk5=benckmark_data['risk5']\n context.benckmark_risk6=benckmark_data['risk6']\n context.benckmark_ret120=benckmark_data['ret120']\n context.benckmark_zd1=benckmark_data['zd1']\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"","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":"10000000","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":"000905.HIX","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-6060"},{"name":"options_data","node_id":"-6060"},{"name":"history_ds","node_id":"-6060"},{"name":"benchmark_ds","node_id":"-6060"},{"name":"trading_calendar","node_id":"-6060"}],"output_ports":[{"name":"raw_perf","node_id":"-6060"}],"cacheable":false,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-116","module_id":"BigQuantSpace.filtet_st_stock.filtet_st_stock-v7","parameters":[],"input_ports":[{"name":"input_1","node_id":"-116"}],"output_ports":[{"name":"data_1","node_id":"-116"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-126","module_id":"BigQuantSpace.filtet_st_stock.filtet_st_stock-v7","parameters":[],"input_ports":[{"name":"input_1","node_id":"-126"}],"output_ports":[{"name":"data_1","node_id":"-126"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"-134","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nclose_0\nhigh_0\nta_ema_5_0\nta_ema_10_0\nin_csi500_0\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-134"}],"output_ports":[{"name":"data","node_id":"-134"}],"cacheable":true,"seq_num":20,"comment":"","comment_collapsed":true},{"node_id":"-154","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n import talib\n # 示例代码如下。在这里编写您的代码\n input_df = input_1.read_df().reset_index(drop=True)\n \n def cal(df):\n # 将价格数据转化成float类型\n close = [float(x) for x in df['close_0']]\n # 通过talib计算移动平均值(方法2)\n df['MA10'] = talib.MA(np.array(close), timeperiod=20)\n df['cond'] = (df['close_0']>df['MA10']).astype(int)\n df.drop('MA10',axis=1)\n return df\n # 计算指标条件\n result = input_df.groupby('instrument').apply(cal)\n # 过滤\n filter_result = result[result['cond']>0]\n # 输出\n data_1 = DataSource.write_df(filter_result)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-154"},{"name":"input_2","node_id":"-154"},{"name":"input_3","node_id":"-154"}],"output_ports":[{"name":"data_1","node_id":"-154"},{"name":"data_2","node_id":"-154"},{"name":"data_3","node_id":"-154"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-166","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n import talib \n # 示例代码如下。在这里编写您的代码\n input_df = input_1.read_df().reset_index(drop=True)\n \n def cal(df):\n # 将价格数据转化成float类型\n close = [float(x) for x in df['close_0']]\n # 通过talib计算移动平均值(方法2)\n df['MA10'] = talib.MA(np.array(close), timeperiod=20)\n df['cond'] = (df['close_0']>df['MA10']).astype(int)\n df.drop('MA10',axis=1)\n return df\n # 计算指标条件\n result = input_df.groupby('instrument').apply(cal)\n # 过滤\n filter_result = result[result['cond']>0]\n # 输出\n data_1 = DataSource.write_df(filter_result)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-166"},{"name":"input_2","node_id":"-166"},{"name":"input_3","node_id":"-166"}],"output_ports":[{"name":"data_1","node_id":"-166"},{"name":"data_2","node_id":"-166"},{"name":"data_3","node_id":"-166"}],"cacheable":true,"seq_num":19,"comment":"","comment_collapsed":true},{"node_id":"-2480","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"in_csi500_0==1","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":"-2480"}],"output_ports":[{"name":"data","node_id":"-2480"},{"name":"left_data","node_id":"-2480"}],"cacheable":true,"seq_num":12,"comment":"","comment_collapsed":true},{"node_id":"-2486","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"in_csi500_0==1","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":"-2486"}],"output_ports":[{"name":"data","node_id":"-2486"},{"name":"left_data","node_id":"-2486"}],"cacheable":true,"seq_num":21,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='212,65,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='70,183,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='768,-40,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-43' Position='589,645,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='211,408,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='857,728,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1318,73,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='433,528,200,200'/><node_position Node='-86' Position='1062,652,200,200'/><node_position Node='-274' Position='381,188,200,200'/><node_position Node='-281' Position='385,280,200,200'/><node_position Node='-288' Position='1078,236,200,200'/><node_position Node='-295' Position='1081,327,200,200'/><node_position Node='-6060' Position='1082,816,200,200'/><node_position Node='-116' Position='80,556,200,200'/><node_position Node='-126' Position='1242,415,200,200'/><node_position Node='-134' Position='685,86,200,200'/><node_position Node='-154' Position='1289,561,200,200'/><node_position Node='-166' Position='509,358,200,200'/><node_position Node='-2480' Position='1423,478,200,200'/><node_position Node='-2486' Position='20,474,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
[2023-04-25 13:00:08.841748] INFO: moduleinvoker: instruments.v2 开始运行..
[2023-04-25 13:00:09.016370] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.017925] INFO: moduleinvoker: instruments.v2 运行完成[0.176182s].
[2023-04-25 13:00:09.025044] INFO: moduleinvoker: advanced_auto_labeler.v2 开始运行..
[2023-04-25 13:00:09.040524] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.041887] INFO: moduleinvoker: advanced_auto_labeler.v2 运行完成[0.016841s].
[2023-04-25 13:00:09.045422] INFO: moduleinvoker: input_features.v1 开始运行..
[2023-04-25 13:00:09.057159] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.058902] INFO: moduleinvoker: input_features.v1 运行完成[0.013478s].
[2023-04-25 13:00:09.062821] INFO: moduleinvoker: input_features.v1 开始运行..
[2023-04-25 13:00:09.079172] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.080800] INFO: moduleinvoker: input_features.v1 运行完成[0.017951s].
[2023-04-25 13:00:09.115840] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2023-04-25 13:00:09.124917] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.126371] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.010551s].
[2023-04-25 13:00:09.133394] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2023-04-25 13:00:09.147248] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.149186] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.015784s].
[2023-04-25 13:00:09.160498] INFO: moduleinvoker: cached.v3 开始运行..
[2023-04-25 13:00:09.170027] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.171532] INFO: moduleinvoker: cached.v3 运行完成[0.01104s].
[2023-04-25 13:00:09.178841] INFO: moduleinvoker: join.v3 开始运行..
[2023-04-25 13:00:09.193206] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.194616] INFO: moduleinvoker: join.v3 运行完成[0.015773s].
[2023-04-25 13:00:09.201808] INFO: moduleinvoker: filter.v3 开始运行..
[2023-04-25 13:00:09.212214] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.213650] INFO: moduleinvoker: filter.v3 运行完成[0.011857s].
[2023-04-25 13:00:09.235663] INFO: moduleinvoker: filtet_st_stock.v7 开始运行..
[2023-04-25 13:00:09.251856] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.253303] INFO: moduleinvoker: filtet_st_stock.v7 运行完成[0.017651s].
[2023-04-25 13:00:09.261449] INFO: moduleinvoker: dropnan.v1 开始运行..
[2023-04-25 13:00:09.273070] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.274591] INFO: moduleinvoker: dropnan.v1 运行完成[0.013152s].
[2023-04-25 13:00:09.282470] INFO: moduleinvoker: stock_ranker_train.v6 开始运行..
[2023-04-25 13:00:09.307610] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.471621] INFO: moduleinvoker: stock_ranker_train.v6 运行完成[0.189132s].
[2023-04-25 13:00:09.480296] INFO: moduleinvoker: instruments.v2 开始运行..
[2023-04-25 13:00:09.494290] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.495897] INFO: moduleinvoker: instruments.v2 运行完成[0.015603s].
[2023-04-25 13:00:09.513206] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2023-04-25 13:00:09.525290] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.526972] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.01378s].
[2023-04-25 13:00:09.535100] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2023-04-25 13:00:09.546931] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.548587] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.013492s].
[2023-04-25 13:00:09.560615] INFO: moduleinvoker: filtet_st_stock.v7 开始运行..
[2023-04-25 13:00:09.573671] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.575085] INFO: moduleinvoker: filtet_st_stock.v7 运行完成[0.014481s].
[2023-04-25 13:00:09.584510] INFO: moduleinvoker: filter.v3 开始运行..
[2023-04-25 13:00:09.598052] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.599541] INFO: moduleinvoker: filter.v3 运行完成[0.015032s].
[2023-04-25 13:00:09.611892] INFO: moduleinvoker: cached.v3 开始运行..
[2023-04-25 13:00:09.622699] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.624426] INFO: moduleinvoker: cached.v3 运行完成[0.012544s].
[2023-04-25 13:00:09.632667] INFO: moduleinvoker: dropnan.v1 开始运行..
[2023-04-25 13:00:09.644543] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.646138] INFO: moduleinvoker: dropnan.v1 运行完成[0.013464s].
[2023-04-25 13:00:09.654101] INFO: moduleinvoker: stock_ranker_predict.v5 开始运行..
[2023-04-25 13:00:09.664822] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:09.666233] INFO: moduleinvoker: stock_ranker_predict.v5 运行完成[0.012127s].
[2023-04-25 13:00:09.718238] INFO: moduleinvoker: backtest.v8 开始运行..
[2023-04-25 13:00:09.724961] INFO: backtest: biglearning backtest:V8.6.3
[2023-04-25 13:00:09.985581] INFO: backtest: product_type:stock by specified
[2023-04-25 13:00:10.058815] INFO: moduleinvoker: cached.v2 开始运行..
[2023-04-25 13:00:10.072134] INFO: moduleinvoker: 命中缓存
[2023-04-25 13:00:10.074269] INFO: moduleinvoker: cached.v2 运行完成[0.015471s].
bigcharts-data-start/{"__type":"tabs","__id":"bigchart-402ee9c75a1646b9bcad8d1b1b483cff"}/bigcharts-data-end