复制链接
克隆策略

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-29:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15: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":"-13313:features_ds","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-14127:input_2","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-14414:input_2","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-3841:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-3846:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-1545:input_2","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-1563:input_2","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35:input_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-29:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35:data"},{"to_node_id":"-17504:input_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:input_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:data"},{"to_node_id":"-17510:input_data","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:data"},{"to_node_id":"-14127:input_1","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:data"},{"to_node_id":"-14414:input_1","from_node_id":"-86:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-29:features","from_node_id":"-13313:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35:features","from_node_id":"-13313:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:features","from_node_id":"-13313:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:features","from_node_id":"-13313:data"},{"to_node_id":"-6806:input_1","from_node_id":"-17504:data"},{"to_node_id":"-8718:input_1","from_node_id":"-17510:data"},{"to_node_id":"-6810:input_1","from_node_id":"-6806:data_1"},{"to_node_id":"-7994:input_1","from_node_id":"-6810:data_1"},{"to_node_id":"-8704:input_1","from_node_id":"-7994:data_1"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","from_node_id":"-8704:data_1"},{"to_node_id":"-86:input_data","from_node_id":"-8714:data_1"},{"to_node_id":"-8721:input_1","from_node_id":"-8718:data_1"},{"to_node_id":"-8724:input_1","from_node_id":"-8721:data_1"},{"to_node_id":"-8714:input_1","from_node_id":"-8724:data_1"},{"to_node_id":"-3841:input_data","from_node_id":"-14127:data"},{"to_node_id":"-3846:input_data","from_node_id":"-14414:data"},{"to_node_id":"-1545:input_1","from_node_id":"-3841:data"},{"to_node_id":"-1563:input_3","from_node_id":"-3846:data"},{"to_node_id":"-1563:input_1","from_node_id":"-1545:data_1"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:options_data","from_node_id":"-1563:data_1"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2005-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2017-12-31","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. 可用数据字段见 {{web_host_url}}docs/data_history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <{{web_host_url}}docs/big_expr.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\n#rank(where(shift(close, -5) / shift(open, -1)>1, rank(shift(close, -5) / shift(open, -1)), 0))\nwhere(shift(close, -20) / shift(open, -1)>=1, 1, -1)\n\n\n# 极值处理:用1%和99%分位的值做clip\n#clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 2)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","type":"Literal","bound_global_parameter":null},{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.HIX","type":"Literal","bound_global_parameter":null},{"name":"drop_na_label","value":"True","type":"Literal","bound_global_parameter":null},{"name":"cast_label_int","value":"True","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-15"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"open_0\nclose_0\nhigh_0\nlow_0\nturn_0\nreturn_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-29","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v6","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":"50","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-29"},{"name":"features","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-29"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-29"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v2","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35"},{"name":"features","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-35"}],"cacheable":true,"seq_num":5,"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-62","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2018-01-01","type":"Literal","bound_global_parameter":"交易日期"},{"name":"end_date","value":"2022-03-24","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-70","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v6","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"},{"name":"features","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v2","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"},{"name":"features","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","module_id":"BigQuantSpace.trade.trade-v3","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"#import random\n# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n\n # 1. 资金分配\n # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金\n # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)\n is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)\n cash_avg = context.portfolio.portfolio_value / context.options['hold_days']\n cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)\n cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.perf_tracker.position_tracker.positions.items()}\n\n # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰\n if not is_staging and cash_for_sell > 0:\n equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}\n instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(\n lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))\n # print('rank order for sell %s' % instruments)\n for instrument in instruments:\n context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n if cash_for_sell <= 0:\n break\n\n # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票\n buy_cash_weights = context.stock_weights\n #random.shuffle(buy_cash_weights)\n #buy_cash_weights=buy_cash_weights[:3]\n buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n \n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n for i, instrument in enumerate(buy_instruments):\n cash = cash_for_buy * buy_cash_weights[i]\n if cash > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash = max_cash_per_instrument - positions.get(instrument, 0)\n if cash > 0:\n context.order_value(context.symbol(instrument), cash)\n","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","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 = 3\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 0.33\n context.options['hold_days'] = 20\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":"0","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":"150000","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.HIX","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":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"},{"name":"options_data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"}],"output_ports":[{"name":"raw_perf","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"}],"cacheable":false,"seq_num":12,"comment":"","comment_collapsed":true},{"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":"-13313","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n#ST状态:0:正常股票,1:ST,2:*ST,11:暂停上市\nst_status_0\n#上市板,主板:1,中小企业板:2,创业板:3\nlist_board_0\n#已上市的天数\nlist_days_0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-13313"}],"output_ports":[{"name":"data","node_id":"-13313"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-17504","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"list_board_0==1 and list_days_0>=365","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":"-17504"}],"output_ports":[{"name":"data","node_id":"-17504"},{"name":"left_data","node_id":"-17504"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true},{"node_id":"-17510","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"list_board_0==1 and list_days_0>=365 ","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":"-17510"}],"output_ports":[{"name":"data","node_id":"-17510"},{"name":"left_data","node_id":"-17510"}],"cacheable":true,"seq_num":17,"comment":"","comment_collapsed":true},{"node_id":"-6806","module_id":"BigQuantSpace.filter_delist_stock.filter_delist_stock-v6","parameters":[],"input_ports":[{"name":"input_1","node_id":"-6806"}],"output_ports":[{"name":"data_1","node_id":"-6806"}],"cacheable":true,"seq_num":18,"comment":"","comment_collapsed":true},{"node_id":"-6810","module_id":"BigQuantSpace.filtet_st_stock_tomo.filtet_st_stock_tomo-v3","parameters":[],"input_ports":[{"name":"input_1","node_id":"-6810"}],"output_ports":[{"name":"data_1","node_id":"-6810"}],"cacheable":true,"seq_num":20,"comment":"","comment_collapsed":true},{"node_id":"-7994","module_id":"BigQuantSpace.filtet_st_stock.filtet_st_stock-v7","parameters":[],"input_ports":[{"name":"input_1","node_id":"-7994"}],"output_ports":[{"name":"data_1","node_id":"-7994"}],"cacheable":true,"seq_num":19,"comment":"","comment_collapsed":true},{"node_id":"-8704","module_id":"BigQuantSpace.filter_stockmarket.filter_stockmarket-v2","parameters":[{"name":"start","value":"3","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-8704"}],"output_ports":[{"name":"data_1","node_id":"-8704"}],"cacheable":true,"seq_num":21,"comment":"","comment_collapsed":true},{"node_id":"-8714","module_id":"BigQuantSpace.filter_stockmarket.filter_stockmarket-v2","parameters":[{"name":"start","value":"3","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-8714"}],"output_ports":[{"name":"data_1","node_id":"-8714"}],"cacheable":true,"seq_num":23,"comment":"","comment_collapsed":true},{"node_id":"-8718","module_id":"BigQuantSpace.filter_delist_stock.filter_delist_stock-v6","parameters":[],"input_ports":[{"name":"input_1","node_id":"-8718"}],"output_ports":[{"name":"data_1","node_id":"-8718"}],"cacheable":true,"seq_num":24,"comment":"","comment_collapsed":true},{"node_id":"-8721","module_id":"BigQuantSpace.filtet_st_stock_tomo.filtet_st_stock_tomo-v3","parameters":[],"input_ports":[{"name":"input_1","node_id":"-8721"}],"output_ports":[{"name":"data_1","node_id":"-8721"}],"cacheable":true,"seq_num":25,"comment":"","comment_collapsed":true},{"node_id":"-8724","module_id":"BigQuantSpace.filtet_st_stock.filtet_st_stock-v7","parameters":[],"input_ports":[{"name":"input_1","node_id":"-8724"}],"output_ports":[{"name":"data_1","node_id":"-8724"}],"cacheable":true,"seq_num":26,"comment":"","comment_collapsed":true},{"node_id":"-14127","module_id":"BigQuantSpace.standardlize.standardlize-v9","parameters":[{"name":"standard_func","value":"RobustZScoreNorm","type":"Literal","bound_global_parameter":null},{"name":"columns_input","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-14127"},{"name":"input_2","node_id":"-14127"}],"output_ports":[{"name":"data","node_id":"-14127"}],"cacheable":true,"seq_num":22,"comment":"","comment_collapsed":true},{"node_id":"-14414","module_id":"BigQuantSpace.standardlize.standardlize-v9","parameters":[{"name":"standard_func","value":"RobustZScoreNorm","type":"Literal","bound_global_parameter":null},{"name":"columns_input","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-14414"},{"name":"input_2","node_id":"-14414"}],"output_ports":[{"name":"data","node_id":"-14414"}],"cacheable":true,"seq_num":27,"comment":"","comment_collapsed":true},{"node_id":"-3841","module_id":"BigQuantSpace.fillnan.fillnan-v1","parameters":[{"name":"fill_value","value":"0.0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-3841"},{"name":"features","node_id":"-3841"}],"output_ports":[{"name":"data","node_id":"-3841"}],"cacheable":true,"seq_num":28,"comment":"","comment_collapsed":true},{"node_id":"-3846","module_id":"BigQuantSpace.fillnan.fillnan-v1","parameters":[{"name":"fill_value","value":"0.0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-3846"},{"name":"features","node_id":"-3846"}],"output_ports":[{"name":"data","node_id":"-3846"}],"cacheable":true,"seq_num":29,"comment":"","comment_collapsed":true},{"node_id":"-1545","module_id":"BigQuantSpace.gplearn_train.gplearn_train-v3","parameters":[{"name":"IS_USE_BEST_GP","value":"True","type":"Literal","bound_global_parameter":null},{"name":"gnrtn","value":3,"type":"Literal","bound_global_parameter":null},{"name":"ppltn","value":80,"type":"Literal","bound_global_parameter":null},{"name":"max_samples","value":0.9,"type":"Literal","bound_global_parameter":null},{"name":"metric","value":"mse","type":"Literal","bound_global_parameter":null},{"name":"stopping_criteria","value":0.01,"type":"Literal","bound_global_parameter":null},{"name":"init_depth_min","value":"2","type":"Literal","bound_global_parameter":null},{"name":"init_depth_max","value":"6","type":"Literal","bound_global_parameter":null},{"name":"p_crossover","value":0.9,"type":"Literal","bound_global_parameter":null},{"name":"p_hoist_mutation","value":0.01,"type":"Literal","bound_global_parameter":null},{"name":"p_point_mutation","value":0.05,"type":"Literal","bound_global_parameter":null},{"name":"p_point_replace","value":0.01,"type":"Literal","bound_global_parameter":null},{"name":"p_subtree_mutation","value":0.01,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-1545"},{"name":"input_2","node_id":"-1545"}],"output_ports":[{"name":"data_1","node_id":"-1545"},{"name":"data_2","node_id":"-1545"}],"cacheable":true,"seq_num":30,"comment":"","comment_collapsed":true},{"node_id":"-1563","module_id":"BigQuantSpace.GP_predict.GP_predict-v2","parameters":[],"input_ports":[{"name":"input_1","node_id":"-1563"},{"name":"input_2","node_id":"-1563"},{"name":"input_3","node_id":"-1563"}],"output_ports":[{"name":"data_1","node_id":"-1563"}],"cacheable":true,"seq_num":31,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='240,-167,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='589,-89,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-29' Position='381,188,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-35' Position='385,280,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='254,374,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='947,-162,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-70' Position='977,170,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-76' Position='976,263,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-81' Position='986,1227,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='267,902,200,200'/><node_position Node='-86' Position='853,788,200,200'/><node_position Node='-13313' Position='510,75,200,200'/><node_position Node='-17504' Position='251,470,200,200'/><node_position Node='-17510' Position='973,359,200,200'/><node_position Node='-6806' Position='243,552,200,200'/><node_position Node='-6810' Position='249,625,200,200'/><node_position Node='-7994' Position='260,722,200,200'/><node_position Node='-8704' Position='261,807,200,200'/><node_position Node='-8714' Position='880,699,200,200'/><node_position Node='-8718' Position='937,437,200,200'/><node_position Node='-8721' Position='918,510,200,200'/><node_position Node='-8724' Position='904,602,200,200'/><node_position Node='-14127' Position='618,622,200,200'/><node_position Node='-14414' Position='1030,886,200,200'/><node_position Node='-3841' Position='583,824,200,200'/><node_position Node='-3846' Position='970,995,200,200'/><node_position Node='-1545' Position='229,1087,200,200'/><node_position Node='-1563' Position='528,1220,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [1]:
    # 本代码由可视化策略环境自动生成 2022年3月26日 08:46
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    #import random
    # 回测引擎:每日数据处理函数,每天执行一次
    def m12_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        ranker_prediction = context.ranker_prediction[
            context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
    
        # 1. 资金分配
        # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
        # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
        is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
        cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
        cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
        cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
        positions = {e.symbol: p.amount * p.last_sale_price
                     for e, p in context.perf_tracker.position_tracker.positions.items()}
    
        # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰
        if not is_staging and cash_for_sell > 0:
            equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
            instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
                    lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
            # print('rank order for sell %s' % instruments)
            for instrument in instruments:
                context.order_target(context.symbol(instrument), 0)
                cash_for_sell -= positions[instrument]
                if cash_for_sell <= 0:
                    break
    
        # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票
        buy_cash_weights = context.stock_weights
        #random.shuffle(buy_cash_weights)
        #buy_cash_weights=buy_cash_weights[:3]
        buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
        
        max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
        for i, instrument in enumerate(buy_instruments):
            cash = cash_for_buy * buy_cash_weights[i]
            if cash > max_cash_per_instrument - positions.get(instrument, 0):
                # 确保股票持仓量不会超过每次股票最大的占用资金量
                cash = max_cash_per_instrument - positions.get(instrument, 0)
            if cash > 0:
                context.order_value(context.symbol(instrument), cash)
    
    # 回测引擎:准备数据,只执行一次
    def m12_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:初始化函数,只执行一次
    def m12_initialize_bigquant_run(context):
        # 加载预测数据
        context.ranker_prediction = context.options['data'].read_df()
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        stock_count = 3
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
        # 设置每只股票占用的最大资金比例
        context.max_cash_per_instrument = 0.33
        context.options['hold_days'] = 20
    
    
    m1 = M.instruments.v2(
        start_date='2005-01-01',
        end_date='2017-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.advanced_auto_labeler.v2(
        instruments=m1.data,
        label_expr="""# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 {{web_host_url}}docs/data_history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <{{web_host_url}}docs/big_expr.html>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    #rank(where(shift(close, -5) / shift(open, -1)>1, rank(shift(close, -5) / shift(open, -1)), 0))
    where(shift(close, -20) / shift(open, -1)>=1, 1, -1)
    
    
    # 极值处理:用1%和99%分位的值做clip
    #clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 2)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.HIX',
        drop_na_label=True,
        cast_label_int=True
    )
    
    m3 = M.input_features.v1(
        features="""open_0
    close_0
    high_0
    low_0
    turn_0
    return_0"""
    )
    
    m15 = M.input_features.v1(
        features_ds=m3.data,
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    #ST状态:0:正常股票,1:ST,2:*ST,11:暂停上市
    st_status_0
    #上市板,主板:1,中小企业板:2,创业板:3
    list_board_0
    #已上市的天数
    list_days_0"""
    )
    
    m4 = M.general_feature_extractor.v6(
        instruments=m1.data,
        features=m15.data,
        start_date='',
        end_date='',
        before_start_days=50
    )
    
    m5 = M.derived_feature_extractor.v2(
        input_data=m4.data,
        features=m15.data,
        date_col='date',
        instrument_col='instrument'
    )
    
    m7 = M.join.v3(
        data1=m2.data,
        data2=m5.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m16 = M.filter.v3(
        input_data=m7.data,
        expr='list_board_0==1 and list_days_0>=365',
        output_left_data=False
    )
    
    m18 = M.filter_delist_stock.v6(
        input_1=m16.data
    )
    
    m20 = M.filtet_st_stock_tomo.v3(
        input_1=m18.data_1
    )
    
    m19 = M.filtet_st_stock.v7(
        input_1=m20.data_1
    )
    
    m21 = M.filter_stockmarket.v2(
        input_1=m19.data_1,
        start='3'
    )
    
    m13 = M.dropnan.v1(
        input_data=m21.data_1
    )
    
    m22 = M.standardlize.v9(
        input_1=m13.data,
        input_2=m3.data,
        standard_func='RobustZScoreNorm',
        columns_input=''
    )
    
    m28 = M.fillnan.v1(
        input_data=m22.data,
        features=m3.data,
        fill_value='0.0'
    )
    
    m30 = M.gplearn_train.v3(
        input_1=m28.data,
        input_2=m3.data,
        IS_USE_BEST_GP=True,
        gnrtn=3,
        ppltn=80,
        max_samples=0.9,
        metric='mse',
        stopping_criteria=0.01,
        init_depth_min=2,
        init_depth_max=6,
        p_crossover=0.9,
        p_hoist_mutation=0.01,
        p_point_mutation=0.05,
        p_point_replace=0.01,
        p_subtree_mutation=0.01
    )
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2018-01-01'),
        end_date=T.live_run_param('trading_date', '2022-03-24'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m10 = M.general_feature_extractor.v6(
        instruments=m9.data,
        features=m15.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m11 = M.derived_feature_extractor.v2(
        input_data=m10.data,
        features=m15.data,
        date_col='date',
        instrument_col='instrument'
    )
    
    m17 = M.filter.v3(
        input_data=m11.data,
        expr='list_board_0==1 and list_days_0>=365 ',
        output_left_data=False
    )
    
    m24 = M.filter_delist_stock.v6(
        input_1=m17.data
    )
    
    m25 = M.filtet_st_stock_tomo.v3(
        input_1=m24.data_1
    )
    
    m26 = M.filtet_st_stock.v7(
        input_1=m25.data_1
    )
    
    m23 = M.filter_stockmarket.v2(
        input_1=m26.data_1,
        start='3'
    )
    
    m14 = M.dropnan.v1(
        input_data=m23.data_1
    )
    
    m27 = M.standardlize.v9(
        input_1=m14.data,
        input_2=m3.data,
        standard_func='RobustZScoreNorm',
        columns_input=''
    )
    
    m29 = M.fillnan.v1(
        input_data=m27.data,
        features=m3.data,
        fill_value='0.0'
    )
    
    m31 = M.GP_predict.v2(
        input_1=m30.data_1,
        input_2=m3.data,
        input_3=m29.data
    )
    
    m12 = M.trade.v3(
        instruments=m9.data,
        options_data=m31.data_1,
        start_date='',
        end_date='',
        handle_data=m12_handle_data_bigquant_run,
        prepare=m12_prepare_bigquant_run,
        initialize=m12_initialize_bigquant_run,
        volume_limit=0,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=150000,
        benchmark='000300.HIX',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        plot_charts=True,
        backtest_only=False
    )
    
    参与训练模型的因子有:  ['open_0', 'close_0', 'high_0', 'low_0', 'turn_0', 'return_0']
        |   Population Average    |             Best Individual              |
    ---- ------------------------- ------------------------------------------ ----------
     Gen   Length          Fitness   Length          Fitness      OOB Fitness  Time Left
       0    14.49      5.69083e+22        2         0.518789         0.516984      1.66m
       1     4.04      6.01263e+14        3         0.326106         0.326368     30.92s
       2     6.54      5.98697e+14        4         0.299645         0.299022      0.00s
    ############################################# 末代最佳表达式:  abs(add(0.203, -0.963)) 筛选后最佳表达式: sqrt(sqrt(min(min(add(0.203, -0.963), log(X2)), abs(X3))))
    ############################################# 因子有效性: True
    ############################################# 使用全局最优表达式 True
    
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-1-74e9b346f46e> in <module>
        189 )
        190 
    --> 191 m30 = M.gplearn_train.v3(
        192     input_1=m28.data,
        193     input_2=m3.data,
    
    AttributeError: 'DataFrame' object has no attribute 'price'