复制链接
克隆策略

    {"description":"实验创建于2020/2/14","graph":{"edges":[{"to_node_id":"-89:features","from_node_id":"-70:data"},{"to_node_id":"-72:features","from_node_id":"-70:data"},{"to_node_id":"-567:features","from_node_id":"-70:data"},{"to_node_id":"-596:features","from_node_id":"-70:data"},{"to_node_id":"-603:features","from_node_id":"-70:data"},{"to_node_id":"-141:features","from_node_id":"-70:data"},{"to_node_id":"-146:features","from_node_id":"-70:data"},{"to_node_id":"-656:data2","from_node_id":"-72:data"},{"to_node_id":"-72:input_data","from_node_id":"-89:data"},{"to_node_id":"-583:model","from_node_id":"-567:model"},{"to_node_id":"-625:options_data","from_node_id":"-583:predictions"},{"to_node_id":"-596:instruments","from_node_id":"-587:data"},{"to_node_id":"-625:instruments","from_node_id":"-587:data"},{"to_node_id":"-603:input_data","from_node_id":"-596:data"},{"to_node_id":"-612:input_data","from_node_id":"-603:data"},{"to_node_id":"-146:input_data","from_node_id":"-612:data"},{"to_node_id":"-656:data1","from_node_id":"-645:data"},{"to_node_id":"-205:input_data","from_node_id":"-656:data"},{"to_node_id":"-645:instruments","from_node_id":"-185:data"},{"to_node_id":"-89:instruments","from_node_id":"-185:data"},{"to_node_id":"-141:input_data","from_node_id":"-205:data"},{"to_node_id":"-567:training_ds","from_node_id":"-860:data"},{"to_node_id":"-583:data","from_node_id":"-1356:data"},{"to_node_id":"-860:input_data","from_node_id":"-141:data"},{"to_node_id":"-1356:input_data","from_node_id":"-146:data"}],"nodes":[{"node_id":"-70","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\nv_s_sqrt(v_add(v_add(delta(mf_net_amount_main_0, 8), mf_net_amount_main_0), v_add(v_add(delta(v_add(v_add(delta(mf_net_amount_main_0, s_shu(s_shu(s_shu(10)))), mf_net_amount_main_0), mf_net_amount_main_0), s_shu(s_shu(s_shu(3)))), mf_net_amount_main_0), mf_net_amount_main_0)))\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-70"}],"output_ports":[{"name":"data","node_id":"-70"}],"cacheable":false,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-72","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":"'''\ns 开头的函数 输入值是 数值\nv 开头的函数 输入值是 数组\n\n\n\ns_shu\ns_max\ns_min\nv_add\nv_sub\nv_mul\nv_div\nv_sqrt\nv_log\nv_neg\nv_sign\nv_s_sqrt\nv_s_log\nv_abs\nv_shift\nv_delta\nv_ts_min\nv_ts_max\nv_ts_argmin\nv_ts_argmax\nv_ts_incv\nv_ts_rank\nv_ts_wma2\nv_ts_ema\nv_sum\nv_mean\nv_std\nv_correlation\nv_covariance\nv_product\nv_rank\n\n\n\nv_sign 被其他2个函数调用了,参数也要把df传进去\n\n\n'''\n\n\n# 自定义运算符函数\nimport numpy as np\nimport pandas as pd\nfrom functools import reduce # 算子里求累乘用\nimport talib as ta\n\n\n# 子函数\n\n#把为0的值处理为nan\ndef _zero_to_nan(A):\n \n A2 = (A !=0)\n A3 = np.where(A2,A,np.nan)\n return A3\n\n\n# 求列表累乘\ndef _red(aaa):\n re = reduce(lambda x,y:x*y,aaa)\n return re\n\n# 传入array返回最后一个元素排序值的函数,这里除以n则是为了正则化(这里很重要)\ndef _get_sort_value(x):\n s = pd.Series(x) \n return s.rank(method='first')[len(s)-1]\n\n\n# 过去n天加权移动平均,m需要小于n\n# alpha为 指定平滑系数,alpha 参数为 n/m\n# alpha must satisfy: 0 < alpha <= 1\ndef _my_ewm(temp,alpha1):\n data2 =pd.DataFrame(temp)\n bak = data2.ewm(alpha=alpha1,adjust=False,axis=0).mean()\n bak = bak[0].values\n bak = bak[len(temp)-1]\n return bak\n\n\n\n# 过去n天指数移动平均值\n# 当com=2时,α=1/(1+com)=1/3\n\ndef _my_ema(temp): \n data2 =pd.DataFrame(temp) \n bak = data2.ewm(com=2,adjust=False,axis=0).mean()\n bak = bak[0].values\n bak = bak[len(temp)-1]\n return bak\n\n\n\ndef s_shu(df,a): \n return a \n\n# 这个函数主要是用于解决算子返回值必须有Int类型的错误的\ndef s_max(df,a, b): \n if a>=b:\n return a\n else:\n return b\n\n# 这个函数主要是用于解决算子返回值必须有Int类型的错误的\ndef s_min(df,a, b): \n if a>=b:\n return b\n else:\n return a \n\n# 加减乘除\ndef v_add(df,A, B): \n return A + B\n\ndef v_sub(df,A, B): \n return A - B\n\ndef v_mul(df,A, B):\n return A * B\n\ndef v_div(df,A, B):\n B = _zero_to_nan(B) #把为0的值处理为nan\n return A / B\n \n\n# 不含符号的开方\ndef v_sqrt(df,A): \n return np.sqrt(abs(A))\n\n\n# 自然对数\ndef v_log(df,A): \n A = _zero_to_nan(A) #把为0的值处理为nan\n return np.log(abs(A)) \n \n\n# 相反数\ndef v_neg(df,A):\n return -A\n\n# 符号函数\ndef v_sign(df,A):\n data4 = np.sign(A)\n return data4\n\n# 带符号的开方\ndef v_s_sqrt(df,A): \n return np.sqrt(abs(A))*v_sign(df,A)\n\n# 带符号的Log\ndef v_s_log(df,A): \n A = _zero_to_nan(A) #把为0的值处理为nan\n return np.log(abs(A))*v_sign(df,A)\n\n\n# 绝对值\ndef v_abs(df,A):\n return abs(A)\n\n# 过去 n 天的数值\ndef v_shift(df,A, n):\n data1 =pd.DataFrame(A)\n data4 = data1.shift(periods=n, axis=0)\n return data4.values\n\n# 当日值-过去 n 天值\ndef v_delta(df,A, n):\n data1 =pd.DataFrame(A)\n data4 = data1.shift(periods=n, axis=0)\n data5 = data1-data4\n return data5.values\n \n# 过去 n 天的最小值 \ndef v_ts_min(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).min()\n return data6.values\n \n# 过去 n 天的最大值 \ndef v_ts_max(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).max()\n return data6.values\n\n\n# 过去 n 天最小值序号(即过去 n 天最小值的下标)\ndef v_ts_argmin(df,A, n):\n data1 = pd.DataFrame(A)\n data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmin(),raw=True)\n return data7.values\n\n# 过去 n 天最大值序号(即过去 n 天最大值的下标)\ndef v_ts_argmax(df,A, n):\n data1 = pd.DataFrame(A)\n data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmax(),raw=True)\n return data7.values \n\n\n# 过去 n 天的均值除以标准差\ndef v_ts_incv(df,A, n):\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()\n data7 = data1.rolling(n, min_periods=1,center=False,axis=0).std()\n data8 = data6/data7\n return data8.values \n\n\n# 过去 n 天排序\ndef v_ts_rank(df,A, n):\n data1 = pd.DataFrame(A)\n data2 = data1.rolling(n,axis=0).apply(lambda x: _get_sort_value(x)/n,raw=True)\n data2\n return data2.values\n\n\n\n\n# ts_wma参数不能有浮点数,有浮点数,就意味着必须引入bak为浮点的函数!!\ndef v_ts_wma2(df,A,n,m):\n a = m/n\n if a > 1:\n a = 1/a\n a = round(a, 2)\n df = pd.DataFrame(A)\n aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ewm(x,a) ,raw=True)\n return aaa.values\n\n\n\ndef v_ts_ema(df,A,n): \n df = pd.DataFrame(A)\n aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ema(x) ,raw=True) \n return aaa.values\n\n\n\n\n# 过去 n 天的求和 \ndef v_sum(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).sum()\n return data6.values\n\n# 过去 n 天的均值 \ndef v_mean(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()\n return data6.values \n\n\n\n\n# 过去 n 天的标准差 \ndef v_std(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).std()\n return data6.values \n\n\n\n# 过去 n 天的相关系数 \ndef v_correlation(df,A,B,n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data2 = pd.DataFrame(B)\n aaa = data1.rolling(n, min_periods=1,center=False,axis=0)\n bbb = data2.rolling(n, min_periods=1,center=False,axis=0)\n ccc = aaa.corr(bbb)\n return ccc.values \n\n# 过去 n 天的协方差 \ndef v_covariance(df,A,B,n):\n #这里的 center 必须是 False!\n '''\n rolling 第一个参数是包含今天的 窗口大小,1就是代表当天\n \n '''\n data1 = pd.DataFrame(A)\n data2 = pd.DataFrame(B)\n aaa = data1.rolling(n, min_periods=1,center=False,axis=0)#min_periods 最少观测值数量\n bbb = data2.rolling(n, min_periods=1,center=False,axis=0)\n ccc = aaa.cov(bbb)\n return ccc.values \n\n\n\n\n# 过去 n 天累乘\ndef v_product(df,A, n):\n data1 = pd.DataFrame(A)\n aaa = data1.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x: _red(x),raw=True)\n return aaa.values\n\n\n\n# 截面排序\ndef v_rank(df,A):\n data1 = pd.DataFrame(A)\n data2 = data1.rank(method=\"first\",axis=1)\n return data2.values \n\n# 过去 n 天的排序(今天的因子值在过去n天的排序)\n# 考虑固定窗口不一样时,计算结果难以相互比较,故而可以将每次得到的序值除以窗口长度,从而将结果正则到[0,1]之间\n\n\n\nbigquant_run = {\n 's_shu': s_shu,\n 's_max': s_max,\n 's_min': s_min,\n 'v_add': v_add,\n 'v_sub': v_sub,\n 'v_mul': v_mul,\n 'v_div': v_div,\n 'v_sqrt': v_sqrt,\n 'v_log': v_log,\n 'v_neg': v_neg,\n 'v_sign': v_sign,\n 'v_s_sqrt': v_s_sqrt,\n 'v_s_log': v_s_log,\n 'v_abs': v_abs,\n 'v_shift': v_shift,\n 'v_delta': v_delta,\n 'v_ts_min': v_ts_min,\n 'v_ts_max': v_ts_max,\n 'v_ts_argmin': v_ts_argmin,\n 'v_ts_argmax': v_ts_argmax,\n 'v_ts_incv': v_ts_incv,\n 'v_ts_rank': v_ts_rank,\n 'v_ts_wma2': v_ts_wma2,\n 'v_ts_ema': v_ts_ema,\n 'v_sum': v_sum,\n 'v_mean': v_mean,\n 'v_std': v_std,\n 'v_correlation': v_correlation,\n 'v_covariance': v_covariance,\n 'v_product': v_product,\n 'v_rank': v_rank\n}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-72"},{"name":"features","node_id":"-72"}],"output_ports":[{"name":"data","node_id":"-72"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-89","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"90","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-89"},{"name":"features","node_id":"-89"}],"output_ports":[{"name":"data","node_id":"-89"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-567","module_id":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v6","parameters":[{"name":"learning_algorithm","value":"排序","type":"Literal","bound_global_parameter":null},{"name":"number_of_leaves","value":30,"type":"Literal","bound_global_parameter":null},{"name":"minimum_docs_per_leaf","value":1000,"type":"Literal","bound_global_parameter":null},{"name":"number_of_trees","value":20,"type":"Literal","bound_global_parameter":null},{"name":"learning_rate","value":0.1,"type":"Literal","bound_global_parameter":null},{"name":"max_bins","value":1023,"type":"Literal","bound_global_parameter":null},{"name":"feature_fraction","value":1,"type":"Literal","bound_global_parameter":null},{"name":"data_row_fraction","value":1,"type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"ndcg_discount_base","value":1,"type":"Literal","bound_global_parameter":null},{"name":"m_lazy_run","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"training_ds","node_id":"-567"},{"name":"features","node_id":"-567"},{"name":"test_ds","node_id":"-567"},{"name":"base_model","node_id":"-567"}],"output_ports":[{"name":"model","node_id":"-567"},{"name":"feature_gains","node_id":"-567"},{"name":"m_lazy_run","node_id":"-567"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"-583","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":"-583"},{"name":"data","node_id":"-583"}],"output_ports":[{"name":"predictions","node_id":"-583"},{"name":"m_lazy_run","node_id":"-583"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-587","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"20190101","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"20190201","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-587"}],"output_ports":[{"name":"data","node_id":"-587"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-596","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"90","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-596"},{"name":"features","node_id":"-596"}],"output_ports":[{"name":"data","node_id":"-596"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-603","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":"'''\ns 开头的函数 输入值是 数值\nv 开头的函数 输入值是 数组\n\n\n\ns_shu\ns_max\ns_min\nv_add\nv_sub\nv_mul\nv_div\nv_sqrt\nv_log\nv_neg\nv_sign\nv_s_sqrt\nv_s_log\nv_abs\nv_shift\nv_delta\nv_ts_min\nv_ts_max\nv_ts_argmin\nv_ts_argmax\nv_ts_incv\nv_ts_rank\nv_ts_wma2\nv_ts_ema\nv_sum\nv_mean\nv_std\nv_correlation\nv_covariance\nv_product\nv_rank\n\n\n\nv_sign 被其他2个函数调用了,参数也要把df传进去\n\n\n'''\n\n\n# 自定义运算符函数\nimport numpy as np\nimport pandas as pd\nfrom functools import reduce # 算子里求累乘用\nimport talib as ta\n\n\n# 子函数\n\n#把为0的值处理为nan\ndef _zero_to_nan(A):\n \n A2 = (A !=0)\n A3 = np.where(A2,A,np.nan)\n return A3\n\n\n# 求列表累乘\ndef _red(aaa):\n re = reduce(lambda x,y:x*y,aaa)\n return re\n\n# 传入array返回最后一个元素排序值的函数,这里除以n则是为了正则化(这里很重要)\ndef _get_sort_value(x):\n s = pd.Series(x) \n return s.rank(method='first')[len(s)-1]\n\n\n# 过去n天加权移动平均,m需要小于n\n# alpha为 指定平滑系数,alpha 参数为 n/m\n# alpha must satisfy: 0 < alpha <= 1\ndef _my_ewm(temp,alpha1):\n data2 =pd.DataFrame(temp)\n bak = data2.ewm(alpha=alpha1,adjust=False,axis=0).mean()\n bak = bak[0].values\n bak = bak[len(temp)-1]\n return bak\n\n\n\n# 过去n天指数移动平均值\n# 当com=2时,α=1/(1+com)=1/3\n\ndef _my_ema(temp): \n data2 =pd.DataFrame(temp) \n bak = data2.ewm(com=2,adjust=False,axis=0).mean()\n bak = bak[0].values\n bak = bak[len(temp)-1]\n return bak\n\n\n\ndef s_shu(df,a): \n return a \n\n# 这个函数主要是用于解决算子返回值必须有Int类型的错误的\ndef s_max(df,a, b): \n if a>=b:\n return a\n else:\n return b\n\n# 这个函数主要是用于解决算子返回值必须有Int类型的错误的\ndef s_min(df,a, b): \n if a>=b:\n return b\n else:\n return a \n\n# 加减乘除\ndef v_add(df,A, B): \n return A + B\n\ndef v_sub(df,A, B): \n return A - B\n\ndef v_mul(df,A, B):\n return A * B\n\ndef v_div(df,A, B):\n B = _zero_to_nan(B) #把为0的值处理为nan\n return A / B\n \n\n# 不含符号的开方\ndef v_sqrt(df,A): \n return np.sqrt(abs(A))\n\n\n# 自然对数\ndef v_log(df,A): \n A = _zero_to_nan(A) #把为0的值处理为nan\n return np.log(abs(A)) \n \n\n# 相反数\ndef v_neg(df,A):\n return -A\n\n# 符号函数\ndef v_sign(df,A):\n data4 = np.sign(A)\n return data4\n\n# 带符号的开方\ndef v_s_sqrt(df,A): \n return np.sqrt(abs(A))*v_sign(df,A)\n\n# 带符号的Log\ndef v_s_log(df,A): \n A = _zero_to_nan(A) #把为0的值处理为nan\n return np.log(abs(A))*v_sign(df,A)\n\n\n# 绝对值\ndef v_abs(df,A):\n return abs(A)\n\n# 过去 n 天的数值\ndef v_shift(df,A, n):\n data1 =pd.DataFrame(A)\n data4 = data1.shift(periods=n, axis=0)\n return data4.values\n\n# 当日值-过去 n 天值\ndef v_delta(df,A, n):\n data1 =pd.DataFrame(A)\n data4 = data1.shift(periods=n, axis=0)\n data5 = data1-data4\n return data5.values\n \n# 过去 n 天的最小值 \ndef v_ts_min(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).min()\n return data6.values\n \n# 过去 n 天的最大值 \ndef v_ts_max(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).max()\n return data6.values\n\n\n# 过去 n 天最小值序号(即过去 n 天最小值的下标)\ndef v_ts_argmin(df,A, n):\n data1 = pd.DataFrame(A)\n data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmin(),raw=True)\n return data7.values\n\n# 过去 n 天最大值序号(即过去 n 天最大值的下标)\ndef v_ts_argmax(df,A, n):\n data1 = pd.DataFrame(A)\n data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmax(),raw=True)\n return data7.values \n\n\n# 过去 n 天的均值除以标准差\ndef v_ts_incv(df,A, n):\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()\n data7 = data1.rolling(n, min_periods=1,center=False,axis=0).std()\n data8 = data6/data7\n return data8.values \n\n\n# 过去 n 天排序\ndef v_ts_rank(df,A, n):\n data1 = pd.DataFrame(A)\n data2 = data1.rolling(n,axis=0).apply(lambda x: _get_sort_value(x)/n,raw=True)\n data2\n return data2.values\n\n\n\n\n# ts_wma参数不能有浮点数,有浮点数,就意味着必须引入bak为浮点的函数!!\ndef v_ts_wma2(df,A,n,m):\n a = m/n\n if a > 1:\n a = 1/a\n a = round(a, 2)\n df = pd.DataFrame(A)\n aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ewm(x,a) ,raw=True)\n return aaa.values\n\n\n\ndef v_ts_ema(df,A,n): \n df = pd.DataFrame(A)\n aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ema(x) ,raw=True) \n return aaa.values\n\n\n\n\n# 过去 n 天的求和 \ndef v_sum(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).sum()\n return data6.values\n\n# 过去 n 天的均值 \ndef v_mean(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()\n return data6.values \n\n\n\n\n# 过去 n 天的标准差 \ndef v_std(df,A, n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data6 = data1.rolling(n, min_periods=1,center=False,axis=0).std()\n return data6.values \n\n\n\n# 过去 n 天的相关系数 \ndef v_correlation(df,A,B,n):\n #这里的 center 必须是 False!\n data1 = pd.DataFrame(A)\n data2 = pd.DataFrame(B)\n aaa = data1.rolling(n, min_periods=1,center=False,axis=0)\n bbb = data2.rolling(n, min_periods=1,center=False,axis=0)\n ccc = aaa.corr(bbb)\n return ccc.values \n\n# 过去 n 天的协方差 \ndef v_covariance(df,A,B,n):\n #这里的 center 必须是 False!\n '''\n rolling 第一个参数是包含今天的 窗口大小,1就是代表当天\n \n '''\n data1 = pd.DataFrame(A)\n data2 = pd.DataFrame(B)\n aaa = data1.rolling(n, min_periods=1,center=False,axis=0)#min_periods 最少观测值数量\n bbb = data2.rolling(n, min_periods=1,center=False,axis=0)\n ccc = aaa.cov(bbb)\n return ccc.values \n\n\n\n\n# 过去 n 天累乘\ndef v_product(df,A, n):\n data1 = pd.DataFrame(A)\n aaa = data1.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x: _red(x),raw=True)\n return aaa.values\n\n\n\n# 截面排序\ndef v_rank(df,A):\n data1 = pd.DataFrame(A)\n data2 = data1.rank(method=\"first\",axis=1)\n return data2.values \n\n# 过去 n 天的排序(今天的因子值在过去n天的排序)\n# 考虑固定窗口不一样时,计算结果难以相互比较,故而可以将每次得到的序值除以窗口长度,从而将结果正则到[0,1]之间\n\n\n\nbigquant_run = {\n 's_shu': s_shu,\n 's_max': s_max,\n 's_min': s_min,\n 'v_add': v_add,\n 'v_sub': v_sub,\n 'v_mul': v_mul,\n 'v_div': v_div,\n 'v_sqrt': v_sqrt,\n 'v_log': v_log,\n 'v_neg': v_neg,\n 'v_sign': v_sign,\n 'v_s_sqrt': v_s_sqrt,\n 'v_s_log': v_s_log,\n 'v_abs': v_abs,\n 'v_shift': v_shift,\n 'v_delta': v_delta,\n 'v_ts_min': v_ts_min,\n 'v_ts_max': v_ts_max,\n 'v_ts_argmin': v_ts_argmin,\n 'v_ts_argmax': v_ts_argmax,\n 'v_ts_incv': v_ts_incv,\n 'v_ts_rank': v_ts_rank,\n 'v_ts_wma2': v_ts_wma2,\n 'v_ts_ema': v_ts_ema,\n 'v_sum': v_sum,\n 'v_mean': v_mean,\n 'v_std': v_std,\n 'v_correlation': v_correlation,\n 'v_covariance': v_covariance,\n 'v_product': v_product,\n 'v_rank': v_rank\n}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-603"},{"name":"features","node_id":"-603"}],"output_ports":[{"name":"data","node_id":"-603"}],"cacheable":true,"seq_num":9,"comment":"","comment_collapsed":true},{"node_id":"-612","module_id":"BigQuantSpace.chinaa_stock_filter.chinaa_stock_filter-v1","parameters":[{"name":"index_constituent_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%8150%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%8150%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B2%AA%E6%B7%B1300%22%2C%22displayValue%22%3A%22%E6%B2%AA%E6%B7%B1300%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81500%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81500%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81800%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81800%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%81180%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%81180%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81100%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81100%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B7%B1%E8%AF%81100%22%2C%22displayValue%22%3A%22%E6%B7%B1%E8%AF%81100%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"board_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B7%B1%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22displayValue%22%3A%22%E6%B7%B1%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%88%9B%E4%B8%9A%E6%9D%BF%22%2C%22displayValue%22%3A%22%E5%88%9B%E4%B8%9A%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%A7%91%E5%88%9B%E6%9D%BF%22%2C%22displayValue%22%3A%22%E7%A7%91%E5%88%9B%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%97%E4%BA%A4%E6%89%80%22%2C%22displayValue%22%3A%22%E5%8C%97%E4%BA%A4%E6%89%80%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"industry_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%BA%A4%E9%80%9A%E8%BF%90%E8%BE%93%22%2C%22displayValue%22%3A%22%E4%BA%A4%E9%80%9A%E8%BF%90%E8%BE%93%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%BC%91%E9%97%B2%E6%9C%8D%E5%8A%A1%22%2C%22displayValue%22%3A%22%E4%BC%91%E9%97%B2%E6%9C%8D%E5%8A%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%BC%A0%E5%AA%92%2F%E4%BF%A1%E6%81%AF%E6%9C%8D%E5%8A%A1%22%2C%22displayValue%22%3A%22%E4%BC%A0%E5%AA%92%2F%E4%BF%A1%E6%81%AF%E6%9C%8D%E5%8A%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%85%AC%E7%94%A8%E4%BA%8B%E4%B8%9A%22%2C%22displayValue%22%3A%22%E5%85%AC%E7%94%A8%E4%BA%8B%E4%B8%9A%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%86%9C%E6%9E%97%E7%89%A7%E6%B8%94%22%2C%22displayValue%22%3A%22%E5%86%9C%E6%9E%97%E7%89%A7%E6%B8%94%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%96%E5%B7%A5%22%2C%22displayValue%22%3A%22%E5%8C%96%E5%B7%A5%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%BB%E8%8D%AF%E7%94%9F%E7%89%A9%22%2C%22displayValue%22%3A%22%E5%8C%BB%E8%8D%AF%E7%94%9F%E7%89%A9%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%95%86%E4%B8%9A%E8%B4%B8%E6%98%93%22%2C%22displayValue%22%3A%22%E5%95%86%E4%B8%9A%E8%B4%B8%E6%98%93%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%9B%BD%E9%98%B2%E5%86%9B%E5%B7%A5%22%2C%22displayValue%22%3A%22%E5%9B%BD%E9%98%B2%E5%86%9B%E5%B7%A5%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%AE%B6%E7%94%A8%E7%94%B5%E5%99%A8%22%2C%22displayValue%22%3A%22%E5%AE%B6%E7%94%A8%E7%94%B5%E5%99%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%BB%BA%E7%AD%91%E6%9D%90%E6%96%99%2F%E5%BB%BA%E7%AD%91%E5%BB%BA%E6%9D%90%22%2C%22displayValue%22%3A%22%E5%BB%BA%E7%AD%91%E6%9D%90%E6%96%99%2F%E5%BB%BA%E7%AD%91%E5%BB%BA%E6%9D%90%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%BB%BA%E7%AD%91%E8%A3%85%E9%A5%B0%22%2C%22displayValue%22%3A%22%E5%BB%BA%E7%AD%91%E8%A3%85%E9%A5%B0%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%88%BF%E5%9C%B0%E4%BA%A7%22%2C%22displayValue%22%3A%22%E6%88%BF%E5%9C%B0%E4%BA%A7%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9C%89%E8%89%B2%E9%87%91%E5%B1%9E%22%2C%22displayValue%22%3A%22%E6%9C%89%E8%89%B2%E9%87%91%E5%B1%9E%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9C%BA%E6%A2%B0%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E6%9C%BA%E6%A2%B0%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B1%BD%E8%BD%A6%2F%E4%BA%A4%E8%BF%90%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E6%B1%BD%E8%BD%A6%2F%E4%BA%A4%E8%BF%90%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%94%B5%E5%AD%90%22%2C%22displayValue%22%3A%22%E7%94%B5%E5%AD%90%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%94%B5%E6%B0%94%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E7%94%B5%E6%B0%94%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%BA%BA%E7%BB%87%E6%9C%8D%E8%A3%85%22%2C%22displayValue%22%3A%22%E7%BA%BA%E7%BB%87%E6%9C%8D%E8%A3%85%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%BB%BC%E5%90%88%22%2C%22displayValue%22%3A%22%E7%BB%BC%E5%90%88%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E8%AE%A1%E7%AE%97%E6%9C%BA%22%2C%22displayValue%22%3A%22%E8%AE%A1%E7%AE%97%E6%9C%BA%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E8%BD%BB%E5%B7%A5%E5%88%B6%E9%80%A0%22%2C%22displayValue%22%3A%22%E8%BD%BB%E5%B7%A5%E5%88%B6%E9%80%A0%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%80%9A%E4%BF%A1%22%2C%22displayValue%22%3A%22%E9%80%9A%E4%BF%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%87%87%E6%8E%98%22%2C%22displayValue%22%3A%22%E9%87%87%E6%8E%98%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%92%A2%E9%93%81%22%2C%22displayValue%22%3A%22%E9%92%A2%E9%93%81%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%93%B6%E8%A1%8C%22%2C%22displayValue%22%3A%22%E9%93%B6%E8%A1%8C%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%9D%9E%E9%93%B6%E9%87%91%E8%9E%8D%22%2C%22displayValue%22%3A%22%E9%9D%9E%E9%93%B6%E9%87%91%E8%9E%8D%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%A3%9F%E5%93%81%E9%A5%AE%E6%96%99%22%2C%22displayValue%22%3A%22%E9%A3%9F%E5%93%81%E9%A5%AE%E6%96%99%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"st_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E6%AD%A3%E5%B8%B8%22%2C%22displayValue%22%3A%22%E6%AD%A3%E5%B8%B8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22ST%22%2C%22displayValue%22%3A%22ST%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22*ST%22%2C%22displayValue%22%3A%22*ST%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9A%82%E5%81%9C%E4%B8%8A%E5%B8%82%22%2C%22displayValue%22%3A%22%E6%9A%82%E5%81%9C%E4%B8%8A%E5%B8%82%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"delist_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E9%80%80%E5%B8%82%22%2C%22displayValue%22%3A%22%E9%80%80%E5%B8%82%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%9D%9E%E9%80%80%E5%B8%82%22%2C%22displayValue%22%3A%22%E9%9D%9E%E9%80%80%E5%B8%82%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-612"}],"output_ports":[{"name":"data","node_id":"-612"},{"name":"left_data","node_id":"-612"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"-625","module_id":"BigQuantSpace.trade.trade-v4","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n context.ranker_prediction = context.options['data'].read_df()\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)\n # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只\n stock_count = 5\n # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]\n context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])\n # 设置每只股票占用的最大资金比例\n context.max_cash_per_instrument = 0.2\n context.hold_days = 1\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n ranker_prediction = context.ranker_prediction[\n context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n\n # 1. 资金分配\n # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金\n # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)\n is_staging = context.trading_day_index < context.hold_days # 是否在建仓期间(前 hold_days 天)\n cash_avg = context.portfolio.portfolio_value / context.hold_days\n cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)\n cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)\n positions = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.perf_tracker.position_tracker.positions.items()}\n\n # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰\n if not is_staging and cash_for_sell > 0:\n equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}\n instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(\n lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))\n # print('rank order for sell %s' % instruments)\n for instrument in instruments:\n context.order_target(context.symbol(instrument), 0)\n cash_for_sell -= positions[instrument]\n if cash_for_sell <= 0:\n break\n\n # 3. 生成买入订单:按StockRanker预测的排序,买入前面的stock_count只股票\n buy_cash_weights = context.stock_weights\n buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])\n max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument\n for i, instrument in enumerate(buy_instruments):\n cash = cash_for_buy * buy_cash_weights[i]\n if cash > max_cash_per_instrument - positions.get(instrument, 0):\n # 确保股票持仓量不会超过每次股票最大的占用资金量\n cash = max_cash_per_instrument - positions.get(instrument, 0)\n if cash > 0:\n context.order_value(context.symbol(instrument), cash)\n","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":0.025,"type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"open","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"open","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":1000000,"type":"Literal","bound_global_parameter":null},{"name":"auto_cancel_non_tradable_orders","value":"True","type":"Literal","bound_global_parameter":null},{"name":"data_frequency","value":"daily","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"真实价格","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"股票","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.HIX","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-625"},{"name":"options_data","node_id":"-625"},{"name":"history_ds","node_id":"-625"},{"name":"benchmark_ds","node_id":"-625"},{"name":"trading_calendar","node_id":"-625"}],"output_ports":[{"name":"raw_perf","node_id":"-625"}],"cacheable":false,"seq_num":12,"comment":"","comment_collapsed":true},{"node_id":"-645","module_id":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","parameters":[{"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# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(open, -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":"start_date","value":"20180928","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"20181227","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.SHA","type":"Literal","bound_global_parameter":null},{"name":"drop_na_label","value":"True","type":"Literal","bound_global_parameter":null},{"name":"cast_label_int","value":"True","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"{}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-645"}],"output_ports":[{"name":"data","node_id":"-645"}],"cacheable":true,"seq_num":13,"comment":"","comment_collapsed":true},{"node_id":"-656","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":"-656"},{"name":"data2","node_id":"-656"}],"output_ports":[{"name":"data","node_id":"-656"}],"cacheable":true,"seq_num":14,"comment":"","comment_collapsed":true},{"node_id":"-185","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"20180628","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"20181227","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-185"}],"output_ports":[{"name":"data","node_id":"-185"}],"cacheable":true,"seq_num":17,"comment":"","comment_collapsed":true},{"node_id":"-205","module_id":"BigQuantSpace.chinaa_stock_filter.chinaa_stock_filter-v1","parameters":[{"name":"index_constituent_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%8150%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%8150%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B2%AA%E6%B7%B1300%22%2C%22displayValue%22%3A%22%E6%B2%AA%E6%B7%B1300%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81500%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81500%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81800%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81800%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%81180%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%81180%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%B8%AD%E8%AF%81100%22%2C%22displayValue%22%3A%22%E4%B8%AD%E8%AF%81100%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B7%B1%E8%AF%81100%22%2C%22displayValue%22%3A%22%E6%B7%B1%E8%AF%81100%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"board_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%B8%8A%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22displayValue%22%3A%22%E4%B8%8A%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B7%B1%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22displayValue%22%3A%22%E6%B7%B1%E8%AF%81%E4%B8%BB%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%88%9B%E4%B8%9A%E6%9D%BF%22%2C%22displayValue%22%3A%22%E5%88%9B%E4%B8%9A%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%A7%91%E5%88%9B%E6%9D%BF%22%2C%22displayValue%22%3A%22%E7%A7%91%E5%88%9B%E6%9D%BF%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%97%E4%BA%A4%E6%89%80%22%2C%22displayValue%22%3A%22%E5%8C%97%E4%BA%A4%E6%89%80%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"industry_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E4%BA%A4%E9%80%9A%E8%BF%90%E8%BE%93%22%2C%22displayValue%22%3A%22%E4%BA%A4%E9%80%9A%E8%BF%90%E8%BE%93%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%BC%91%E9%97%B2%E6%9C%8D%E5%8A%A1%22%2C%22displayValue%22%3A%22%E4%BC%91%E9%97%B2%E6%9C%8D%E5%8A%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E4%BC%A0%E5%AA%92%2F%E4%BF%A1%E6%81%AF%E6%9C%8D%E5%8A%A1%22%2C%22displayValue%22%3A%22%E4%BC%A0%E5%AA%92%2F%E4%BF%A1%E6%81%AF%E6%9C%8D%E5%8A%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%85%AC%E7%94%A8%E4%BA%8B%E4%B8%9A%22%2C%22displayValue%22%3A%22%E5%85%AC%E7%94%A8%E4%BA%8B%E4%B8%9A%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%86%9C%E6%9E%97%E7%89%A7%E6%B8%94%22%2C%22displayValue%22%3A%22%E5%86%9C%E6%9E%97%E7%89%A7%E6%B8%94%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%96%E5%B7%A5%22%2C%22displayValue%22%3A%22%E5%8C%96%E5%B7%A5%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%8C%BB%E8%8D%AF%E7%94%9F%E7%89%A9%22%2C%22displayValue%22%3A%22%E5%8C%BB%E8%8D%AF%E7%94%9F%E7%89%A9%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%95%86%E4%B8%9A%E8%B4%B8%E6%98%93%22%2C%22displayValue%22%3A%22%E5%95%86%E4%B8%9A%E8%B4%B8%E6%98%93%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%9B%BD%E9%98%B2%E5%86%9B%E5%B7%A5%22%2C%22displayValue%22%3A%22%E5%9B%BD%E9%98%B2%E5%86%9B%E5%B7%A5%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%AE%B6%E7%94%A8%E7%94%B5%E5%99%A8%22%2C%22displayValue%22%3A%22%E5%AE%B6%E7%94%A8%E7%94%B5%E5%99%A8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%BB%BA%E7%AD%91%E6%9D%90%E6%96%99%2F%E5%BB%BA%E7%AD%91%E5%BB%BA%E6%9D%90%22%2C%22displayValue%22%3A%22%E5%BB%BA%E7%AD%91%E6%9D%90%E6%96%99%2F%E5%BB%BA%E7%AD%91%E5%BB%BA%E6%9D%90%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E5%BB%BA%E7%AD%91%E8%A3%85%E9%A5%B0%22%2C%22displayValue%22%3A%22%E5%BB%BA%E7%AD%91%E8%A3%85%E9%A5%B0%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%88%BF%E5%9C%B0%E4%BA%A7%22%2C%22displayValue%22%3A%22%E6%88%BF%E5%9C%B0%E4%BA%A7%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9C%89%E8%89%B2%E9%87%91%E5%B1%9E%22%2C%22displayValue%22%3A%22%E6%9C%89%E8%89%B2%E9%87%91%E5%B1%9E%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9C%BA%E6%A2%B0%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E6%9C%BA%E6%A2%B0%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%B1%BD%E8%BD%A6%2F%E4%BA%A4%E8%BF%90%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E6%B1%BD%E8%BD%A6%2F%E4%BA%A4%E8%BF%90%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%94%B5%E5%AD%90%22%2C%22displayValue%22%3A%22%E7%94%B5%E5%AD%90%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%94%B5%E6%B0%94%E8%AE%BE%E5%A4%87%22%2C%22displayValue%22%3A%22%E7%94%B5%E6%B0%94%E8%AE%BE%E5%A4%87%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%BA%BA%E7%BB%87%E6%9C%8D%E8%A3%85%22%2C%22displayValue%22%3A%22%E7%BA%BA%E7%BB%87%E6%9C%8D%E8%A3%85%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E7%BB%BC%E5%90%88%22%2C%22displayValue%22%3A%22%E7%BB%BC%E5%90%88%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E8%AE%A1%E7%AE%97%E6%9C%BA%22%2C%22displayValue%22%3A%22%E8%AE%A1%E7%AE%97%E6%9C%BA%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E8%BD%BB%E5%B7%A5%E5%88%B6%E9%80%A0%22%2C%22displayValue%22%3A%22%E8%BD%BB%E5%B7%A5%E5%88%B6%E9%80%A0%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%80%9A%E4%BF%A1%22%2C%22displayValue%22%3A%22%E9%80%9A%E4%BF%A1%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%87%87%E6%8E%98%22%2C%22displayValue%22%3A%22%E9%87%87%E6%8E%98%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%92%A2%E9%93%81%22%2C%22displayValue%22%3A%22%E9%92%A2%E9%93%81%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%93%B6%E8%A1%8C%22%2C%22displayValue%22%3A%22%E9%93%B6%E8%A1%8C%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%9D%9E%E9%93%B6%E9%87%91%E8%9E%8D%22%2C%22displayValue%22%3A%22%E9%9D%9E%E9%93%B6%E9%87%91%E8%9E%8D%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%A3%9F%E5%93%81%E9%A5%AE%E6%96%99%22%2C%22displayValue%22%3A%22%E9%A3%9F%E5%93%81%E9%A5%AE%E6%96%99%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"st_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E6%AD%A3%E5%B8%B8%22%2C%22displayValue%22%3A%22%E6%AD%A3%E5%B8%B8%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22ST%22%2C%22displayValue%22%3A%22ST%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22*ST%22%2C%22displayValue%22%3A%22*ST%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E6%9A%82%E5%81%9C%E4%B8%8A%E5%B8%82%22%2C%22displayValue%22%3A%22%E6%9A%82%E5%81%9C%E4%B8%8A%E5%B8%82%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"delist_cond","value":"%7B%22enumItems%22%3A%5B%7B%22value%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22displayValue%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22selected%22%3Atrue%7D%2C%7B%22value%22%3A%22%E9%80%80%E5%B8%82%22%2C%22displayValue%22%3A%22%E9%80%80%E5%B8%82%22%2C%22selected%22%3Afalse%7D%2C%7B%22value%22%3A%22%E9%9D%9E%E9%80%80%E5%B8%82%22%2C%22displayValue%22%3A%22%E9%9D%9E%E9%80%80%E5%B8%82%22%2C%22selected%22%3Afalse%7D%5D%7D","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-205"}],"output_ports":[{"name":"data","node_id":"-205"},{"name":"left_data","node_id":"-205"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-860","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-860"},{"name":"features","node_id":"-860"}],"output_ports":[{"name":"data","node_id":"-860"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-1356","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-1356"},{"name":"features","node_id":"-1356"}],"output_ports":[{"name":"data","node_id":"-1356"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-141","module_id":"BigQuantSpace.fillnan.fillnan-v1","parameters":[{"name":"fill_value","value":"mean","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-141"},{"name":"features","node_id":"-141"}],"output_ports":[{"name":"data","node_id":"-141"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true},{"node_id":"-146","module_id":"BigQuantSpace.fillnan.fillnan-v1","parameters":[{"name":"fill_value","value":"mean","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-146"},{"name":"features","node_id":"-146"}],"output_ports":[{"name":"data","node_id":"-146"}],"cacheable":true,"seq_num":18,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-70' Position='83.59271240234375,-251,200,200'/><node_position Node='-72' Position='-140,-59,200,200'/><node_position Node='-89' Position='-151,-143,200,200'/><node_position Node='-567' Position='-292,297,200,200'/><node_position Node='-583' Position='-86,411,200,200'/><node_position Node='-587' Position='490,-338,200,200'/><node_position Node='-596' Position='457,-68,200,200'/><node_position Node='-603' Position='471,7,200,200'/><node_position Node='-612' Position='472,89,200,200'/><node_position Node='-625' Position='-149,504,200,200'/><node_position Node='-645' Position='-524,-142,200,200'/><node_position Node='-656' Position='-513.5963745117188,17.290904998779297,200,200'/><node_position Node='-185' Position='-330,-334,200,200'/><node_position Node='-205' Position='-496.47637939453125,86.29090881347656,200,200'/><node_position Node='-860' Position='-445.8218078613281,228.24000549316406,200,200'/><node_position Node='-1356' Position='312.28729248046875,268.0109100341797,200,200'/><node_position Node='-141' Position='-439.5254669189453,158.77455139160156,200,200'/><node_position Node='-146' Position='281.9727478027344,185.4217987060547,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [33]:
    # 本代码由可视化策略环境自动生成 2022年7月7日 07:59
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    '''
    s 开头的函数 输入值是 数值
    v 开头的函数 输入值是 数组
    
    
    
    s_shu
    s_max
    s_min
    v_add
    v_sub
    v_mul
    v_div
    v_sqrt
    v_log
    v_neg
    v_sign
    v_s_sqrt
    v_s_log
    v_abs
    v_shift
    v_delta
    v_ts_min
    v_ts_max
    v_ts_argmin
    v_ts_argmax
    v_ts_incv
    v_ts_rank
    v_ts_wma2
    v_ts_ema
    v_sum
    v_mean
    v_std
    v_correlation
    v_covariance
    v_product
    v_rank
    
    
    
    v_sign 被其他2个函数调用了,参数也要把df传进去
    
    
    '''
    
    
    # 自定义运算符函数
    import numpy as np
    import pandas as pd
    from functools import reduce  # 算子里求累乘用
    import talib as ta
    
    
    # 子函数
    
    #把为0的值处理为nan
    def _zero_to_nan(A):
        
        A2 = (A !=0)
        A3 = np.where(A2,A,np.nan)
        return A3
    
    
    # 求列表累乘
    def _red(aaa):
        re = reduce(lambda x,y:x*y,aaa)
        return re
    
    # 传入array返回最后一个元素排序值的函数,这里除以n则是为了正则化(这里很重要)
    def _get_sort_value(x):
        s = pd.Series(x)    
        return s.rank(method='first')[len(s)-1]
    
    
    # 过去n天加权移动平均,m需要小于n
    # alpha为 指定平滑系数,alpha 参数为 n/m
    # alpha must satisfy: 0 < alpha <= 1
    def _my_ewm(temp,alpha1):
        data2 =pd.DataFrame(temp)
        bak = data2.ewm(alpha=alpha1,adjust=False,axis=0).mean()
        bak = bak[0].values
        bak = bak[len(temp)-1]
        return bak
    
    
    
    # 过去n天指数移动平均值
    # 当com=2时,α=1/(1+com)=1/3
    
    def _my_ema(temp):    
        data2 =pd.DataFrame(temp)    
        bak = data2.ewm(com=2,adjust=False,axis=0).mean()
        bak = bak[0].values
        bak = bak[len(temp)-1]
        return bak
    
    
    
    def s_shu(df,a):    
        return a 
    
    # 这个函数主要是用于解决算子返回值必须有Int类型的错误的
    def s_max(df,a, b):   
        if a>=b:
            return a
        else:
            return b
    
    # 这个函数主要是用于解决算子返回值必须有Int类型的错误的
    def s_min(df,a, b):   
        if a>=b:
            return b
        else:
            return a  
    
    # 加减乘除
    def v_add(df,A, B):   
        return A + B
    
    def v_sub(df,A, B):   
        return A - B
    
    def v_mul(df,A, B):
        return A * B
    
    def v_div(df,A, B):
        B = _zero_to_nan(B) #把为0的值处理为nan
        return A / B
        
    
    # 不含符号的开方
    def v_sqrt(df,A):   
        return np.sqrt(abs(A))
    
    
    # 自然对数
    def v_log(df,A):   
        A = _zero_to_nan(A) #把为0的值处理为nan
        return np.log(abs(A))    
        
    
    # 相反数
    def v_neg(df,A):
        return -A
    
    # 符号函数
    def v_sign(df,A):
        data4 = np.sign(A)
        return data4
    
    # 带符号的开方
    def v_s_sqrt(df,A):   
        return np.sqrt(abs(A))*v_sign(df,A)
    
    # 带符号的Log
    def v_s_log(df,A):  
        A = _zero_to_nan(A) #把为0的值处理为nan
        return np.log(abs(A))*v_sign(df,A)
    
    
    # 绝对值
    def v_abs(df,A):
        return abs(A)
    
    # 过去 n 天的数值
    def v_shift(df,A, n):
        data1 =pd.DataFrame(A)
        data4 = data1.shift(periods=n, axis=0)
        return data4.values
    
    # 当日值-过去 n 天值
    def v_delta(df,A, n):
        data1 =pd.DataFrame(A)
        data4 = data1.shift(periods=n, axis=0)
        data5 = data1-data4
        return data5.values
        
    # 过去 n 天的最小值       
    def v_ts_min(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).min()
        return data6.values
        
    # 过去 n 天的最大值      
    def v_ts_max(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).max()
        return data6.values
    
    
    # 过去 n 天最小值序号(即过去 n 天最小值的下标)
    def v_ts_argmin(df,A, n):
        data1 = pd.DataFrame(A)
        data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmin(),raw=True)
        return data7.values
    
    # 过去 n 天最大值序号(即过去 n 天最大值的下标)
    def v_ts_argmax(df,A, n):
        data1 = pd.DataFrame(A)
        data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmax(),raw=True)
        return data7.values  
    
    
    # 过去 n 天的均值除以标准差
    def v_ts_incv(df,A, n):
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()
        data7 = data1.rolling(n, min_periods=1,center=False,axis=0).std()
        data8 = data6/data7
        return data8.values 
    
    
    # 过去 n 天排序
    def v_ts_rank(df,A, n):
        data1 = pd.DataFrame(A)
        data2 = data1.rolling(n,axis=0).apply(lambda x: _get_sort_value(x)/n,raw=True)
        data2
        return data2.values
    
    
    
    
    # ts_wma参数不能有浮点数,有浮点数,就意味着必须引入bak为浮点的函数!!
    def v_ts_wma2(df,A,n,m):
        a = m/n
        if a > 1:
            a = 1/a
        a = round(a, 2)
        df = pd.DataFrame(A)
        aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ewm(x,a) ,raw=True)
        return aaa.values
    
    
    
    def v_ts_ema(df,A,n):        
        df = pd.DataFrame(A)
        aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ema(x) ,raw=True)    
        return aaa.values
    
    
    
    
    # 过去 n 天的求和      
    def v_sum(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).sum()
        return data6.values
    
    # 过去 n 天的均值      
    def v_mean(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()
        return data6.values  
    
    
    
    
    # 过去 n 天的标准差      
    def v_std(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).std()
        return data6.values 
    
    
    
    # 过去 n 天的相关系数      
    def v_correlation(df,A,B,n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data2 = pd.DataFrame(B)
        aaa = data1.rolling(n, min_periods=1,center=False,axis=0)
        bbb = data2.rolling(n, min_periods=1,center=False,axis=0)
        ccc = aaa.corr(bbb)
        return ccc.values 
    
    # 过去 n 天的协方差   
    def v_covariance(df,A,B,n):
        #这里的 center 必须是 False!
        '''
        rolling 第一个参数是包含今天的 窗口大小,1就是代表当天
        
        '''
        data1 = pd.DataFrame(A)
        data2 = pd.DataFrame(B)
        aaa = data1.rolling(n, min_periods=1,center=False,axis=0)#min_periods 最少观测值数量
        bbb = data2.rolling(n, min_periods=1,center=False,axis=0)
        ccc = aaa.cov(bbb)
        return ccc.values 
    
    
    
    
    # 过去 n 天累乘
    def v_product(df,A, n):
        data1 = pd.DataFrame(A)
        aaa = data1.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x: _red(x),raw=True)
        return aaa.values
    
    
    
    # 截面排序
    def v_rank(df,A):
        data1 = pd.DataFrame(A)
        data2 = data1.rank(method="first",axis=1)
        return data2.values 
    
    # 过去 n 天的排序(今天的因子值在过去n天的排序)
    # 考虑固定窗口不一样时,计算结果难以相互比较,故而可以将每次得到的序值除以窗口长度,从而将结果正则到[0,1]之间
    
    
    
    m9_user_functions_bigquant_run = {
        's_shu':  s_shu,
        's_max':  s_max,
        's_min':  s_min,
        'v_add':  v_add,
        'v_sub':  v_sub,
        'v_mul':  v_mul,
        'v_div':  v_div,
        'v_sqrt':  v_sqrt,
        'v_log':  v_log,
        'v_neg':  v_neg,
        'v_sign':  v_sign,
        'v_s_sqrt':  v_s_sqrt,
        'v_s_log':  v_s_log,
        'v_abs':  v_abs,
        'v_shift':  v_shift,
        'v_delta':  v_delta,
        'v_ts_min':  v_ts_min,
        'v_ts_max':  v_ts_max,
        'v_ts_argmin':  v_ts_argmin,
        'v_ts_argmax':  v_ts_argmax,
        'v_ts_incv':  v_ts_incv,
        'v_ts_rank':  v_ts_rank,
        'v_ts_wma2':  v_ts_wma2,
        'v_ts_ema':  v_ts_ema,
        'v_sum':  v_sum,
        'v_mean':  v_mean,
        'v_std':  v_std,
        'v_correlation':  v_correlation,
        'v_covariance':  v_covariance,
        'v_product':  v_product,
        'v_rank':  v_rank
    }
    '''
    s 开头的函数 输入值是 数值
    v 开头的函数 输入值是 数组
    
    
    
    s_shu
    s_max
    s_min
    v_add
    v_sub
    v_mul
    v_div
    v_sqrt
    v_log
    v_neg
    v_sign
    v_s_sqrt
    v_s_log
    v_abs
    v_shift
    v_delta
    v_ts_min
    v_ts_max
    v_ts_argmin
    v_ts_argmax
    v_ts_incv
    v_ts_rank
    v_ts_wma2
    v_ts_ema
    v_sum
    v_mean
    v_std
    v_correlation
    v_covariance
    v_product
    v_rank
    
    
    
    v_sign 被其他2个函数调用了,参数也要把df传进去
    
    
    '''
    
    
    # 自定义运算符函数
    import numpy as np
    import pandas as pd
    from functools import reduce  # 算子里求累乘用
    import talib as ta
    
    
    # 子函数
    
    #把为0的值处理为nan
    def _zero_to_nan(A):
        
        A2 = (A !=0)
        A3 = np.where(A2,A,np.nan)
        return A3
    
    
    # 求列表累乘
    def _red(aaa):
        re = reduce(lambda x,y:x*y,aaa)
        return re
    
    # 传入array返回最后一个元素排序值的函数,这里除以n则是为了正则化(这里很重要)
    def _get_sort_value(x):
        s = pd.Series(x)    
        return s.rank(method='first')[len(s)-1]
    
    
    # 过去n天加权移动平均,m需要小于n
    # alpha为 指定平滑系数,alpha 参数为 n/m
    # alpha must satisfy: 0 < alpha <= 1
    def _my_ewm(temp,alpha1):
        data2 =pd.DataFrame(temp)
        bak = data2.ewm(alpha=alpha1,adjust=False,axis=0).mean()
        bak = bak[0].values
        bak = bak[len(temp)-1]
        return bak
    
    
    
    # 过去n天指数移动平均值
    # 当com=2时,α=1/(1+com)=1/3
    
    def _my_ema(temp):    
        data2 =pd.DataFrame(temp)    
        bak = data2.ewm(com=2,adjust=False,axis=0).mean()
        bak = bak[0].values
        bak = bak[len(temp)-1]
        return bak
    
    
    
    def s_shu(df,a):    
        return a 
    
    # 这个函数主要是用于解决算子返回值必须有Int类型的错误的
    def s_max(df,a, b):   
        if a>=b:
            return a
        else:
            return b
    
    # 这个函数主要是用于解决算子返回值必须有Int类型的错误的
    def s_min(df,a, b):   
        if a>=b:
            return b
        else:
            return a  
    
    # 加减乘除
    def v_add(df,A, B):   
        return A + B
    
    def v_sub(df,A, B):   
        return A - B
    
    def v_mul(df,A, B):
        return A * B
    
    def v_div(df,A, B):
        B = _zero_to_nan(B) #把为0的值处理为nan
        return A / B
        
    
    # 不含符号的开方
    def v_sqrt(df,A):   
        return np.sqrt(abs(A))
    
    
    # 自然对数
    def v_log(df,A):   
        A = _zero_to_nan(A) #把为0的值处理为nan
        return np.log(abs(A))    
        
    
    # 相反数
    def v_neg(df,A):
        return -A
    
    # 符号函数
    def v_sign(df,A):
        data4 = np.sign(A)
        return data4
    
    # 带符号的开方
    def v_s_sqrt(df,A):   
        return np.sqrt(abs(A))*v_sign(df,A)
    
    # 带符号的Log
    def v_s_log(df,A):  
        A = _zero_to_nan(A) #把为0的值处理为nan
        return np.log(abs(A))*v_sign(df,A)
    
    
    # 绝对值
    def v_abs(df,A):
        return abs(A)
    
    # 过去 n 天的数值
    def v_shift(df,A, n):
        data1 =pd.DataFrame(A)
        data4 = data1.shift(periods=n, axis=0)
        return data4.values
    
    # 当日值-过去 n 天值
    def v_delta(df,A, n):
        data1 =pd.DataFrame(A)
        data4 = data1.shift(periods=n, axis=0)
        data5 = data1-data4
        return data5.values
        
    # 过去 n 天的最小值       
    def v_ts_min(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).min()
        return data6.values
        
    # 过去 n 天的最大值      
    def v_ts_max(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).max()
        return data6.values
    
    
    # 过去 n 天最小值序号(即过去 n 天最小值的下标)
    def v_ts_argmin(df,A, n):
        data1 = pd.DataFrame(A)
        data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmin(),raw=True)
        return data7.values
    
    # 过去 n 天最大值序号(即过去 n 天最大值的下标)
    def v_ts_argmax(df,A, n):
        data1 = pd.DataFrame(A)
        data7 = data1.rolling(n, min_periods=1,axis=0).apply(lambda x: x.argmax(),raw=True)
        return data7.values  
    
    
    # 过去 n 天的均值除以标准差
    def v_ts_incv(df,A, n):
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()
        data7 = data1.rolling(n, min_periods=1,center=False,axis=0).std()
        data8 = data6/data7
        return data8.values 
    
    
    # 过去 n 天排序
    def v_ts_rank(df,A, n):
        data1 = pd.DataFrame(A)
        data2 = data1.rolling(n,axis=0).apply(lambda x: _get_sort_value(x)/n,raw=True)
        data2
        return data2.values
    
    
    
    
    # ts_wma参数不能有浮点数,有浮点数,就意味着必须引入bak为浮点的函数!!
    def v_ts_wma2(df,A,n,m):
        a = m/n
        if a > 1:
            a = 1/a
        a = round(a, 2)
        df = pd.DataFrame(A)
        aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ewm(x,a) ,raw=True)
        return aaa.values
    
    
    
    def v_ts_ema(df,A,n):        
        df = pd.DataFrame(A)
        aaa = df.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x:_my_ema(x) ,raw=True)    
        return aaa.values
    
    
    
    
    # 过去 n 天的求和      
    def v_sum(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).sum()
        return data6.values
    
    # 过去 n 天的均值      
    def v_mean(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).mean()
        return data6.values  
    
    
    
    
    # 过去 n 天的标准差      
    def v_std(df,A, n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data6 = data1.rolling(n, min_periods=1,center=False,axis=0).std()
        return data6.values 
    
    
    
    # 过去 n 天的相关系数      
    def v_correlation(df,A,B,n):
        #这里的 center 必须是 False!
        data1 = pd.DataFrame(A)
        data2 = pd.DataFrame(B)
        aaa = data1.rolling(n, min_periods=1,center=False,axis=0)
        bbb = data2.rolling(n, min_periods=1,center=False,axis=0)
        ccc = aaa.corr(bbb)
        return ccc.values 
    
    # 过去 n 天的协方差   
    def v_covariance(df,A,B,n):
        #这里的 center 必须是 False!
        '''
        rolling 第一个参数是包含今天的 窗口大小,1就是代表当天
        
        '''
        data1 = pd.DataFrame(A)
        data2 = pd.DataFrame(B)
        aaa = data1.rolling(n, min_periods=1,center=False,axis=0)#min_periods 最少观测值数量
        bbb = data2.rolling(n, min_periods=1,center=False,axis=0)
        ccc = aaa.cov(bbb)
        return ccc.values 
    
    
    
    
    # 过去 n 天累乘
    def v_product(df,A, n):
        data1 = pd.DataFrame(A)
        aaa = data1.rolling(n, min_periods=1,center=False,axis=0).apply(lambda x: _red(x),raw=True)
        return aaa.values
    
    
    
    # 截面排序
    def v_rank(df,A):
        data1 = pd.DataFrame(A)
        data2 = data1.rank(method="first",axis=1)
        return data2.values 
    
    # 过去 n 天的排序(今天的因子值在过去n天的排序)
    # 考虑固定窗口不一样时,计算结果难以相互比较,故而可以将每次得到的序值除以窗口长度,从而将结果正则到[0,1]之间
    
    
    
    m2_user_functions_bigquant_run = {
        's_shu':  s_shu,
        's_max':  s_max,
        's_min':  s_min,
        'v_add':  v_add,
        'v_sub':  v_sub,
        'v_mul':  v_mul,
        'v_div':  v_div,
        'v_sqrt':  v_sqrt,
        'v_log':  v_log,
        'v_neg':  v_neg,
        'v_sign':  v_sign,
        'v_s_sqrt':  v_s_sqrt,
        'v_s_log':  v_s_log,
        'v_abs':  v_abs,
        'v_shift':  v_shift,
        'v_delta':  v_delta,
        'v_ts_min':  v_ts_min,
        'v_ts_max':  v_ts_max,
        'v_ts_argmin':  v_ts_argmin,
        'v_ts_argmax':  v_ts_argmax,
        'v_ts_incv':  v_ts_incv,
        'v_ts_rank':  v_ts_rank,
        'v_ts_wma2':  v_ts_wma2,
        'v_ts_ema':  v_ts_ema,
        'v_sum':  v_sum,
        'v_mean':  v_mean,
        'v_std':  v_std,
        'v_correlation':  v_correlation,
        'v_covariance':  v_covariance,
        'v_product':  v_product,
        'v_rank':  v_rank
    }
    # 回测引擎:初始化函数,只执行一次
    def m12_initialize_bigquant_run(context):
        # 加载预测数据
        context.ranker_prediction = context.options['data'].read_df()
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
        # 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
        stock_count = 5
        # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
        context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
        # 设置每只股票占用的最大资金比例
        context.max_cash_per_instrument = 0.2
        context.hold_days = 1
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m12_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        ranker_prediction = context.ranker_prediction[
            context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
    
        # 1. 资金分配
        # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
        # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
        is_staging = context.trading_day_index < context.hold_days # 是否在建仓期间(前 hold_days 天)
        cash_avg = context.portfolio.portfolio_value / context.hold_days
        cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
        cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
        positions = {e.symbol: p.amount * p.last_sale_price
                     for e, p in context.perf_tracker.position_tracker.positions.items()}
    
        # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰
        if not is_staging and cash_for_sell > 0:
            equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
            instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
                    lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
            # print('rank order for sell %s' % instruments)
            for instrument in instruments:
                context.order_target(context.symbol(instrument), 0)
                cash_for_sell -= positions[instrument]
                if cash_for_sell <= 0:
                    break
    
        # 3. 生成买入订单:按StockRanker预测的排序,买入前面的stock_count只股票
        buy_cash_weights = context.stock_weights
        buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
        max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
        for i, instrument in enumerate(buy_instruments):
            cash = cash_for_buy * buy_cash_weights[i]
            if cash > max_cash_per_instrument - positions.get(instrument, 0):
                # 确保股票持仓量不会超过每次股票最大的占用资金量
                cash = max_cash_per_instrument - positions.get(instrument, 0)
            if cash > 0:
                context.order_value(context.symbol(instrument), cash)
    
    # 回测引擎:准备数据,只执行一次
    def m12_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m12_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.input_features.v1(
        features="""
    v_s_sqrt(v_add(v_add(delta(mf_net_amount_main_0, 8), mf_net_amount_main_0), v_add(v_add(delta(v_add(v_add(delta(mf_net_amount_main_0, s_shu(s_shu(s_shu(10)))), mf_net_amount_main_0), mf_net_amount_main_0), s_shu(s_shu(s_shu(3)))), mf_net_amount_main_0), mf_net_amount_main_0)))
    """,
        m_cached=False
    )
    
    m4 = M.instruments.v2(
        start_date='20190101',
        end_date='20190201',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m8 = M.general_feature_extractor.v7(
        instruments=m4.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m9 = M.derived_feature_extractor.v3(
        input_data=m8.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m9_user_functions_bigquant_run
    )
    
    m11 = M.chinaa_stock_filter.v1(
        input_data=m9.data,
        index_constituent_cond=['中证500'],
        board_cond=['全部'],
        industry_cond=['全部'],
        st_cond=['全部'],
        delist_cond=['全部'],
        output_left_data=False
    )
    
    m18 = M.fillnan.v1(
        input_data=m11.data,
        features=m1.data,
        fill_value='mean'
    )
    
    m15 = M.dropnan.v2(
        input_data=m18.data
    )
    
    m17 = M.instruments.v2(
        start_date='20180628',
        end_date='20181227',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m13 = M.advanced_auto_labeler.v2(
        instruments=m17.data,
        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>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(open, -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)
    """,
        start_date='20180928',
        end_date='20181227',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True,
        user_functions={}
    )
    
    m6 = M.general_feature_extractor.v7(
        instruments=m17.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m2 = M.derived_feature_extractor.v3(
        input_data=m6.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m2_user_functions_bigquant_run
    )
    
    m14 = M.join.v3(
        data1=m13.data,
        data2=m2.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m5 = M.chinaa_stock_filter.v1(
        input_data=m14.data,
        index_constituent_cond=['中证500'],
        board_cond=['全部'],
        industry_cond=['全部'],
        st_cond=['全部'],
        delist_cond=['全部'],
        output_left_data=False
    )
    
    m16 = M.fillnan.v1(
        input_data=m5.data,
        features=m1.data,
        fill_value='mean'
    )
    
    m10 = M.dropnan.v2(
        input_data=m16.data
    )
    
    m7 = M.stock_ranker_train.v6(
        training_ds=m10.data,
        features=m1.data,
        learning_algorithm='排序',
        number_of_leaves=30,
        minimum_docs_per_leaf=1000,
        number_of_trees=20,
        learning_rate=0.1,
        max_bins=1023,
        feature_fraction=1,
        data_row_fraction=1,
        plot_charts=True,
        ndcg_discount_base=1,
        m_lazy_run=False
    )
    
    m3 = M.stock_ranker_predict.v5(
        model=m7.model,
        data=m15.data,
        m_lazy_run=False
    )
    
    m12 = M.trade.v4(
        instruments=m4.data,
        options_data=m3.predictions,
        start_date='',
        end_date='',
        initialize=m12_initialize_bigquant_run,
        handle_data=m12_handle_data_bigquant_run,
        prepare=m12_prepare_bigquant_run,
        before_trading_start=m12_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000300.HIX'
    )
    
    ---------------------------------------------------------------------------
    Exception                                 Traceback (most recent call last)
    <ipython-input-33-b1cc4e450add> in <module>
        837 )
        838 
    --> 839 m7 = M.stock_ranker_train.v6(
        840     training_ds=m10.data,
        841     features=m1.data,
    
    Exception: 模型训练失败:可能导致错误的原因是训练数据问题,请检查训练数据, err_code=1 (33853134fd8211ecbcd1422a9c1a9dc9)