复制链接
克隆策略

    {"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":"-339: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":"-339:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-346:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-362:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-213:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-355:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-2576:input_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"to_node_id":"-390:data1","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"to_node_id":"-355:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-374:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-2158:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-346:input_data","from_node_id":"-339:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","from_node_id":"-346:data"},{"to_node_id":"-362:input_data","from_node_id":"-355:data"},{"to_node_id":"-2171:data2","from_node_id":"-362:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:model","from_node_id":"-213:model"},{"to_node_id":"-213:training_ds","from_node_id":"-2576:data"},{"to_node_id":"-374:options_data","from_node_id":"-390:data"},{"to_node_id":"-2158:features","from_node_id":"-956:data"},{"to_node_id":"-2178:features","from_node_id":"-956:data"},{"to_node_id":"-2178:input_data","from_node_id":"-2158:data"},{"to_node_id":"-440:input_1","from_node_id":"-2165:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:data","from_node_id":"-2171:data"},{"to_node_id":"-390:data2","from_node_id":"-2171:data"},{"to_node_id":"-2165:input_data","from_node_id":"-2178:data"},{"to_node_id":"-2171:data1","from_node_id":"-440:data_1"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2017-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2021-01-01","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_99】【前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -3) / 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":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nclose_0/mean(close_0,5)\nclose_0/mean(close_0,10)\nclose_0/mean(close_0,20)\nclose_0/open_0\nopen_0/mean(close_0,5)\nopen_0/mean(close_0,10)\nopen_0/mean(close_0,20)\navg_turn_0/avg_turn_5\navg_turn_0/avg_turn_10\nmf_net_pct_main_0","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-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":"2021-01-01","type":"Literal","bound_global_parameter":"交易日期"},{"name":"end_date","value":"2022-04-28","type":"Literal","bound_global_parameter":"交易日期"},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"300505.SZA\n002226.SZA","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":"-339","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":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-339"},{"name":"features","node_id":"-339"}],"output_ports":[{"name":"data","node_id":"-339"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-346","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":"-346"},{"name":"features","node_id":"-346"}],"output_ports":[{"name":"data","node_id":"-346"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true},{"node_id":"-355","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":"258","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-355"},{"name":"features","node_id":"-355"}],"output_ports":[{"name":"data","node_id":"-355"}],"cacheable":true,"seq_num":17,"comment":"","comment_collapsed":true},{"node_id":"-362","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":"True","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":"-362"},{"name":"features","node_id":"-362"}],"output_ports":[{"name":"data","node_id":"-362"}],"cacheable":true,"seq_num":18,"comment":"","comment_collapsed":true},{"node_id":"-374","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 #context.stock_count = 1\n context.stock_count = 10\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n #context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, context.stock_count)])\n context.stock_weights = [1/context.stock_count for k in range(context.stock_count)] #半仓买入,每只股票等资金分配\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 0.2\n context.options['hold_days'] = 5\n\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"def getMa(today,context,instruments):\n # 加载原始数据\n import datetime\n stock_raw_data = D.history_data(instruments, today+datetime.timedelta(days=-40), today, ['date','close','low'])\n #print('11111',stock_raw_data)\n stock_raw_data=stock_raw_data.iloc[-20:] \n #stock_raw_data=stock_raw_data[:55]\n #print('22222',stock_raw_data)\n sumMa=0\n lastClose=0\n for index,row in stock_raw_data.iterrows():\n #print(index,row)\n sumMa += row['close']\n lastClose=row['close']\n \n # 包含多个周期均线值的股票数据\n # stock_ma_data = stock_raw_data.groupby('instrument').apply(ma_calculate)\n return [sumMa/20,lastClose]\n\n\n\n# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n #context.ranker_prediction.set_index('date')\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n today = data.current_dt.strftime('%Y-%m-%d')\n # 当日可用资金\n cash_for_buy = context.portfolio.cash\n \n # 获取当日买入列表,每天选取context.stock_count只\n buy_list=[]\n buy1_list = list(ranker_prediction[ranker_prediction.my1 == 1].instrument[:30])\n buy2_list = list(ranker_prediction[ranker_prediction.my2== 1].instrument[:10])\n print(today, '--买一选股-buy1_list==',buy1_list,'===买二选股-buy2_list:', len(buy2_list),buy2_list,)\n if len(buy1_list) > 0 :\n buy_list.append(buy1_list)\n if len(buy2_list) > 0 :\n buy_list.append(buy2_list)\n \n # 获取当前持仓资金\n stock_hold_now = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.perf_tracker.position_tracker.positions.items() if p.amount>0} \n\n equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}\n \n # 需要卖出的股票:已有持仓中不在买入列表的股票\n #stock_to_sell = [ i for i in stock_hold_now if i not in buy_list ]\n stock_to_sell = [ i for i in stock_hold_now ]\n #stock_to_buy = [ i for i in buy_list if i not in stock_hold_now ]\n stock_to_buy1 = [] # [ i for i in buy1_list ] 只测试my2\n stock_to_buy2 = [ i for i in buy2_list ]\n \n #print(today,' 选股stock_to_buy: ',len(stock_to_buy),' 个: ',stock_to_buy)\n #print(today,' context.ranker_prediction ==: ',context.ranker_prediction)\n # print('ranker_prediction.dtypes',ranker_prediction.dtypes)\n\n # 处理一阳穿多线的买二准备 \n # for i, instrument in enumerate(stock_to_buy):\n # print(today, i,instrument )\n # dfinstrument=ranker_prediction[ranker_prediction.instrument==instrument]\n # junxians=list(dfinstrument['junxian'])\n # junxian=junxians[0]\n #print(today, i ,' junxian==',junxian)\n # if(junxian==1):\n # 通过标签赋值\n # dfinstrument.loc[0,'isReadyBuy2'] = 1\n #dfinstrument.loc[0,'date'] = '2022-03-25'\n # dfinstrument[dfinstrument['date'] == today] =str(today) + str(datetime.timedelta(days=1))\n #dfinstrument[dfinstrument['date'] == today] = (today.date()+datetime.timedelta(days=1)).strftime('%Y-%m-%d')\n #再处理非交易日和2天后数据也还有该条数据的问题\n # print(today,' append dfinstrument ',dfinstrument)\n # dfinstrument.set_index('date')\n # context.ranker_prediction.append(dfinstrument)\n\n \n # print(today,' after context.ranker_prediction ==: ',context.ranker_prediction) \n \n #stocks=len(stock_hold_now) #?\n stocksBuy1N=0\n stocksBuy2N=0\n stockBuy1_hold_now=[] \n stockBuy2_hold_now=[] \n # 卖出列表进行卖出操作\n if len(stock_to_sell)>0:\n for instrument in stock_to_sell:\n sid = context.symbol(instrument) # 将标的转化为equity格式\n cur_position = context.portfolio.positions[sid].amount # 持仓\n if cur_position > 0 and data.can_trade(sid):\n instruments=[]\n instruments.append(instrument) \n #ri10=getMa(data.current_dt,context,instruments)\n #print('df after get ma 55==',ri55)\n try :\n a=getMa(data.current_dt,context,instruments)\n ri20=a[0]\n lastClose=a[1]\n except :\n ri20=0\n lastClose=0\n #print(instrument,today,'ma10===',ri10,'lastClose===',lastClose)\n \n stock_market_price = data.current(context.symbol(instrument), 'price') # 最新市场价格\n stock_market_today_high = data.current(context.symbol(instrument), 'high') #今日最高价 \n stock_market_today_close = data.current(context.symbol(instrument), 'close') #今日收盘价\n last_sale_date = equities[instrument].last_sale_date # 上次交易日期\n last_cost_price = equities[instrument].cost_basis # 上次交易金额\n delta_days = data.current_dt - last_sale_date \n hold_days = delta_days.days # 持仓天数\n # 最高收益\n #high_return = (highclose_price_since_buy-last_cost_price)/last_cost_price\n \n target_return = stock_market_today_close/last_cost_price\n #\n isBuy1=stock_hold_now[instrument]/context.portfolio.portfolio_value\n #print(instrument,' isBuy1 or buy2 ==',isBuy1)\n if isBuy1< 1/30 : #处理买一订单,买一股票池300,持仓10天轮动,每天30只\n \n if hold_days>=10 :\n context.order_target(sid, 0)\n print(today,'买一超期卖出 :','收益: ',target_return,sid)\n cash_for_buy += stock_hold_now[instrument]\n #stocks = stocks-1\n #elif target_return>=1.15 :\n # context.order_target(sid, 0)\n # print(today,'盈利卖出 :','收益: ',target_return,sid)\n # cash_for_buy += stock_hold_now[instrument]\n # stocks = stocks-1\n #elif lastClose<ri10 :\n # context.order_target(sid, 0)\n # print(today,' 跌破10日线止损卖出 ri10=:',ri10,' 收益: ',target_return,sid)\n # cash_for_buy += stock_hold_now[instrument]\n # stocks = stocks-1\n elif target_return<=0.92 :\n context.order_target(sid, 0)\n print(today,'买一止损卖出 :','收益: ',target_return,sid)\n cash_for_buy += stock_hold_now[instrument]\n #stocks = stocks-1\n else:\n stockBuy1_hold_now.append(instrument)\n stocksBuy1N=stocksBuy1N+1\n else : #处理买二订单\n stockBuy2_hold_now.append(instrument)\n if hold_days>=5 :\n context.order_target(sid, 0)\n print(today,'买二超期卖出 :','收益: ',target_return,sid)\n cash_for_buy += stock_hold_now[instrument]\n #stocks = stocks-1\n elif target_return>=1.1 :\n context.order_target(sid, 0)\n print(today,'买二盈利卖出 :','收益: ',target_return,sid)\n cash_for_buy += stock_hold_now[instrument]\n #stocks = stocks-1\n elif lastClose<ri20 :\n context.order_target(sid, 0)\n print(today,' 买二跌破20日线止损卖出 ri20=:',ri20,' 收益: ',target_return,sid)\n cash_for_buy += stock_hold_now[instrument]\n #stocks = stocks-1\n elif target_return<=0.95 :\n context.order_target(sid, 0)\n print(today,'买二止损卖出 :','收益: ',target_return,sid)\n cash_for_buy += stock_hold_now[instrument]\n # stocks = stocks-1\n else:\n stockBuy2_hold_now.append(instrument)\n stocksBuy2N=stocksBuy2N+1 \n \n #context.order_target_percent(sid, 0) # 全部卖出\n # 如果是早盘买早盘卖,卖出的资金可以用于买股票,此时应将下面的注释打开,卖出股票时更新可用现金;\n # 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用\n #cash_for_buy += stock_hold_now[instrument]\n \n # 买入列表执行买操作\n #if len(stock_to_buy)>0:\n # for instrument,weight in zip(stock_to_buy,context.stock_weights):\n # sid = context.symbol(instrument) # 将标的转化为equity格式\n # if data.can_trade(sid):\n # context.order_target_value(sid, min(cash_for_buy,context.portfolio.portfolio_value*0.5)) # 买入 \n\n #print(today,'--stockBuy1_hold_now--=:',stockBuy1_hold_now)\n stockBuy1_hold_now\n # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票\n buy_cash_weights = context.stock_weights\n #buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n buy1N=min(30,(300-stocksBuy1N))\n buy2N=min(10,(20-stocksBuy2N))\n buy1_instruments = []\n if len(stock_to_buy1) > 0 :\n buy1_instruments=list(stock_to_buy1[:buy1N])\n buy2_instruments = []\n if len(stock_to_buy2) > 0 :\n buy2_instruments=list(stock_to_buy2[:buy2N])\n #print(today,' buy_instruments:',buy_instruments,' 权重: ',buy_cash_weights)\n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n for i, instrument in enumerate(buy1_instruments):\n #cash = cash_for_buy * buy_cash_weights[i] \n #cash = context.portfolio.portfolio_value * buy_cash_weights[i]\n #用20分1的资金买一(300zhi)\n cash = (cash_for_buy/20)/(200-stocksBuy1N)\n #print('cash_for_buy==',cash_for_buy, ' cash==',cash)\n #if cash > max_cash_per_instrument - stock_hold_now.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n # cash = max_cash_per_instrument - stock_hold_now.get(instrument, 0)\n if cash > 0:\n if instrument not in stockBuy1_hold_now : #不重复买入\n context.order_value(context.symbol(instrument), cash)\n # print(today,' 买入一买 ',instrument)\n \n for i, instrument in enumerate(buy2_instruments):\n #cash = cash_for_buy * buy_cash_weights[i] \n #cash = context.portfolio.portfolio_value * buy_cash_weights[i]\n cash = (cash_for_buy*0.9)/(20-stocksBuy2N)\n #print('cash_for_buy==',cash_for_buy, ' cash==',cash)\n # if cash > max_cash_per_instrument - stock_hold_now.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n # cash = max_cash_per_instrument - stock_hold_now.get(instrument, 0)\n if cash > 0:\n #if instrument in stockBuy1_hold_now :\n context.order_value(context.symbol(instrument), cash)\n print(today,' 买入二买 ',instrument)\n # else:\n # print(today,' 发现二买,但没有一买,放弃买入:---',instrument)\n else:\n print(today,' 无资金买入二买 ',instrument)\n \n ","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\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":"twap_1","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"twap_1","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":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-374"},{"name":"options_data","node_id":"-374"},{"name":"history_ds","node_id":"-374"},{"name":"benchmark_ds","node_id":"-374"},{"name":"trading_calendar","node_id":"-374"}],"output_ports":[{"name":"raw_perf","node_id":"-374"}],"cacheable":false,"seq_num":19,"comment":"","comment_collapsed":true},{"node_id":"-213","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":"-213"},{"name":"features","node_id":"-213"},{"name":"test_ds","node_id":"-213"},{"name":"base_model","node_id":"-213"}],"output_ports":[{"name":"model","node_id":"-213"},{"name":"feature_gains","node_id":"-213"},{"name":"m_lazy_run","node_id":"-213"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-2576","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-2576"},{"name":"features","node_id":"-2576"}],"output_ports":[{"name":"data","node_id":"-2576"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-390","module_id":"BigQuantSpace.join.join-v3","parameters":[{"name":"on","value":"date,instrument","type":"Literal","bound_global_parameter":null},{"name":"how","value":"left","type":"Literal","bound_global_parameter":null},{"name":"sort","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"data1","node_id":"-390"},{"name":"data2","node_id":"-390"}],"output_ports":[{"name":"data","node_id":"-390"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"-956","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"#600335.SHA\n#300061.SZA\n#300364.SZA\n#002173.SZA\n#002433.SZA\n#601020.SHA\n#000545.SZA\n#000514.SZA\n#300505.SZA\n#002226.SZA\n#300299.SZA\n#600425.SHA\n#000632.SZA\n#002060.SZA\n\n#gaodianN5=where(ts_argmax(high_0, 258*5)<800,1,0)\n#gaodianN4=where(ts_argmax(high_0, 258*4)<800,1,0)\n#5年高点,1年按258个交易日算\n#isgaodian=max(gaodianN5,gaodianN4)\n\n#1年低点,1年按258个交易日算\n#didianN=ts_argmin(low_0, 258*1)\n#isdidian=where(didianN<200.0,1,0)\n\n#半年高点,1年按258个交易日算\nbanniangaodianN=ts_argmax(high_0, 128)\nisbanniangaodian=where(banniangaodianN<80.0,1,0)\n\n#timeperiod移动平均线\n#zhouma34=ta_ma(close_0, timeperiod=166)\n#timeperiod移动平均线\nzhouma55=ta_ma(close_0, timeperiod=258)\n#在周55均线之上,\nisupzhou55=where(close_0>zhouma55,1,0)\n#回调程度\nhuitiaodu=ts_max(high_0, 128)/low_1\n\n#反弹比例,选比例最高的10-20只交易,后面调试\n#yiniandidian=ts_min(low_0, 258*1)\nbanniangaodian=ts_max(high_0, 128*1)\n#fantanbili=banniangaodian/yiniandidian\n\n#macd1=ta_macd_macd_12_26_9_0\n#macd2=ta_macd_macdhist_12_26_9_0\n#macd3=ta_macd_macdsignal_12_26_9_0\n#sw1=swing_volatility_5_0 #振幅波动率,timeperiod=5* 取值: , 5, 10, 30, 60, 120, 240\n#sw2=swing_volatility_10_0\n#sw3=swing_volatility_30_0\n#sw4=swing_volatility_60_0\nvl1=volatility_5_0 #波动率,timeperiod=240* 取值: , 5, 10, 30, 60, 120, 240\nvl2=volatility_10_0\nvl3=volatility_30_0\n#vl4=volatility_60_0\n\n\nmacd_dif=ta_macd_dif(close_0/adjust_factor_0)\nmacd_h=ta_macd_hist(close_0/adjust_factor_0)\nduanqiup1=where(low_0>low_1,1,0)\nduanqiup2=where(macd_h>shift(macd_h,1),1,0)\nduanqiup=max(duanqiup1,duanqiup2)\n\n\n#st_status_0\n#my1==1&(vl2<vl3)&(vl2>0.012)&(vl2<0.038)&(macd1<0)&date>'2022-04-01'\n#my1=where((isbanniangaodian==1)&\\\n# (huitiaodu>1.2)&\\\n# (vl2<vl3)&(vl2>0.012)&(vl2<0.038)&(st_status_0==0),1,0)\n\nmy1=where((huitiaodu>1.2)&\\\n (vl2<vl3)&(vl2>0.012)&(vl2<0.038)&(st_status_0==0),1,0)\n\n#isgaodian==1&isdidian==1&isbanniangaodian==1&isbanniangaodian==1&fantanbili>1.5&date>='2022-03-08'\n#调试一下不要半年高点?只要回调度,不管什么时候回调的?涨停上穿也算\n\n#按前一天算ma,即线重合后第三天再买如\nma5=ta_ma(close_0, timeperiod=5)\nma10=ta_ma(close_0, timeperiod=10)\nma13=ta_ma(close_0, timeperiod=13)\nma20=ta_ma(close_0, timeperiod=20)\nma34=ta_ma(close_0, timeperiod=34)\nma55=ta_ma(close_0, timeperiod=55)\n\n#4-5线重合 真阳线 后面再调试是否要过滤returen0《5-8;-这样可以亏损幅度可以小一些;再调试回调幅度是否要大于1.5\n#junxian=where((ma5<=high_0) & (ma10<=high_0) & (ma13<=high_0) &(ma20<=high_0) & (ma34<=high_0) & (ma55<=high_0) &\\\n# (ma5 >=low_0) & (ma10>=low_0) & (ma13>=low_0) & (ma20>=low_0) & (ma34>=low_0) & (ma55>=low_0) &\\\n# (return_0>1.01)&(close_0>open_0)&(return_0<1.08)&(isbanniangaodian==1),1,0) \n#试试2日穿阳\njunxian=where((ma5<=high_0) & (ma10<=high_0) & (ma13<=high_0) &(ma20<=high_0) & (ma34<=high_0) & (ma55<=high_0) &\\\n (return_0>1.01)&(close_0>open_0)&(return_0<1.08),1,0) \n\nmaxline=max(ma5, ma10,ma13, ma20,ma34,ma55)\nminline=min(ma5, ma10,ma13, ma20,ma34,ma55)\nzhanshang55=close_0/maxline\n#4-5线重合度\njunxianchonghedu=minline/maxline\njunxianchonghedu5=shift(junxianchonghedu,5)\njunxianchonghedu18=shift(junxianchonghedu,18)\n# volume_* 第前 * 个交易日的交易量* 取值: 0 .. 20\n#一阳穿线需要放量1.5倍以上,后面调试. #nanmean(x, d), #过去 d 天 x 的均值(去除NaN值)\n\nfanliang=volume_0/volume_1 #放量\ndeaLn=deal_number_0/deal_number_1\ndaDan=fanliang/deaLn #交易笔数放量比不上交易额放量,说明是大单进入多,用大于1.18进行调试\n\n#my2ready=where((isbanniangaodian==1)&(junxian==1)&(zhanshang55>=1.0)&\n #(junxianchonghedu>0.94)&(huitiaodu>1.15)&(fanliang>1.45)&(daDan>1.18),1,0) #需要收拢条件\nmy2ready=where((junxian==1)&(zhanshang55>=1.0)&\\\n (junxianchonghedu>junxianchonghedu5)&(junxianchonghedu5>junxianchonghedu18)&\\\n (junxianchonghedu>0.95)&(huitiaodu>1.15)&(fanliang>1.5)&(daDan>1.2),1,0)\nmy2=where((shift(my2ready,1)==1)&(low_0>low_1)&(close_0>=ma5),1,0)\n#low_0>low_1这个条件太松了\n#最好不离55线太远,--my1使用\nnearMa55=ma55/close_0\n\nSW_code=industry_sw_level1_0\nmyrank=rank_fs_roe_ttm_0+rank_fs_net_profit_qoq_0-rank_pb_lf_0\n\npe_ttm_0 #滚动市盈率\nfs_eps_0 #每股收益\nfs_eps_yoy_0 #每股收益同比增长率\nfs_bps_0 #每股净资产\nfs_roa_ttm_0 #总资产报酬率 (TTM)\n\n#isbanniangaodian==1&junxian==1&zhanshang55>1&junxianchonghedu>0.95&huitiaodu>1.3&date>='2022-01-01'\nmy=max(my1,my2)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-956"}],"output_ports":[{"name":"data","node_id":"-956"}],"cacheable":true,"seq_num":12,"comment":"","comment_collapsed":true},{"node_id":"-2158","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":"580","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-2158"},{"name":"features","node_id":"-2158"}],"output_ports":[{"name":"data","node_id":"-2158"}],"cacheable":true,"seq_num":13,"comment":"","comment_collapsed":true},{"node_id":"-2165","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"date>='2022-03-10'&my2==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":"-2165"}],"output_ports":[{"name":"data","node_id":"-2165"},{"name":"left_data","node_id":"-2165"}],"cacheable":true,"seq_num":14,"comment":"my1","comment_collapsed":true},{"node_id":"-2171","module_id":"BigQuantSpace.join.join-v3","parameters":[{"name":"on","value":"date,instrument","type":"Literal","bound_global_parameter":null},{"name":"how","value":"left","type":"Literal","bound_global_parameter":null},{"name":"sort","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"data1","node_id":"-2171"},{"name":"data2","node_id":"-2171"}],"output_ports":[{"name":"data","node_id":"-2171"}],"cacheable":true,"seq_num":20,"comment":"","comment_collapsed":true},{"node_id":"-2178","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":"True","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":"-2178"},{"name":"features","node_id":"-2178"}],"output_ports":[{"name":"data","node_id":"-2178"}],"cacheable":true,"seq_num":21,"comment":"","comment_collapsed":true},{"node_id":"-440","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 # 示例代码如下。在这里编写您的代码\n # 计算行业行业收益\n industry_df = DataSource(\"basic_info_index_CN_STOCK_A\").read()\n\n SW_industry_list = industry_df[industry_df.instrument.str.startswith('SW')].instrument.tolist()\n\n SW_industry_df = DataSource(\"bar1d_index_CN_STOCK_A\").read(SW_industry_list)\n\n SW_industry_ret = SW_industry_df.groupby('instrument').apply(calcu_ret)\n SW_industry_ret.reset_index(inplace=True, drop=True)\n SW_industry_ret['date'] = SW_industry_ret['date'].apply(lambda x:x.strftime('%Y-%m-%d'))\n print('after apply SW_industry_ret==\\n',SW_industry_ret) \n \n #????daily_buy_industry_dict = {dt:seek_head_industry(SW_industry_ret.set_index('date').loc[dt]) for dt in list(set(SW_industry_ret.date))}\n #print('daily_buy_industry_dict==\\n',daily_buy_industry_dict) \n \n daily_stock = input_1.read()\n daily_stock['SW_code'] = daily_stock['SW_code'].apply(lambda x:'SW'+str(x)+'.HIX')\n print('daily_stock=111=\\n',daily_stock[:10]) \n #daily_stock = daily_stock.groupby(['date', 'industry_sw_level1_0']).apply(seek_head_stock).reset_index()\n #print('daily_stock=222=\\n',daily_stock[:10]) \n buy_df = pd.DataFrame()\n \n print('daily_stock.SW_code.tolist()',daily_stock.SW_code.tolist())\n #for dt in list(set(SW_industry_ret.date)): t=test.loc[(test[\"所属区域\"].isin([\"苏州\",\"昆\n buy_df=seek_head_industry(SW_industry_ret.loc[(SW_industry_ret['instrument'].isin(daily_stock.SW_code.tolist()))])\n \n #for k,v in daily_buy_industry_dict.items():\n # buy_df = buy_df.append(daily_stock[(daily_stock.date==k)&(daily_stock.industry_sw_level1_0.apply(lambda x: x in v))])\n #print('buy_df=====\\n',buy_df) \n #buy_df = buy_df.groupby('date').apply(lambda x:x[0].values.tolist()).reset_index()\n # buy_df[0] = buy_df[0].apply(lambda x:x[0]+x[1]+x[2])\n daily_stock['date'] = daily_stock['date'].apply(lambda x:x.strftime('%Y-%m-%d')) \n buy_df=buy_df[['date','SW_code','score']]\n #buy_df['date'] = buy_df['date'].apply(lambda x:x.strftime('%Y-%m-%d')) \n print('buy_df=222====\\n',buy_df) \n \n data=pd.merge(daily_stock,buy_df,on=['date','SW_code'],suffixes=('_left','_right'),how='inner')\n \n print('data=dddd=\\n',data)\n data_1 = DataSource.write_df(data)\n #data_2 = DataSource.write_df(buy_df)\n \n \n return Outputs(data_1=data_1, data_2=None, data_3=None)\n\n\n\n\n# 计算不同周期的动量\ndef calcu_ret(df):\n df = df.sort_values('date')\n for i in [42, 84, 126]: # 分别代表2月、4月、半年的动量\n df['ret_%s'%i] = df['close']/df['close'].shift(i)-1 \n return df\n\n# 计算出得分\ndef seek_head_industry(df):\n for j in ['ret_42','ret_84','ret_126']:\n df['%s'%j] = df['%s'%j].rank(ascending=True) \n df['score'] = 0.4*df['ret_42']+0.3*df['ret_84']+0.3*df['ret_126'] # 得分的权重分别为0.4、0.3、0.3\n df['SW_code'] = df['instrument']\n #result = df.groupby('date').sort_values('score', ascending=False)\n #i=0\n #for instrument in result.keys():\n # i=i+1\n # instrument['SW_p']=instrument\n \n print('result-----',df)\n return df\n\n# 选出特定行业优质股票\ndef seek_head_stock(df):\n result = df.sort_values(['myrank'], ascending=False)\n #return list(result.instrument[:10]) # 每个行业选10只股票\n return list(result.instrument) # 每个行业选10只股票","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":"data_1","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-440"},{"name":"input_2","node_id":"-440"},{"name":"input_3","node_id":"-440"}],"output_ports":[{"name":"data_1","node_id":"-440"},{"name":"data_2","node_id":"-440"},{"name":"data_3","node_id":"-440"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='209,64,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-15' Position='64,173,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='633,-23,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='877,620,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1002,-2,200,200'/><node_position Node='-339' Position='392,142,200,200'/><node_position Node='-346' Position='385,280,200,200'/><node_position Node='-355' Position='1078,234,200,200'/><node_position Node='-362' Position='1061,339,200,200'/><node_position Node='-374' Position='874,871,200,200'/><node_position Node='-213' Position='640,539,200,200'/><node_position Node='-2576' Position='496,462,200,200'/><node_position Node='-390' Position='1018,718,200,200'/><node_position Node='-956' Position='1328,66,200,200'/><node_position Node='-2158' Position='1342,175,200,200'/><node_position Node='-2165' Position='1406,357,200,200'/><node_position Node='-2171' Position='1084,534,200,200'/><node_position Node='-2178' Position='1407,288,200,200'/><node_position Node='-440' Position='1449,431,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [13]:
    # 本代码由可视化策略环境自动生成 2022年4月22日 17:01
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m6_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        # 计算行业行业收益
        industry_df = DataSource("basic_info_index_CN_STOCK_A").read()
    
        SW_industry_list = industry_df[industry_df.instrument.str.startswith('SW')].instrument.tolist()
    
        SW_industry_df = DataSource("bar1d_index_CN_STOCK_A").read(SW_industry_list)
    
        SW_industry_ret = SW_industry_df.groupby('instrument').apply(calcu_ret)
        SW_industry_ret.reset_index(inplace=True, drop=True)
        SW_industry_ret['date'] = SW_industry_ret['date'].apply(lambda x:x.strftime('%Y-%m-%d'))
        print('after apply SW_industry_ret==\n',SW_industry_ret) 
        
        #????daily_buy_industry_dict = {dt:seek_head_industry(SW_industry_ret.set_index('date').loc[dt]) for dt in list(set(SW_industry_ret.date))}
        #print('daily_buy_industry_dict==\n',daily_buy_industry_dict) 
        
        daily_stock = input_1.read()
        daily_stock['SW_code'] = daily_stock['SW_code'].apply(lambda x:'SW'+str(x)+'.HIX')
        print('daily_stock=111=\n',daily_stock[:10]) 
        #daily_stock = daily_stock.groupby(['date', 'industry_sw_level1_0']).apply(seek_head_stock).reset_index()
        #print('daily_stock=222=\n',daily_stock[:10]) 
        buy_df = pd.DataFrame()
        
        print('daily_stock.SW_code.tolist()',daily_stock.SW_code.tolist())
        #for dt in list(set(SW_industry_ret.date)): t=test.loc[(test["所属区域"].isin(["苏州","昆
        buy_df=seek_head_industry(SW_industry_ret.loc[(SW_industry_ret['instrument'].isin(daily_stock.SW_code.tolist()))])
            
        #for k,v in daily_buy_industry_dict.items():
       #     buy_df = buy_df.append(daily_stock[(daily_stock.date==k)&(daily_stock.industry_sw_level1_0.apply(lambda x: x in v))])
        #print('buy_df=====\n',buy_df) 
        #buy_df = buy_df.groupby('date').apply(lambda x:x[0].values.tolist()).reset_index()
       # buy_df[0] = buy_df[0].apply(lambda x:x[0]+x[1]+x[2])
        daily_stock['date'] = daily_stock['date'].apply(lambda x:x.strftime('%Y-%m-%d'))  
        buy_df=buy_df[['date','SW_code','score']]
        #buy_df['date'] = buy_df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))  
        print('buy_df=222====\n',buy_df) 
        
        data=pd.merge(daily_stock,buy_df,on=['date','SW_code'],suffixes=('_left','_right'),how='inner')
       
        print('data=dddd=\n',data)
        data_1 = DataSource.write_df(data)
        #data_2 = DataSource.write_df(buy_df)
        
        
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    
    
    
    # 计算不同周期的动量
    def calcu_ret(df):
        df = df.sort_values('date')
        for i in [42, 84, 126]: # 分别代表2月、4月、半年的动量
            df['ret_%s'%i] = df['close']/df['close'].shift(i)-1 
        return df
    
    # 计算出得分
    def seek_head_industry(df):
        for j in ['ret_42','ret_84','ret_126']:
            df['%s'%j] = df['%s'%j].rank(ascending=True) 
        df['score'] = 0.4*df['ret_42']+0.3*df['ret_84']+0.3*df['ret_126']  # 得分的权重分别为0.4、0.3、0.3
        df['SW_code'] = df['instrument']
        #result = df.groupby('date').sort_values('score', ascending=False)
        #i=0
        #for instrument in result.keys():
        #    i=i+1
        #    instrument['SW_p']=instrument
            
        print('result-----',df)
        return df
    
    # 选出特定行业优质股票
    def seek_head_stock(df):
        result = df.sort_values(['myrank'], ascending=False)
        #return list(result.instrument[:10]) # 每个行业选10只股票
        return list(result.instrument) # 每个行业选10只股票
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m6_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    def m19_initialize_bigquant_run(context):
        # 加载预测数据
        context.ranker_prediction = context.options['data'].read_df()
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        #context.stock_count = 1
        context.stock_count = 10
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        #context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, context.stock_count)])
        context.stock_weights = [1/context.stock_count for k in range(context.stock_count)]  #半仓买入,每只股票等资金分配
        # 设置每只股票占用的最大资金比例
        context.max_cash_per_instrument = 0.2
        context.options['hold_days'] = 5
    
    
    def getMa(today,context,instruments):
        # 加载原始数据
        import datetime
        stock_raw_data = D.history_data(instruments, today+datetime.timedelta(days=-40), today, ['date','close','low'])
        #print('11111',stock_raw_data)
        stock_raw_data=stock_raw_data.iloc[-20:] 
        #stock_raw_data=stock_raw_data[:55]
        #print('22222',stock_raw_data)
        sumMa=0
        lastClose=0
        for index,row in stock_raw_data.iterrows():
            #print(index,row)
            sumMa += row['close']
            lastClose=row['close']
            
        # 包含多个周期均线值的股票数据
       # stock_ma_data = stock_raw_data.groupby('instrument').apply(ma_calculate)
        return [sumMa/20,lastClose]
    
    
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m19_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        #context.ranker_prediction.set_index('date')
        ranker_prediction = context.ranker_prediction[
            context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
        today = data.current_dt.strftime('%Y-%m-%d')
        # 当日可用资金
        cash_for_buy = context.portfolio.cash
        
        # 获取当日买入列表,每天选取context.stock_count只
        buy_list=[]
        buy1_list = list(ranker_prediction[ranker_prediction.my1 == 1].instrument[:30])
        buy2_list = list(ranker_prediction[ranker_prediction.my2== 1].instrument[:10])
        print(today, '--买一选股-buy1_list==',buy1_list,'===买二选股-buy2_list:', len(buy2_list),buy2_list,)
        if len(buy1_list) > 0 :
            buy_list.append(buy1_list)
        if len(buy2_list) > 0 :
            buy_list.append(buy2_list)
       
        # 获取当前持仓资金
        stock_hold_now = {e.symbol: p.amount * p.last_sale_price
                          for e, p in context.perf_tracker.position_tracker.positions.items() if p.amount>0} 
    
        equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}
        
        # 需要卖出的股票:已有持仓中不在买入列表的股票
        #stock_to_sell = [ i for i in stock_hold_now if i not in buy_list ]
        stock_to_sell = [ i for i in stock_hold_now ]
        #stock_to_buy =  [ i for i in buy_list if i not in stock_hold_now ]
        stock_to_buy1 = [] # [ i for i in buy1_list ] 只测试my2
        stock_to_buy2 =  [ i for i in buy2_list ]
        
        #print(today,' 选股stock_to_buy: ',len(stock_to_buy),' 个: ',stock_to_buy)
        #print(today,' context.ranker_prediction ==: ',context.ranker_prediction)
       # print('ranker_prediction.dtypes',ranker_prediction.dtypes)
    
         # 处理一阳穿多线的买二准备    
       # for i, instrument in enumerate(stock_to_buy):
       #     print(today, i,instrument )
       #     dfinstrument=ranker_prediction[ranker_prediction.instrument==instrument]
       #     junxians=list(dfinstrument['junxian'])
       #     junxian=junxians[0]
            #print(today, i ,' junxian==',junxian)
      #      if(junxian==1):
                # 通过标签赋值
       #         dfinstrument.loc[0,'isReadyBuy2'] = 1
                #dfinstrument.loc[0,'date'] = '2022-03-25'
       #         dfinstrument[dfinstrument['date'] == today] =str(today) + str(datetime.timedelta(days=1))
                #dfinstrument[dfinstrument['date'] == today] = (today.date()+datetime.timedelta(days=1)).strftime('%Y-%m-%d')
                #再处理非交易日和2天后数据也还有该条数据的问题
      #          print(today,' append dfinstrument ',dfinstrument)
       #         dfinstrument.set_index('date')
      #          context.ranker_prediction.append(dfinstrument)
    
                                                     
      #  print(today,' after context.ranker_prediction ==: ',context.ranker_prediction)             
                    
        #stocks=len(stock_hold_now) #?
        stocksBuy1N=0
        stocksBuy2N=0
        stockBuy1_hold_now=[]             
        stockBuy2_hold_now=[]            
        # 卖出列表进行卖出操作
        if len(stock_to_sell)>0:
            for instrument in stock_to_sell:
                sid = context.symbol(instrument) # 将标的转化为equity格式
                cur_position = context.portfolio.positions[sid].amount # 持仓
                if cur_position > 0 and data.can_trade(sid):
                    instruments=[]
                    instruments.append(instrument)  
                    #ri10=getMa(data.current_dt,context,instruments)
                    #print('df after get ma 55==',ri55)
                    try :
                        a=getMa(data.current_dt,context,instruments)
                        ri20=a[0]
                        lastClose=a[1]
                    except :
                        ri20=0
                        lastClose=0
                    #print(instrument,today,'ma10===',ri10,'lastClose===',lastClose)
                    
                    stock_market_price = data.current(context.symbol(instrument), 'price')  # 最新市场价格
                    stock_market_today_high = data.current(context.symbol(instrument), 'high') #今日最高价      
                    stock_market_today_close = data.current(context.symbol(instrument), 'close') #今日收盘价
                    last_sale_date = equities[instrument].last_sale_date   # 上次交易日期
                    last_cost_price = equities[instrument].cost_basis # 上次交易金额
                    delta_days = data.current_dt - last_sale_date  
                    hold_days = delta_days.days # 持仓天数
            # 最高收益
            #high_return = (highclose_price_since_buy-last_cost_price)/last_cost_price
            
                    target_return = stock_market_today_close/last_cost_price
                    #
                    isBuy1=stock_hold_now[instrument]/context.portfolio.portfolio_value
                    #print(instrument,'  isBuy1 or buy2 ==',isBuy1)
                    if isBuy1< 1/30 :  #处理买一订单,买一股票池300,持仓10天轮动,每天30只
                        
                        if hold_days>=10 :
                            context.order_target(sid, 0)
                            print(today,'买一超期卖出 :','收益: ',target_return,sid)
                            cash_for_buy += stock_hold_now[instrument]
                            #stocks = stocks-1
                        #elif target_return>=1.15 :
                        #    context.order_target(sid, 0)
                        #    print(today,'盈利卖出 :','收益: ',target_return,sid)
                        #    cash_for_buy += stock_hold_now[instrument]
                         #   stocks = stocks-1
                        #elif lastClose<ri10 :
                      #     context.order_target(sid, 0)
                      #     print(today,' 跌破10日线止损卖出 ri10=:',ri10,' 收益: ',target_return,sid)
                      #     cash_for_buy += stock_hold_now[instrument]
                      #     stocks = stocks-1
                        elif target_return<=0.92 :
                            context.order_target(sid, 0)
                            print(today,'买一止损卖出 :','收益: ',target_return,sid)
                            cash_for_buy += stock_hold_now[instrument]
                            #stocks = stocks-1
                        else:
                            stockBuy1_hold_now.append(instrument)
                            stocksBuy1N=stocksBuy1N+1
                    else :  #处理买二订单
                        stockBuy2_hold_now.append(instrument)
                        if hold_days>=5 :
                            context.order_target(sid, 0)
                            print(today,'买二超期卖出 :','收益: ',target_return,sid)
                            cash_for_buy += stock_hold_now[instrument]
                            #stocks = stocks-1
                        elif target_return>=1.1 :
                            context.order_target(sid, 0)
                            print(today,'买二盈利卖出 :','收益: ',target_return,sid)
                            cash_for_buy += stock_hold_now[instrument]
                            #stocks = stocks-1
                        elif lastClose<ri20 :
                            context.order_target(sid, 0)
                            print(today,' 买二跌破20日线止损卖出 ri20=:',ri20,' 收益: ',target_return,sid)
                            cash_for_buy += stock_hold_now[instrument]
                            #stocks = stocks-1
                        elif target_return<=0.95 :
                            context.order_target(sid, 0)
                            print(today,'买二止损卖出 :','收益: ',target_return,sid)
                            cash_for_buy += stock_hold_now[instrument]
                           # stocks = stocks-1
                        else:
                            stockBuy2_hold_now.append(instrument)
                            stocksBuy2N=stocksBuy2N+1                        
                 
                    #context.order_target_percent(sid, 0) # 全部卖出
                    # 如果是早盘买早盘卖,卖出的资金可以用于买股票,此时应将下面的注释打开,卖出股票时更新可用现金;
                    # 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用
                    #cash_for_buy += stock_hold_now[instrument]
                    
        # 买入列表执行买操作
        #if len(stock_to_buy)>0:
        #    for instrument,weight in zip(stock_to_buy,context.stock_weights):
        #        sid = context.symbol(instrument) # 将标的转化为equity格式
        #        if data.can_trade(sid):
       #             context.order_target_value(sid, min(cash_for_buy,context.portfolio.portfolio_value*0.5)) # 买入                         
    
        #print(today,'--stockBuy1_hold_now--=:',stockBuy1_hold_now)
        stockBuy1_hold_now
        # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票
        buy_cash_weights = context.stock_weights
        #buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
        buy1N=min(30,(300-stocksBuy1N))
        buy2N=min(10,(20-stocksBuy2N))
        buy1_instruments = []
        if len(stock_to_buy1) > 0 :
            buy1_instruments=list(stock_to_buy1[:buy1N])
        buy2_instruments = []
        if len(stock_to_buy2) > 0 :
            buy2_instruments=list(stock_to_buy2[:buy2N])
        #print(today,' buy_instruments:',buy_instruments,'  权重: ',buy_cash_weights)
        max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
        for i, instrument in enumerate(buy1_instruments):
            #cash = cash_for_buy * buy_cash_weights[i] 
            #cash = context.portfolio.portfolio_value * buy_cash_weights[i]
             #用20分1的资金买一(300zhi)
            cash = (cash_for_buy/20)/(200-stocksBuy1N)
            #print('cash_for_buy==',cash_for_buy, ' cash==',cash)
            #if cash > max_cash_per_instrument - stock_hold_now.get(instrument, 0):
                # 确保股票持仓量不会超过每次股票最大的占用资金量
            #    cash = max_cash_per_instrument - stock_hold_now.get(instrument, 0)
            if cash > 0:
                 if instrument not in stockBuy1_hold_now :  #不重复买入
                    context.order_value(context.symbol(instrument), cash)
                   # print(today,' 买入一买 ',instrument)
                  
        for i, instrument in enumerate(buy2_instruments):
            #cash = cash_for_buy * buy_cash_weights[i] 
            #cash = context.portfolio.portfolio_value * buy_cash_weights[i]
            cash = (cash_for_buy*0.9)/(20-stocksBuy2N)
            #print('cash_for_buy==',cash_for_buy, ' cash==',cash)
           # if cash > max_cash_per_instrument - stock_hold_now.get(instrument, 0):
                # 确保股票持仓量不会超过每次股票最大的占用资金量
            #    cash = max_cash_per_instrument - stock_hold_now.get(instrument, 0)
            if cash > 0:
                #if instrument in stockBuy1_hold_now :
                    context.order_value(context.symbol(instrument), cash)
                    print(today,' 买入二买 ',instrument)
               # else:
               #     print(today,' 发现二买,但没有一买,放弃买入:---',instrument)
            else:
                print(today,' 无资金买入二买 ',instrument)
                  
                
    # 回测引擎:准备数据,只执行一次
    def m19_prepare_bigquant_run(context):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2017-01-01',
        end_date='2021-01-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.advanced_auto_labeler.v2(
        instruments=m1.data,
        label_expr="""# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/data_history_data.html
    #   添加benchmark_99】【前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -3) / shift(open, -1)
    
    # 极值处理:用1%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True
    )
    
    m3 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    close_0/mean(close_0,5)
    close_0/mean(close_0,10)
    close_0/mean(close_0,20)
    close_0/open_0
    open_0/mean(close_0,5)
    open_0/mean(close_0,10)
    open_0/mean(close_0,20)
    avg_turn_0/avg_turn_5
    avg_turn_0/avg_turn_10
    mf_net_pct_main_0"""
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    m7 = M.join.v3(
        data1=m2.data,
        data2=m16.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m5 = M.dropnan.v2(
        input_data=m7.data
    )
    
    m4 = M.stock_ranker_train.v6(
        training_ds=m5.data,
        features=m3.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,
        plot_charts=True,
        ndcg_discount_base=1,
        m_lazy_run=False
    )
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2021-01-01'),
        end_date=T.live_run_param('trading_date', '2022-04-28'),
        market='CN_STOCK_A',
        instrument_list="""300505.SZA
    002226.SZA""",
        max_count=0
    )
    
    m17 = M.general_feature_extractor.v7(
        instruments=m9.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=258
    )
    
    m18 = M.derived_feature_extractor.v3(
        input_data=m17.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False
    )
    
    m12 = M.input_features.v1(
        features="""#600335.SHA
    #300061.SZA
    #300364.SZA
    #002173.SZA
    #002433.SZA
    #601020.SHA
    #000545.SZA
    #000514.SZA
    #300505.SZA
    #002226.SZA
    #300299.SZA
    #600425.SHA
    #000632.SZA
    #002060.SZA
    
    #gaodianN5=where(ts_argmax(high_0, 258*5)<800,1,0)
    #gaodianN4=where(ts_argmax(high_0, 258*4)<800,1,0)
    #5年高点,1年按258个交易日算
    #isgaodian=max(gaodianN5,gaodianN4)
    
    #1年低点,1年按258个交易日算
    #didianN=ts_argmin(low_0, 258*1)
    #isdidian=where(didianN<200.0,1,0)
    
    #半年高点,1年按258个交易日算
    banniangaodianN=ts_argmax(high_0, 128)
    isbanniangaodian=where(banniangaodianN<80.0,1,0)
    
    #timeperiod移动平均线
    #zhouma34=ta_ma(close_0, timeperiod=166)
    #timeperiod移动平均线
    zhouma55=ta_ma(close_0, timeperiod=258)
    #在周55均线之上,
    isupzhou55=where(close_0>zhouma55,1,0)
    #回调程度
    huitiaodu=ts_max(high_0, 128)/low_1
    
    #反弹比例,选比例最高的10-20只交易,后面调试
    #yiniandidian=ts_min(low_0, 258*1)
    banniangaodian=ts_max(high_0, 128*1)
    #fantanbili=banniangaodian/yiniandidian
    
    #macd1=ta_macd_macd_12_26_9_0
    #macd2=ta_macd_macdhist_12_26_9_0
    #macd3=ta_macd_macdsignal_12_26_9_0
    #sw1=swing_volatility_5_0 #振幅波动率,timeperiod=5* 取值: , 5, 10, 30, 60, 120, 240
    #sw2=swing_volatility_10_0
    #sw3=swing_volatility_30_0
    #sw4=swing_volatility_60_0
    vl1=volatility_5_0 #波动率,timeperiod=240* 取值: , 5, 10, 30, 60, 120, 240
    vl2=volatility_10_0
    vl3=volatility_30_0
    #vl4=volatility_60_0
    
    
    macd_dif=ta_macd_dif(close_0/adjust_factor_0)
    macd_h=ta_macd_hist(close_0/adjust_factor_0)
    duanqiup1=where(low_0>low_1,1,0)
    duanqiup2=where(macd_h>shift(macd_h,1),1,0)
    duanqiup=max(duanqiup1,duanqiup2)
    
    
    #st_status_0
    #my1==1&(vl2<vl3)&(vl2>0.012)&(vl2<0.038)&(macd1<0)&date>'2022-04-01'
    #my1=where((isbanniangaodian==1)&\
    #          (huitiaodu>1.2)&\
    #          (vl2<vl3)&(vl2>0.012)&(vl2<0.038)&(st_status_0==0),1,0)
    
    my1=where((huitiaodu>1.2)&\
              (vl2<vl3)&(vl2>0.012)&(vl2<0.038)&(st_status_0==0),1,0)
    
    #isgaodian==1&isdidian==1&isbanniangaodian==1&isbanniangaodian==1&fantanbili>1.5&date>='2022-03-08'
    #调试一下不要半年高点?只要回调度,不管什么时候回调的?涨停上穿也算
    
    #按前一天算ma,即线重合后第三天再买如
    ma5=ta_ma(close_0, timeperiod=5)
    ma10=ta_ma(close_0, timeperiod=10)
    ma13=ta_ma(close_0, timeperiod=13)
    ma20=ta_ma(close_0, timeperiod=20)
    ma34=ta_ma(close_0, timeperiod=34)
    ma55=ta_ma(close_0, timeperiod=55)
    
    #4-5线重合 真阳线 后面再调试是否要过滤returen0《5-8;-这样可以亏损幅度可以小一些;再调试回调幅度是否要大于1.5
    #junxian=where((ma5<=high_0) & (ma10<=high_0) & (ma13<=high_0) &(ma20<=high_0) & (ma34<=high_0) & (ma55<=high_0) &\
    #              (ma5 >=low_0) & (ma10>=low_0) & (ma13>=low_0) & (ma20>=low_0) &  (ma34>=low_0) & (ma55>=low_0) &\
    #              (return_0>1.01)&(close_0>open_0)&(return_0<1.08)&(isbanniangaodian==1),1,0) 
    #试试2日穿阳
    junxian=where((ma5<=high_0) & (ma10<=high_0) & (ma13<=high_0) &(ma20<=high_0) & (ma34<=high_0) & (ma55<=high_0) &\
                 (return_0>1.01)&(close_0>open_0)&(return_0<1.08),1,0) 
    
    maxline=max(ma5, ma10,ma13, ma20,ma34,ma55)
    minline=min(ma5, ma10,ma13, ma20,ma34,ma55)
    zhanshang55=close_0/maxline
    #4-5线重合度
    junxianchonghedu=minline/maxline
    junxianchonghedu5=shift(junxianchonghedu,5)
    junxianchonghedu18=shift(junxianchonghedu,18)
    # volume_* 第前 * 个交易日的交易量* 取值: 0 .. 20
    #一阳穿线需要放量1.5倍以上,后面调试.  #nanmean(x, d), #过去 d 天 x 的均值(去除NaN值)
    
    fanliang=volume_0/volume_1  #放量
    deaLn=deal_number_0/deal_number_1
    daDan=fanliang/deaLn  #交易笔数放量比不上交易额放量,说明是大单进入多,用大于1.18进行调试
    
    #my2ready=where((isbanniangaodian==1)&(junxian==1)&(zhanshang55>=1.0)&
                   #(junxianchonghedu>0.94)&(huitiaodu>1.15)&(fanliang>1.45)&(daDan>1.18),1,0)    #需要收拢条件
    my2ready=where((junxian==1)&(zhanshang55>=1.0)&\
                   (junxianchonghedu>junxianchonghedu5)&(junxianchonghedu5>junxianchonghedu18)&\
                   (junxianchonghedu>0.95)&(huitiaodu>1.15)&(fanliang>1.5)&(daDan>1.2),1,0)
    my2=where((shift(my2ready,1)==1)&(low_0>low_1)&(close_0>=ma5),1,0)
    #low_0>low_1这个条件太松了
    #最好不离55线太远,--my1使用
    nearMa55=ma55/close_0
    
    SW_code=industry_sw_level1_0
    myrank=rank_fs_roe_ttm_0+rank_fs_net_profit_qoq_0-rank_pb_lf_0
    
    pe_ttm_0 #滚动市盈率
    fs_eps_0 #每股收益
    fs_eps_yoy_0 #每股收益同比增长率
    fs_bps_0 #每股净资产
    fs_roa_ttm_0 #总资产报酬率 (TTM)
    
    #isbanniangaodian==1&junxian==1&zhanshang55>1&junxianchonghedu>0.95&huitiaodu>1.3&date>='2022-01-01'
    my=max(my1,my2)"""
    )
    
    m13 = M.general_feature_extractor.v7(
        instruments=m9.data,
        features=m12.data,
        start_date='',
        end_date='',
        before_start_days=580
    )
    
    m21 = M.derived_feature_extractor.v3(
        input_data=m13.data,
        features=m12.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m14 = M.filter.v3(
        input_data=m21.data,
        expr='date>=\'2022-03-10\'&my2==1',
        output_left_data=False
    )
    
    m6 = M.cached.v3(
        input_1=m14.data,
        run=m6_run_bigquant_run,
        post_run=m6_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports='data_1'
    )
    
    m20 = M.join.v3(
        data1=m6.data_1,
        data2=m18.data,
        on='date,instrument',
        how='left',
        sort=False
    )
    
    m8 = M.stock_ranker_predict.v5(
        model=m4.model,
        data=m20.data,
        m_lazy_run=False
    )
    
    m11 = M.join.v3(
        data1=m8.predictions,
        data2=m20.data,
        on='date,instrument',
        how='left',
        sort=False
    )
    
    m19 = M.trade.v4(
        instruments=m9.data,
        options_data=m11.data,
        start_date='',
        end_date='',
        initialize=m19_initialize_bigquant_run,
        handle_data=m19_handle_data_bigquant_run,
        prepare=m19_prepare_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='twap_1',
        order_price_field_sell='twap_1',
        capital_base=10000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    after apply SW_industry_ret==
               turn  adjust_factor    instrument        date         open  \
    0       0.4444            1.0  SW110000.HIX  2005-01-04   487.959991   
    1       0.6792            1.0  SW110000.HIX  2005-01-05   483.820007   
    2       0.5826            1.0  SW110000.HIX  2005-01-06   493.359985   
    3       0.5680            1.0  SW110000.HIX  2005-01-07   488.609985   
    4       0.5365            1.0  SW110000.HIX  2005-01-10   490.790009   
    ...        ...            ...           ...         ...          ...   
    117505  1.0204            1.0  SW730000.HIX  2022-04-18  1750.504028   
    117506  0.9824            1.0  SW730000.HIX  2022-04-19  1792.421997   
    117507  1.0049            1.0  SW730000.HIX  2022-04-20  1802.852051   
    117508  1.0049            1.0  SW730000.HIX  2022-04-21  1769.225952   
    117509  1.3357            1.0  SW730000.HIX  2022-04-22  1715.608032   
    
                volume          low        close         high        amount  \
    0         28700000   482.820007   485.750000   490.989990  1.006400e+08   
    1         41910000   483.420013   493.750000   496.380005  1.607900e+08   
    2         34700000   487.209991   489.339996   493.859985  1.423500e+08   
    3         35520000   487.570007   492.470001   495.839996  1.279000e+08   
    4         35160000   489.709991   497.399994   497.399994  1.182600e+08   
    ...            ...          ...          ...          ...           ...   
    117505   922953600  1735.328979  1794.791016  1795.562012  1.041696e+10   
    117506   888600300  1783.961060  1793.348022  1807.285034  9.420230e+09   
    117507   908932200  1771.775024  1777.057007  1805.589966  9.515557e+09   
    117508  1208178500  1717.232056  1723.087036  1782.120972  1.063953e+10   
    117509  1139707900  1702.421021  1717.829956  1736.412964  1.090968e+10   
    
              ret_42    ret_84   ret_126  
    0            NaN       NaN       NaN  
    1            NaN       NaN       NaN  
    2            NaN       NaN       NaN  
    3            NaN       NaN       NaN  
    4            NaN       NaN       NaN  
    ...          ...       ...       ...  
    117505 -0.128457 -0.149896 -0.092903  
    117506 -0.134407 -0.152475 -0.099380  
    117507 -0.138598 -0.173140 -0.100520  
    117508 -0.176741 -0.206647 -0.129446  
    117509 -0.202582 -0.211662 -0.129368  
    
    [117510 rows x 13 columns]
    daily_stock=111=
           adjust_factor_0    close_0       date  deal_number_0  deal_number_1  \
    1376         1.331137  40.919151 2022-03-25          20741          21227   
    
          fs_bps_0  fs_eps_0  fs_eps_yoy_0  fs_roa_ttm_0     high_0  ...  my2  \
    1376      8.59    0.7511     99.495354        6.4608  41.677898  ...    1   
    
          nearMa55       SW_code    myrank  pe_ttm_0 #滚动市盈率  fs_eps_0 #每股收益  \
    1376  0.962039  SW220000.HIX  0.624394        50.403099          0.7511   
    
          fs_eps_yoy_0 #每股收益同比增长率  fs_bps_0 #每股净资产  fs_roa_ttm_0 #总资产报酬率 (TTM)  my  
    1376                99.495354             8.59                      6.4608   1  
    
    [1 rows x 68 columns]
    daily_stock.SW_code.tolist() ['SW220000.HIX']
    result-----          turn  adjust_factor    instrument        date         open  \
    8315   0.8721            1.0  SW220000.HIX  2005-01-04   861.630005   
    8316   0.6684            1.0  SW220000.HIX  2005-01-05   851.109985   
    8317   0.5341            1.0  SW220000.HIX  2005-01-06   856.510010   
    8318   0.5391            1.0  SW220000.HIX  2005-01-07   843.380005   
    8319   0.5385            1.0  SW220000.HIX  2005-01-10   843.450012   
    ...       ...            ...           ...         ...          ...   
    12510  2.0069            1.0  SW220000.HIX  2022-04-18  4312.645020   
    12511  2.1519            1.0  SW220000.HIX  2022-04-19  4346.229004   
    12512  2.3881            1.0  SW220000.HIX  2022-04-20  4405.258789   
    12513  2.4622            1.0  SW220000.HIX  2022-04-21  4284.430176   
    12514  1.8926            1.0  SW220000.HIX  2022-04-22  4077.868896   
    
               volume          low        close         high        amount  \
    8315    176480000   849.349976   852.640015   866.549988  9.839500e+08   
    8316    136810000   846.479980   857.909973   862.820007  7.456400e+08   
    8317    110700000   841.409973   844.830017   858.640015  6.014700e+08   
    8318    113500000   840.479980   846.400024   853.150024  6.070300e+08   
    8319    108170000   838.960022   848.890015   849.409973  5.607100e+08   
    ...           ...          ...          ...          ...           ...   
    12510  4161104100  4262.524902  4339.005859  4360.784180  5.224896e+10   
    12511  4461741500  4346.229004  4405.312012  4408.581055  5.928265e+10   
    12512  4957535600  4285.546875  4309.544922  4405.261230  6.081475e+10   
    12513  5112460000  4097.570801  4109.229004  4296.171875  6.100293e+10   
    12514  3930212400  4013.625977  4075.458008  4117.765137  4.692035e+10   
    
           ret_42  ret_84  ret_126  score       SW_code  
    8315      NaN     NaN      NaN    NaN  SW220000.HIX  
    8316      NaN     NaN      NaN    NaN  SW220000.HIX  
    8317      NaN     NaN      NaN    NaN  SW220000.HIX  
    8318      NaN     NaN      NaN    NaN  SW220000.HIX  
    8319      NaN     NaN      NaN    NaN  SW220000.HIX  
    ...       ...     ...      ...    ...           ...  
    12510   959.0   638.0    987.0  871.1  SW220000.HIX  
    12511   981.0   666.0   1048.0  906.6  SW220000.HIX  
    12512   662.0   514.0    903.0  689.9  SW220000.HIX  
    12513   374.0   389.0    609.0  449.0  SW220000.HIX  
    12514   328.0   391.0    464.0  387.7  SW220000.HIX  
    
    [4200 rows x 15 columns]
    buy_df=222====
                  date       SW_code  score
    8315   2005-01-04  SW220000.HIX    NaN
    8316   2005-01-05  SW220000.HIX    NaN
    8317   2005-01-06  SW220000.HIX    NaN
    8318   2005-01-07  SW220000.HIX    NaN
    8319   2005-01-10  SW220000.HIX    NaN
    ...           ...           ...    ...
    12510  2022-04-18  SW220000.HIX  871.1
    12511  2022-04-19  SW220000.HIX  906.6
    12512  2022-04-20  SW220000.HIX  689.9
    12513  2022-04-21  SW220000.HIX  449.0
    12514  2022-04-22  SW220000.HIX  387.7
    
    [4200 rows x 3 columns]
    data=dddd=
        adjust_factor_0    close_0        date  deal_number_0  deal_number_1  \
    0         1.331137  40.919151  2022-03-25          20741          21227   
    
       fs_bps_0  fs_eps_0  fs_eps_yoy_0  fs_roa_ttm_0     high_0  ...  nearMa55  \
    0      8.59    0.7511     99.495354        6.4608  41.677898  ...  0.962039   
    
            SW_code    myrank  pe_ttm_0 #滚动市盈率  fs_eps_0 #每股收益  \
    0  SW220000.HIX  0.624394        50.403099          0.7511   
    
       fs_eps_yoy_0 #每股收益同比增长率  fs_bps_0 #每股净资产  fs_roa_ttm_0 #总资产报酬率 (TTM)  my  \
    0                99.495354             8.59                      6.4608   1   
    
       score  
    0  792.4  
    
    [1 rows x 69 columns]
    
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-13-d7a229da8fc7> in <module>
        288 )
        289 
    --> 290 m20 = M.join.v3(
        291     data1=m6.data_1,
        292     data2=m18.data,
    
    ValueError: You are trying to merge on object and datetime64[ns] columns. If you wish to proceed you should use pd.concat