Browse Source

小修小补

Joey 1 month ago
parent
commit
9a08af1d57
3 changed files with 20 additions and 16 deletions
  1. 2 2
      modules/dataSaver.dos
  2. 14 10
      modules/navCalculator.dos
  3. 4 4
      modules/task_weeklyPerformnce.dos

+ 2 - 2
modules/dataSaver.dos

@@ -99,8 +99,8 @@ def create_entity_nav(is_id_integer=false) {
 def create_mc_nav() {
 
     return table(1000:0, 
-                ['entity_id', 'curve_type', 'strategy', 'year_week', 'price_date', 'cumulative_nav', 'fund_num'],
-                [SYMBOL, INT, INT, STRING, DATE, DOUBLE, INT]);
+                ['entity_id', 'curve_type', 'strategy', 'year_week', 'price_date', 'cumulative_nav', 'ret_1w', 'fund_num'],
+                [SYMBOL, INT, INT, STRING, DATE, DOUBLE, DOUBLE, INT]);
 }
 
 /*

+ 14 - 10
modules/navCalculator.dos

@@ -321,20 +321,24 @@ def cal_mc_nav_by_return(entity_type, entity_ret, freq='m') {
 	          FROM entity_ret
 	          GROUP BY entity_id, curve_type, strategy).toStdJson();
 
+	t_nav = entity_ret.join(take(double(NULL), entity_ret.size()) AS nav);
+
 	// 取净值前值
 	t_pre_nav = get_mc_nav_for_return_calculation(entity_type, s_json, 1, freq);
 
-	// 没有前值时候, 做一个假记录,把净值1和日期填入
-	INSERT INTO t_pre_nav
-		SELECT entity_id, curve_type, strategy, effective_date.min() AS effective_date, 1 AS cumulative_nav
-        FROM entity_ret
-        WHERE NOT exists (SELECT * FROM t_pre_nav 
-	                      WHERE entity_ret.entity_id = t_pre_nav.entity_id
-	                        AND entity_ret.curve_type = t_pre_nav.curve_type
-	                        AND entity_ret.strategy = t_pre_nav.strategy)
+	if(t_pre_nav.size() == 0) {
+
+		// 没有前值时候, 做一个假记录,把净值1和日期填入
+		INSERT INTO t_pre_nav
+			SELECT entity_id, curve_type, strategy, effective_date.min() AS effective_date, 1 AS cumulative_nav
+	        FROM entity_ret
+	        GROUP BY entity_id, curve_type, strategy;
+		// 设置初始净值为1,收益为0
+	    UPDATE t_nav n
+	    	SET ret = 0, nav = 1
+	    FROM ej(t_nav, t_pre_nav, ['entity_id', 'effective_date']);
+	}
 	
-	t_nav = entity_ret.join(take(double(NULL), entity_ret.size()) AS nav);
-
     // 通过收益反算净值: nav_i = nav_0 * ∏(1 + ret_i)
     UPDATE t_nav 
         SET nav = (t_pre_nav.cumulative_nav * (1+ret).cumprod()).round(6) 

+ 4 - 4
modules/task_weeklyPerformnce.dos

@@ -339,9 +339,9 @@ def cal_and_save_mc_weekly_nav(entity_type, entity_date, is_save_local) {
 
 		s_json = tb_entity.toStdJson();
 		t_ret = get_mc_average_return(entity_type, 'w', s_json, 0, 1, true);
-        
+     
         for(cur in d_curve_type.keys()) {
-
+//cur=7
 			tmp = SELECT entity_id, cur AS curve_type, 0 AS strategy, effective_date, price_date, ret, incl_cal_cnt 
 			      FROM t_ret WHERE raise_type = d_curve_type[cur] AND strategy = -99; // 目前只需要全策略
 
@@ -349,7 +349,7 @@ def cal_and_save_mc_weekly_nav(entity_type, entity_date, is_save_local) {
         	tb_nav = cal_mc_nav_by_return(entity_type, tmp, 'w');
 
 	        INSERT INTO tb_entity_nav 
-	        	SELECT entity_id, curve_type, strategy, effective_date AS year_week, price_date, nav, incl_cal_cnt
+	        	SELECT entity_id, curve_type, strategy, effective_date AS year_week, price_date, nav, ret, incl_cal_cnt
 	            FROM ej(tb_nav, tmp, ['entity_id', 'curve_type', 'strategy', 'effective_date']);
 
         }
@@ -391,7 +391,7 @@ def cal_and_save_mc_weekly_nav(entity_type, entity_date, is_save_local) {
  */
 def CalMCWeeklyNavTask(entity_type, updatetime) {
 //updatetime = 2024.12.05;
-//entity_type = 'CO';
+//entity_type = 'PL';
 	if(!(entity_type IN ['PL', 'CO'])) return;
 
 	is_save_local = iif(updatetime <= get_ini_data_const()['updatetime'], true, false);