瀏覽代碼

小修小补

Joey 2 月之前
父節點
當前提交
e229d30377

+ 4 - 4
modules/dataSaver.dos

@@ -592,13 +592,13 @@ def create_entity_indicator_ranking(is_id_integer=false) {
 def create_mc_indicator_ranking(is_id_integer=false) {
 
     return table(1000:0, 
-                ['entity_id', 'curve_type', 'category_id', 'end_date', 'indicator_id',
+                ['entity_id', 'curve_type', 'strategy', 'category_id', 'end_date', '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'],
-                [SYMBOL, INT, SYMBOL, STRING, INT,
+                [SYMBOL, INT, INT, SYMBOL, STRING, INT,
                  DOUBLE, INT, INT, DOUBLE, INT, INT,
                  DOUBLE, INT, INT, DOUBLE, INT, INT,
                  DOUBLE, INT, INT, DOUBLE, INT, INT,
@@ -660,7 +660,7 @@ def create_entity_indicator_ranking_num() {
 def create_mc_indicator_ranking_num() {
 
     return table(1000:0, 
-                ['curve_type', 'category_id', 'end_date', 'indicator_id',
+                ['curve_type', 'strategy', 'category_id', 'end_date', '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',
@@ -679,7 +679,7 @@ def create_mc_indicator_ranking_num() {
                  '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'],
-                [INT, SYMBOL, STRING, INT,
+                [INT, INT, SYMBOL, STRING, INT,
                  DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
                  DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
                  DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,

+ 29 - 9
modules/rankingCalculator.dos

@@ -66,7 +66,7 @@ def gen_ranking_sql(entity_type, data_table, indicator_table) {
     ranking_num = iif(entity_type IN ['PL', 'CO'], create_mc_indicator_ranking_num(), create_entity_indicator_ranking_num());
 
     if(entity_type IN ['PL', 'CO'])
-    	v_groupby = ['curve_type', 'category_id', 'end_date'];
+    	v_groupby = ['curve_type', 'strategy', 'category_id', 'end_date'];
     else
     	v_groupby = ['category_id', 'end_date'];
 
@@ -223,10 +223,13 @@ def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isF
 	tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
 
     // 做个大宽表
-    if(entity_type IN ['PL', 'CO'])
+    if(entity_type IN ['PL', 'CO']) {
     	matchingCols = [entity_id_name, 'curve_type', 'strategy', 'end_date'];
-    else
+    	matchingCols2 = [entity_id_name, 'curve_type', 'strategy', 'end_date', 'factor_id'];
+    } else {
 		matchingCols = [entity_id_name, 'end_date'];
+		matchingCols2 = [entity_id_name, 'end_date', 'factor_id'];
+    }
 	tb_data = lj(lj(lj(tb_data_return, tb_data_indicator_stats, matchingCols), tb_data_risk_stats, matchingCols), tb_data_riskadjret_stats, matchingCols);
 
 	if(ranking_by == 'bfi') {
@@ -247,7 +250,6 @@ def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isF
 		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);
 
-		matchingCols2 = [entity_id_name, 'end_date', 'factor_id'];
 		tb_data = lj(ej(tb_data, tb_bfi, matchingCols), tb_data_bfi_indicator, matchingCols2);
 
     	v_indicator_id = [1,                                       // 对应 fund_performance, 取消39(年化收益) 因为没有意义
@@ -459,28 +461,46 @@ def save_ranking_tables(entity_type, ranking_by, ranking_tables) {
     if(ranking_tables.isVoid()) return;
 
 	des_strategy = get_indicator_ranking_table_description(entity_type)[0];
-	des_bfi = get_indicator_ranking_table_description(entity_type)[0];
+	des_bfi = get_bfi_bm_indicator_ranking_table_description(entity_type)[0];
 
     entity_id_col = des_strategy.sec_id_col;
-    
+
+    t = ranking_tables[0];
 	if(ranking_by == 'bfi') {
 
 	  	source_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
    		target_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
    		category_id_col = 'factor_id';
+   		t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]);
 		
 	} else {
 	  	source_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
    		target_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
    		category_id_col = 'strategy';
+
+   		// 基金经理和公司自带 strategy 字段,而 category_id 就是 strategy 的复制,所以要去掉
+   		// 基金和组合则没有这个字段,将 category_id 改名改回 strategy
+   		if(entity_type IN ['PL', 'CO'])
+			t.rename!('entity_id', entity_id_col).dropColumns!('category_id');
+		else
+			t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]);
 	}
 
-    t = ranking_tables[0];
-    save_and_sync(t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]), source_table, target_table);
+    save_and_sync(t, source_table, target_table);
 
     t = ranking_tables[1];
-    save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
+	if(ranking_by == 'bfi') {
+   		t.rename!('category_id', category_id_col);
+	} else {
+		if(entity_type IN ['PL', 'CO'])
+			t.dropColumns!('category_id');
+		else
+			t.rename!('category_id', category_id_col);
+	}
+
+    save_and_sync(t, source_table + '_num', target_table + '_num');
 
+	// 基金有二级策略排名
     if(ranking_by == 'strategy' && entity_type IN ['HF', 'MF']) {
 
 	    source_table = source_table.strReplace('_ranking', '_substrategy_ranking');

+ 5 - 5
modules/sqlUtilities.dos

@@ -306,7 +306,7 @@ def get_capture_style_table_description(entity_type) {
 /*
  *  根据不同类型的主体返回其BFI指标的表名、字段名
  * 
- *  Example: get_risk_stats_table_description('HF');
+ *  Example: get_bfi_indicator_table_description('PL');
  */
 def get_bfi_indicator_table_description(entity_type) {
 
@@ -314,11 +314,11 @@ def get_bfi_indicator_table_description(entity_type) {
                          ['type', 'table_name', 'sec_id_col'],
                          [STRING, STRING, STRING]);
 
-    // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合,基金经理
+    // 分别对应:私募,公募,组合,基金经理
     INSERT INTO tmp_universe VALUES ( 
-        ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF', 'PL'],
-        ['mfdb.fund_ty_bfi_bm_indicator', 'mfdb.fund_ty_bfi_bm_indicator', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_ty_bfi_bm_indicator'],
-        ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id', 'manager_id'] );
+        ['HF', 'MF', 'PF', 'PL'],
+        ['mfdb.fund_ty_bfi_bm_indicator', 'mfdb.fund_ty_bfi_bm_indicator', 'pfdb.pf_portfolio_ty_bfi_bm_indicator', 'mfdb.manager_ty_bfi_bm_indicator'],
+        ['fund_id', 'fund_id', 'portfolio_id', 'manager_id'] );
 
     return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
 

+ 12 - 11
modules/task_monthlyPerformance.dos

@@ -37,22 +37,23 @@ def CalEntityRankingTask(entityType, endDate, isFromMySQL=true) {
 /*
  *   [定时任务] 计算基金BFI排名并存入数据库
  * 
- *   @param entity_type <STRING>: 'MF', 'HF' (MF=HF)
- *   @param end_date <MONTH>:
+ *   @param entityType <STRING>: 'MF', 'HF' (MF=HF), 'PL'
+ *   @param endDate <MONTH>:
  *   @param isFromMySQL <BOOL>: false 时读取dolphin本地的收益及指标表,用于初始化数据
  *   
  * 
  *   Example: CalEntityBfiRankingTask('MF', 2024.09M, true);
+ *            CalEntityBfiRankingTask('PL', 2024.10M, true);
  */
-def CalEntityBfiRankingTask(entity_type, end_date, isFromMySQL=true) {
+def CalEntityBfiRankingTask(entityType, endDate, isFromMySQL=true) {
 
-	if(!(entity_type in ['MF', 'HF'])) return NULL;
+	if(!(entityType in ['MF', 'HF', 'PL'])) return NULL;
 	
-	entity_info = get_entity_info(entity_type, NULL);
-
-	v_ranking_tables = cal_indicator_ranking('bfi', entity_type, entity_info, end_date, isFromMySQL);
-		
-    save_ranking_tables(entity_type, v_ranking_tables);
+	entity_info = get_entity_info(entityType, NULL);
+	// 16 sec
+	v_ranking_tables = cal_indicator_ranking('bfi', entityType, entity_info, endDate, isFromMySQL);
+	// 39 sec
+    save_ranking_tables(entityType, 'bfi', v_ranking_tables);
 
 }
 
@@ -308,7 +309,7 @@ def cal_and_save_mc_indicator(entity_type, entity_date, monthly_returns, indicat
 	        } catch(ex) {
 	
 	            //TODO: Log errors
-	            rt += ex + '\n';
+	            rt += ex;
 	        }
 	    }
     } else {
@@ -515,7 +516,7 @@ def MatchManagerBFITask(updatetime) {
 			
 		} catch (ex) {
             //TODO: Log errors
-            rt += ex + '\n';
+            rt += ex;
 		}
 
 		i += batch_size

+ 1 - 1
modules/task_weeklyPerformnce.dos

@@ -184,7 +184,7 @@ def MatchEntityBFITask(entityType, date) {
 		} catch (ex) {
 
             //TODO: Log errors
-            rt += ex + '\n';
+            rt += ex;
         }
 	    
 	    i = i + batch_size;