一次新尝试

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

(lulalulaheiii) #1
克隆策略

    {"Description":"实验创建于2020/9/18","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-4150:instruments","SourceOutputPortId":"-130:data"},{"DestinationInputPortId":"-5985:instruments","SourceOutputPortId":"-130:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data1","SourceOutputPortId":"-4150:data"},{"DestinationInputPortId":"-1553:input_data","SourceOutputPortId":"-5980:data"},{"DestinationInputPortId":"-5992:input_data","SourceOutputPortId":"-5985:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","SourceOutputPortId":"-5992:data"},{"DestinationInputPortId":"-5985:features","SourceOutputPortId":"-6000:data"},{"DestinationInputPortId":"-5992:features","SourceOutputPortId":"-6000:data"},{"DestinationInputPortId":"-4334:features","SourceOutputPortId":"-6000:data"},{"DestinationInputPortId":"-4341:features","SourceOutputPortId":"-6000:data"},{"DestinationInputPortId":"-5980:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"DestinationInputPortId":"-2784:training_ds","SourceOutputPortId":"-1553:data"},{"DestinationInputPortId":"-2784:features","SourceOutputPortId":"-1966:data"},{"DestinationInputPortId":"-4334:instruments","SourceOutputPortId":"-4315:data"},{"DestinationInputPortId":"-728:instruments","SourceOutputPortId":"-4315:data"},{"DestinationInputPortId":"-4341:input_data","SourceOutputPortId":"-4334:data"},{"DestinationInputPortId":"-4356:input_data","SourceOutputPortId":"-4341:data"},{"DestinationInputPortId":"-4361:input_data","SourceOutputPortId":"-4356:data"},{"DestinationInputPortId":"-596:data","SourceOutputPortId":"-4361:data"},{"DestinationInputPortId":"-596:model","SourceOutputPortId":"-2784:model"},{"DestinationInputPortId":"-728:options_data","SourceOutputPortId":"-596:predictions"}],"ModuleNodes":[{"Id":"-130","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-12-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-130"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-130","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-4150","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:3日开盘价(作为卖出价格)除以今日收盘价(作为买入价格)\nshift(open, -3) / shift(close, 0)\n\n# 极值处理:用1%和99%分位的值做clip\n#clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n#where(label>all_quantile(label, 0.99),NaN,label)\n\nwhere(label>1.12,NaN,label)\nall_wbins(label, 20)\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000001.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na_label","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-4150"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4150","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-5980","ModuleId":"BigQuantSpace.datahub_handler_column.datahub_handler_column-v1","ModuleParameters":[{"Name":"handler","Value":"# Python 代码处理数据\ndef bigquant_run(df):\n # 示例代码如下。在这里编写您的代码 \n pd = df.sort_values(['date','fs_roe_0','fs_net_profit_margin_0'],ascending=[True,False,False],inplace=False)\n \n pd = pd.reset_index(drop=True)\n data_out = pd.groupby(['date']).head(600)\n \n data_out = data_out.sort_values(['instrument'],ascending=True)\n \n # 用5日10日简单移动均线构建 金叉 指标\n SMA_5_10 = data_out['ta_sma_5_0/ta_sma_10_0']\n gold_S = (SMA_5_10 >1).astype('int') + (SMA_5_10.shift(1)<1).astype('int') == 2\n data_out['goldC_SMA'] = np.where(gold_S==1,SMA_5_10,0)\n \n # 用14,28日三重指数平滑平均线(TRIX)构建金叉指标\n TRIX = data_out['ta_trix_14_0/ta_trix_28_0']\n gold_T = (TRIX >1).astype('int') + (TRIX.shift(1)<1).astype('int') == 2\n data_out['goldC_TRIX'] = np.where(gold_T==1,TRIX,0)\n \n #用DIF,DEA构建 MACD金叉指标\n MACD = data_out['ta_macd_macd_12_26_9_0/ta_macd_macdsignal_12_26_9_0']\n gold_M = (MACD >1).astype('int') + (MACD.shift(1)<1).astype('int') == 2\n data_out['goldC_MACD'] = np.where(gold_M==1,MACD,0)\n \n data_out = data_out.sort_values(['date'],ascending=True,inplace=False)\n data_out = data_out.loc[:,[ 'date',\n 'instrument',\n 'rank_avg_amount_5/rank_avg_amount_10',\n 'amount_0/avg_amount_5',\n 'avg_amount_1/avg_amount_10',\n 'turn_0/avg_turn_2',\n 'mf_net_pct_main_0',\n 'rank_sh_holder_num_0',\n 'goldC_SMA',\n 'goldC_TRIX',\n 'goldC_MACD',\n 'ta_rsi_14_0',\n 'ta_aroonosc_14_0',\n 'ta_mom_10_0','label']]\n \n \"\"\"data_out.columns = ['date', 'instrument', 'avg_amount_1/5',\n 'amount_0/avg_5', 'avg_amount_1/10',\n 'turn_0/avg_turn_2', 'mf_net_pct_main_0', 'rank_sh_holder_num_0',\n 'goldC_SMA', 'goldC_TRIX', 'goldC_MACD', \n 'ta_rsi_14_0','ta_aroonosc_14_0', 'ta_mom_10_0', 'label']\n \"\"\"\n return data_out\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-5980"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5980","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-5985","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-5985"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-5985"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5985","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-5992","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-5992"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-5992"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5992","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-6000","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\n\n# 基本面初筛指标\nfs_roe_0\nfs_net_profit_margin_0\n\n\n# 资金因子\nrank_avg_amount_5/rank_avg_amount_10\namount_0/avg_amount_5\navg_amount_1/avg_amount_10\nturn_0/avg_turn_2\nmf_net_pct_main_0\nrank_sh_holder_num_0\n\n# 金叉趋势指标\nta_sma_5_0/ta_sma_10_0\nta_trix_14_0/ta_trix_28_0\nta_macd_macd_12_26_9_0/ta_macd_macdsignal_12_26_9_0\n\n# 其它技术指标\nta_rsi_14_0\nta_aroonosc_14_0\nta_mom_10_0\n\n\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-6000"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-6000","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-53","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1553","ModuleId":"BigQuantSpace.dropnan.dropnan-v2","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1553"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1553"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1553","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1966","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\n\n# 资金因子\nrank_avg_amount_5/rank_avg_amount_10\namount_0/avg_amount_5\navg_amount_1/avg_amount_10\nturn_0/avg_turn_2\nmf_net_pct_main_0\nrank_sh_holder_num_0\n\n\n# 人性因子\ngoldC_SMA\ngoldC_TRIX\ngoldC_MACD\nta_rsi_14_0\nta_aroonosc_14_0\nta_mom_10_0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-1966"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1966","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-4315","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-09-18","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2020-09-20","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-4315"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4315","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":17,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-4334","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-4334"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-4334"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4334","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-4341","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-4341"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-4341"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4341","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":20,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-4356","ModuleId":"BigQuantSpace.datahub_handler_column.datahub_handler_column-v1","ModuleParameters":[{"Name":"handler","Value":"# Python 代码处理数据\ndef bigquant_run(df):\n # 示例代码如下。在这里编写您的代码 \n pd = df.sort_values(['date','fs_roe_0','fs_net_profit_margin_0'],ascending=[True,False,False],inplace=False)\n \n pd = pd.reset_index(drop=True)\n data_out = pd.groupby(['date']).head(600)\n \n data_out = data_out.sort_values(['instrument'],ascending=True)\n \n # 用5日10日简单移动均线构建 金叉 指标\n SMA_5_10 = data_out['ta_sma_5_0/ta_sma_10_0']\n gold_S = (SMA_5_10 >1).astype('int') + (SMA_5_10.shift(1)<1).astype('int') == 2\n data_out['goldC_SMA'] = np.where(gold_S==1,SMA_5_10,0)\n \n # 用14,28日三重指数平滑平均线(TRIX)构建金叉指标\n TRIX = data_out['ta_trix_14_0/ta_trix_28_0']\n gold_T = (TRIX >1).astype('int') + (TRIX.shift(1)<1).astype('int') == 2\n data_out['goldC_TRIX'] = np.where(gold_T==1,TRIX,0)\n \n #用DIF,DEA构建 MACD金叉指标\n MACD = data_out['ta_macd_macd_12_26_9_0/ta_macd_macdsignal_12_26_9_0']\n gold_M = (MACD >1).astype('int') + (MACD.shift(1)<1).astype('int') == 2\n data_out['goldC_MACD'] = np.where(gold_M==1,MACD,0)\n \n data_out = data_out.sort_values(['date'],ascending=True,inplace=False)\n data_out = data_out.loc[:,[ 'date',\n 'instrument',\n 'rank_avg_amount_5/rank_avg_amount_10',\n 'amount_0/avg_amount_5',\n 'avg_amount_1/avg_amount_10',\n 'turn_0/avg_turn_2',\n 'mf_net_pct_main_0',\n 'rank_sh_holder_num_0',\n 'goldC_SMA',\n 'goldC_TRIX',\n 'goldC_MACD',\n 'ta_rsi_14_0',\n 'ta_aroonosc_14_0',\n 'ta_mom_10_0']]\n \n \"\"\"data_out.columns = ['date', 'instrument', 'avg_amount_1/5',\n 'amount_0/avg_5', 'avg_amount_1/10',\n 'turn_0/avg_turn_2', 'mf_net_pct_main_0', 'rank_sh_holder_num_0',\n 'goldC_SMA', 'goldC_TRIX', 'goldC_MACD', \n 'ta_rsi_14_0','ta_aroonosc_14_0', 'ta_mom_10_0', 'label']\n \"\"\"\n return data_out\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-4356"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4356","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":22,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-4361","ModuleId":"BigQuantSpace.dropnan.dropnan-v2","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-4361"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-4361"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-4361","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":23,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-728","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n context.ranker_prediction = context.options['data'].read_df() # 就是 读取前面传进来的predition表格\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n # 买入时手续费为成交金额的万分之3,卖出时手续费为成交金额的千分之1.3,手续费不足5元按5元收取\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n \n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n context.stock_count = 5\n # 每只股票赋予相等的权重\n context.stock_weights = 1/context.stock_count\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = context.portfolio.portfolio_value/context.stock_count\n context.options['hold_days'] = 3","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n # 每隔3天是操作日\n if context.trading_day_index % context.options['hold_days']==0:\n \n # 第一天,只出预测结果,不卖股票,但买股票\n if context.trading_day_index == 0:\n \n #########\n # step 1: 按日期过滤得到今日的预测数据\n #########\n # 从全局变量 context.ranker_prediction中获取日期为当日,即data.current_dt.strftime('%Y-%m-%d')的股票预测排序\n ranker_prediction = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n context.instruments = list(ranker_prediction.instrument[:context.stock_count])\n \n # 计算用来买入股票的钱\n cash_each_stock = context.portfolio.portfolio_value /context.stock_count\n \n # 全仓买入预测最前的几只股票\n for instrument in context.instruments: \n if cash_each_stock>0:\n # 最后用cash这么多钱去买指定的股票,这里cash>0代表是买入(做多)的钱,即这里暂时不接受做空\n context.order_value(context.symbol(instrument), cash_each_stock)\n\n \n # 以后,每隔3天的操作日里,先预测,后清盘,再买入\n else:\n # 先预测新的一轮股票\n ranker_prediction = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n best_predicted_stocks = list(ranker_prediction.instrument[:context.stock_count])\n\n # 目前持仓\n positions = {e.symbol: p.amount * p.last_sale_price for e, p in context.portfolio.positions.items()}\n\n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity.symbol for equity in context.portfolio.positions ]\n\n # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有\n no_need_to_sell = [i for i in stock_hold_now if i in best_predicted_stocks]\n\n # 需要卖出的股票\n stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]\n \n # 真正需要从新的预测集中买入的股票\n stock_to_buy = [i for i in best_predicted_stocks if i not in no_need_to_sell]\n \n \n # 如果当天没有买入的股票(即也没有卖出的股票),就返回(终止当日交易程序)\n if len(stock_to_buy) == 0:\n return \n else:\n #########\n # step 2: 先卖出\n ########## \n # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式\n for stock in stock_to_sell:\n if data.can_trade(context.symbol(stock)):\n # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,\n # 即卖出全部股票,可参考回测文档\n context.order_target_percent(context.symbol(stock), 0)\n\n #########\n # step 3: 再买入\n ########## \n cash_each_stock = min( context.portfolio.cash/len(stock_to_buy) , context.max_cash_per_instrument )\n\n for stock in stock_to_buy:\n # 计算用来买入股票的钱\n if cash_each_stock>0 and data.can_trade(context.symbol(stock)):\n # 下单\n context.order_value(context.symbol(stock), cash_each_stock)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"twap_8","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"twap_1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"500000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000001.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-728"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-728"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-728"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-728"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-728"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-728","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":18,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2784","ModuleId":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v6","ModuleParameters":[{"Name":"learning_algorithm","Value":"排序","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_leaves","Value":"30","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"minimum_docs_per_leaf","Value":"1023","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_trees","Value":20,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"learning_rate","Value":"0.2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_bins","Value":"1023","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_row_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"ndcg_discount_base","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-2784"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-2784"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-2784"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-2784"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-2784","OutputType":null},{"Name":"feature_gains","NodeId":"-2784","OutputType":null},{"Name":"m_lazy_run","NodeId":"-2784","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-596","ModuleId":"BigQuantSpace.stock_ranker_predict.stock_ranker_predict-v5","ModuleParameters":[{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"model","NodeId":"-596"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data","NodeId":"-596"}],"OutputPortsInternal":[{"Name":"predictions","NodeId":"-596","OutputType":null},{"Name":"m_lazy_run","NodeId":"-596","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-117","ModuleId":"BigQuantSpace.hyper_parameter_search.hyper_parameter_search-v1","ModuleParameters":[{"Name":"param_grid_builder","Value":"def bigquant_run():\n param_grid = {}\n\n fraction_list = []\n for i in range(4):\n a = 0.19+0.005*i\n fraction_list.append(a)\n \n param_grid['m6.number_of_leaves'] = list(range(28,33,1))\n #param_grid['m6.minimum_docs_per_leaf'] = list(range(800,2001,100))\n param_grid['m6.learning_rate'] = fraction_list\n \n param_grid['m6.max_bins'] = list(range(1013,1033,1))\n \n return param_grid\n\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"scoring","Value":"def bigquant_run(result):\n score = result.get('m18').read_raw_perf()['sharpe'].tail(1)[0]\n\n return score\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"search_algorithm","Value":"随机搜索","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"search_iterations","Value":"25","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"random_state","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"workers","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"worker_distributed_run","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"worker_silent","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"run_now","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"bq_graph","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"bq_graph_port","NodeId":"-117"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-117"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-117"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-117"}],"OutputPortsInternal":[{"Name":"result","NodeId":"-117","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"最优化 sharpe ratio","CommentCollapsed":false}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-130' Position='275,13,200,200'/><NodePosition Node='-4150' Position='131,90,200,200'/><NodePosition Node='-5980' Position='228,295.7760009765625,200,200'/><NodePosition Node='-5985' Position='425,93,200,200'/><NodePosition Node='-5992' Position='422,158,200,200'/><NodePosition Node='-6000' Position='667,-21,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='222,233,200,200'/><NodePosition Node='-1553' Position='227,357,200,200'/><NodePosition Node='-1966' Position='570.2239990234375,363,200,200'/><NodePosition Node='-4315' Position='1086,8,200,200'/><NodePosition Node='-4334' Position='982,88,200,200'/><NodePosition Node='-4341' Position='982,161,200,200'/><NodePosition Node='-4356' Position='982,240,200,200'/><NodePosition Node='-4361' Position='981,310,200,200'/><NodePosition Node='-728' Position='690,615,200,200'/><NodePosition Node='-2784' Position='368,450,200,200'/><NodePosition Node='-596' Position='486,531,200,200'/><NodePosition Node='-117' Position='437,759,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [28]:
    # 本代码由可视化策略环境自动生成 2020年9月30日 19:56
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码处理数据
    def m7_handler_bigquant_run(df):
        # 示例代码如下。在这里编写您的代码    
        pd = df.sort_values(['date','fs_roe_0','fs_net_profit_margin_0'],ascending=[True,False,False],inplace=False)
        
        pd = pd.reset_index(drop=True)
        data_out = pd.groupby(['date']).head(600)
        
        data_out = data_out.sort_values(['instrument'],ascending=True)
        
        # 用5日10日简单移动均线构建 金叉 指标
        SMA_5_10 = data_out['ta_sma_5_0/ta_sma_10_0']
        gold_S = (SMA_5_10 >1).astype('int') + (SMA_5_10.shift(1)<1).astype('int') == 2
        data_out['goldC_SMA'] =  np.where(gold_S==1,SMA_5_10,0)
        
        # 用14,28日三重指数平滑平均线(TRIX)构建金叉指标
        TRIX = data_out['ta_trix_14_0/ta_trix_28_0']
        gold_T = (TRIX >1).astype('int') + (TRIX.shift(1)<1).astype('int') == 2
        data_out['goldC_TRIX'] = np.where(gold_T==1,TRIX,0)
        
        #用DIF,DEA构建 MACD金叉指标
        MACD = data_out['ta_macd_macd_12_26_9_0/ta_macd_macdsignal_12_26_9_0']
        gold_M = (MACD >1).astype('int') + (MACD.shift(1)<1).astype('int') == 2
        data_out['goldC_MACD'] =  np.where(gold_M==1,MACD,0)
        
        data_out = data_out.sort_values(['date'],ascending=True,inplace=False)
        data_out = data_out.loc[:,[ 'date',
                                    'instrument',
                                    'rank_avg_amount_5/rank_avg_amount_10',
                                    'amount_0/avg_amount_5',
                                    'avg_amount_1/avg_amount_10',
                                    'turn_0/avg_turn_2',
                                    'mf_net_pct_main_0',
                                    'rank_sh_holder_num_0',
                                    'goldC_SMA',
                                    'goldC_TRIX',
                                    'goldC_MACD',
                                    'ta_rsi_14_0',
                                    'ta_aroonosc_14_0',
                                    'ta_mom_10_0','label']]
        
        """data_out.columns = ['date', 'instrument', 'avg_amount_1/5',
           'amount_0/avg_5', 'avg_amount_1/10',
           'turn_0/avg_turn_2', 'mf_net_pct_main_0', 'rank_sh_holder_num_0',
           'goldC_SMA', 'goldC_TRIX', 'goldC_MACD', 
           'ta_rsi_14_0','ta_aroonosc_14_0', 'ta_mom_10_0', 'label']
        """
        return data_out
    
    # Python 代码处理数据
    def m22_handler_bigquant_run(df):
         # 示例代码如下。在这里编写您的代码    
        pd = df.sort_values(['date','fs_roe_0','fs_net_profit_margin_0'],ascending=[True,False,False],inplace=False)
        
        pd = pd.reset_index(drop=True)
        data_out = pd.groupby(['date']).head(600)
        
        data_out = data_out.sort_values(['instrument'],ascending=True)
        
        # 用5日10日简单移动均线构建 金叉 指标
        SMA_5_10 = data_out['ta_sma_5_0/ta_sma_10_0']
        gold_S = (SMA_5_10 >1).astype('int') + (SMA_5_10.shift(1)<1).astype('int') == 2
        data_out['goldC_SMA'] =  np.where(gold_S==1,SMA_5_10,0)
        
        # 用14,28日三重指数平滑平均线(TRIX)构建金叉指标
        TRIX = data_out['ta_trix_14_0/ta_trix_28_0']
        gold_T = (TRIX >1).astype('int') + (TRIX.shift(1)<1).astype('int') == 2
        data_out['goldC_TRIX'] = np.where(gold_T==1,TRIX,0)
        
        #用DIF,DEA构建 MACD金叉指标
        MACD = data_out['ta_macd_macd_12_26_9_0/ta_macd_macdsignal_12_26_9_0']
        gold_M = (MACD >1).astype('int') + (MACD.shift(1)<1).astype('int') == 2
        data_out['goldC_MACD'] =  np.where(gold_M==1,MACD,0)
        
        data_out = data_out.sort_values(['date'],ascending=True,inplace=False)
        data_out = data_out.loc[:,[ 'date',
                                    'instrument',
                                    'rank_avg_amount_5/rank_avg_amount_10',
                                    'amount_0/avg_amount_5',
                                    'avg_amount_1/avg_amount_10',
                                    'turn_0/avg_turn_2',
                                    'mf_net_pct_main_0',
                                    'rank_sh_holder_num_0',
                                    'goldC_SMA',
                                    'goldC_TRIX',
                                    'goldC_MACD',
                                    'ta_rsi_14_0',
                                    'ta_aroonosc_14_0',
                                    'ta_mom_10_0']]
        
        """data_out.columns = ['date', 'instrument', 'avg_amount_1/5',
           'amount_0/avg_5', 'avg_amount_1/10',
           'turn_0/avg_turn_2', 'mf_net_pct_main_0', 'rank_sh_holder_num_0',
           'goldC_SMA', 'goldC_TRIX', 'goldC_MACD', 
           'ta_rsi_14_0','ta_aroonosc_14_0', 'ta_mom_10_0', 'label']
        """
        return data_out
    
    # 回测引擎:初始化函数,只执行一次
    def m18_initialize_bigquant_run(context):
        # 加载预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        context.ranker_prediction = context.options['data'].read_df()  # 就是 读取前面传进来的predition表格
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        # 买入时手续费为成交金额的万分之3,卖出时手续费为成交金额的千分之1.3,手续费不足5元按5元收取
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        context.stock_count = 5
        # 每只股票赋予相等的权重
        context.stock_weights = 1/context.stock_count
        # 设置每只股票占用的最大资金比例
        context.max_cash_per_instrument = context.portfolio.portfolio_value/context.stock_count
        context.options['hold_days'] = 3
    # 回测引擎:每日数据处理函数,每天执行一次
    def m18_handle_data_bigquant_run(context, data):
    
        # 每隔3天是操作日
        if context.trading_day_index % context.options['hold_days']==0:
            
            # 第一天,只出预测结果,不卖股票,但买股票
            if context.trading_day_index == 0:
                
                #########
                # step 1: 按日期过滤得到今日的预测数据
                #########
                # 从全局变量 context.ranker_prediction中获取日期为当日,即data.current_dt.strftime('%Y-%m-%d')的股票预测排序
                ranker_prediction = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
                context.instruments = list(ranker_prediction.instrument[:context.stock_count])
         
                # 计算用来买入股票的钱
                cash_each_stock = context.portfolio.portfolio_value /context.stock_count
                
                # 全仓买入预测最前的几只股票
                for instrument in context.instruments:     
                    if cash_each_stock>0:
                        # 最后用cash这么多钱去买指定的股票,这里cash>0代表是买入(做多)的钱,即这里暂时不接受做空
                        context.order_value(context.symbol(instrument), cash_each_stock)
    
        
            #  以后,每隔3天的操作日里,先预测,后清盘,再买入
            else:
                # 先预测新的一轮股票
                ranker_prediction = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
                best_predicted_stocks = list(ranker_prediction.instrument[:context.stock_count])
    
                # 目前持仓
                positions = {e.symbol: p.amount * p.last_sale_price for e, p in context.portfolio.positions.items()}
    
                # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
                stock_hold_now = [equity.symbol for equity in context.portfolio.positions ]
    
                # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
                no_need_to_sell = [i for i in stock_hold_now if i in best_predicted_stocks]
    
                # 需要卖出的股票
                stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
                
                # 真正需要从新的预测集中买入的股票
                stock_to_buy = [i for i in best_predicted_stocks if i not in no_need_to_sell]
                
                
                # 如果当天没有买入的股票(即也没有卖出的股票),就返回(终止当日交易程序)
                if len(stock_to_buy) == 0:
                    return                
                else:
                #########
                # step 2: 先卖出
                ########## 
                    # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
                    # 如果返回真值,则可以正常下单,否则会出错
                    # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
                    for stock in stock_to_sell:
                        if data.can_trade(context.symbol(stock)):
                            # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,
                            # 即卖出全部股票,可参考回测文档
                            context.order_target_percent(context.symbol(stock), 0)
    
                #########
                # step 3: 再买入
                ##########                    
                    cash_each_stock = min( context.portfolio.cash/len(stock_to_buy) , context.max_cash_per_instrument )
    
                    for stock in stock_to_buy:
                        # 计算用来买入股票的钱
                        if cash_each_stock>0 and data.can_trade(context.symbol(stock)):
                            # 下单
                            context.order_value(context.symbol(stock), cash_each_stock)
    
    # 回测引擎:准备数据,只执行一次
    def m18_prepare_bigquant_run(context):
        pass
    
    
    g = T.Graph({
    
        'm1': 'M.instruments.v2',
        'm1.start_date': '2015-01-01',
        'm1.end_date': '2018-12-31',
        'm1.market': 'CN_STOCK_A',
        'm1.instrument_list': '',
        'm1.max_count': 0,
    
        'm5': 'M.advanced_auto_labeler.v2',
        'm5.instruments': T.Graph.OutputPort('m1.data'),
        'm5.label_expr': """# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
    
    # 计算收益:3日开盘价(作为卖出价格)除以今日收盘价(作为买入价格)
    shift(open, -3) / shift(close, 0)
    
    # 极值处理:用1%和99%分位的值做clip
    #clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    #where(label>all_quantile(label, 0.99),NaN,label)
    
    where(label>1.12,NaN,label)
    all_wbins(label, 20)
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        'm5.start_date': '',
        'm5.end_date': '',
        'm5.benchmark': '000001.SHA',
        'm5.drop_na_label': True,
        'm5.cast_label_int': False,
        'm5.user_functions': {},
    
        'm10': 'M.input_features.v1',
        'm10.features': """
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    
    # 基本面初筛指标
    fs_roe_0
    fs_net_profit_margin_0
    
    
    # 资金因子
    rank_avg_amount_5/rank_avg_amount_10
    amount_0/avg_amount_5
    avg_amount_1/avg_amount_10
    turn_0/avg_turn_2
    mf_net_pct_main_0
    rank_sh_holder_num_0
    
    # 金叉趋势指标
    ta_sma_5_0/ta_sma_10_0
    ta_trix_14_0/ta_trix_28_0
    ta_macd_macd_12_26_9_0/ta_macd_macdsignal_12_26_9_0
    
    # 其它技术指标
    ta_rsi_14_0
    ta_aroonosc_14_0
    ta_mom_10_0
    
    
    
    """,
    
        'm8': 'M.general_feature_extractor.v7',
        'm8.instruments': T.Graph.OutputPort('m1.data'),
        'm8.features': T.Graph.OutputPort('m10.data'),
        'm8.start_date': '',
        'm8.end_date': '',
        'm8.before_start_days': 90,
    
        'm9': 'M.derived_feature_extractor.v3',
        'm9.input_data': T.Graph.OutputPort('m8.data'),
        'm9.features': T.Graph.OutputPort('m10.data'),
        'm9.date_col': 'date',
        'm9.instrument_col': 'instrument',
        'm9.drop_na': False,
        'm9.remove_extra_columns': True,
        'm9.user_functions': {},
    
        'm3': 'M.join.v3',
        'm3.data1': T.Graph.OutputPort('m5.data'),
        'm3.data2': T.Graph.OutputPort('m9.data'),
        'm3.on': 'date,instrument',
        'm3.how': 'inner',
        'm3.sort': True,
    
        'm7': 'M.datahub_handler_column.v1',
        'm7.input_data': T.Graph.OutputPort('m3.data'),
        'm7.handler': m7_handler_bigquant_run,
    
        'm12': 'M.dropnan.v2',
        'm12.input_data': T.Graph.OutputPort('m7.data'),
    
        'm13': 'M.input_features.v1',
        'm13.features': """
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    
    # 资金因子
    rank_avg_amount_5/rank_avg_amount_10
    amount_0/avg_amount_5
    avg_amount_1/avg_amount_10
    turn_0/avg_turn_2
    mf_net_pct_main_0
    rank_sh_holder_num_0
    
    
    # 人性因子
    goldC_SMA
    goldC_TRIX
    goldC_MACD
    ta_rsi_14_0
    ta_aroonosc_14_0
    ta_mom_10_0""",
    
        'm6': 'M.stock_ranker_train.v6',
        'm6.training_ds': T.Graph.OutputPort('m12.data'),
        'm6.features': T.Graph.OutputPort('m13.data'),
        'm6.learning_algorithm': '排序',
        'm6.number_of_leaves': 30,
        'm6.minimum_docs_per_leaf': 1023,
        'm6.number_of_trees': 20,
        'm6.learning_rate': 0.2,
        'm6.max_bins': 1023,
        'm6.feature_fraction': 1,
        'm6.data_row_fraction': 1,
        'm6.ndcg_discount_base': 1,
        'm6.m_lazy_run': False,
    
        'm17': 'M.instruments.v2',
        'm17.start_date': T.live_run_param('trading_date', '2017-09-18'),
        'm17.end_date': T.live_run_param('trading_date', '2020-09-20'),
        'm17.market': 'CN_STOCK_A',
        'm17.instrument_list': '',
        'm17.max_count': 0,
    
        'm19': 'M.general_feature_extractor.v7',
        'm19.instruments': T.Graph.OutputPort('m17.data'),
        'm19.features': T.Graph.OutputPort('m10.data'),
        'm19.start_date': '',
        'm19.end_date': '',
        'm19.before_start_days': 90,
    
        'm20': 'M.derived_feature_extractor.v3',
        'm20.input_data': T.Graph.OutputPort('m19.data'),
        'm20.features': T.Graph.OutputPort('m10.data'),
        'm20.date_col': 'date',
        'm20.instrument_col': 'instrument',
        'm20.drop_na': False,
        'm20.remove_extra_columns': False,
        'm20.user_functions': {},
    
        'm22': 'M.datahub_handler_column.v1',
        'm22.input_data': T.Graph.OutputPort('m20.data'),
        'm22.handler': m22_handler_bigquant_run,
    
        'm23': 'M.dropnan.v2',
        'm23.input_data': T.Graph.OutputPort('m22.data'),
    
        'm4': 'M.stock_ranker_predict.v5',
        'm4.model': T.Graph.OutputPort('m6.model'),
        'm4.data': T.Graph.OutputPort('m23.data'),
        'm4.m_lazy_run': False,
    
        'm18': 'M.trade.v4',
        'm18.instruments': T.Graph.OutputPort('m17.data'),
        'm18.options_data': T.Graph.OutputPort('m4.predictions'),
        'm18.start_date': '',
        'm18.end_date': '',
        'm18.initialize': m18_initialize_bigquant_run,
        'm18.handle_data': m18_handle_data_bigquant_run,
        'm18.prepare': m18_prepare_bigquant_run,
        'm18.volume_limit': 0,
        'm18.order_price_field_buy': 'twap_8',
        'm18.order_price_field_sell': 'twap_1',
        'm18.capital_base': 500000,
        'm18.auto_cancel_non_tradable_orders': True,
        'm18.data_frequency': 'daily',
        'm18.price_type': '后复权',
        'm18.product_type': '股票',
        'm18.plot_charts': True,
        'm18.backtest_only': False,
        'm18.benchmark': '000001.SHA',
    })
    
    # g.run({})
    
    
    def m2_param_grid_builder_bigquant_run():
        param_grid = {}
    
        fraction_list = []
        for i in range(4):
            a = 0.19+0.005*i
            fraction_list.append(a)
            
        param_grid['m6.number_of_leaves'] = list(range(28,33,1))
        #param_grid['m6.minimum_docs_per_leaf'] = list(range(800,2001,100))
        param_grid['m6.learning_rate'] = fraction_list
        
        param_grid['m6.max_bins'] = list(range(1013,1033,1))
        
        return param_grid
    
    
    def m2_scoring_bigquant_run(result):
        score = result.get('m18').read_raw_perf()['sharpe'].tail(1)[0]
    
        return score
    
    
    m2 = M.hyper_parameter_search.v1(
        param_grid_builder=m2_param_grid_builder_bigquant_run,
        scoring=m2_scoring_bigquant_run,
        search_algorithm='随机搜索',
        search_iterations=25,
        workers=1,
        worker_distributed_run=True,
        worker_silent=True,
        run_now=True,
        bq_graph=g
    )
    

    数据处理(自定义) 数据统计 (前 145379 行) </font></font>

    date instrument rank_avg_amount_5/rank_avg_amount_10 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 turn_0/avg_turn_2 mf_net_pct_main_0 rank_sh_holder_num_0 goldC_SMA goldC_TRIX goldC_MACD ta_rsi_14_0 ta_aroonosc_14_0 ta_mom_10_0 label
    count(Nan) 0 0 1 0 1 0 0 1 0 0 0 1 1 1 0
    type datetime64[ns] object float32 float64 float64 float32 float32 float32 float32 float32 float32 float32 float32 float32 float64

    数据处理(自定义) 数据预览 (前 5 行) </font></font>

    date instrument rank_avg_amount_5/rank_avg_amount_10 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 turn_0/avg_turn_2 mf_net_pct_main_0 rank_sh_holder_num_0 goldC_SMA goldC_TRIX goldC_MACD ta_rsi_14_0 ta_aroonosc_14_0 ta_mom_10_0 label
    21 2015-01-05 600535.SHA 1.021978 1.305692 1.544646 1.032810 -0.0017 0.582135 0.0 0.000000 0.000000 55.100998 -92.857140 5.354492 17.0
    213 2015-01-05 000100.SZA 0.994217 1.683481 0.968417 1.588452 0.1617 0.992157 0.0 1.101832 0.000000 65.681519 21.428572 0.606256 17.0
    575 2015-01-05 300287.SZA 0.862775 0.947471 0.757537 0.965683 -0.0673 0.127669 0.0 0.000000 1.286218 39.474949 -85.714287 -16.975349 17.0
    7 2015-01-05 600398.SHA 0.990909 1.549263 1.050617 1.460766 -0.0553 0.500218 0.0 0.000000 0.000000 44.243572 -28.571428 -0.802765 17.0
    429 2015-01-05 002108.SZA 0.998860 1.538475 1.030158 1.336371 0.0757 0.498039 0.0 0.000000 0.000000 41.319191 -92.857140 -3.212315 16.0
    设置评估测试数据集,查看训练曲线
    [视频教程]StockRanker训练曲线
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-04e0e01e2ac4488fb6eb4f0791620d52"}/bigcharts-data-end

    数据处理(自定义) 数据统计 (前 80400 行) </font></font>

    date instrument rank_avg_amount_5/rank_avg_amount_10 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 turn_0/avg_turn_2 mf_net_pct_main_0 rank_sh_holder_num_0 goldC_SMA goldC_TRIX goldC_MACD ta_rsi_14_0 ta_aroonosc_14_0 ta_mom_10_0
    count(Nan) 0 0 0 0 0 0 22 0 0 0 0 0 0 0
    type datetime64[ns] object float32 float64 float64 float32 float32 float32 float32 float32 float32 float32 float32 float32

    数据处理(自定义) 数据预览 (前 5 行) </font></font>

    date instrument rank_avg_amount_5/rank_avg_amount_10 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 turn_0/avg_turn_2 mf_net_pct_main_0 rank_sh_holder_num_0 goldC_SMA goldC_TRIX goldC_MACD ta_rsi_14_0 ta_aroonosc_14_0 ta_mom_10_0
    260 2017-06-20 002035.SZA 0.911441 0.994898 0.829877 1.086396 -0.1410 0.023016 0.000000 0.0 1.230114 57.235569 92.857140 3.747391
    184 2017-06-20 600815.SHA 0.935625 1.201508 1.128923 1.066654 -0.0860 0.746479 1.003849 0.0 0.000000 43.831268 -14.285714 0.676306
    464 2017-06-20 601566.SHA 0.912188 0.901022 0.897414 0.944413 0.0505 0.120234 0.000000 0.0 0.000000 47.890377 -50.000000 -0.524529
    210 2017-06-20 600036.SHA 0.998671 0.945760 0.814779 0.991179 0.0672 0.968052 0.000000 0.0 0.000000 53.611595 -35.714287 -0.594452
    94 2017-06-20 603239.SHA 0.981690 0.793821 0.717795 1.006033 -0.1319 0.002405 0.000000 0.0 0.000000 46.466702 -14.285714 4.740356
    • 收益率80.07%
    • 年化收益率22.44%
    • 基准收益率-0.46%
    • 阿尔法0.19
    • 贝塔0.46
    • 夏普比率1.12
    • 胜率0.55
    • 盈亏比1.32
    • 收益波动率16.71%
    • 信息比率0.07
    • 最大回撤12.44%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-af39f661f3d24853b49b60871790efce"}/bigcharts-data-end
    In [12]:
    m2.result.best_params_
    
    Out[12]:
    {'m6.learning_rate': 0.2, 'm6.max_bins': 1021, 'm6.number_of_leaves': 32}
    In [ ]:
    best result so far:
    'm6.number_of_leaves' = 30
    'm6.minimum_docs_per_leaf' = 1021
    'm6.learning_rate' = 0.2
        
    'm6.max_bins' = 1023
    
    In [84]:
    pp.columns = ['date', 'instrument', 'avg_amount_1/5',
           'amount_0/avg_5', 'avg_amount_1/10',
           'turn_0/avg_turn_2', 'mf_net_pct_main_0', 'rank_sh_holder_num_0',
           'goldC_SMA', 'goldC_TRIX', 'goldC_MACD', 'ta_rsi_14_0',
           'ta_aroonosc_14_0', 'ta_mom_10_0', 'label']
    pp
    
    Out[84]:
    date instrument avg_amount_1/5 amount_0/avg_5 avg_amount_1/10 turn_0/avg_turn_2 mf_net_pct_main_0 rank_sh_holder_num_0 goldC_SMA goldC_TRIX goldC_MACD ta_rsi_14_0 ta_aroonosc_14_0 ta_mom_10_0 label
    21 2015-01-05 600535.SHA 1.393468 1.305692 1.544646 1.032810 -0.0017 0.582135 0.0 0.000000 0.000000 55.100998 -92.857140 5.354492 1.036066
    213 2015-01-05 000100.SZA 1.165056 1.683481 0.968417 1.588452 0.1617 0.992157 0.0 1.101832 0.000000 65.681519 21.428572 0.606256 1.034739
    575 2015-01-05 300287.SZA 1.020640 0.947471 0.757537 0.965683 -0.0673 0.127669 0.0 0.000000 1.286218 39.474949 -85.714287 -16.975349 1.044714
    7 2015-01-05 600398.SHA 1.184116 1.549263 1.050617 1.460766 -0.0553 0.500218 0.0 0.000000 0.000000 44.243572 -28.571428 -0.802765 1.033697
    429 2015-01-05 002108.SZA 1.167669 1.538475 1.030158 1.336371 0.0757 0.498039 0.0 0.000000 0.000000 41.319191 -92.857140 -3.212315 1.000724
    In [175]:
    m15.feature_gains()
    
    Out[175]:
    feature gain
    1 amount_0/avg_amount_5 0.240083
    8 ta_mom_10_0 0.239415
    6 ta_rsi_14_0 0.166631
    7 ta_aroonosc_14_0 0.166368
    2 avg_amount_1/avg_amount_10 0.092919
    0 avg_amount_1/avg_amount_5 0.072955
    3 goldC_SMA 0.012292
    5 goldC_MACD 0.009336
    4 goldC_TRIX 0.000000
    In [154]:
    train = m12.data.read_df().head(10).loc[:,['date','instrument','goldC_SMA','goldC_TRIX','goldC_MACD']]
    train
    
    Out[154]:
    date instrument goldC_SMA goldC_TRIX goldC_MACD
    5 2015-01-05 600382.SHA 0 0 0
    198 2015-01-05 601818.SHA 0 1 0
    239 2015-01-05 300375.SZA 0 0 0
    0 2015-01-05 600381.SHA 0 0 1
    279 2015-01-05 002064.SZA 0 0 0
    36 2015-01-05 000810.SZA 0 0 0
    141 2015-01-05 600763.SHA 0 0 0
    135 2015-01-05 000065.SZA 0 0 0
    399 2015-01-05 600377.SHA 0 0 0
    129 2015-01-05 300005.SZA 0 0 0
    In [123]:
    WMA_5_10 = train['ta_wma_5_0-ta_wma_10_0']
    train['golden_cross'] =  0
    #train
    (WMA_5_10 >0).astype('float')
    
    Out[123]:
    50349     0.0
    432296    1.0
    322111    0.0
    338655    0.0
    462400    0.0
    493901    0.0
    174687    1.0
    346793    1.0
    59588     0.0
    248539    0.0
    Name: ta_wma_5_0-ta_wma_10_0, dtype: float64
    In [108]:
    (WMA_5_10 >0) + (WMA_5_10.shift(1)<0)
    train['golden_cross'].where((WMA_5_10 >0) and (WMA_5_10.shift(1)<0),1,0)
    train
    
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-108-dd138399d450> in <module>()
          1 (WMA_5_10 >0) + (WMA_5_10.shift(1)<0)
    ----> 2 train['golden_cross'].where((WMA_5_10 >0) and (WMA_5_10.shift(1)<0),1,0)
          3 train
    
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
    In [66]:
    kk = m3.data.read_df().iloc[:,0:5].head(5)
    #kk.sort_values(['fs_roe_0'],ascending=False,inplace=True)
    kk['add'] = np.array([2,4,1,3,8])
    kk
    
    Out[66]:
    amount_0 avg_amount_1 avg_amount_10 avg_amount_5 date add
    0 4.565388e+09 4.162803e+09 3.634715e+09 3.743381e+09 2015-01-05 2
    1 9.700712e+09 8.097821e+09 4.299543e+09 5.433790e+09 2015-01-05 4
    2 4.374656e+07 3.458902e+07 4.168367e+07 3.800769e+07 2015-01-05 1
    3 7.753464e+08 6.604133e+08 3.299278e+08 3.974295e+08 2015-01-05 3
    4 4.123256e+07 6.048192e+07 3.497239e+07 3.489284e+07 2015-01-05 8
    In [81]:
    kk['add'].shift(1)
    
    Out[81]:
    0    NaN
    1    2.0
    2    4.0
    3    1.0
    4    3.0
    Name: add, dtype: float64
    In [3]:
    df = m3.data.read_df()
    pp =df.loc[df.date == '2015-01-06',:].head(5)
    pp
    
    Out[3]:
    amount_0 avg_amount_1 avg_amount_10 avg_amount_5 date fs_net_profit_margin_0 fs_roe_0 industry_sw_level1_0 instrument return_3 avg_amount_1/avg_amount_5 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 m:amount m:open m:high m:low m:close label
    2290 3.453446e+09 4.009417e+09 3.610948e+09 3.837692e+09 2015-01-06 28.716801 13.1431 480000 000001.SZA 1.057641 1.044747 0.899876 1.110350 3.453446e+09 1126.201050 1164.570068 1104.885010 1121.227295 0.944233
    2291 4.839616e+09 7.270164e+09 4.478165e+09 5.941164e+09 2015-01-06 12.044700 8.3103 430000 000002.SZA 1.155270 1.223693 0.814591 1.623470 4.839616e+09 1800.122681 1848.208252 1732.309937 1770.531738 0.942897
    2292 5.386331e+07 4.880494e+07 4.448731e+07 3.954934e+07 2015-01-06 -8.290500 -7.5598 370000 000004.SZA 1.041772 1.234026 1.361927 1.097053 5.386331e+07 63.599442 67.297554 62.989861 66.891167 1.027947
    2293 3.881573e+08 5.817519e+08 3.316153e+08 4.409910e+08 2015-01-06 36.683102 5.9817 210000 000006.SZA 1.031626 1.319192 0.880193 1.754298 3.881573e+08 222.460449 222.460449 214.515442 217.693451 0.979562
    2294 3.666531e+07 3.894894e+07 3.536966e+07 3.702014e+07 2015-01-06 -7.329500 -1.8516 460000 000007.SZA 1.048494 1.052101 0.990415 1.101196 3.666531e+07 79.781227 79.781227 77.241478 78.787407 1.019622
    In [27]:
    import pandas as pd
    kk = m3.data.read_df().head(5)
    dff = pd.concat([pp,kk])
    dff
    
    Out[27]:
    amount_0 avg_amount_1 avg_amount_10 avg_amount_5 date fs_net_profit_margin_0 fs_roe_0 industry_sw_level1_0 instrument return_3 avg_amount_1/avg_amount_5 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 m:amount m:open m:high m:low m:close label
    2290 3.453446e+09 4.009417e+09 3.610948e+09 3.837692e+09 2015-01-06 28.716801 13.1431 480000 000001.SZA 1.057641 1.044747 0.899876 1.110350 3.453446e+09 1126.201050 1164.570068 1104.885010 1121.227295 0.944233
    2291 4.839616e+09 7.270164e+09 4.478165e+09 5.941164e+09 2015-01-06 12.044700 8.3103 430000 000002.SZA 1.155270 1.223693 0.814591 1.623470 4.839616e+09 1800.122681 1848.208252 1732.309937 1770.531738 0.942897
    2292 5.386331e+07 4.880494e+07 4.448731e+07 3.954934e+07 2015-01-06 -8.290500 -7.5598 370000 000004.SZA 1.041772 1.234026 1.361927 1.097053 5.386331e+07 63.599442 67.297554 62.989861 66.891167 1.027947
    2293 3.881573e+08 5.817519e+08 3.316153e+08 4.409910e+08 2015-01-06 36.683102 5.9817 210000 000006.SZA 1.031626 1.319192 0.880193 1.754298 3.881573e+08 222.460449 222.460449 214.515442 217.693451 0.979562
    2294 3.666531e+07 3.894894e+07 3.536966e+07 3.702014e+07 2015-01-06 -7.329500 -1.8516 460000 000007.SZA 1.048494 1.052101 0.990415 1.101196 3.666531e+07 79.781227 79.781227 77.241478 78.787407 1.019622
    0 4.565388e+09 4.162803e+09 3.634715e+09 3.743381e+09 2015-01-05 28.716801 13.1431 480000 000001.SZA 1.060927 1.112044 1.219589 1.145290 4.565388e+09 1136.148560 1156.754150 1108.437622 1138.280273 0.967541
    1 9.700712e+09 8.097821e+09 4.299543e+09 5.433790e+09 2015-01-05 12.044700 8.3103 430000 000002.SZA 1.265705 1.490271 1.785257 1.883414 9.700712e+09 1774.230591 1885.197021 1753.270264 1838.344482 0.960429
    2 4.374656e+07 3.458902e+07 4.168367e+07 3.800769e+07 2015-01-05 -8.290500 -7.5598 370000 000004.SZA 0.948609 0.910053 1.150992 0.829798 4.374656e+07 63.314968 64.615402 62.177090 63.761993 1.045889
    3 7.753464e+08 6.604133e+08 3.299278e+08 3.974295e+08 2015-01-05 36.683102 5.9817 210000 000006.SZA 1.084227 1.661712 1.950903 2.001690 7.753464e+08 225.638458 234.854675 223.096054 225.002853 0.974576
    4 4.123256e+07 6.048192e+07 3.497239e+07 3.489284e+07 2015-01-05 -7.329500 -1.8516 460000 000007.SZA 1.067647 1.733362 1.181691 1.729419 4.123256e+07 80.112495 81.713638 78.676987 80.167709 1.004132
    In [28]:
    pd = dff.sort_values(['date','fs_roe_0'],ascending=[True,False],inplace=False)
    pdd = pd.groupby(['date']).head(3)
    pdd
    
    Out[28]:
    amount_0 avg_amount_1 avg_amount_10 avg_amount_5 date fs_net_profit_margin_0 fs_roe_0 industry_sw_level1_0 instrument return_3 avg_amount_1/avg_amount_5 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 m:amount m:open m:high m:low m:close label
    0 4.565388e+09 4.162803e+09 3.634715e+09 3.743381e+09 2015-01-05 28.716801 13.1431 480000 000001.SZA 1.060927 1.112044 1.219589 1.145290 4.565388e+09 1136.148560 1156.754150 1108.437622 1138.280273 0.967541
    1 9.700712e+09 8.097821e+09 4.299543e+09 5.433790e+09 2015-01-05 12.044700 8.3103 430000 000002.SZA 1.265705 1.490271 1.785257 1.883414 9.700712e+09 1774.230591 1885.197021 1753.270264 1838.344482 0.960429
    3 7.753464e+08 6.604133e+08 3.299278e+08 3.974295e+08 2015-01-05 36.683102 5.9817 210000 000006.SZA 1.084227 1.661712 1.950903 2.001690 7.753464e+08 225.638458 234.854675 223.096054 225.002853 0.974576
    2290 3.453446e+09 4.009417e+09 3.610948e+09 3.837692e+09 2015-01-06 28.716801 13.1431 480000 000001.SZA 1.057641 1.044747 0.899876 1.110350 3.453446e+09 1126.201050 1164.570068 1104.885010 1121.227295 0.944233
    2291 4.839616e+09 7.270164e+09 4.478165e+09 5.941164e+09 2015-01-06 12.044700 8.3103 430000 000002.SZA 1.155270 1.223693 0.814591 1.623470 4.839616e+09 1800.122681 1848.208252 1732.309937 1770.531738 0.942897
    2293 3.881573e+08 5.817519e+08 3.316153e+08 4.409910e+08 2015-01-06 36.683102 5.9817 210000 000006.SZA 1.031626 1.319192 0.880193 1.754298 3.881573e+08 222.460449 222.460449 214.515442 217.693451 0.979562
    In [29]:
    qd = dff.sort_values(['date','fs_net_profit_margin_0'],ascending=[True,False],inplace=False)
    qdd = qd.groupby(['date']).head(3)
    qdd
    
    Out[29]:
    amount_0 avg_amount_1 avg_amount_10 avg_amount_5 date fs_net_profit_margin_0 fs_roe_0 industry_sw_level1_0 instrument return_3 avg_amount_1/avg_amount_5 amount_0/avg_amount_5 avg_amount_1/avg_amount_10 m:amount m:open m:high m:low m:close label
    3 7.753464e+08 6.604133e+08 3.299278e+08 3.974295e+08 2015-01-05 36.683102 5.9817 210000 000006.SZA 1.084227 1.661712 1.950903 2.001690 7.753464e+08 225.638458 234.854675 223.096054 225.002853 0.974576
    0 4.565388e+09 4.162803e+09 3.634715e+09 3.743381e+09 2015-01-05 28.716801 13.1431 480000 000001.SZA 1.060927 1.112044 1.219589 1.145290 4.565388e+09 1136.148560 1156.754150 1108.437622 1138.280273 0.967541
    1 9.700712e+09 8.097821e+09 4.299543e+09 5.433790e+09 2015-01-05 12.044700 8.3103 430000 000002.SZA 1.265705 1.490271 1.785257 1.883414 9.700712e+09 1774.230591 1885.197021 1753.270264 1838.344482 0.960429
    2293 3.881573e+08 5.817519e+08 3.316153e+08 4.409910e+08 2015-01-06 36.683102 5.9817 210000 000006.SZA 1.031626 1.319192 0.880193 1.754298 3.881573e+08 222.460449 222.460449 214.515442 217.693451 0.979562
    2290 3.453446e+09 4.009417e+09 3.610948e+09 3.837692e+09 2015-01-06 28.716801 13.1431 480000 000001.SZA 1.057641 1.044747 0.899876 1.110350 3.453446e+09 1126.201050 1164.570068 1104.885010 1121.227295 0.944233
    2291 4.839616e+09 7.270164e+09 4.478165e+09 5.941164e+09 2015-01-06 12.044700 8.3103 430000 000002.SZA 1.155270 1.223693 0.814591 1.623470 4.839616e+09 1800.122681 1848.208252 1732.309937 1770.531738 0.942897
    In [31]:
    pdd.merge(qdd,'outer',on=['instrument','return_3'])
    
    Out[31]:
    amount_0_x avg_amount_1_x avg_amount_10_x avg_amount_5_x date_x fs_net_profit_margin_0_x fs_roe_0_x industry_sw_level1_0_x instrument return_3 ... industry_sw_level1_0_y avg_amount_1/avg_amount_5_y amount_0/avg_amount_5_y avg_amount_1/avg_amount_10_y m:amount_y m:open_y m:high_y m:low_y m:close_y label_y
    0 4.565388e+09 4.162803e+09 3.634715e+09 3.743381e+09 2015-01-05 28.716801 13.1431 480000 000001.SZA 1.060927 ... 480000 1.112044 1.219589 1.145290 4.565388e+09 1136.148560 1156.754150 1108.437622 1138.280273 0.967541
    1 9.700712e+09 8.097821e+09 4.299543e+09 5.433790e+09 2015-01-05 12.044700 8.3103 430000 000002.SZA 1.265705 ... 430000 1.490271 1.785257 1.883414 9.700712e+09 1774.230591 1885.197021 1753.270264 1838.344482 0.960429
    2 7.753464e+08 6.604133e+08 3.299278e+08 3.974295e+08 2015-01-05 36.683102 5.9817 210000 000006.SZA 1.084227 ... 210000 1.661712 1.950903 2.001690 7.753464e+08 225.638458 234.854675 223.096054 225.002853 0.974576
    3 3.453446e+09 4.009417e+09 3.610948e+09 3.837692e+09 2015-01-06 28.716801 13.1431 480000 000001.SZA 1.057641 ... 480000 1.044747 0.899876 1.110350 3.453446e+09 1126.201050 1164.570068 1104.885010 1121.227295 0.944233
    4 4.839616e+09 7.270164e+09 4.478165e+09 5.941164e+09 2015-01-06 12.044700 8.3103 430000 000002.SZA 1.155270 ... 430000 1.223693 0.814591 1.623470 4.839616e+09 1800.122681 1848.208252 1732.309937 1770.531738 0.942897
    5 3.881573e+08 5.817519e+08 3.316153e+08 4.409910e+08 2015-01-06 36.683102 5.9817 210000 000006.SZA 1.031626 ... 210000 1.319192 0.880193 1.754298 3.881573e+08 222.460449 222.460449 214.515442 217.693451 0.979562

    6 rows × 36 columns

    In [1]:
    T.norm([1 / math.log(i + 2) for i in range(0, 5)])
    
    Out[1]:
    [0.33916020527361607,
     0.2139862647345275,
     0.16958010263680803,
     0.14606834984270645,
     0.13120507751234176]
    In [ ]:
     
    

    (wuming1001) #2

    代码报错没法运行