复制链接
克隆策略
In [22]:
# #m7.data.read_df().columns
# #m17.predictions.read_df()
# #m8.predictions.read_df().head(100)
# #m8.predictions.read_df().tail(100)
# #m2.data_2.read_df()
# #m23.data_1.read_df()
# #m23.data_2.read_df()
# # df = m11.data_1.read()['data']
# # print(df[df['date']=='2021-01-26'])
# # print(df[df['date']=='2021-01-27'])

# # 计算n日涨幅
# def zhangfu(data, timeperiod=13):          
#     adj_close = data['close']- data['close'].shift(timeperiod)             
#     return adj_close

# # 计算动量
# def barAdjMon(data, timeperiod=2):          
#     adj_close = (data['close'] + data['high'] + data['low']) / 3               
#     return np.log(adj_close / adj_close.shift(timeperiod))

# def del_main(start_date_input,end_date_input):
#     instruments = ['510330.HOF','161017.ZOF','159949.ZOF']
#     df = DataSource("bar1d_CN_FUND").read(start_date=start_date_input,instruments = instruments, end_date=end_date_input)
#     groups = df.groupby(df['instrument'])
#     df_expend = pd.DataFrame()
#     for x in instruments:
#         tp = groups.get_group(x)
#         tp = tp.sort_values(by=['date'],na_position='first')
#         tp['pre_close'] = tp['close'].shift(1)
#         print(tp.head(2))
#         df_expend = df_expend.append(tp)
#     #df_159949 = groups.get_group('159949.ZOF')
#     df_expend.sort_values(['date'],na_position='first',inplace=True)
#     df_expend = df_expend.reset_index()
#     df_expend.drop('index',axis= 1,inplace = True)
    
#     #计算其他特征
#     df_expend['mom_20'] = barAdjMon(df_expend,timeperiod=20)
#     df_expend['mom_20'] = df_expend['mom_20'].fillna(value=0.0) #列向前填充
#     # 涨幅因子
#     df_expend['zhangfu'] = zhangfu(df_expend,timeperiod=13)
#     df_expend['zhangfu'] = df_expend['zhangfu'].fillna(value=0.0) #列向前填充
#     return df_expend

# # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
# def bigquant_run(input_1, input_2, input_3,start_date_input,end_date_input,pre_start_date,pre_end_date):
#     # 示例代码如下。在这里编写您的代码
#     # 读取数据  默认会返回全部证券代码数据, 通过指定参数 instruments 可以读取到指定的证券代码数据
#     df_expend = del_main(start_date_input,end_date_input)
#     data_1 = DataSource.write_df(df_expend)
#     data_2 = DataSource.write_df(df_expend)
#     df_pre = del_main(pre_start_date,pre_end_date)
#     data_3 = DataSource.write_df(df_pre)
#     return Outputs(data_1=data_1, data_2=data_2, data_3=data_3)
In [5]:
#m18.data.read().tail(24)
# num = m18.data.read().isna().sum()
# print(type(num))
# print(num.index[0])
# for i in range(len(num)):
#     if num[i] > 10:
#         print(num.index[i])
# import empyrical
# returns = result.get('m21').read_raw_perf()['returns'].tolist()
# sharp_ratio = empyrical.sharpe_ratio(np.array(returns),0.03/252)
from itertools import combinations

# def combine(temp_list, n):
#     '''根据n获得列表中的所有可能组合(n个元素为一组)'''
#     temp_list2 = []
#     for c in combinations(temp_list, n):
#         if len(c) != 0:
#             temp_list2.append(list(c))
#     return temp_list2

