瀏覽代碼

BUG FIXED

Joey 6 月之前
父節點
當前提交
f87bb92e1b
共有 1 個文件被更改,包括 41 次插入47 次删除
  1. 41 47
      modules/indicatorCalculator.dos

+ 41 - 47
modules/indicatorCalculator.dos

@@ -456,77 +456,68 @@ def cal_capture_ratio(ret, benchmarks, bmk_ret, trailing_month) {
 
     if(trailing_month == 'incep') {
 
-        t1 = SELECT t.entity_id, t.end_date, 
-                    (1+t.ret).cumprod() AS upside_ret, (1+bmk.ret).cumprod() AS bmk_upside_ret, bmk.end_date.cumcount() AS bmk_upside_cnt, bm.benchmark_id
-             FROM ret t
-             INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
-             INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
-             WHERE t.ret > -1
-               AND bmk.ret >= 0
-             CONTEXT BY t.entity_id, bm.benchmark_id;
-    
-        t2 = SELECT t.entity_id, t.end_date, 
-                    (1+t.ret).cumprod() AS downside_ret, (1+bmk.ret).cumprod() AS bmk_downside_ret, bmk.end_date.cumcount() AS bmk_downside_cnt, bm.benchmark_id
+        t1 = SELECT t.entity_id, t.end_date,
+                    (1 + iif(bmk.ret >= 0, t.ret, 0)).cumprod() AS upside_ret,
+                    (1 + iif(bmk.ret >= 0, bmk.ret, 0)).cumprod() AS bmk_upside_ret,
+                    cumcount(iif(bmk.ret >= 0, 1, null)) AS bmk_upside_cnt,
+                    (1 + iif(bmk.ret < 0, t.ret, 0)).cumprod() AS downside_ret,
+                    (1 + iif(bmk.ret < 0, bmk.ret, 0)).cumprod() AS bmk_downside_ret,
+                    cumcount(iif(bmk.ret < 0, 1, null)) AS bmk_downside_cnt,
+                    bm.benchmark_id
              FROM ret t
              INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
              INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
              WHERE t.ret > -1
-               AND bmk.ret < 0
+               AND bmk.ret > -1
              CONTEXT BY t.entity_id, bm.benchmark_id;
 
+
     } else if(trailing_month == 'ytd') {
 
         t1 = SELECT t.entity_id, t.end_date,
-                    (1+t.ret).cumprod() AS upside_ret, (1+bmk.ret).cumprod() AS bmk_upside_ret, bmk.end_date.cumcount() AS bmk_upside_cnt, bm.benchmark_id
+                    (1 + iif(bmk.ret >= 0, t.ret, 0)).cumprod() AS upside_ret,
+                    (1 + iif(bmk.ret >= 0, bmk.ret, 0)).cumprod() AS bmk_upside_ret,
+                    cumcount(iif(bmk.ret >= 0, 1, null)) AS bmk_upside_cnt,
+                    (1 + iif(bmk.ret < 0, t.ret, 0)).cumprod() AS downside_ret,
+                    (1 + iif(bmk.ret < 0, bmk.ret, 0)).cumprod() AS bmk_downside_ret,
+                    cumcount(iif(bmk.ret < 0, 1, null)) AS bmk_downside_cnt,
+                    bm.benchmark_id
              FROM ret t
              INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
              INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
              WHERE t.ret > -1
-               AND bmk.ret >= 0
-             CONTEXT BY t.entity_id, bm.benchmark_id, t.end_date.year();
-    
-        t2 = SELECT t.entity_id, t.end_date,
-                    (1+t.ret).cumprod() AS downside_ret, (1+bmk.ret).cumprod() AS bmk_downside_ret, bmk.end_date.cumcount() AS bmk_downside_cnt, bm.benchmark_id
-             FROM ret t
-             INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
-             INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
-             WHERE t.ret > -1
-               AND bmk.ret < 0
+               AND bmk.ret > -1
              CONTEXT BY t.entity_id, bm.benchmark_id, t.end_date.year();
 
+
     } else {
 
         win = trailing_month$INT;
 
         t1 = SELECT t.entity_id, t.end_date,
-                    (1+t.ret).mprod(win) AS upside_ret, (1+bmk.ret).mprod(win) AS bmk_upside_ret, bmk.end_date.mcount(win) AS bmk_upside_cnt, bm.benchmark_id
-             FROM ret t
-             INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
-             INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
-             WHERE t.ret > -1
-               AND bmk.ret >= 0
-             CONTEXT BY t.entity_id, bm.benchmark_id;
-    
-        t2 = SELECT t.entity_id, t.end_date,
-                    (1+t.ret).mprod(win) AS downside_ret, (1+bmk.ret).mprod(win) AS bmk_downside_ret, bmk.end_date.mcount(win) AS bmk_downside_cnt,bm.benchmark_id
+                    (1 + iif(bmk.ret >= 0, t.ret, 0)).mprod(win) AS upside_ret,
+                    (1 + iif(bmk.ret >= 0, bmk.ret, 0)).mprod(win) AS bmk_upside_ret,
+                    mcount(iif(bmk.ret >= 0, 1, null), win) AS bmk_upside_cnt,
+                    (1 + iif(bmk.ret < 0, t.ret, 0)).mprod(win) AS downside_ret,
+                    (1 + iif(bmk.ret < 0, bmk.ret, 0)).mprod(win) AS bmk_downside_ret,
+                    mcount(iif(bmk.ret < 0, 1, null), win) AS bmk_downside_cnt,
+                    bm.benchmark_id
              FROM ret t
              INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
              INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id AND t.end_date = bmk.end_date
              WHERE t.ret > -1
-               AND bmk.ret < 0
+               AND bmk.ret > -1
              CONTEXT BY t.entity_id, bm.benchmark_id;
+
     }
 
-    t = (SELECT * FROM (
-             SELECT iif(isNull(t1.entity_id), t2.entity_id, t1.entity_id) AS entity_id,
-                    iif(isNull(t1.end_date), t2.end_date, t1.end_date) AS end_date,
-                    iif(isNull(t1.benchmark_id), t2.benchmark_id, t1.benchmark_id) AS benchmark_id,
+    t = SELECT entity_id, end_date, benchmark_id,
                     t1.upside_ret.pow(1 \ t1.bmk_upside_cnt)-1 AS upside_capture_ret,
                    (t1.upside_ret.pow(1 \ t1.bmk_upside_cnt)-1)/(t1.bmk_upside_ret.pow(1 \ t1.bmk_upside_cnt)-1) AS upside_capture_ratio,
-                    t2.downside_ret.pow(1 \ t2.bmk_downside_cnt)-1 AS downside_capture_ret,
-                   (t2.downside_ret.pow(1 \ t2.bmk_downside_cnt)-1)/(t2.bmk_downside_ret.pow(1 \ t2.bmk_downside_cnt)-1) AS downside_capture_ratio
-             FROM t1 FULL JOIN t2 ON t1.entity_id = t2.entity_id AND t1.benchmark_id = t2.benchmark_id AND t1.end_date = t2.end_date)
-             ORDER BY entity_id, benchmark_id, end_date).ffill();
+                    t1.downside_ret.pow(1 \ t1.bmk_downside_cnt)-1 AS downside_capture_ret,
+                   (t1.downside_ret.pow(1 \ t1.bmk_downside_cnt)-1)/(t1.bmk_downside_ret.pow(1 \ t1.bmk_downside_cnt)-1) AS downside_capture_ratio
+             FROM t1 
+             ORDER BY entity_id, benchmark_id, end_date;
 
     return t;
 }
