Browse Source

支持市场指数的各种计算

Joey 5 months ago
parent
commit
34acfa8fce

+ 46 - 4
modules/dataPuller.dos

@@ -355,6 +355,34 @@ def get_fund_info(fund_ids) {
 }
 
 /*
+ * 取有效指数基本信息
+ *
+ * Example: get_index_info("'IN00000008','IN000002GE'");
+ * 
+ */
+def get_index_info(index_ids) {
+
+    s_entity_ids = ids_to_string(index_ids);
+    
+    if(s_entity_ids == NULL || s_entity_ids == '') return null;
+
+    s_query = "SELECT fi.index_id, fi.inception_date, NULL AS benchmark_id, IFNULL(fi.index_initial_value, 1) AS ini_value, fi.index_code, fi.index_type_id
+               FROM mfdb.indexes_profile fi
+               WHERE fi.index_id IN (" + s_entity_ids + ")
+               AND fi.isvalid = 1
+               ORDER BY fi.index_id"
+
+    conn = connect_mysql()
+
+    t = odbc::query(conn, s_query)
+
+    conn.close()
+
+    return t
+
+}
+
+/*
  *  取组合有效信息
  * 
  *  Example: get_portfolio_info('166002,166114');
@@ -400,14 +428,18 @@ def get_entity_info(entity_type, entity_ids) {
     if(entity_type == 'MF' || entity_type == 'HF') {
 
     	t = get_fund_info(s_entity_ids);
-
         t.rename!('fund_id', 'entity_id');
 
     } else if(entity_type == 'PF') {
 
     	t = get_portfolio_info(s_entity_ids);
-
         t.rename!('portfolio_id', 'entity_id');    
+
+    } else if(entity_type IN ['MI', 'FI']) {
+
+    	t = get_index_info(s_entity_ids);
+        t.rename!('index_id', 'entity_id');    
+   	
     }
 
 	return t;
@@ -550,13 +582,17 @@ def get_portfolio_primary_benchmark(portfolio_ids, month_start, month_end) {
 /*
  *  取某时间段的基金组合主基准
  *
+ *  NOTE: 指数和因子的”主基准”设置为沪深300
  * 
  *  Example: get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  *           get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
+ *           get_entity_primary_benchmark('MI', ['IN00000008', 'IN0000000M'], '2024-07', '2024-08');
  */
 def get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
 
-    t = null;
+    t = table(100:0,
+             ['entity_id', 'end_date', 'benchmark_id'],
+             [iif(entity_type == 'PF', INT, SYMBOL), MONTH, SYMBOL]);
 
     s_entity_ids = ids_to_string(entity_ids);
 
@@ -572,7 +608,13 @@ def get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end
 
     	t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
 
-        t.rename!('portfolio_id', 'entity_id');    
+        t.rename!('portfolio_id', 'entity_id');
+
+    } else if(entity_type IN ['MI', 'FI', 'FA', 'CI', 'EQ']) {
+        // 对于指数、因子来说,没有什么基准。但为了指标计算不得不在这里设个假的
+    	t = SELECT entity_id, end_date, 'IN00000008' AS benchmark_id
+    	    FROM cj(get_entity_info(entity_type, s_entity_ids), table(temporalParse(month_start, 'yyyy-MM')..temporalParse(month_end, 'yyyy-MM') AS end_date))
+    	    WHERE end_date >= iif(inception_date.isNull(), 1990.01M, inception_date.month())
     }
 
 	return t;

+ 2 - 1
modules/dataSaver.dos

@@ -5,7 +5,7 @@ use fundit::sqlUtilities
 
 
 /*
- * 存数据表到mySQL或本地dolphindb,原数据会被替代!
+ * 存数据表到mySQL或本地dolphindb,原数据表会被TRUNCATE
  *
  * save_table(tb_fund_performance, "raw_db.fund_performance", false)
  */
@@ -61,6 +61,7 @@ def save_hedge_fund_nav_to_local(tb_nav) {
 
 /*
  *  将数据存回MySQL并同步至正式表
+ *  
  * 
  */
 def save_and_sync(table, source_table_name, target_table_name) {

+ 1 - 1
modules/indicatorCalculator.dos

@@ -1004,7 +1004,7 @@ def cal_trailing_bfi_indicators(entity_info, benchmarks, end_day, tb_ret, bmk_re
  */
 def cal_monthly_indicators(entity_type, indicator_type, monthly_returns) {
 
-    if(find(['MF', 'HF', 'PF'], entity_type) < 0) return null;
+    if(find(['MF', 'HF', 'PF', 'MI', 'FI'], entity_type) < 0) return null;
 
     if(monthly_returns.isVoid() || monthly_returns.size() < 1) return null;
 

+ 1 - 2
modules/returnCalculator.dos

@@ -183,8 +183,7 @@ def cal_weekly_returns(entity_type, entity_info){
 
     tb_rets_1w = null;
 
-    // 暂时只支持公私募基金和组合
-    if(!(entity_type IN ['HF', 'MF', 'PF'])) return tb_rets_1w;
+    if(!(entity_type IN ['HF', 'MF', 'PF', 'MI', 'FI'])) return tb_rets_1w;
     
     // 将每支证券ID+某个日期转为JSON用于调用sp
     s_json = (SELECT entity_id AS sec_id, price_date FROM entity_info).toStdJson();

+ 5 - 3
modules/task_portfolioPerformance.dos

@@ -19,7 +19,7 @@ use fundit::indicatorCalculator;
  *            calPortfolioNAV([143109, 145041], 2024.10.28);
  */
 def calPortfolioNAV(portfolio_ids, updatetime=1900.01.01) {
-// portfolio_ids=[143109, 145041];
+// portfolio_ids=[364743, 364744];
 // updatetime=1900.01.01;
     port_info = get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, true);
     
@@ -62,7 +62,7 @@ def calPortfolioPerformance(navs) {
 /*
  *   计算组合净值并存入数据库
  * 
- * 
+ *   TODO: release 时改变同步目标表为正式表
  */
 def cal_and_save_portfolio_nav(cal_portfolio_info) {
 
@@ -115,7 +115,7 @@ def cal_and_save_portfolio_nav(cal_portfolio_info) {
 /*
  *  计算组合标准指标并存入数据库
  * 
- * 
+ *  TODO: release 时改变同步目标表为正式表
  */
 def cal_and_save_portfolio_indicators(cal_portfolio_info) {
 
@@ -248,4 +248,6 @@ def CalPortfolioPerformanceTask(updatetime=1900.01.01) {
     rt = cal_and_save_portfolio_nav(tb_cal_ports);
     // 9 min
     rt = rt + '; ' + cal_and_save_portfolio_indicators(tb_cal_ports);
+
+    return rt;
 }