# list1 = ['a', 'b', 'c', 'd', 'e', 'f']
# end_list = []
# for i in range(len(list1)):
#     end_list.extend(combine(list1, i))
# print(end_list)
[['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['a', 'b'], ['a', 'c'], ['a', 'd'], ['a', 'e'], ['a', 'f'], ['b', 'c'], ['b', 'd'], ['b', 'e'], ['b', 'f'], ['c', 'd'], ['c', 'e'], ['c', 'f'], ['d', 'e'], ['d', 'f'], ['e', 'f'], ['a', 'b', 'c'], ['a', 'b', 'd'], ['a', 'b', 'e'], ['a', 'b', 'f'], ['a', 'c', 'd'], ['a', 'c', 'e'], ['a', 'c', 'f'], ['a', 'd', 'e'], ['a', 'd', 'f'], ['a', 'e', 'f'], ['b', 'c', 'd'], ['b', 'c', 'e'], ['b', 'c', 'f'], ['b', 'd', 'e'], ['b', 'd', 'f'], ['b', 'e', 'f'], ['c', 'd', 'e'], ['c', 'd', 'f'], ['c', 'e', 'f'], ['d', 'e', 'f'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'e'], ['a', 'b', 'c', 'f'], ['a', 'b', 'd', 'e'], ['a', 'b', 'd', 'f'], ['a', 'b', 'e', 'f'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'f'], ['a', 'c', 'e', 'f'], ['a', 'd', 'e', 'f'], ['b', 'c', 'd', 'e'], ['b', 'c', 'd', 'f'], ['b', 'c', 'e', 'f'], ['b', 'd', 'e', 'f'], ['c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'f'], ['a', 'b', 'c', 'e', 'f'], ['a', 'b', 'd', 'e', 'f'], ['a', 'c', 'd', 'e', 'f'], ['b', 'c', 'd', 'e', 'f']]

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-281:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-295:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-560:input_3","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-609:input_1","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:model","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:model"},{"to_node_id":"-109:input_1","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:predictions"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data2","from_node_id":"-281:data"},{"to_node_id":"-159:input_2","from_node_id":"-295:data"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data1","from_node_id":"-129:data"},{"to_node_id":"-129:input_data","from_node_id":"-147:data_1"},{"to_node_id":"-228:input_1","from_node_id":"-147:data_2"},{"to_node_id":"-228:input_2","from_node_id":"-147:data_3"},{"to_node_id":"-166:input_1","from_node_id":"-1481:raw_perf"},{"to_node_id":"-159:input_1","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-53:data"},{"to_node_id":"-281:input_data","from_node_id":"-228:data_1"},{"to_node_id":"-295:input_data","from_node_id":"-228:data_2"},{"to_node_id":"-1481:options_data","from_node_id":"-109:data_1"},{"to_node_id":"-109:input_2","from_node_id":"-121:data_1"},{"to_node_id":"-1481:instruments","from_node_id":"-307:data"},{"to_node_id":"-147:input_1","from_node_id":"-307:data"},{"to_node_id":"-560:input_1","from_node_id":"-159:data_1"},{"to_node_id":"-560:input_2","from_node_id":"-159:data_2"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43:training_ds","from_node_id":"-560:data_1"},{"to_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60:data","from_node_id":"-560:data_2"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n\n# rank_swing_volatility_5 = nanstd((high-low)/pre_close, 5)*sqrt(200)*100\n# alpha_001 = (rank(ts_argmax(signedpower(where(((close_0/shift(close_0,1)-1) < 0), std((close_0/shift(close_0,1)-1), 20), close_0), 2), 5)) -0.5)\n# shift(close_0,5)/close_0\n\n# mom_20\n# zhangfu\n# ln_pre_high = log(pre_high)\n#ln_pre_open = nanstd(log(pre_open))\n# std_ln_pre_open\n# delta(close_0, 3)\npre_close\npre_high\npre_volume\n# pre_amount\n\n# high\n# open\n# close\n# amount\n# volume\n# std_mom_20\n# std_zhangfu\n# std_pre_high\n# std_pre_close\n# std_pre_open\n# std_pre_low\n# std_pre_volume\n# std_pre_amount\n\n# ln_open = log(open_0)\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43","module_id":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v6","parameters":[{"name":"learning_algorithm","value":"排序","type":"Literal","bound_global_parameter":null},{"name":"number_of_leaves","value":"3","type":"Literal","bound_global_parameter":null},{"name":"minimum_docs_per_leaf","value":"100","type":"Literal","bound_global_parameter":null},{"name":"number_of_trees","value":"20","type":"Literal","bound_global_parameter":null},{"name":"learning_rate","value":0.1,"type":"Literal","bound_global_parameter":null},{"name":"max_bins","value":1023,"type":"Literal","bound_global_parameter":null},{"name":"feature_fraction","value":1,"type":"Literal","bound_global_parameter":null},{"name":"data_row_fraction","value":1,"type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"ndcg_discount_base","value":1,"type":"Literal","bound_global_parameter":null},{"name":"m_lazy_run","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"training_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"features","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"test_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"base_model","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"}],"output_ports":[{"name":"model","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"feature_gains","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"},{"name":"m_lazy_run","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-43"}],"cacheable":false,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60","module_id":"BigQuantSpace.stock_ranker_predict.stock_ranker_predict-v5","parameters":[{"name":"m_lazy_run","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"model","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"},{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"}],"output_ports":[{"name":"predictions","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"},{"name":"m_lazy_run","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-60"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-281","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-281"},{"name":"features","node_id":"-281"}],"output_ports":[{"name":"data","node_id":"-281"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true},{"node_id":"-295","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-295"},{"name":"features","node_id":"-295"}],"output_ports":[{"name":"data","node_id":"-295"}],"cacheable":true,"seq_num":18,"comment":"","comment_collapsed":true},{"node_id":"-129","module_id":"BigQuantSpace.auto_labeler_on_datasource.auto_labeler_on_datasource-v1","parameters":[{"name":"label_expr","value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -2) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","type":"Literal","bound_global_parameter":null},{"name":"drop_na_label","value":"True","type":"Literal","bound_global_parameter":null},{"name":"cast_label_int","value":"True","type":"Literal","bound_global_parameter":null},{"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":"-129"}],"output_ports":[{"name":"data","node_id":"-129"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-147","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"from scipy import nanstd\nfrom sklearn import preprocessing\nimport math\nimport numpy as np\ndef print_debug(**kwargs):\n if True:\n for k,v in kwargs.items():\n print(k,\":\")\n print(v)\n \n# 计算n日涨幅\ndef zhangfu(data, timeperiod=13): \n adj_close = data['pre_close']- data['pre_close'].shift(timeperiod) \n return adj_close\ndef rank_swing_volatility(data,timeperiod=5):\n return nanstd((data['pre_high']-data['pre_low'])/data['pre_close'], timeperiod)*sqrt(200)*100\n# 计算动量\ndef barAdjMon(data, timeperiod=2): \n adj_close = (data['pre_close'] + data['pre_high'] + data['pre_low']) / 3 \n return np.log(adj_close / adj_close.shift(timeperiod))\n\ndef del_main(start_date_input,end_date_input,instruments):\n #instruments = ['510330.HOF','161017.ZOF','159949.ZOF']\n df = DataSource(\"bar1d_CN_FUND\").read(start_date=start_date_input,instruments = instruments, end_date=end_date_input)\n groups = df.groupby(df['instrument'])\n df_expend = pd.DataFrame()\n for x in instruments:\n #分组排序后重新设置索引\n tp = groups.get_group(x)\n tp = tp.sort_values(by=['date'],na_position='first')\n\n tp = tp.reset_index() \n tp.drop('index',axis= 1,inplace = True) \n tp = tp.reset_index() \n print_debug(tp_index=tp.index)\n # 基础特征列表\n tp['open_0'] = tp['open']\n tp['high_0'] = tp['high']\n tp['low_0'] = tp['low']\n tp['close_0'] = tp['close']\n tp['volume_0'] = tp['volume']\n tp['pre_close'] = tp['close'].shift(1)\n tp['std_pre_close'] = preprocessing.scale(tp['pre_close'].values)\n tp['pre_high'] = tp['high'].shift(1)\n tp['std_pre_high'] = preprocessing.scale(tp['pre_high'].values)\n tp['pre_low'] = tp['low'].shift(1)\n tp['std_pre_low'] = preprocessing.scale(tp['pre_low'].values)\n tp['pre_open'] = tp['open'].shift(1)\n tp['std_pre_open'] = preprocessing.scale(tp['pre_open'].values)\n tp['pre_volume'] = tp['volume'].shift(1)\n tp['std_pre_volume'] = preprocessing.scale(tp['pre_volume'].values)\n tp['pre_amount'] = tp['amount'].shift(1)\n tp['std_pre_amount'] = preprocessing.scale(tp['pre_amount'].values)\n #计算其他特征\n #tp['alpha_001_my'] = (rank(ts_argmax(signedpower(where(((tp['close_0']/shift(tp['close_0'],1)-1) < 0), std((tp['close_0']/shift(tp['close_0'],1)-1), 20), tp['close_0']), 2), 5)) -0.5)\n #20日动量\n tp['mom_20'] = barAdjMon(tp,timeperiod=20)\n tp['std_mom_20'] = preprocessing.scale(tp['mom_20'].values)\n #13日涨幅\n tp['zhangfu'] = zhangfu(tp,timeperiod=13)\n tp['std_zhangfu'] = preprocessing.scale(tp['zhangfu'].values)\n # std ln pre open\n tp['std_ln_pre_open'] = preprocessing.scale(np.log(tp['pre_open']))\n print(tp.head(2))\n df_expend = df_expend.append(tp)\n #df_159949 = groups.get_group('159949.ZOF')\n df_expend.sort_values(['date'],na_position='first',inplace=True)\n df_expend = df_expend.reset_index()\n df_expend.drop('index',axis= 1,inplace = True)\n # 异常数据处理\n #df_expend = df_expend.fillna(value=0.0) #列向前填充\n# # 波动率因子\n# df_expend['rank_swing_volatility_5'] = rank_swing_volatility(df_expend,5)\n# df_expend['rank_swing_volatility_5'] = df_expend['rank_swing_volatility_5'].fillna(value=0.0) #列向前填充\n return df_expend\n\n# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3,start_date_input,end_date_input,pre_start_date,pre_end_date):\n pre_start_date = input_1.read()['start_date']\n pre_end_date = input_1.read()['end_date']\n instruments = input_1.read()['instruments']\n # 示例代码如下。在这里编写您的代码\n # 读取数据 默认会返回全部证券代码数据, 通过指定参数 instruments 可以读取到指定的证券代码数据\n df_expend = del_main(start_date_input,end_date_input,instruments)\n data_1 = DataSource.write_df(df_expend)\n data_2 = DataSource.write_df(df_expend)\n print('pre_start_date,pre_end_date',pre_start_date,pre_end_date)\n df_pre = del_main(pre_start_date,pre_end_date,instruments)\n data_3 = DataSource.write_df(df_pre)\n return Outputs(data_1=data_1, data_2=data_2, data_3=data_3)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{\"start_date_input\":\"2020-06-01\",\n\"end_date_input\":\"2021-05-01\",\n\"pre_start_date\":\"2021-06-01\",\n\"pre_end_date\":\"2022-06-01\"}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-147"},{"name":"input_2","node_id":"-147"},{"name":"input_3","node_id":"-147"}],"output_ports":[{"name":"data_1","node_id":"-147"},{"name":"data_2","node_id":"-147"},{"name":"data_3","node_id":"-147"}],"cacheable":false,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-1481","module_id":"BigQuantSpace.hftrade.hftrade-v2","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n #print('初始化函数开始:')\n #print('context.options',context.options)\n # 加载预测数据\n context.ranker_prediction = context.options.get('data').read()['data']\n context.param = context.options['data'].read()[\"param\"]\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0003, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n stock_count = context.param[\"stock_count\"]\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 = 1.01\n context.hold_days = context.param[\"hold_days\"]\n #print('初始化函数结束')\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 交易引擎:每个单位时间开盘前调用一次。\ndef bigquant_run(context, data):\n # 盘前处理,订阅行情等\n\n context.subscribe(context.instruments)\n\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_tick","value":"# 交易引擎:tick数据处理函数,每个tick执行一次\ndef bigquant_run(context, tick):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"import math\n# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n #print('当前日期data.current_dt',data.current_dt)\n #print('data',data)\n try:\n context.ranker_prediction = context.options.get('data').read()['data']\n # 相隔几天(hold_days)进行一下换仓\n if context.trading_day_index % context.hold_days != 0:\n return \n\n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n # 目前持仓\n positions = {e: p.amount * p.last_sale_price for e, p in context.portfolio.positions.items()}\n # 权重\n buy_cash_weights = context.stock_weights\n # 今日买入股票列表\n stock_to_buy = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n # 持仓上限\n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n print(\"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\")\n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = [equity for equity in context.portfolio.positions ]\n # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有\n no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]\n # 需要卖出的股票\n stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]\n\n # 卖出\n for stock in stock_to_sell:\n # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式\n if data.can_trade(context.symbol(stock)):\n # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,\n # 即卖出全部股票,可参考回测文档\n #print('卖出order target percent',context.symbol(stock))\n #print('卖出结果',context.order_target_percent(context.symbol(stock), 0))\n context.order_target_percent(context.symbol(stock), 0)\n\n # 如果当天没有买入的股票,就返回\n if len(stock_to_buy) == 0:\n return\n\n # 买入\n print('买入列表',stock_to_buy)\n for i, instrument in enumerate(stock_to_buy):\n cash = context.portfolio.portfolio_value * 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 > 500:\n cash = int(math.floor(cash))\n #print('买入order_value',context.symbol(instrument),' cash ',cash)\n #print(context.order_value(context.symbol(instrument), cash))\n context.order_value(context.symbol(instrument), cash)\n except Exception as e:\n print('抛出异常',e)","type":"Literal","bound_global_parameter":null},{"name":"handle_trade","value":"# 交易引擎:成交回报处理函数,每个成交发生时执行一次\ndef bigquant_run(context, trade):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_order","value":"# 交易引擎:委托回报处理函数,每个委托变化时执行一次\ndef bigquant_run(context, order):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"after_trading","value":"# 交易引擎:盘后处理函数,每日盘后执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":1000000,"type":"Literal","bound_global_parameter":null},{"name":"frequency","value":"daily","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"真实价格","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"自动","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"80","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":"benchmark","value":"000300.HIX","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"disable_cache","value":"False","type":"Literal","bound_global_parameter":null},{"name":"replay_bdb","value":"False","type":"Literal","bound_global_parameter":null},{"name":"show_debug_info","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":"-1481"},{"name":"options_data","node_id":"-1481"},{"name":"history_ds","node_id":"-1481"},{"name":"benchmark_ds","node_id":"-1481"}],"output_ports":[{"name":"raw_perf","node_id":"-1481"}],"cacheable":false,"seq_num":21,"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":"-228","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"def del_input(input_1):\n df = input_1.read_df()\n data_1 = DataSource.write_df(df)\n return data_1\n# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n data_1 = del_input(input_1)\n data_2 = del_input(input_2)\n return Outputs(data_1=data_1, data_2=data_2, data_3=None)","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-228"},{"name":"input_2","node_id":"-228"},{"name":"input_3","node_id":"-228"}],"output_ports":[{"name":"data_1","node_id":"-228"},{"name":"data_2","node_id":"-228"},{"name":"data_3","node_id":"-228"}],"cacheable":false,"seq_num":23,"comment":"","comment_collapsed":true},{"node_id":"-109","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n print('m11.input1',input_1)\n df = input_1.read()\n param = input_2.read()\n \n data = {\n \"param\": param,\n \"data\": df\n }\n data_1 = DataSource.write_pickle(data)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-109"},{"name":"input_2","node_id":"-109"},{"name":"input_3","node_id":"-109"}],"output_ports":[{"name":"data_1","node_id":"-109"},{"name":"data_2","node_id":"-109"},{"name":"data_3","node_id":"-109"}],"cacheable":true,"seq_num":11,"comment":"合并数据和Trade参数","comment_collapsed":true},{"node_id":"-121","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3, stock_count, hold_days):\n # 示例代码如下。在这里编写您的代码\n param = {\n \"stock_count\": stock_count,\n \"hold_days\": hold_days\n }\n data_1 = DataSource.write_pickle(param)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{\n \"stock_count\": 1,\n \"hold_days\": 1 \n}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-121"},{"name":"input_2","node_id":"-121"},{"name":"input_3","node_id":"-121"}],"output_ports":[{"name":"data_1","node_id":"-121"},{"name":"data_2","node_id":"-121"},{"name":"data_3","node_id":"-121"}],"cacheable":true,"seq_num":14,"comment":"暴露Trade的参数","comment_collapsed":true},{"node_id":"-307","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2021-06-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-06-01","type":"Literal","bound_global_parameter":"交易日期"},{"name":"market","value":"CN_FUND","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"510330.HOF\n161017.ZOF\n159949.ZOF","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-307"}],"output_ports":[{"name":"data","node_id":"-307"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-159","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef del_dirty_data(df):\n # 异常数据处理\n num = df.isna().sum()\n for i in range(len(num)):\n if num[i] > 10:\n print('!!!!!!!!!含有大量脏数据',num.index[i],num[i])\n if num[i] > 100:\n print(num.index[i],num[i])\n df = df.drop([str(num.index[i])],axis = 1)\n # 删除值全为nan的列\n df = df.dropna(axis = 1,how = 'all')\n # 删除任意含有nan的行\n #df = df.dropna()\n df = df.fillna(value=0.0) #列向前填充 \n return df\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n df1 = input_1.read()\n df1 = del_dirty_data(df1)\n data_1 = DataSource.write_df(df1)\n \n df2 = input_2.read()\n df2 = del_dirty_data(df2)\n data_2 = DataSource.write_df(df2) \n return Outputs(data_1=data_1, data_2=data_2, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-159"},{"name":"input_2","node_id":"-159"},{"name":"input_3","node_id":"-159"}],"output_ports":[{"name":"data_1","node_id":"-159"},{"name":"data_2","node_id":"-159"},{"name":"data_3","node_id":"-159"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-166","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef deal_result_data(df):\n \n return df\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n print(input_1)\n print(input_1.read())\n data_1 = input_1\n data_2 = input_1\n# df1 = input_1.read_raw_perf()\n# df1 = deal_result_data(df1)\n# data_1 = DataSource.write_df(df1)\n \n# df2 = input_2.read()\n# df2 = deal_result_data(df2)\n# data_2 = DataSource.write_df(df2) \n return Outputs(data_1=data_1, data_2=data_2, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-166"},{"name":"input_2","node_id":"-166"},{"name":"input_3","node_id":"-166"}],"output_ports":[{"name":"data_1","node_id":"-166"},{"name":"data_2","node_id":"-166"},{"name":"data_3","node_id":"-166"}],"cacheable":true,"seq_num":12,"comment":"","comment_collapsed":true},{"node_id":"-560","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n\n print(input_3.read())\n train_df = input_1.read()\n predict_df = input_2.read()\n features = input_3.read()\n data_1 = input_1\n data_2 = input_2\n for i in features:\n print(i)\n \n return Outputs(data_1=data_1, data_2=data_2, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{'index_my':0}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-560"},{"name":"input_2","node_id":"-560"},{"name":"input_3","node_id":"-560"}],"output_ports":[{"name":"data_1","node_id":"-560"},{"name":"data_2","node_id":"-560"},{"name":"data_3","node_id":"-560"}],"cacheable":true,"seq_num":17,"comment":"","comment_collapsed":true},{"node_id":"-609","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"from itertools import combinations\n\ndef combine(temp_list, n):\n '''根据n获得列表中的所有可能组合(n个元素为一组)'''\n temp_list2 = []\n for c in combinations(temp_list, n):\n if len(c) != 0:\n temp_list2.append(list(c))\n return temp_list2\n\n\n# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n list1 = input_1.read()\n end_list = []\n for i in range(len(list1)):\n end_list.extend(combine(list1, i))\n print(end_list)\n df = end_list\n data_1 = DataSource.write_pickle(df)\n data_2 = DataSource.write_pickle(df)\n return Outputs(data_1=data_1, data_2=data_2, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-609"},{"name":"input_2","node_id":"-609"},{"name":"input_3","node_id":"-609"}],"output_ports":[{"name":"data_1","node_id":"-609"},{"name":"data_2","node_id":"-609"},{"name":"data_3","node_id":"-609"}],"cacheable":true,"seq_num":9,"comment":"","comment_collapsed":true},{"node_id":"-629","module_id":"BigQuantSpace.hyper_parameter_search.hyper_parameter_search-v1","parameters":[{"name":"param_grid_builder","value":"def bigquant_run():\n param_grid = {}\n # 在这里设置需要调优的参数备选\n # param_grid['m3.features'] = ['close_1/close_0', 'close_2/close_0\\nclose_3/close_0']\n # param_grid['m6.number_of_trees'] = [5, 10, 20]\n param_grid[\"m17.params\"] = [\n \"\"\"{\"index_my\": 3}\"\"\",\n \"\"\"{\"index_my\": 4}\"\"\",\n ]\n return param_grid\n","type":"Literal","bound_global_parameter":null},{"name":"scoring","value":"def bigquant_run(result):\n score = result.get('m19').read_raw_perf()['sharpe'].tail(1)[0]\n\n return {'score': score}\n","type":"Literal","bound_global_parameter":null},{"name":"search_algorithm","value":"网格搜索","type":"Literal","bound_global_parameter":null},{"name":"search_iterations","value":10,"type":"Literal","bound_global_parameter":null},{"name":"random_state","value":"","type":"Literal","bound_global_parameter":null},{"name":"workers","value":1,"type":"Literal","bound_global_parameter":null},{"name":"worker_distributed_run","value":"True","type":"Literal","bound_global_parameter":null},{"name":"worker_silent","value":"True","type":"Literal","bound_global_parameter":null},{"name":"run_now","value":"True","type":"Literal","bound_global_parameter":null},{"name":"bq_graph","value":"True","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"bq_graph_port","node_id":"-629"},{"name":"input_1","node_id":"-629"},{"name":"input_2","node_id":"-629"},{"name":"input_3","node_id":"-629"}],"output_ports":[{"name":"result","node_id":"-629"}],"cacheable":false,"seq_num":5,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='233,-477,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-43' Position='-142,576,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-60' Position='256,317,200,200'/><node_position Node='-281' Position='-49,-49,200,200'/><node_position Node='-295' Position='273,-51,200,200'/><node_position Node='-129' Position='-374,-47,200,200'/><node_position Node='-147' Position='-197,-437,200,200'/><node_position Node='-1481' Position='633,561,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-53' Position='-164,88,200,200'/><node_position Node='-228' Position='-31,-278,200,200'/><node_position Node='-109' Position='643,147,200,200'/><node_position Node='-121' Position='755,-158,200,200'/><node_position Node='-307' Position='679,-429,200,200'/><node_position Node='-159' Position='-154,220,200,200'/><node_position Node='-166' Position='356,488,200,200'/><node_position Node='-560' Position='-160,358,200,200'/><node_position Node='-609' Position='-817,32,200,200'/><node_position Node='-629' Position='-737,174,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [115]:
    # 本代码由可视化策略环境自动生成 2022年7月25日 01:26
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    from itertools import combinations
    
    def combine(temp_list, n):
        '''根据n获得列表中的所有可能组合(n个元素为一组)'''
        temp_list2 = []
        for c in combinations(temp_list, n):
            if len(c) != 0:
                temp_list2.append(list(c))
        return temp_list2
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m9_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        list1 = input_1.read()
        end_list = []
        for i in range(len(list1)):
            end_list.extend(combine(list1, i))
        print(end_list)
        df = end_list
        data_1 = DataSource.write_pickle(df)
        data_2 = DataSource.write_pickle(df)
        return Outputs(data_1=data_1, data_2=data_2, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m9_post_run_bigquant_run(outputs):
        return outputs
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m14_run_bigquant_run(input_1, input_2, input_3, stock_count, hold_days):
        # 示例代码如下。在这里编写您的代码
        param = {
            "stock_count": stock_count,
            "hold_days": hold_days
            }
        data_1 = DataSource.write_pickle(param)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m14_post_run_bigquant_run(outputs):
        return outputs
    
    from scipy import nanstd
    from sklearn import preprocessing
    import math
    import numpy as np
    def print_debug(**kwargs):
        if True:
            for k,v in kwargs.items():
                print(k,":")
                print(v)
                
    # 计算n日涨幅
    def zhangfu(data, timeperiod=13):          
        adj_close = data['pre_close']- data['pre_close'].shift(timeperiod)             
        return adj_close
    def rank_swing_volatility(data,timeperiod=5):
        return nanstd((data['pre_high']-data['pre_low'])/data['pre_close'], timeperiod)*sqrt(200)*100
    # 计算动量
    def barAdjMon(data, timeperiod=2):          
        adj_close = (data['pre_close'] + data['pre_high'] + data['pre_low']) / 3               
        return np.log(adj_close / adj_close.shift(timeperiod))
    
    def del_main(start_date_input,end_date_input,instruments):
        #instruments = ['510330.HOF','161017.ZOF','159949.ZOF']
        df = DataSource("bar1d_CN_FUND").read(start_date=start_date_input,instruments = instruments, end_date=end_date_input)
        groups = df.groupby(df['instrument'])
        df_expend = pd.DataFrame()
        for x in instruments:
            #分组排序后重新设置索引
            tp = groups.get_group(x)
            tp = tp.sort_values(by=['date'],na_position='first')
    
            tp = tp.reset_index() 
            tp.drop('index',axis= 1,inplace = True) 
            tp = tp.reset_index() 
            print_debug(tp_index=tp.index)
            # 基础特征列表
            tp['open_0'] = tp['open']
            tp['high_0'] = tp['high']
            tp['low_0'] = tp['low']
            tp['close_0'] = tp['close']
            tp['volume_0'] = tp['volume']
            tp['pre_close'] = tp['close'].shift(1)
            tp['std_pre_close'] = preprocessing.scale(tp['pre_close'].values)
            tp['pre_high'] = tp['high'].shift(1)
            tp['std_pre_high'] = preprocessing.scale(tp['pre_high'].values)
            tp['pre_low'] = tp['low'].shift(1)
            tp['std_pre_low'] = preprocessing.scale(tp['pre_low'].values)
            tp['pre_open'] = tp['open'].shift(1)
            tp['std_pre_open'] = preprocessing.scale(tp['pre_open'].values)
            tp['pre_volume'] = tp['volume'].shift(1)
            tp['std_pre_volume'] = preprocessing.scale(tp['pre_volume'].values)
            tp['pre_amount'] = tp['amount'].shift(1)
            tp['std_pre_amount'] = preprocessing.scale(tp['pre_amount'].values)
            #计算其他特征
            #tp['alpha_001_my'] = (rank(ts_argmax(signedpower(where(((tp['close_0']/shift(tp['close_0'],1)-1) < 0), std((tp['close_0']/shift(tp['close_0'],1)-1), 20), tp['close_0']), 2), 5)) -0.5)
            #20日动量
            tp['mom_20'] = barAdjMon(tp,timeperiod=20)
            tp['std_mom_20'] = preprocessing.scale(tp['mom_20'].values)
            #13日涨幅
            tp['zhangfu'] = zhangfu(tp,timeperiod=13)
            tp['std_zhangfu'] = preprocessing.scale(tp['zhangfu'].values)
            # std ln pre open
            tp['std_ln_pre_open'] = preprocessing.scale(np.log(tp['pre_open']))
            print(tp.head(2))
            df_expend = df_expend.append(tp)
        #df_159949 = groups.get_group('159949.ZOF')
        df_expend.sort_values(['date'],na_position='first',inplace=True)
        df_expend = df_expend.reset_index()
        df_expend.drop('index',axis= 1,inplace = True)
        # 异常数据处理
        #df_expend = df_expend.fillna(value=0.0) #列向前填充
    #     # 波动率因子
    #     df_expend['rank_swing_volatility_5'] = rank_swing_volatility(df_expend,5)
    #     df_expend['rank_swing_volatility_5'] = df_expend['rank_swing_volatility_5'].fillna(value=0.0) #列向前填充
        return df_expend
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m2_run_bigquant_run(input_1, input_2, input_3,start_date_input,end_date_input,pre_start_date,pre_end_date):
        pre_start_date = input_1.read()['start_date']
        pre_end_date = input_1.read()['end_date']
        instruments = input_1.read()['instruments']
        # 示例代码如下。在这里编写您的代码
        # 读取数据  默认会返回全部证券代码数据, 通过指定参数 instruments 可以读取到指定的证券代码数据
        df_expend = del_main(start_date_input,end_date_input,instruments)
        data_1 = DataSource.write_df(df_expend)
        data_2 = DataSource.write_df(df_expend)
        print('pre_start_date,pre_end_date',pre_start_date,pre_end_date)
        df_pre = del_main(pre_start_date,pre_end_date,instruments)
        data_3 = DataSource.write_df(df_pre)
        return Outputs(data_1=data_1, data_2=data_2, data_3=data_3)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m2_post_run_bigquant_run(outputs):
        return outputs
    
    def del_input(input_1):
        df = input_1.read_df()
        data_1 = DataSource.write_df(df)
        return data_1
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m23_run_bigquant_run(input_1, input_2, input_3):
        data_1 = del_input(input_1)
        data_2 = del_input(input_2)
        return Outputs(data_1=data_1, data_2=data_2, data_3=None)
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m23_post_run_bigquant_run(outputs):
        return outputs
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def del_dirty_data(df):
        # 异常数据处理
        num = df.isna().sum()
        for i in range(len(num)):
            if num[i] > 10:
                print('!!!!!!!!!含有大量脏数据',num.index[i],num[i])
            if num[i] > 100:
                print(num.index[i],num[i])
                df = df.drop([str(num.index[i])],axis = 1)
        # 删除值全为nan的列
        df = df.dropna(axis = 1,how = 'all')
        # 删除任意含有nan的行
        #df = df.dropna()
        df = df.fillna(value=0.0) #列向前填充  
        return df
    def m1_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df1 = input_1.read()
        df1 = del_dirty_data(df1)
        data_1 = DataSource.write_df(df1)
        
        df2 = input_2.read()
        df2 = del_dirty_data(df2)
        data_2 = DataSource.write_df(df2)    
        return Outputs(data_1=data_1, data_2=data_2, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m1_post_run_bigquant_run(outputs):
        return outputs
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m17_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
    
        print(input_3.read())
        train_df = input_1.read()
        predict_df = input_2.read()
        features = input_3.read()
        data_1 = input_1
        data_2 = input_2
        for i in features:
            print(i)
        
        return Outputs(data_1=data_1, data_2=data_2, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m17_post_run_bigquant_run(outputs):
        return outputs
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m11_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        print('m11.input1',input_1)
        df = input_1.read()
        param = input_2.read()
        
        data = {
            "param": param,
            "data": df
        }
        data_1 = DataSource.write_pickle(data)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m11_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    def m21_initialize_bigquant_run(context):
        #print('初始化函数开始:')
        #print('context.options',context.options)
        # 加载预测数据
        context.ranker_prediction = context.options.get('data').read()['data']
        context.param = context.options['data'].read()["param"]
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0003, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        stock_count = context.param["stock_count"]
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[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 = 1.01
        context.hold_days = context.param["hold_days"]
        #print('初始化函数结束')
    
    # 交易引擎:每个单位时间开盘前调用一次。
    def m21_before_trading_start_bigquant_run(context, data):
        # 盘前处理,订阅行情等
    
        context.subscribe(context.instruments)
    
        pass
    
    # 交易引擎:tick数据处理函数,每个tick执行一次
    def m21_handle_tick_bigquant_run(context, tick):
        pass
    
    import math
    # 回测引擎:每日数据处理函数,每天执行一次
    def m21_handle_data_bigquant_run(context, data):
        #print('当前日期data.current_dt',data.current_dt)
        #print('data',data)
        try:
            context.ranker_prediction = context.options.get('data').read()['data']
            # 相隔几天(hold_days)进行一下换仓
            if context.trading_day_index % context.hold_days != 0:
                return 
    
            # 按日期过滤得到今日的预测数据
            ranker_prediction = context.ranker_prediction[
                context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
            # 目前持仓
            positions = {e: p.amount * p.last_sale_price for e, p in context.portfolio.positions.items()}
            # 权重
            buy_cash_weights = context.stock_weights
            # 今日买入股票列表
            stock_to_buy = list(ranker_prediction.instrument[:len(buy_cash_weights)])
            # 持仓上限
            max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
            print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
            # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
            stock_hold_now = [equity for equity in context.portfolio.positions ]
            # 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
            no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]
            # 需要卖出的股票
            stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
    
            # 卖出
            for stock in stock_to_sell:
                # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
                # 如果返回真值,则可以正常下单,否则会出错
                # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
                if data.can_trade(context.symbol(stock)):
                    # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,
                    #   即卖出全部股票,可参考回测文档
                    #print('卖出order target percent',context.symbol(stock))
                    #print('卖出结果',context.order_target_percent(context.symbol(stock), 0))
                    context.order_target_percent(context.symbol(stock), 0)
    
            # 如果当天没有买入的股票,就返回
            if len(stock_to_buy) == 0:
                return
    
            # 买入
            print('买入列表',stock_to_buy)
            for i, instrument in enumerate(stock_to_buy):
                cash = context.portfolio.portfolio_value * 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 > 500:
                    cash = int(math.floor(cash))
                    #print('买入order_value',context.symbol(instrument),' cash ',cash)
                    #print(context.order_value(context.symbol(instrument), cash))
                    context.order_value(context.symbol(instrument), cash)
        except Exception as e:
            print('抛出异常',e)
    # 交易引擎:成交回报处理函数,每个成交发生时执行一次
    def m21_handle_trade_bigquant_run(context, trade):
        pass
    
    # 交易引擎:委托回报处理函数,每个委托变化时执行一次
    def m21_handle_order_bigquant_run(context, order):
        pass
    
    # 交易引擎:盘后处理函数,每日盘后执行一次
    def m21_after_trading_bigquant_run(context, data):
        pass
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def deal_result_data(df):
        
        return df
    def m12_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        print(input_1)
        print(input_1.read())
        data_1 = input_1
        data_2 = input_1
    #     df1 = input_1.read_raw_perf()
    #     df1 = deal_result_data(df1)
    #     data_1 = DataSource.write_df(df1)
        
    #     df2 = input_2.read()
    #     df2 = deal_result_data(df2)
    #     data_2 = DataSource.write_df(df2)    
        return Outputs(data_1=data_1, data_2=data_2, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m12_post_run_bigquant_run(outputs):
        return outputs
    
    
    g = T.Graph({
    
        'm3': 'M.input_features.v1',
        'm3.features': """# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    
    # rank_swing_volatility_5 = nanstd((high-low)/pre_close, 5)*sqrt(200)*100
    # alpha_001 = (rank(ts_argmax(signedpower(where(((close_0/shift(close_0,1)-1) < 0), std((close_0/shift(close_0,1)-1), 20), close_0), 2), 5)) -0.5)
    # shift(close_0,5)/close_0
    
    # mom_20
    # zhangfu
    # ln_pre_high = log(pre_high)
    #ln_pre_open = nanstd(log(pre_open))
    # std_ln_pre_open
    # delta(close_0, 3)
    pre_close
    pre_high
    pre_volume
    # pre_amount
    
    # high
    # open
    # close
    # amount
    # volume
    # std_mom_20
    # std_zhangfu
    # std_pre_high
    # std_pre_close
    # std_pre_open
    # std_pre_low
    # std_pre_volume
    # std_pre_amount
    
    # ln_open = log(open_0)
    """,
    
        'm9': 'M.cached.v3',
        'm9.input_1': T.Graph.OutputPort('m3.data'),
        'm9.run': m9_run_bigquant_run,
        'm9.post_run': m9_post_run_bigquant_run,
        'm9.input_ports': '',
        'm9.params': '{}',
        'm9.output_ports': '',
    
        'm14': 'M.cached.v3',
        'm14.run': m14_run_bigquant_run,
        'm14.post_run': m14_post_run_bigquant_run,
        'm14.input_ports': '',
        'm14.params': """{
        "stock_count": 1,
        "hold_days": 1 
    }""",
        'm14.output_ports': '',
    
        'm4': 'M.instruments.v2',
        'm4.start_date': '2021-06-01',
        'm4.end_date': T.live_run_param('trading_date', '2022-06-01'),
        'm4.market': 'CN_FUND',
        'm4.instrument_list': """510330.HOF
    161017.ZOF
    159949.ZOF""",
        'm4.max_count': 0,
    
        'm2': 'M.cached.v3',
        'm2.input_1': T.Graph.OutputPort('m4.data'),
        'm2.run': m2_run_bigquant_run,
        'm2.post_run': m2_post_run_bigquant_run,
        'm2.input_ports': '',
        'm2.params': """{"start_date_input":"2020-06-01",
    "end_date_input":"2021-05-01",
    "pre_start_date":"2021-06-01",
    "pre_end_date":"2022-06-01"}""",
        'm2.output_ports': '',
        'm2.m_cached': False,
    
        'm10': 'M.auto_labeler_on_datasource.v1',
        'm10.input_data': T.Graph.OutputPort('m2.data_1'),
        'm10.label_expr': """# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -2) / shift(open, -1)
    
    # 极值处理:用1%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        'm10.drop_na_label': True,
        'm10.cast_label_int': True,
        'm10.date_col': 'date',
        'm10.instrument_col': 'instrument',
        'm10.user_functions': {},
    
        'm23': 'M.cached.v3',
        'm23.input_1': T.Graph.OutputPort('m2.data_2'),
        'm23.input_2': T.Graph.OutputPort('m2.data_3'),
        'm23.run': m23_run_bigquant_run,
        'm23.post_run': m23_post_run_bigquant_run,
        'm23.input_ports': '',
        'm23.params': '{}',
        'm23.output_ports': '',
        'm23.m_cached': False,
    
        'm16': 'M.derived_feature_extractor.v3',
        'm16.input_data': T.Graph.OutputPort('m23.data_1'),
        'm16.features': T.Graph.OutputPort('m3.data'),
        'm16.date_col': 'date',
        'm16.instrument_col': 'instrument',
        'm16.drop_na': False,
        'm16.remove_extra_columns': False,
    
        'm7': 'M.join.v3',
        'm7.data1': T.Graph.OutputPort('m10.data'),
        'm7.data2': T.Graph.OutputPort('m16.data'),
        'm7.on': 'date,instrument',
        'm7.how': 'inner',
        'm7.sort': False,
    
        'm18': 'M.derived_feature_extractor.v3',
        'm18.input_data': T.Graph.OutputPort('m23.data_2'),
        'm18.features': T.Graph.OutputPort('m3.data'),
        'm18.date_col': 'date',
        'm18.instrument_col': 'instrument',
        'm18.drop_na': False,
        'm18.remove_extra_columns': False,
    
        'm1': 'M.cached.v3',
        'm1.input_1': T.Graph.OutputPort('m7.data'),
        'm1.input_2': T.Graph.OutputPort('m18.data'),
        'm1.run': m1_run_bigquant_run,
        'm1.post_run': m1_post_run_bigquant_run,
        'm1.input_ports': '',
        'm1.params': '{}',
        'm1.output_ports': '',
    
        'm17': 'M.cached.v3',
        'm17.input_1': T.Graph.OutputPort('m1.data_1'),
        'm17.input_2': T.Graph.OutputPort('m1.data_2'),
        'm17.input_3': T.Graph.OutputPort('m3.data'),
        'm17.run': m17_run_bigquant_run,
        'm17.post_run': m17_post_run_bigquant_run,
        'm17.input_ports': '',
        'm17.params': '{\'index_my\':0}',
        'm17.output_ports': '',
    
        'm6': 'M.stock_ranker_train.v6',
        'm6.training_ds': T.Graph.OutputPort('m17.data_1'),
        'm6.features': T.Graph.OutputPort('m3.data'),
        'm6.learning_algorithm': '排序',
        'm6.number_of_leaves': 3,
        'm6.minimum_docs_per_leaf': 100,
        'm6.number_of_trees': 20,
        'm6.learning_rate': 0.1,
        'm6.max_bins': 1023,
        'm6.feature_fraction': 1,
        'm6.data_row_fraction': 1,
        'm6.plot_charts': True,
        'm6.ndcg_discount_base': 1,
        'm6.m_lazy_run': False,
        'm6.m_cached': False,
    
        'm8': 'M.stock_ranker_predict.v5',
        'm8.model': T.Graph.OutputPort('m6.model'),
        'm8.data': T.Graph.OutputPort('m17.data_2'),
        'm8.m_lazy_run': False,
    
        'm11': 'M.cached.v3',
        'm11.input_1': T.Graph.OutputPort('m8.predictions'),
        'm11.input_2': T.Graph.OutputPort('m14.data_1'),
        'm11.run': m11_run_bigquant_run,
        'm11.post_run': m11_post_run_bigquant_run,
        'm11.input_ports': '',
        'm11.params': '{}',
        'm11.output_ports': '',
    
        'm21': 'M.hftrade.v2',
        'm21.instruments': T.Graph.OutputPort('m4.data'),
        'm21.options_data': T.Graph.OutputPort('m11.data_1'),
        'm21.start_date': '',
        'm21.end_date': '',
        'm21.initialize': m21_initialize_bigquant_run,
        'm21.before_trading_start': m21_before_trading_start_bigquant_run,
        'm21.handle_tick': m21_handle_tick_bigquant_run,
        'm21.handle_data': m21_handle_data_bigquant_run,
        'm21.handle_trade': m21_handle_trade_bigquant_run,
        'm21.handle_order': m21_handle_order_bigquant_run,
        'm21.after_trading': m21_after_trading_bigquant_run,
        'm21.capital_base': 1000000,
        'm21.frequency': 'daily',
        'm21.price_type': '真实价格',
        'm21.product_type': '自动',
        'm21.before_start_days': '80',
        'm21.order_price_field_buy': 'open',
        'm21.order_price_field_sell': 'close',
        'm21.benchmark': '000300.HIX',
        'm21.plot_charts': True,
        'm21.disable_cache': False,
        'm21.replay_bdb': False,
        'm21.show_debug_info': True,
        'm21.backtest_only': False,
    
        'm12': 'M.cached.v3',
        'm12.input_1': T.Graph.OutputPort('m21.raw_perf'),
        'm12.run': m12_run_bigquant_run,
        'm12.post_run': m12_post_run_bigquant_run,
        'm12.input_ports': '',
        'm12.params': '{}',
        'm12.output_ports': '',
    })
    
    # g.run({})
    
    
    def m5_param_grid_builder_bigquant_run():
        param_grid = {}
        # 在这里设置需要调优的参数备选
        # param_grid['m3.features'] = ['close_1/close_0', 'close_2/close_0\nclose_3/close_0']
        # param_grid['m6.number_of_trees'] = [5, 10, 20]
        param_grid["m17.params"] = [
            """{"index_my": 3}""",
            """{"index_my": 4}""",
        ]
        return param_grid
    
    def m5_scoring_bigquant_run(result):
        score = result.get('m19').read_raw_perf()['sharpe'].tail(1)[0]
    
        return {'score': score}
    
    
    m5 = M.hyper_parameter_search.v1(
        param_grid_builder=m5_param_grid_builder_bigquant_run,
        scoring=m5_scoring_bigquant_run,
        search_algorithm='网格搜索',
        search_iterations=10,
        workers=1,
        worker_distributed_run=True,
        worker_silent=True,
        run_now=True,
        bq_graph=g
    )
    
    Fitting 1 folds for each of 2 candidates, totalling 2 fits
    [Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
    [CV 1/1; 1/2] START m17.params={"index_my": 3}..................................
    
    [CV 1/1; 1/2] END ................m17.params={"index_my": 3}; total time= 3.0min
    [Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:  3.0min remaining:    0.0s
    [CV 1/1; 2/2] START m17.params={"index_my": 4}..................................
    
    [CV 1/1; 2/2] END ................m17.params={"index_my": 4}; total time= 1.7min
    [Parallel(n_jobs=1)]: Done   2 out of   2 | elapsed:  4.7min remaining:    0.0s
    [Parallel(n_jobs=1)]: Done   2 out of   2 | elapsed:  4.7min finished