浏览代码

支持基金BFI排名

Joey 4 月之前
父节点
当前提交
cd05c717be
共有 3 个文件被更改,包括 182 次插入655 次删除
  1. 6 74
      modules/dataSaver.dos
  2. 149 556
      modules/rankingCalculator.dos
  3. 27 25
      modules/task_monthlyPerformance.dos

+ 6 - 74
modules/dataSaver.dos

@@ -274,13 +274,13 @@ def create_entity_index_coe(is_id_integer=false) {
 def create_entity_indicator_ranking(is_id_integer=false) {
 
     return table(1000:0, 
-                ['entity_id', 'end_date', 'strategy', 'indicator_id',
+                ['entity_id', 'end_date', 'category_id', 'indicator_id',
                  'indicator_1m', 'absrank_1m', 'perrank_1m', 'indicator_3m', 'absrank_3m', 'perrank_3m', 
                  'indicator_6m', 'absrank_6m', 'perrank_6m', 'indicator_1y', 'absrank_1y', 'perrank_1y', 
                  'indicator_2y', 'absrank_2y', 'perrank_2y', 'indicator_3y', 'absrank_3y', 'perrank_3y', 
                  'indicator_5y', 'absrank_5y', 'perrank_5y', 
                  'indicator_10y', 'absrank_10y', 'perrank_10y', 'indicator_ytd', 'absrank_ytd', 'perrank_ytd'],
-                [iif(is_id_integer, INT, SYMBOL), STRING, INT, INT,
+                [iif(is_id_integer, INT, SYMBOL), STRING, SYMBOL, INT,
                  DOUBLE, INT, INT, DOUBLE, INT, INT,
                  DOUBLE, INT, INT, DOUBLE, INT, INT,
                  DOUBLE, INT, INT, DOUBLE, INT, INT,
@@ -289,12 +289,12 @@ def create_entity_indicator_ranking(is_id_integer=false) {
 }
 
 /*
- *   建表 XXXX_indicator_ranking_num
+ *   建表 XXXX_indicator_ranking_num, raise_type 没有用
  */
-def create_entity_indicator_ranking_num(is_id_integer=false) {
+def create_entity_indicator_ranking_num() {
 
     return table(1000:0, 
-                ['end_date', 'strategy', 'raise_type', 'indicator_id',
+                ['end_date', 'category_id', 'raise_type', 'indicator_id',
                  'avg_1m', 'avg_1m_cnt', 'perrank_percent_5_1m', 'perrank_percent_10_1m', 'perrank_percent_25_1m', 'perrank_percent_50_1m',
                  'perrank_percent_75_1m', 'perrank_percent_90_1m', 'perrank_percent_95_1m', 'best_1m', 'worst_1m',
                  'avg_3m', 'avg_3m_cnt', 'perrank_percent_5_3m', 'perrank_percent_10_3m', 'perrank_percent_25_3m', 'perrank_percent_50_3m',
@@ -313,75 +313,7 @@ def create_entity_indicator_ranking_num(is_id_integer=false) {
                  'perrank_percent_75_10y', 'perrank_percent_90_10y', 'perrank_percent_95_10y', 'best_10y', 'worst_10y',
                  'avg_ytd', 'avg_ytd_cnt', 'perrank_percent_5_ytd', 'perrank_percent_10_ytd', 'perrank_percent_25_ytd', 'perrank_percent_50_ytd',
                  'perrank_percent_75_ytd', 'perrank_percent_90_ytd', 'perrank_percent_95_ytd', 'best_ytd', 'worst_ytd'],
-                [STRING, INT, INT, INT,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-                 DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE ]);
-}
-
-
-
-/*
- *   建表 XXXX_indicator_substrategy_ranking
- */
-def create_entity_indicator_substrategy_ranking(is_id_integer=false) {
-
-    return table(1000:0, 
-                ['entity_id', 'end_date', 'substrategy', 'indicator_id',
-                 'indicator_1m', 'absrank_1m', 'perrank_1m', 'indicator_3m', 'absrank_3m', 'perrank_3m', 
-                 'indicator_6m', 'absrank_6m', 'perrank_6m', 'indicator_1y', 'absrank_1y', 'perrank_1y', 
-                 'indicator_2y', 'absrank_2y', 'perrank_2y', 'indicator_3y', 'absrank_3y', 'perrank_3y', 
-                 'indicator_5y', 'absrank_5y', 'perrank_5y', 
-                 'indicator_10y', 'absrank_10y', 'perrank_10y', 'indicator_ytd', 'absrank_ytd', 'perrank_ytd'],
-                [iif(is_id_integer, INT, SYMBOL), STRING, INT, INT,
-                 DOUBLE, INT, INT, DOUBLE, INT, INT,
-                 DOUBLE, INT, INT, DOUBLE, INT, INT,
-                 DOUBLE, INT, INT, DOUBLE, INT, INT,
-                 DOUBLE, INT, INT,
-                 DOUBLE, INT, INT, DOUBLE, INT, INT]);
-}
-
-/*
- *   建表 XXXX_indicator_ranking_num
- */
-def create_entity_indicator_substrategy_ranking_num(is_id_integer=false) {
-
-    return table(1000:0, 
-                ['end_date', 'substrategy', 'raise_type', 'indicator_id',
-                 'avg_1m', 'avg_1m_cnt', 'perrank_percent_5_1m', 'perrank_percent_10_1m', 'perrank_percent_25_1m', 'perrank_percent_50_1m',
-                 'perrank_percent_75_1m', 'perrank_percent_90_1m', 'perrank_percent_95_1m', 'best_1m', 'worst_1m',
-                 'avg_3m', 'avg_3m_cnt', 'perrank_percent_5_3m', 'perrank_percent_10_3m', 'perrank_percent_25_3m', 'perrank_percent_50_3m',
-                 'perrank_percent_75_3m', 'perrank_percent_90_3m', 'perrank_percent_95_3m', 'best_3m', 'worst_3m',
-                 'avg_6m', 'avg_6m_cnt', 'perrank_percent_5_6m', 'perrank_percent_10_6m', 'perrank_percent_25_6m', 'perrank_percent_50_6m',
-                 'perrank_percent_75_6m', 'perrank_percent_90_6m', 'perrank_percent_95_6m', 'best_6m', 'worst_6m',
-                 'avg_1y', 'avg_1y_cnt', 'perrank_percent_5_1y', 'perrank_percent_10_1y', 'perrank_percent_25_1y', 'perrank_percent_50_1y',
-                 'perrank_percent_75_1y', 'perrank_percent_90_1y', 'perrank_percent_95_1y', 'best_1y', 'worst_1y',
-                 'avg_2y', 'avg_2y_cnt', 'perrank_percent_5_2y', 'perrank_percent_10_2y', 'perrank_percent_25_2y', 'perrank_percent_50_2y',
-                 'perrank_percent_75_2y', 'perrank_percent_90_2y', 'perrank_percent_95_2y', 'best_2y', 'worst_2y',
-                 'avg_3y', 'avg_3y_cnt', 'perrank_percent_5_3y', 'perrank_percent_10_3y', 'perrank_percent_25_3y', 'perrank_percent_50_3y',
-                 'perrank_percent_75_3y', 'perrank_percent_90_3y', 'perrank_percent_95_3y', 'best_3y', 'worst_3y',
-                 'avg_5y', 'avg_5y_cnt', 'perrank_percent_5_5y', 'perrank_percent_10_5y', 'perrank_percent_25_5y', 'perrank_percent_50_5y',
-                 'perrank_percent_75_5y', 'perrank_percent_90_5y', 'perrank_percent_95_5y', 'best_5y', 'worst_5y',
-                 'avg_10y', 'avg_10y_cnt', 'perrank_percent_5_10y', 'perrank_percent_10_10y', 'perrank_percent_25_10y', 'perrank_percent_50_10y',
-                 'perrank_percent_75_10y', 'perrank_percent_90_10y', 'perrank_percent_95_10y', 'best_10y', 'worst_10y',
-                 'avg_ytd', 'avg_ytd_cnt', 'perrank_percent_5_ytd', 'perrank_percent_10_ytd', 'perrank_percent_25_ytd', 'perrank_percent_50_ytd',
-                 'perrank_percent_75_ytd', 'perrank_percent_90_ytd', 'perrank_percent_95_ytd', 'best_ytd', 'worst_ytd'],
-                [STRING, INT, INT, INT,
+                [STRING, SYMBOL, INT, INT,
                  DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
                  DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
                  DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,

+ 149 - 556
modules/rankingCalculator.dos

@@ -38,165 +38,6 @@ def get_indicator_info() {
 
 }
 
-/*
- *   计算收益率排名
- * 
- *   TODO: 整合入 gen_ranking_sql
- */
-def cal_ret_ranking(entity_type, entity_info, end_date, isFromMySQL) {
-
-    // 当前只对基金做排名, 其它类型参考基金排名做相对排名
-    if(!(entity_type in ['MF', 'HF'])) return null;
-
-	table_desc = get_performance_table_description(entity_type);
-
-	tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data.rename!(sec_id_col, 'entity_id');
-
-	tb_strategy = get_strategy_list();
-	tb_substrategy = get_substrategy_list();
-
-	t = SELECT * 
-        FROM entity_info en 
-        INNER JOIN tb_data d ON en.entity_id = d.entity_id
-	    WHERE en.strategy IS NOT NULL
-	      AND (en.entity_id LIKE 'MF%' OR en.entity_id LIKE 'HF%')
-
-    // 按照 MySQL 字段建表
-    t_s = create_entity_indicator_ranking(false);
-    t_s_num = create_entity_indicator_ranking_num(false);
-    t_ss = create_entity_indicator_substrategy_ranking(false);
-    t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
-    v_tables = [t_s, t_s_num, t_ss, t_ss_num];
-
-    v_tables[0] = SELECT entity_id, end_date, strategy, 1 AS indicator_id,
-           ret_1m AS indicator_1m, ret_1m.rank(false) AS absrank_1m, (ret_1m.rank(false, percent=true)*100).round(0) AS perrank_1m,
-           ret_3m AS indicator_3m, ret_3m.rank(false) AS absrank_3m, (ret_3m.rank(false, percent=true)*100).round(0) AS perrank_3m,
-           ret_6m AS indicator_6m, ret_6m.rank(false) AS absrank_6m, (ret_6m.rank(false, percent=true)*100).round(0) AS perrank_6m,
-           ret_1y AS indicator_1y, ret_1y.rank(false) AS absrank_1y, (ret_1y.rank(false, percent=true)*100).round(0) AS perrank_1y,
-           ret_2y AS indicator_2y, ret_2y.rank(false) AS absrank_2y, (ret_2y.rank(false, percent=true)*100).round(0) AS perrank_2y,
-           ret_3y AS indicator_3y, ret_3y.rank(false) AS absrank_3y, (ret_3y.rank(false, percent=true)*100).round(0) AS perrank_3y,
-           ret_5y AS indicator_5y, ret_5y.rank(false) AS absrank_5y, (ret_5y.rank(false, percent=true)*100).round(0) AS perrank_5y,
-           ret_10y AS indicator_10y, ret_10y.rank(false) AS absrank_10y, (ret_10y.rank(false, percent=true)*100).round(0) AS perrank_10y,
-           ret_ytd AS indicator_ytd, ret_ytd.rank(false) AS absrank_ytd, (ret_ytd.rank(false, percent=true)*100).round(0) AS perrank_ytd
-    FROM t CONTEXT BY strategy, end_date;
-
-    v_tables[1] = SELECT t.end_date, t.strategy, s.raise_type[0], 1 AS indicator_id,
-           ret_1m.mean() AS avg_1m, ret_1m.count() AS avg_1m_cnt, ret_1m.percentile(95) AS perrank_percent_5_1m,
-           ret_1m.percentile(90) AS perrank_percent_10_1m, ret_1m.percentile(75) AS perrank_percent_25_1m,
-           ret_1m.percentile(50) AS perrank_percent_50_1m, ret_1m.percentile(25) AS perrank_percent_75_1m,
-           ret_1m.percentile(10) AS perrank_percent_90_1m, ret_1m.percentile(5) AS perrank_percent_95_1m,
-           ret_1m.max() AS best_1m, ret_1m.min() AS worst_1m,
-           ret_3m.mean() AS avg_3m, ret_3m.count() AS avg_3m_cnt, ret_3m.percentile(95) AS perrank_percent_5_3m,
-           ret_3m.percentile(90) AS perrank_percent_10_3m, ret_3m.percentile(75) AS perrank_percent_25_3m,
-           ret_3m.percentile(50) AS perrank_percent_50_3m, ret_3m.percentile(25) AS perrank_percent_75_3m,
-           ret_3m.percentile(10) AS perrank_percent_90_3m, ret_3m.percentile(5) AS perrank_percent_95_3m,
-           ret_3m.max() AS best_3m, ret_3m.min() AS worst_3m,
-           ret_6m.mean() AS avg_6m, ret_6m.count() AS avg_6m_cnt, ret_6m.percentile(95) AS perrank_percent_5_6m,
-           ret_6m.percentile(90) AS perrank_percent_10_6m, ret_6m.percentile(75) AS perrank_percent_25_6m,
-           ret_6m.percentile(50) AS perrank_percent_50_6m, ret_6m.percentile(25) AS perrank_percent_75_6m,
-           ret_6m.percentile(10) AS perrank_percent_90_6m, ret_6m.percentile(5) AS perrank_percent_95_6m,
-           ret_6m.max() AS best_6m, ret_6m.min() AS worst_6m,
-           ret_1y.mean() AS avg_1y, ret_1y.count() AS avg_1y_cnt, ret_1y.percentile(95) AS perrank_percent_5_1y,
-           ret_1y.percentile(90) AS perrank_percent_10_1y, ret_1y.percentile(75) AS perrank_percent_25_1y,
-           ret_1y.percentile(50) AS perrank_percent_50_1y, ret_1y.percentile(25) AS perrank_percent_75_1y,
-           ret_1y.percentile(10) AS perrank_percent_90_1y, ret_1y.percentile(5) AS perrank_percent_95_1y,
-           ret_1y.max() AS best_1y, ret_1y.min() AS worst_1y,
-           ret_2y.mean() AS avg_2y, ret_2y.count() AS avg_2y_cnt, ret_2y.percentile(95) AS perrank_percent_5_2y,
-           ret_2y.percentile(90) AS perrank_percent_10_2y, ret_2y.percentile(75) AS perrank_percent_25_2y,
-           ret_2y.percentile(50) AS perrank_percent_50_2y, ret_2y.percentile(25) AS perrank_percent_75_2y,
-           ret_2y.percentile(10) AS perrank_percent_90_2y, ret_2y.percentile(5) AS perrank_percent_95_2y,
-           ret_2y.max() AS best_2y, ret_2y.min() AS worst_2y,
-           ret_3y.mean() AS avg_3y, ret_3y.count() AS avg_3y_cnt, ret_3y.percentile(95) AS perrank_percent_5_3y,
-           ret_3y.percentile(90) AS perrank_percent_10_3y, ret_3y.percentile(75) AS perrank_percent_25_3y,
-           ret_3y.percentile(50) AS perrank_percent_50_3y, ret_3y.percentile(25) AS perrank_percent_75_3y,
-           ret_3y.percentile(10) AS perrank_percent_90_3y, ret_3y.percentile(5) AS perrank_percent_95_3y,
-           ret_3y.max() AS best_3y, ret_3y.min() AS worst_3y,
-           ret_5y.mean() AS avg_5y, ret_5y.count() AS avg_5y_cnt, ret_5y.percentile(95) AS perrank_percent_5_5y,
-           ret_5y.percentile(90) AS perrank_percent_10_5y, ret_5y.percentile(75) AS perrank_percent_25_5y,
-           ret_5y.percentile(50) AS perrank_percent_50_5y, ret_5y.percentile(25) AS perrank_percent_75_5y,
-           ret_5y.percentile(10) AS perrank_percent_90_5y, ret_5y.percentile(5) AS perrank_percent_95_5y,
-           ret_5y.max() AS best_5y, ret_5y.min() AS worst_5y,
-           ret_10y.mean() AS avg_10y, ret_10y.count() AS avg_10y_cnt, ret_10y.percentile(95) AS perrank_percent_5_10y,
-           ret_10y.percentile(90) AS perrank_percent_10_10y, ret_10y.percentile(75) AS perrank_percent_25_10y,
-           ret_10y.percentile(50) AS perrank_percent_50_10y, ret_10y.percentile(25) AS perrank_percent_75_10y,
-           ret_10y.percentile(10) AS perrank_percent_90_10y, ret_10y.percentile(5) AS perrank_percent_95_10y,
-           ret_10y.max() AS best_10y, ret_10y.min() AS worst_10y,
-           ret_ytd.mean() AS avg_ytd, ret_ytd.count() AS avg_ytd_cnt, ret_ytd.percentile(95) AS perrank_percent_5_ytd,
-           ret_ytd.percentile(90) AS perrank_percent_10_ytd, ret_ytd.percentile(75) AS perrank_percent_25_ytd,
-           ret_ytd.percentile(50) AS perrank_percent_50_ytd, ret_ytd.percentile(25) AS perrank_percent_75_ytd,
-           ret_ytd.percentile(10) AS perrank_percent_90_ytd, ret_ytd.percentile(5) AS perrank_percent_95_ytd,
-           ret_ytd.max() AS best_ytd, ret_ytd.min() AS worst_ytd
-    FROM t 
-    INNER JOIN tb_strategy s ON t.strategy = s.strategy_id
-    GROUP BY t.strategy, t.end_date;
-
-
-    v_tables[2] = SELECT entity_id, end_date, substrategy, 1 AS indicator_id,
-           ret_1m AS indicator_1m, ret_1m.rank(false) AS absrank_1m, (ret_1m.rank(false, percent=true)*100).round(0) AS perrank_1m,
-           ret_3m AS indicator_3m, ret_3m.rank(false) AS absrank_3m, (ret_3m.rank(false, percent=true)*100).round(0) AS perrank_3m,
-           ret_6m AS indicator_6m, ret_6m.rank(false) AS absrank_6m, (ret_6m.rank(false, percent=true)*100).round(0) AS perrank_6m,
-           ret_1y AS indicator_1y, ret_1y.rank(false) AS absrank_1y, (ret_1y.rank(false, percent=true)*100).round(0) AS perrank_1y,
-           ret_2y AS indicator_2y, ret_2y.rank(false) AS absrank_2y, (ret_2y.rank(false, percent=true)*100).round(0) AS perrank_2y,
-           ret_3y AS indicator_3y, ret_3y.rank(false) AS absrank_3y, (ret_3y.rank(false, percent=true)*100).round(0) AS perrank_3y,
-           ret_5y AS indicator_5y, ret_5y.rank(false) AS absrank_5y, (ret_5y.rank(false, percent=true)*100).round(0) AS perrank_5y,
-           ret_10y AS indicator_10y, ret_10y.rank(false) AS absrank_10y, (ret_10y.rank(false, percent=true)*100).round(0) AS perrank_10y,
-           ret_ytd AS indicator_ytd, ret_ytd.rank(false) AS absrank_ytd, (ret_ytd.rank(false, percent=true)*100).round(0) AS perrank_ytd
-    FROM t CONTEXT BY substrategy, end_date;
-
-    v_tables[3] = SELECT t.end_date, t.substrategy, s.raise_type[0], 1 AS indicator_id,
-           ret_1m.mean() AS avg_1m, ret_1m.count() AS avg_1m_cnt, ret_1m.percentile(95) AS perrank_percent_5_1m,
-           ret_1m.percentile(90) AS perrank_percent_10_1m, ret_1m.percentile(75) AS perrank_percent_25_1m,
-           ret_1m.percentile(50) AS perrank_percent_50_1m, ret_1m.percentile(25) AS perrank_percent_75_1m,
-           ret_1m.percentile(10) AS perrank_percent_90_1m, ret_1m.percentile(5) AS perrank_percent_95_1m,
-           ret_1m.max() AS best_1m, ret_1m.min() AS worst_1m,
-           ret_3m.mean() AS avg_3m, ret_3m.count() AS avg_3m_cnt, ret_3m.percentile(95) AS perrank_percent_5_3m,
-           ret_3m.percentile(90) AS perrank_percent_10_3m, ret_3m.percentile(75) AS perrank_percent_25_3m,
-           ret_3m.percentile(50) AS perrank_percent_50_3m, ret_3m.percentile(25) AS perrank_percent_75_3m,
-           ret_3m.percentile(10) AS perrank_percent_90_3m, ret_3m.percentile(5) AS perrank_percent_95_3m,
-           ret_3m.max() AS best_3m, ret_3m.min() AS worst_3m,
-           ret_6m.mean() AS avg_6m, ret_6m.count() AS avg_6m_cnt, ret_6m.percentile(95) AS perrank_percent_5_6m,
-           ret_6m.percentile(90) AS perrank_percent_10_6m, ret_6m.percentile(75) AS perrank_percent_25_6m,
-           ret_6m.percentile(50) AS perrank_percent_50_6m, ret_6m.percentile(25) AS perrank_percent_75_6m,
-           ret_6m.percentile(10) AS perrank_percent_90_6m, ret_6m.percentile(5) AS perrank_percent_95_6m,
-           ret_6m.max() AS best_6m, ret_6m.min() AS worst_6m,
-           ret_1y.mean() AS avg_1y, ret_1y.count() AS avg_1y_cnt, ret_1y.percentile(95) AS perrank_percent_5_1y,
-           ret_1y.percentile(90) AS perrank_percent_10_1y, ret_1y.percentile(75) AS perrank_percent_25_1y,
-           ret_1y.percentile(50) AS perrank_percent_50_1y, ret_1y.percentile(25) AS perrank_percent_75_1y,
-           ret_1y.percentile(10) AS perrank_percent_90_1y, ret_1y.percentile(5) AS perrank_percent_95_1y,
-           ret_1y.max() AS best_1y, ret_1y.min() AS worst_1y,
-           ret_2y.mean() AS avg_2y, ret_2y.count() AS avg_2y_cnt, ret_2y.percentile(95) AS perrank_percent_5_2y,
-           ret_2y.percentile(90) AS perrank_percent_10_2y, ret_2y.percentile(75) AS perrank_percent_25_2y,
-           ret_2y.percentile(50) AS perrank_percent_50_2y, ret_2y.percentile(25) AS perrank_percent_75_2y,
-           ret_2y.percentile(10) AS perrank_percent_90_2y, ret_2y.percentile(5) AS perrank_percent_95_2y,
-           ret_2y.max() AS best_2y, ret_2y.min() AS worst_2y,
-           ret_3y.mean() AS avg_3y, ret_3y.count() AS avg_3y_cnt, ret_3y.percentile(95) AS perrank_percent_5_3y,
-           ret_3y.percentile(90) AS perrank_percent_10_3y, ret_3y.percentile(75) AS perrank_percent_25_3y,
-           ret_3y.percentile(50) AS perrank_percent_50_3y, ret_3y.percentile(25) AS perrank_percent_75_3y,
-           ret_3y.percentile(10) AS perrank_percent_90_3y, ret_3y.percentile(5) AS perrank_percent_95_3y,
-           ret_3y.max() AS best_3y, ret_3y.min() AS worst_3y,
-           ret_5y.mean() AS avg_5y, ret_5y.count() AS avg_5y_cnt, ret_5y.percentile(95) AS perrank_percent_5_5y,
-           ret_5y.percentile(90) AS perrank_percent_10_5y, ret_5y.percentile(75) AS perrank_percent_25_5y,
-           ret_5y.percentile(50) AS perrank_percent_50_5y, ret_5y.percentile(25) AS perrank_percent_75_5y,
-           ret_5y.percentile(10) AS perrank_percent_90_5y, ret_5y.percentile(5) AS perrank_percent_95_5y,
-           ret_5y.max() AS best_5y, ret_5y.min() AS worst_5y,
-           ret_10y.mean() AS avg_10y, ret_10y.count() AS avg_10y_cnt, ret_10y.percentile(95) AS perrank_percent_5_10y,
-           ret_10y.percentile(90) AS perrank_percent_10_10y, ret_10y.percentile(75) AS perrank_percent_25_10y,
-           ret_10y.percentile(50) AS perrank_percent_50_10y, ret_10y.percentile(25) AS perrank_percent_75_10y,
-           ret_10y.percentile(10) AS perrank_percent_90_10y, ret_10y.percentile(5) AS perrank_percent_95_10y,
-           ret_10y.max() AS best_10y, ret_10y.min() AS worst_10y,
-           ret_ytd.mean() AS avg_ytd, ret_ytd.count() AS avg_ytd_cnt, ret_ytd.percentile(95) AS perrank_percent_5_ytd,
-           ret_ytd.percentile(90) AS perrank_percent_10_ytd, ret_ytd.percentile(75) AS perrank_percent_25_ytd,
-           ret_ytd.percentile(50) AS perrank_percent_50_ytd, ret_ytd.percentile(25) AS perrank_percent_75_ytd,
-           ret_ytd.percentile(10) AS perrank_percent_90_ytd, ret_ytd.percentile(5) AS perrank_percent_95_ytd,
-           ret_ytd.max() AS best_ytd, ret_ytd.min() AS worst_ytd
-    FROM t 
-    INNER JOIN tb_substrategy s ON t.substrategy = s.substrategy_id
-    GROUP BY t.substrategy, t.end_date;
-
-    return v_tables;
-}
 
 /*
  *   自定义百分位计算
@@ -211,186 +52,17 @@ defg perRank(x, is_ASC) {
 /*
  *   动态生成用于排序的SQL脚本
  * 
- *   @param indicator_name <STRING>: 指标字段名
- *   @param indicator_id <INT>:指标ID
- *   @param is_ASC <BOOL>: 是否排正序
- *   @param ranking_by <STRING>: 'strategy', 'substrategy', 'factor_id', 'catavg'
+ *   @param data_table <TABLE>: 指标横表
+ *   @param indicator_table <TABLE>: 指标表,有 id, name, is_ASC 字段
  *   
  *   TODO: portfolio, cf, manager, company, 
  *   TODO: bfi & category
  * 
  */
-def gen_ranking_sql0(data_table, indicator_table, ranking_by) {
+def gen_ranking_sql(data_table, indicator_table) {
 
-	for(indicator in indicator_table) {
-
-	    // 近1月和近3月排名仅对收益有效,为了满足表结构的要求,需要建立几个”假”字段,并用NULL赋值
-	    t_tmp = table(1000:0, ['indicator_id', 'indicator_1m', 'absrank_1m', 'perrank_1m',
-	                                           'indicator_3m', 'absrank_3m', 'perrank_3m'],
-	                          [INT, DOUBLE, INT, INT, DOUBLE, INT, INT]);
-	    INSERT INTO t_tmp VALUES (indicator.id, double(NULL), int(NULL), int(NULL), double(NULL), int(NULL), int(NULL));
-	
-	    t_ranking = sql(select = (sqlCol('entity_id'), sqlCol('end_date'), sqlCol(ranking_by), sqlCol('indicator_id'),
-	                              sqlCol('indicator_1m'), sqlCol('absrank_1m'), sqlCol('perrank_1m'),
-	                              sqlCol('indicator_3m'), sqlCol('absrank_3m'), sqlCol('perrank_3m'),
-	                              // 与 MySQL 不同,这里统一把近4年和成立以来的排名去掉
-	                              sqlCol(indicator.name + '_6m',,'indicator_6m'),
-	                              sqlCol(indicator.name + '_6m', rank{, indicator.is_ASC}, 'absrank_6m'),
-	                              sqlCol(indicator.name + '_6m', perRank{, indicator.is_ASC}, 'perrank_6m'),
-	                              sqlCol(indicator.name + '_1y',,'indicator_1y'),
-	                              sqlCol(indicator.name + '_1y', rank{, indicator.is_ASC}, 'absrank_1y'),
-	                              sqlCol(indicator.name + '_1y', perRank{, indicator.is_ASC}, 'perrank_1y'),
-	                              sqlCol(indicator.name + '_2y',,'indicator_2y'),
-	                              sqlCol(indicator.name + '_2y', rank{, indicator.is_ASC}, 'absrank_2y'),
-	                              sqlCol(indicator.name + '_2y', perRank{, indicator.is_ASC}, 'perrank_2y'),
-	                              sqlCol(indicator.name + '_3y',,'indicator_3y'),
-	                              sqlCol(indicator.name + '_3y', rank{, indicator.is_ASC}, 'absrank_3y'),
-	                              sqlCol(indicator.name + '_3y', perRank{, indicator.is_ASC}, 'perrank_3y'),
-	                              sqlCol(indicator.name + '_5y',,'indicator_5y'),
-	                              sqlCol(indicator.name + '_5y', rank{, indicator.is_ASC}, 'absrank_5y'),
-	                              sqlCol(indicator.name + '_5y', perRank{, indicator.is_ASC}, 'perrank_5y'),
-	                              sqlCol(indicator.name + '_10y',,'indicator_10y'),
-	                              sqlCol(indicator.name + '_10y', rank{, indicator.is_ASC}, 'absrank_10y'),
-	                              sqlCol(indicator.name + '_10y', perRank{, indicator.is_ASC}, 'perrank_10y'),
-	                              sqlCol(indicator.name + '_ytd',,'indicator_ytd'),
-	                              sqlCol(indicator.name + '_ytd', rank{, indicator.is_ASC}, 'absrank_ytd'),
-	                              sqlCol(indicator.name + '_ytd', perRank{, indicator.is_ASC}, 'perrank_ytd')
-	                              ), 
-	                    from = cj(data_table, t_tmp),
-	                    where = <_$ranking_by IS NOT NULL>,
-	                    groupBy = (sqlCol(ranking_by), sqlCol('end_date')),
-	                    groupFlag = 0 ).eval(); // context by
-	
-	
-	    // 近1月和近3月排名仅对收益有效,为了满足表结构的要求,需要建立几个”假”字段,并用NULL赋值
-	    t_tmp = table(1000:0, ['indicator_id', 'avg_1m', 'avg_1m_cnt', 'perrank_percent_5_1m', 'perrank_percent_10_1m', 'perrank_percent_25_1m',
-	                           'perrank_percent_50_1m', 'perrank_percent_75_1m', 'perrank_percent_90_1m', 'perrank_percent_95_1m', 'best_1m', 'worst_1m',
-	                           'avg_3m', 'avg_3m_cnt', 'perrank_percent_5_3m', 'perrank_percent_10_3m', 'perrank_percent_25_3m',
-	                           'perrank_percent_50_3m', 'perrank_percent_75_3m', 'perrank_percent_90_3m', 'perrank_percent_95_3m', 'best_3m', 'worst_3m'],
-	                          [INT, DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE,
-	                           DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
-	                           DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE,
-	                           DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
-	
-	    INSERT INTO t_tmp VALUES (indicator.id, double(NULL), int(NULL), double(NULL), double(NULL), double(NULL),
-	                              double(NULL), double(NULL), double(NULL), double(NULL), double(NULL), double(NULL),
-	                              double(NULL), int(NULL), double(NULL), double(NULL), double(NULL),
-	                              double(NULL), double(NULL), double(NULL), double(NULL), double(NULL), double(NULL)); 
-	
-	    t_ranking_num = sql(select = (sqlCol('end_date'), sqlCol(ranking_by), sqlCol('raise_type', mean, 'raise_type'), sqlCol('indicator_id', mean,'indicator_id'),
-	                                  sqlCol('avg_1m', mean, 'avg_1m'), sqlCol('avg_1m_cnt', mean, 'avg_1m_cnt'),
-	                                  sqlCol('perrank_percent_5_1m', mean, 'perrank_percent_5_1m'),
-	                                  sqlCol('perrank_percent_10_1m', mean, 'perrank_percent_10_1m'),
-	                                  sqlCol('perrank_percent_25_1m', mean, 'perrank_percent_25_1m'),
-	                                  sqlCol('perrank_percent_50_1m', mean, 'perrank_percent_50_1m'),
-	                                  sqlCol('perrank_percent_75_1m', mean, 'perrank_percent_75_1m'),
-	                                  sqlCol('perrank_percent_90_1m', mean, 'perrank_percent_90_1m'),
-	                                  sqlCol('perrank_percent_95_1m', mean, 'perrank_percent_95_1m'), 
-	                                  sqlCol('best_1m', mean, 'best_1m'), sqlCol('worst_1m', mean, 'worst_1m'),
-	                                  sqlCol('avg_3m', mean, 'avg_3m'), sqlCol('avg_3m_cnt', mean, 'avg_3m_cnt'),
-	                                  sqlCol('perrank_percent_5_3m', mean, 'perrank_percent_5_3m'),
-	                                  sqlCol('perrank_percent_10_3m', mean, 'perrank_percent_10_3m'),
-	                                  sqlCol('perrank_percent_25_3m', mean, 'perrank_percent_25_3m'),
-	                                  sqlCol('perrank_percent_50_3m', mean, 'perrank_percent_50_3m'),
-	                                  sqlCol('perrank_percent_75_3m', mean, 'perrank_percent_75_3m'),
-	                                  sqlCol('perrank_percent_90_3m', mean, 'perrank_percent_90_3m'),
-	                                  sqlCol('perrank_percent_95_3m', mean, 'perrank_percent_95_3m'), 
-	                                  sqlCol('best_3m', mean, 'best_3m'), sqlCol('worst_3m', mean, 'worst_3m'),
-	                                  // 与 MySQL 不同,这里统一把近4年和成立以来的排名去掉
-	                                  sqlCol(indicator.name + '_6m', mean, 'avg_6m'), sqlCol(indicator.name + '_6m', count, 'avg_6m_cnt'),
-	                                  sqlCol(indicator.name + '_6m', percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_6m'),
-	                                  sqlCol(indicator.name + '_6m', percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_6m'),
-	                                  sqlCol(indicator.name + '_6m', percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_6m'),
-	                                  sqlCol(indicator.name + '_6m', percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_6m'),
-	                                  sqlCol(indicator.name + '_6m', percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_6m'),
-	                                  sqlCol(indicator.name + '_6m', percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_6m'),
-	                                  sqlCol(indicator.name + '_6m', percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_6m'),
-	                                  sqlCol(indicator.name + '_6m', iif(indicator.is_ASC, min, max), 'best_6m'),
-	                                  sqlCol(indicator.name + '_6m', iif(indicator.is_ASC, max, min), 'worst_6m'),
-	                                  sqlCol(indicator.name + '_1y', mean, 'avg_1y'), sqlCol(indicator.name + '_1y', count, 'avg_1y_cnt'),
-	                                  sqlCol(indicator.name + '_1y', percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_1y'),
-	                                  sqlCol(indicator.name + '_1y', percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_1y'),
-	                                  sqlCol(indicator.name + '_1y', percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_1y'),
-	                                  sqlCol(indicator.name + '_1y', percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_1y'),
-	                                  sqlCol(indicator.name + '_1y', percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_1y'),
-	                                  sqlCol(indicator.name + '_1y', percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_1y'),
-	                                  sqlCol(indicator.name + '_1y', percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_1y'),
-	                                  sqlCol(indicator.name + '_1y', iif(indicator.is_ASC, min, max), 'best_1y'),
-	                                  sqlCol(indicator.name + '_1y', iif(indicator.is_ASC, max, min), 'worst_1y'),
-	                                  sqlCol(indicator.name + '_2y', mean, 'avg_2y'), sqlCol(indicator.name + '_2y', count, 'avg_2y_cnt'),
-	                                  sqlCol(indicator.name + '_2y', percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_2y'),
-	                                  sqlCol(indicator.name + '_2y', percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_2y'),
-	                                  sqlCol(indicator.name + '_2y', percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_2y'),
-	                                  sqlCol(indicator.name + '_2y', percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_2y'),
-	                                  sqlCol(indicator.name + '_2y', percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_2y'),
-	                                  sqlCol(indicator.name + '_2y', percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_2y'),
-	                                  sqlCol(indicator.name + '_2y', percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_2y'),
-	                                  sqlCol(indicator.name + '_2y', iif(indicator.is_ASC, min, max), 'best_2y'),
-	                                  sqlCol(indicator.name + '_2y', iif(indicator.is_ASC, max, min), 'worst_2y'),
-	                                  sqlCol(indicator.name + '_3y', mean, 'avg_3y'), sqlCol(indicator.name + '_3y', count, 'avg_3y_cnt'),
-	                                  sqlCol(indicator.name + '_3y', percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_3y'),
-	                                  sqlCol(indicator.name + '_3y', percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_3y'),
-	                                  sqlCol(indicator.name + '_3y', percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_3y'),
-	                                  sqlCol(indicator.name + '_3y', percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_3y'),
-	                                  sqlCol(indicator.name + '_3y', percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_3y'),
-	                                  sqlCol(indicator.name + '_3y', percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_3y'),
-	                                  sqlCol(indicator.name + '_3y', percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_3y'),
-	                                  sqlCol(indicator.name + '_3y', iif(indicator.is_ASC, min, max), 'best_3y'),
-	                                  sqlCol(indicator.name + '_3y', iif(indicator.is_ASC, max, min), 'worst_3y'),
-	                                  sqlCol(indicator.name + '_5y', mean, 'avg_5y'), sqlCol(indicator.name + '_5y', count, 'avg_5y_cnt'),
-	                                  sqlCol(indicator.name + '_5y', percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_5y'),
-	                                  sqlCol(indicator.name + '_5y', percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_5y'),
-	                                  sqlCol(indicator.name + '_5y', percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_5y'),
-	                                  sqlCol(indicator.name + '_5y', percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_5y'),
-	                                  sqlCol(indicator.name + '_5y', percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_5y'),
-	                                  sqlCol(indicator.name + '_5y', percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_5y'),
-	                                  sqlCol(indicator.name + '_5y', percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_5y'),
-	                                  sqlCol(indicator.name + '_5y', iif(indicator.is_ASC, min, max), 'best_5y'),
-	                                  sqlCol(indicator.name + '_5y', iif(indicator.is_ASC, max, min), 'worst_5y'),
-	                                  sqlCol(indicator.name + '_10y', mean, 'avg_10y'), sqlCol(indicator.name + '_10y', count, 'avg_10y_cnt'),
-	                                  sqlCol(indicator.name + '_10y', percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_10y'),
-	                                  sqlCol(indicator.name + '_10y', percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_10y'),
-	                                  sqlCol(indicator.name + '_10y', percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_10y'),
-	                                  sqlCol(indicator.name + '_10y', percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_10y'),
-	                                  sqlCol(indicator.name + '_10y', percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_10y'),
-	                                  sqlCol(indicator.name + '_10y', percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_10y'),
-	                                  sqlCol(indicator.name + '_10y', percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_10y'),
-	                                  sqlCol(indicator.name + '_10y', iif(indicator.is_ASC, min, max), 'best_10y'),
-	                                  sqlCol(indicator.name + '_10y', iif(indicator.is_ASC, max, min), 'worst_10y'),
-	                                  sqlCol(indicator.name + '_ytd', mean, 'avg_ytd'), sqlCol(indicator.name + '_ytd', count, 'avg_ytd_cnt'),
-	                                  sqlCol(indicator.name + '_ytd', percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', iif(indicator.is_ASC, min, max), 'best_ytd'),
-	                                  sqlCol(indicator.name + '_ytd', iif(indicator.is_ASC, max, min), 'worst_ytd')
-	                                  ),
-	                        from = cj(data_table, t_tmp),
-	                        where = <_$ranking_by IS NOT NULL>,
-	                        groupBy = (sqlCol(ranking_by), sqlCol('end_date')),
-	                        groupFlag = 1).eval(); // group by
-	}
-
-    return t_ranking, t_ranking_num;
-}
-
-
-/*
- *   动态生成用于排序的SQL脚本
- * 
- *   @param indicator_name <STRING>: 指标字段名
- *   @param indicator_id <INT>:指标ID
- *   @param is_ASC <BOOL>: 是否排正序
- *   @param ranking_by <STRING>: 'strategy', 'substrategy', 'factor_id', 'catavg'
- *   
- *   TODO: portfolio, cf, manager, company, 
- *   TODO: bfi & category
- * 
- */
-def gen_ranking_sql(data_table, indicator_table, ranking_by) {
+    ranking = create_entity_indicator_ranking();
+    ranking_num = create_entity_indicator_ranking_num();
 
 	for(indicator in indicator_table) {
 
@@ -416,13 +88,16 @@ def gen_ranking_sql(data_table, indicator_table, ranking_by) {
 	    // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
 	    if(indicator.id != 1) {
 
-	    	t_tmp = table(1000:0,
-	    	              ['indicator_' + v_missing_trailing, 'absrank_' + v_missing_trailing, 'perrank_' + v_missing_trailing].flatten(),
-	    	              [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(INT, v_missing_trailing.size())].flatten()
-	    	             );
-	    	
-	    	t_ranking = SELECT * FROM cj(t_ranking, t_tmp);
+	    	v_tmp_col = ['indicator_' + v_missing_trailing, 'absrank_' + v_missing_trailing, 'perrank_' + v_missing_trailing].flatten();
+	    	v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(INT, v_missing_trailing.size())].flatten();
+
+	    	t_ranking.addColumn(v_tmp_col, v_tmp_type);
+
 	    }
+		
+		t_ranking.reorderColumns!(ranking.colNames());
+        ranking.tableInsert(t_ranking);
+
 
 	    // 平均值、集合数量、各分位的阈值
         t_ranking_num = sql(select =(sqlCol(['end_date', 'category_id']),
@@ -447,174 +122,147 @@ def gen_ranking_sql(data_table, indicator_table, ranking_by) {
 	    // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
 	    if(indicator.id != 1) {
 
-	    	t_tmp = table(1000:0,
-	    	              ['avg_' + v_missing_trailing, 'avg_' + v_missing_trailing + '_cnt', 'perrank_percent_5_' + v_missing_trailing,
-	    	               'perrank_percent_10_' + v_missing_trailing, 'perrank_percent_25_' + v_missing_trailing,
-	    	               'perrank_percent_50_' + v_missing_trailing, 'perrank_percent_75_' + v_missing_trailing,
-	    	               'perrank_percent_90_' + v_missing_trailing, 'perrank_percent_95_' + v_missing_trailing,
-	    	               'best_' + v_missing_trailing, 'worst_' + v_missing_trailing ].flatten(),
-	    	              [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
-	    	               take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
-	    	               take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
-	    	               take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
-	    	               take(DOUBLE, v_missing_trailing.size()),take(DOUBLE, v_missing_trailing.size())].flatten()
-	    	             );
-	    	
-	    	t_ranking = SELECT * FROM cj(t_ranking, t_tmp);
+	    	v_tmp_col = ['avg_' + v_missing_trailing, 'avg_' + v_missing_trailing + '_cnt', 'perrank_percent_5_' + v_missing_trailing,
+	    	             'perrank_percent_10_' + v_missing_trailing, 'perrank_percent_25_' + v_missing_trailing,
+	    	             'perrank_percent_50_' + v_missing_trailing, 'perrank_percent_75_' + v_missing_trailing,
+	    	             'perrank_percent_90_' + v_missing_trailing, 'perrank_percent_95_' + v_missing_trailing,
+	    	             'best_' + v_missing_trailing, 'worst_' + v_missing_trailing
+	    	            ].flatten();
+	    	v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
+	    	              take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
+	    	              take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
+	    	              take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
+	    	              take(DOUBLE, v_missing_trailing.size()),take(DOUBLE, v_missing_trailing.size())
+	    	             ].flatten();
+
+	    	t_ranking_num.addColumn(v_tmp_col, v_tmp_type);
 	    }
+
+    	t_ranking_num.reorderColumns!(ranking_num.colNames());
+	    ranking_num.tableInsert(t_ranking_num);
 	}
 
-    return t_ranking, t_ranking_num;
+    return ranking, ranking_num;
 }
 
 /*
  *  运行排名SQL脚本
  * 
- *  NOTE: 没有用 parseExpr 来生成动态脚本的原因是数据表无法传入
+ *
  */
-def run_ranking_sql(data_table, indicator_table, mutable v_tables) {
+def run_ranking_sql(cal_type, mutable data_table, indicator_table) {
 
-    tb_strategy_ranking = gen_ranking_sql(data_table, indicator_table, 'strategy')[0];
-    v_tables[0].tableInsert(tb_strategy_ranking);
+// data_table = t
+// v_tables = v_ranking_tables
+// cal_type = 'strategy'
 
-    tb_strategy_ranking_num = gen_ranking_sql(data_table, indicator_table, 'strategy')[1];
-    v_tables[1].tableInsert(tb_strategy_ranking_num);
+    ret = array(ANY, 0);
 
-    tb_substrategy_ranking = gen_ranking_sql(data_table, indicator_table, 'substrategy')[0];
-    v_tables[2].tableInsert(tb_substrategy_ranking);
+	if(cal_type == 'bfi') {
 
-    tb_substrategy_ranking_num = gen_ranking_sql(data_table, indicator_table, 'substrategy')[1];
-    v_tables[3].tableInsert(tb_substrategy_ranking_num);
+        UPDATE data_table SET category_id = factor_id;
 
-}
+		v_ranking = gen_ranking_sql(data_table, indicator_table);
+		
+	    ret.append!(v_ranking[0]); // ranking table
+		ret.append!(v_ranking[1]); // ranking_num table
+		
+	} else {
 
-/*
- *   计算风险指标排名
- * 
- * 
- */
-def cal_risk_ranking(entity_type, entity_info, end_date, isFromMySQL) {
+        // 策略排名
+        UPDATE data_table SET category_id = strategy$STRING;
 
-    // 当前只对基金做排名, 其它类型参考基金排名做相对排名
-    if(!(entity_type in ['MF', 'HF'])) return null;
+		v_ranking = gen_ranking_sql(data_table, indicator_table);
 
-	table_desc = get_risk_stats_table_description(entity_type);
+	    ret.append!(v_ranking[0]); // ranking table
+		ret.append!(v_ranking[1]); // ranking_num table
 
-	tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data.rename!(sec_id_col, 'entity_id');
-
-	t = SELECT *
-        FROM entity_info en 
-        INNER JOIN tb_data d ON en.entity_id = d.entity_id
-	    WHERE en.strategy IS NOT NULL;
-    
-    // 按照 MySQL 字段建表
-    t_s = create_entity_indicator_ranking(false);
-    t_s_num = create_entity_indicator_ranking_num(false);
-    t_ss = create_entity_indicator_substrategy_ranking(false);
-    t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
-
-    v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
+        // 子策略排名
+		UPDATE data_table SET category_id = substrategy$STRING;
 
-    // 50, 52 dolphin 未计算
-    indicator_table = SELECT * FROM get_indicator_info() WHERE id in [2, 6, 9, 10, 11, 12, 21, 59];
-
-    run_ranking_sql(t, indicator_table, v_ranking_tables);
+	    v_ranking = gen_ranking_sql(data_table, indicator_table);
+	    
+	    ret.append!(v_ranking[0]); // ranking table
+		ret.append!(v_ranking[1]); // ranking_num table
+	}
 
-    return v_ranking_tables;
+	return ret;
 }
 
 
 /*
- *   计算风险调整收益指标排名
- * 
+ *   通用指标排名计算
+ *   
+ *   @param cal_type <STRING>: strategy, bfi
  * 
  */
-def cal_risk_adj_return_ranking(entity_type, entity_info, end_date, isFromMySQL) {
+def cal_indicator_ranking(cal_type, entity_type, entity_info, end_date, isFromMySQL) {
 
     // 当前只对基金做排名, 其它类型参考基金排名做相对排名
     if(!(entity_type in ['MF', 'HF'])) return null;
 
+	// return
+	table_desc = get_performance_table_description(entity_type);
+	tb_data_return = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
+	// risk
+	table_desc = get_risk_stats_table_description(entity_type);
+	tb_data_risk_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
+	// risk adjusted return
 	table_desc = get_riskadjret_stats_table_description(entity_type);
-
-	tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data.rename!(sec_id_col, 'entity_id');
-
-	t = SELECT *
-        FROM entity_info en 
-        INNER JOIN tb_data d ON en.entity_id = d.entity_id
-	    WHERE en.strategy IS NOT NULL;
-
-    // 按照 MySQL 字段建表
-    t_s = create_entity_indicator_ranking(false);
-    t_s_num = create_entity_indicator_ranking_num(false);
-    t_ss = create_entity_indicator_substrategy_ranking(false);
-    t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
-
-    v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
-
-    // 19 (MAR Sortino ratio) dolphin 未计算
-    indicator_table = SELECT * FROM get_indicator_info() WHERE id in [14, 15, 16, 17, 18, 40, 58];
-
-    run_ranking_sql(t, indicator_table, v_ranking_tables);
-
-    return v_ranking_tables;
-}
-
-
-/*
- *   计算杂项指标排名
- * 
- * 
- */
-def cal_other_indicator_ranking(entity_type, entity_info, end_date, isFromMySQL) {
-
-    // 当前只对基金做排名, 其它类型参考基金排名做相对排名
-    if(!(entity_type in ['MF', 'HF'])) return null;
-
+	tb_data_riskadjret_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
+	// others
 	table_desc = get_indicator_table_description(entity_type);
+	tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
 
-	tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data.rename!(sec_id_col, 'entity_id');
+	tb_data = SELECT * 
+	          FROM tb_data_return d1
+	          LEFT JOIN tb_data_indicator_stats d2 ON d1.fund_id = d2.fund_id AND d1.end_date = d2.end_date
+	          LEFT JOIN tb_data_risk_stats d3 ON d1.fund_id = d3.fund_id AND d1.end_date = d3.end_date
+	          LEFT JOIN tb_data_riskadjret_stats d4 ON d1.fund_id = d4.fund_id AND d1.end_date = d4.end_date;
 
-	t = SELECT *
-        FROM entity_info en 
-        INNER JOIN tb_data d ON en.entity_id = d.entity_id
-	    WHERE en.strategy IS NOT NULL;
+	if(cal_type == 'bfi') {
 
-    // 按照 MySQL 字段建表
-    t_s = create_entity_indicator_ranking(false);
-    t_s_num = create_entity_indicator_ranking_num(false);
-    t_ss = create_entity_indicator_substrategy_ranking(false);
-    t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
+		// bfi (as benchmark) indicator
+		table_desc = get_bfi_indicator_table_description(entity_type);
+		tb_data_bfi_indicator = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
 
-    v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
+        // 去掉被移到 fund_ty_bfi_bm_indicator 表中的重复字段
+        v_dups = [38, 48, 11, 12, 59, 16];
+        v_dup_col = EXEC name + suffix
+                    FROM cj(get_indicator_info(), table(['_6m', '_1y', '_2y', '_3y', '_5y', '_10y', '_ytd'] AS suffix))
+                    WHERE id IN v_dups;
+        
+        tb_data.dropColumns!(v_dup_col);
 
-    // 37 (per_con), 43, 44, 45, 46, 47 (smdd模型) dolphin 未计算
-    indicator_table = SELECT * FROM get_indicator_info() WHERE id in [38, 41, 42, 48, 49];
+		tb_data = SELECT * FROM tb_data d1
+		          LEFT JOIN tb_data_bfi_indicator d2 ON d1.fund_id = d2.fund_id AND d1.end_date = d2.end_date;
 
-    run_ranking_sql(t, indicator_table, v_ranking_tables);
+    	v_indicator_id = [1,                                       // 对应 fund_performance, 取消39(年化收益) 因为没有意义
+                	      41, 42, 49,                              // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
+        	              2, 6, 9, 10, 21,                         // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
+            	          14, 15, 17, 18, 40, 58,                  // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
+    					  11, 12, 16, 33, 34, 35, 36, 38, 48, 59   // 对应 fund_ty_bfi_bm_indicator
+                    	 ];                                        // 取消 pf_fund_factor_stability 66 (stabiliy) 因为 dolphin 未计算
+                    	                                           // 取消 fund_rbsa_style 53, 54, 55, 56, 57(风格稳定性) 因为 dolphin 未计算
 
-    return v_ranking_tables;
-}
+	} else {
 
+		// upside/downside capture
+		table_desc = get_capture_style_table_description(entity_type);
+		tb_data_capture_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
 
-/*
- *   计算上下行指标排名
- * 
- * 
- */
-def cal_capture_style_ranking(entity_type, entity_info, end_date, isFromMySQL) {
+		tb_data = SELECT * FROM tb_data d1
+		          LEFT JOIN tb_data_capture_stats d2 ON d1.fund_id = d2.fund_id AND d1.end_date = d2.end_date;
 
-    // 当前只对基金做排名, 其它类型参考基金排名做相对排名
-    if(!(entity_type in ['MF', 'HF'])) return null;
+    	v_indicator_id = [1,                                // 对应 fund_performance, 取消39(年化收益) 因为没有意义
+    					  38, 41, 42, 48, 49,               // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
+        	              2, 6, 9, 10, 11, 12, 21, 59,      // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
+            	          14, 15, 16, 17, 18, 40, 58,       // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
+                	      33, 34, 35, 36                    // 对应 fund_style_stats 
+                    	 ];
 
-	table_desc = get_capture_style_table_description(entity_type);
+    }
 
-	tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
 	sec_id_col = table_desc.sec_id_col[0];
 	tb_data.rename!(sec_id_col, 'entity_id');
 
@@ -622,82 +270,15 @@ def cal_capture_style_ranking(entity_type, entity_info, end_date, isFromMySQL) {
         FROM entity_info en 
         INNER JOIN tb_data d ON en.entity_id = d.entity_id
 	    WHERE en.strategy IS NOT NULL;
-
-    // 按照 MySQL 字段建表
-    t_s = create_entity_indicator_ranking(false);
-    t_s_num = create_entity_indicator_ranking_num(false);
-    t_ss = create_entity_indicator_substrategy_ranking(false);
-    t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
-
-    v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
-
-    indicator_table = SELECT * FROM get_indicator_info() WHERE id in [33, 34, 35, 36];
-
-    run_ranking_sql(t, indicator_table, v_ranking_tables);
-
-    return v_ranking_tables;
-}
-
-
-/*
- *   计算BFI指标排名
- * 
- *   TODO: return
- */
-def cal_bfi_indicator_ranking(entity_type, entity_info, end_date, isFromMySQL) {
-
-    // 当前只对基金做排名, 其它类型参考基金排名做相对排名
-    if(!(entity_type in ['MF', 'HF'])) return null;
-
-	table_desc = get_bfi_indicator_table_description(entity_type);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data_bfi_indicator = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	tb_data_bfi_indicator.rename!(sec_id_col, 'entity_id');
-
-	table_desc = get_risk_stats_table_description(entity_type);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data_risk_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	tb_data_risk_stats.rename!(sec_id_col, 'entity_id');
-
-	table_desc = get_riskadjret_stats_table_description(entity_type);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data_riskadjret_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	tb_data_riskadjret_stats.rename!(sec_id_col, 'entity_id');
-	
-	table_desc = get_indicator_table_description(entity_type);
-	sec_id_col = table_desc.sec_id_col[0];
-	tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
-	tb_data_indicator_stats.rename!(sec_id_col, 'entity_id');
-
-	t = SELECT *
-        FROM entity_info en 
-        INNER JOIN tb_data_bfi_indicator d2 ON en.entity_id = d2.entity_id
-        INNER JOIN tb_data_risk_stats d3 ON en.entity_id = d3.entity_id
-        INNER JOIN tb_data_riskadjret_stats d4 ON en.entity_id = d4.entity_id
-        INNER JOIN tb_data_indicator_stats d5 ON en.entity_id = d5.entity_id
-	    WHERE en.strategy IS NOT NULL;
     
-    // 按照 MySQL 字段建表
-    t_s = create_entity_indicator_ranking(false);
-    t_s_num = create_entity_indicator_ranking_num(false);
-    t_ss = create_entity_indicator_substrategy_ranking(false);
-    t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
+    indicator_table = SELECT * FROM get_indicator_info() WHERE id IN v_indicator_id;
 
-    v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
-
-    // 取消 39, 53, 54, 55, 57, 57
-    v_indicator_id = [11, 12, 16, 33, 34, 35, 36, 38, 48, 59,
-                      2, 6, 9, 10, 21, 50, 52,
-                      14, 15, 16, 17, 18, 19, 58, 21, 40,
-                      37, 41, 42, 43, 44, 45, 46, 47, 49
-                     ];
-    indicator_table = SELECT * FROM get_indicator_info() WHERE id in v_indicator_id;
-
-    run_ranking_sql(t, indicator_table, v_ranking_tables);
+    v_ranking_tables = run_ranking_sql(cal_type, t, indicator_table);
 
     return v_ranking_tables;
 }
 
+
 /*
  *   将源指标表横表变竖表,以方便排名计算
  * 
@@ -801,7 +382,7 @@ def transform_return_for_ranking (entity_type, entity_info, end_date, ranking_by
 /*
  *   将源风险指标表横表变竖表,以方便排名计算
  * 
- *   TODO: 一直缺 portfolio bfi indicator 计算!mysql 里的 pf_fund_bfi_bm_indicator_ranking 是错的... 
+ *
  */
 def transform_risk_stats_for_ranking (entity_type, entity_info, end_date, ranking_by, isFromMySQL=true) {
 
@@ -990,40 +571,52 @@ def cal_relative_ranking(benchmark_ranking, mutable entity_ranking, isFromMySQL=
 /*
  *  排名数据入库
  * 
- *  @param ranking_tables <VECTOR>: 包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
+ *  @param cal_type
+ *  @param ranking_tables <VECTOR>: 当 cal_type = 'strategy' 时包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
+ *                                     cal_type = 'bfi' 时包含2个数据表的向量,分别是bfi策略排名,bfi策略排名阈值
  */
-def save_ranking_tables(entity_type, ranking_tables) {
-
+def save_ranking_tables(cal_type, ranking_tables) {
+//cal_type = 'bfi'
+//ranking_tables=v_ranking_tables
     if(ranking_tables.isVoid()) return;
 
-    source_table = '';
-    target_table = '';
-
-    if(entity_type IN ['MF', 'HF']) {
+    entity_id_col = 'fund_id';
+    
+	if(cal_type == 'bfi') {
+
+	  	source_table = 'raw_db.pf_fund_bfi_bm_indicator_ranking';
+   		target_table = 'raw_db.pf_fund_bfi_bm_indicator_ranking';
+   		category_id_col = 'factor_id';
+		
+	} else {
+	  	source_table = 'raw_db.pf_fund_indicator_ranking';
+   		target_table = 'raw_db.pf_fund_indicator_ranking';
+   		category_id_col = 'strategy';
+	}
 
-        entity_id_col = 'fund_id';
-    	source_table = 'raw_db.pf_fund_indicator_ranking';
-    	target_table = 'raw_db.pf_fund_indicator_ranking'
-    	
-    }
+    t = ranking_tables[0];
+    save_and_sync(t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]), source_table, target_table);
 
-    ranking_tables[0].rename!('entity_id', entity_id_col);
-    save_and_sync(ranking_tables[0], source_table, target_table);
+    t = ranking_tables[1];
+    save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
 
-    save_and_sync(ranking_tables[1], source_table + '_num', target_table + '_num');
-    
-    source_table = source_table.strReplace('_ranking', '_substrategy_ranking');
-    target_table = target_table.strReplace('_ranking', '_substrategy_ranking');
+    if(cal_type == 'strategy') {
 
-    ranking_tables[2].rename!('entity_id', entity_id_col);
-    save_and_sync(ranking_tables[2], source_table, target_table);
+	    source_table = source_table.strReplace('_ranking', '_substrategy_ranking');
+	    target_table = target_table.strReplace('_ranking', '_substrategy_ranking');
+	    category_id_col = 'substrategy';
+	
+	    t = ranking_tables[2];
+    	save_and_sync(t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]), source_table, target_table);
 
-    save_and_sync(ranking_tables[3], source_table + '_num', target_table + '_num');
+		t = ranking_tables[3];
+	    save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
+    }
 	
 }
 
 /*
- *  参考排名数据入库AND a.indicator_id NOT IN (50, 52, 59, 46)
+ *  参考排名数据入库
 
  * 
  *  @param ranking_tables <TABLE>: 

+ 27 - 25
modules/task_monthlyPerformance.dos

@@ -6,43 +6,45 @@ use fundit::bfiMatcher;
 use fundit::rankingCalculator;
 
 /*
- *   计算基金排名并存入数据库
+ *   [定时任务] 计算基金一、二级分类排名并存入数据库
  * 
- *   @param entity_type <STRING>: 目前传入'MF'和'HF'都可以将公私募同时跑
- *   @param end_date <MONTH>: XXXX.YYM
+ *   @param entity_type <STRING>: 'MF', 'HF'
+ *   @param end_date <MONTH>:
  *   @param isFromMySQL <BOOL>: false 时读取dolphin本地的收益及指标表,用于初始化数据
  *   
- *   NOTE: 在假设公募和私募分类互相独立成立的前提下,之用跑一次即可计算公募和私募的排名
- *   TODO: BFI ranking
  * 
- *   Example: cal_entity_ranking('MF', 2024.09M, true);
+ *   Example: CalEntityRankingTask('MF', 2024.09M, true);
  */
-def CalEntityRanking(entity_type, end_date, isFromMySQL=true) {
-/*
-entity_type = 'MF';
-end_date = 2024.09M;
-isFromMySQL = true;
-*/
+def CalEntityRankingTask(entity_type, end_date, isFromMySQL=true) {
+
+	if(!(entity_type in ['MF', 'HF'])) return NULL;
+	
 	entity_info = get_entity_info(entity_type, NULL);
 
-	// 收益
-	v_ranking_tables = cal_ret_ranking(entity_type, entity_info, end_date, isFromMySQL);
-    save_ranking_tables(entity_type, v_ranking_tables);
+	v_ranking_tables = cal_indicator_ranking('strategy', entity_type, entity_info, end_date, isFromMySQL);
 
-    // 风险相关的指标
-    v_ranking_tables = cal_risk_ranking(entity_type, entity_info, end_date, isFromMySQL);
     save_ranking_tables(entity_type, v_ranking_tables);
 
-    // 风险调整收益指标
-    v_ranking_tables = cal_risk_adj_return_ranking(entity_type, entity_info, end_date, isFromMySQL);
-    save_ranking_tables(entity_type, v_ranking_tables);
+}
 
-    // 杂项指标
-    v_ranking_tables = cal_other_indicator_ranking(entity_type, entity_info, end_date, isFromMySQL);
-    save_ranking_tables(entity_type, v_ranking_tables);
+/*
+ *   [定时任务] 计算基金BFI排名并存入数据库
+ * 
+ *   @param entity_type <STRING>: 'MF', 'HF'
+ *   @param end_date <MONTH>:
+ *   @param isFromMySQL <BOOL>: false 时读取dolphin本地的收益及指标表,用于初始化数据
+ *   
+ * 
+ *   Example: CalEntityBfiRankingTask('MF', 2024.09M, true);
+ */
+def CalEntityBfiRankingTask(entity_type, end_date, isFromMySQL=true) {
+
+	if(!(entity_type in ['MF', 'HF'])) return NULL;
+	
+	entity_info = get_entity_info(entity_type, NULL);
 
-    // 上下行捕获率排名
-    v_ranking_tables = cal_capture_style_ranking(entity_type, entity_info, end_date, isFromMySQL);
+	v_ranking_tables = cal_indicator_ranking('bfi', entity_type, entity_info, end_date, isFromMySQL);
+		
     save_ranking_tables(entity_type, v_ranking_tables);
 
 }