@@ -678,7 +669,8 @@ def cal_m2(ret, benchmarks, bmk_ret, risk_free, trailing_month) {
 
     if(trailing_month == 'incep') {
     	
-        m2 = SELECT t.entity_id, t.end_date, (t.ret - rfr.ret).cumavg() / t.ret.cumstd() * bmk.ret.cumstd() + rfr.ret.cumavg() AS m2, bm.benchmark_id
+        m2 = SELECT t.entity_id, t.end_date,
+                    iif(t.entity_id.cumcount() > 5, (t.ret - rfr.ret).cumavg() / t.ret.cumstd() * bmk.ret.cumstd() + rfr.ret.cumavg(), null) AS m2, bm.benchmark_id
              FROM ret t
              INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
              INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
@@ -688,7 +680,8 @@ def cal_m2(ret, benchmarks, bmk_ret, risk_free, trailing_month) {
 
     } else if(trailing_month == 'ytd') {
 
-        m2 = SELECT t.entity_id, t.end_date, (t.ret - rfr.ret).cumavg() / t.ret.cumstd() * bmk.ret.cumstd() + rfr.ret.cumavg() AS m2, bm.benchmark_id
+        m2 = SELECT t.entity_id, t.end_date, 
+                    iif(t.entity_id.cumcount() > 5, (t.ret - rfr.ret).cumavg() / t.ret.cumstd() * bmk.ret.cumstd() + rfr.ret.cumavg(), null) AS m2, bm.benchmark_id
              FROM ret t
              INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
              INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
@@ -700,7 +693,8 @@ def cal_m2(ret, benchmarks, bmk_ret, risk_free, trailing_month) {
 
         win = trailing_month$INT;
 
-        m2 = SELECT t.entity_id, t.end_date, (t.ret - rfr.ret).mavg(win) / t.ret.mstd(win) * bmk.ret.mstd(win) + rfr.ret.mavg(win) AS m2, bm.benchmark_id
+        m2 = SELECT t.entity_id, t.end_date,
+                    iif(t.entity_id.mcount(win) > 5, (t.ret - rfr.ret).mavg(win) / t.ret.mstd(win) * bmk.ret.mstd(win) + rfr.ret.mavg(win), null) AS m2, bm.benchmark_id
              FROM ret t
              INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
              INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
@@ -1208,9 +1202,9 @@ def cal_portfolio_indicators(portfolio_ids, end_day, cal_method, isFromNav) {
 
     if(tb_ret.isVoid() || tb_ret.size() == 0) return null;
 
-    // 沪深300做基准,同SQL保持一致
+    // 混合因子做基准,同SQL保持一致
     t_dates = table(start_month..end_day.month() AS end_date);
-    primary_benchmark = SELECT ei.entity_id, dt.end_date, 'IN00000008' AS benchmark_id 
+    primary_benchmark = SELECT ei.entity_id, dt.end_date, 'FA00000VNB' AS benchmark_id 
                         FROM portfolio_info ei JOIN t_dates dt
                         WHERE dt.end_date >= ei.inception_date.month();