indicatorCalculator.dos 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. module fundit::indicatorCalculator
  2. use fundit::dataPuller
  3. use fundit::returnCalculator
  4. use fundit::navCalculator
  5. /*
  6. * Annulized multiple
  7. */
  8. def get_annulization_multiple(freq) {
  9. ret = 1;
  10. if (freq == 'd') {
  11. ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
  12. } else if (freq == 'w') {
  13. ret = 52;
  14. } else if (freq == 'm') {
  15. ret = 12;
  16. } else if (freq == 'q') {
  17. ret = 4;
  18. } else if (freq == 's') {
  19. ret = 2;
  20. } else if (freq == 'a') {
  21. ret = 1;
  22. }
  23. return ret;
  24. }
  25. /*
  26. * 取主基准和BFI的历史月收益率
  27. *
  28. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表
  29. * @param end_day <DATE>: 收益的截止日期
  30. *
  31. * @return <TABLE>: benchmark_id, end_date, ret
  32. *
  33. */
  34. def get_benchmark_return(benchmarks, end_day) {
  35. s_index_ids = '';
  36. s_factor_ids = '';
  37. if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
  38. // 前缀为 IN 的 benchmark id
  39. t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';
  40. s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
  41. // 前缀为 FA 的 benchmark id
  42. t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
  43. s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
  44. // 目前指数的月度业绩存在 fund_performance 表
  45. t_bmk = SELECT fund_id AS benchmark_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_monthly_ret('IX', s_index_ids, 1990.01.01, end_day, true);
  46. // 而因子的月度业绩存在 cm_factor_performance 表
  47. INSERT INTO t_bmk SELECT factor_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_monthly_ret('FA', s_factor_ids, 1990.01.01, end_day, true);
  48. return t_bmk;
  49. }
  50. /*
  51. * Trailing Return, Standard Deviation, Skewness, Kurtosis, Max Drawdown, VaR, CVaR, Calmar Ratio
  52. * @param ret: 收益表,需要有 entity_id, price_dat, end_date, nav
  53. * @param freq: 数据频率,d, w, m, q, s, a
  54. *
  55. * TODO: max drowdown is off!
  56. * NOTE: standard deviation of Java version is noncompliant-GIPS annulized number
  57. *
  58. * Create: 20240904 Joey
  59. * TODO: var and cvar are silightly off compared with Java version
  60. * calmar is offCalmar
  61. *
  62. */
  63. def cal_basic_performance(ret, freq) {
  64. /* OLD version that can only calculate the latest numbers (group-by version)
  65. t = SELECT entity_id, end_date.max() AS end_date, max(price_date) AS price_date, min(price_date) AS min_date,
  66. ((1+ret).prod()-1).round(6) AS trailing_ret,
  67. iif(price_date.max().month()-price_date.min().month()>12,
  68. ((1+ret).prod()).pow(get_annulization_multiple('m')\(end_date.max()-end_date.min()))-1,
  69. ((1+ret).prod()-1)).round(6) AS trailing_ret_a,
  70. ret.std() AS std_dev,
  71. ret.skew(false) AS skewness,
  72. ret.kurtosis(false) - 3 AS kurtosis,
  73. ret.min() AS wrst_month
  74. max( 1 - nav \ nav.cummax() ) AS drawdown
  75. FROM ret
  76. GROUP BY entity_id;
  77. // var & cvar require return NOT NULL
  78. // NOTE: DolphinDB supports 4 different ways: normal, logNormal, historical, monteCarlo. we use historical
  79. t1 = SELECT entity_id, max(end_date) AS end_date, max(price_date) AS price_date,
  80. ret.VaR('historical', 0.95) AS var,
  81. ret.CVaR('historical', 0.95) AS cvar
  82. FROM ret
  83. WHERE ret.ret > - 1
  84. GROUP BY entity_id;
  85. return (SELECT * FROM t LEFT JOIN t1 ON t.entity_id = t1.entity_id AND t.end_date = t1.end_date AND t.price_date = t1.price_date);
  86. */
  87. t = SELECT max(price_date) AS price_date, min(price_date) AS min_date,
  88. ((1+ret).prod()-1).round(6) AS trailing_ret,
  89. iif(price_date.max().month()-price_date.min().month()>12,
  90. ((1+ret).prod()).pow(get_annulization_multiple('m')\(end_date.max()-end_date.min()))-1,
  91. ((1+ret).prod()-1)).round(6) AS trailing_ret_a,
  92. ret.std() AS std_dev,
  93. ret.skew(false) AS skewness,
  94. ret.kurtosis(false) - 3 AS kurtosis,
  95. ret.min() AS wrst_month
  96. FROM ret
  97. GROUP BY entity_id
  98. CGROUP BY end_date
  99. ORDER BY end_date;
  100. // because neither VaR and CVaR in context-by (cumXXX version) are NOT supported by DolphinDB , nor they are supported in cgroup-by
  101. // we have to implement them using more basic ways:
  102. t1 = SELECT entity_id, end_date, ret,
  103. cummax( 1 - nav \ nav.cummax() ) AS drawdown, // same story: cummax is not supported by cgroup-by, so it is moved here
  104. -ret.cumpercentile(5, 'linear') AS var
  105. FROM ret
  106. CONTEXT BY entity_id;
  107. // CVaR = mean of all returns below VaR
  108. t_cvar = SELECT DISTINCT entity_id, end_date, drawdown, var, cvar
  109. FROM (
  110. SELECT t1.entity_id, t1.end_date, t1.drawdown, t1.var, -avg(t2.ret) AS cvar
  111. FROM t1
  112. INNER JOIN t1 AS t2 ON t1.entity_id = t2.entity_id
  113. WHERE t2.end_date <= t1.end_date
  114. AND t2.ret < -t1.var
  115. CONTEXT BY t1.entity_id, t1.end_date );
  116. return (SELECT *, iif(t_cvar.drawdown == 0, null, t.trailing_ret_a\t_cvar.drawdown) AS calmar
  117. FROM t LEFT JOIN t_cvar ON t.entity_id = t_cvar.entity_id AND t.end_date = t_cvar.end_date ORDER BY entity_id, end_date);
  118. }
  119. /*
  120. * Lower Partial Moment
  121. * NOTE: risk free rate is used as Minimal Accepted Rate (MAR) here
  122. *
  123. */
  124. def cal_LPM(ret, risk_free) {
  125. t = SELECT *, count(entity_id) AS cnt FROM ret WHERE ret > -1 CONTEXT BY entity_id;
  126. lpm = SELECT t.entity_id, t.end_date,
  127. (cumsum (iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ (t.cnt[0])).pow(1\1) AS lpm1,
  128. (cumsum2(iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ (t.cnt[0])).pow(1\2) AS lpm2,
  129. (cumsum3(iif(rfr.ret > t.ret, rfr.ret - t.ret, 0)) \ (t.cnt[0])).pow(1\3) AS lpm3
  130. FROM t
  131. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  132. CONTEXT BY t.entity_id;
  133. return lpm;
  134. }
  135. /*
  136. * Downside Devision, Omega Ratio, Sortino Ratio, Kappa Ratio
  137. *
  138. * TODO: Java version of Downside Deviation (LPM2) uses cnt-1 as denominator to calculate mean excess return, which might be wrong
  139. * Java version of Omega could be wrong because Java uses annualized returns and cnt-1
  140. * Java'version of Kappa could be very wrong
  141. *
  142. */
  143. def cal_omega_sortino_kappa(ret, risk_free) {
  144. lpm = cal_LPM(ret, risk_free);
  145. tb = SELECT t.entity_id, t.end_date,
  146. l.lpm2 AS ds_dev,
  147. (t.ret - rfr.ret ).cumavg() \ l.lpm1 + 1 AS omega,
  148. (t.ret - rfr.ret ).cumavg() \ l.lpm2 AS sortino,
  149. (t.ret - rfr.ret ).cumavg() \ l.lpm3 AS kappa
  150. FROM ret t
  151. INNER JOIN lpm l ON t.entity_id = l.entity_id AND t.end_date = l.end_date
  152. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  153. CONTEXT BY t.entity_id;
  154. return tb;
  155. }
  156. /*
  157. * Alpha & Beta
  158. * NOTE: alpha of Java version is noncompliant-GIPS annulized number
  159. */
  160. def cal_alpha_beta(ret, benchmarks, bmk_ret, risk_free) {
  161. t = SELECT t.entity_id, t.end_date, t.ret, bm.benchmark_id, bmk.ret AS ret_bmk
  162. FROM ret t
  163. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  164. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  165. WHERE t.ret > -1
  166. AND bmk.ret > -1;
  167. beta = SELECT entity_id, end_date, benchmark_id, ret.cumbeta(ret_bmk) AS beta FROM t CONTEXT BY entity_id, benchmark_id;
  168. alpha = SELECT t.entity_id, t.end_date, t.benchmark_id, beta.beta AS beta,
  169. (t.ret - rfr.ret).cumavg() - beta.beta * (t.ret_bmk - rfr.ret).cumavg() AS alpha
  170. FROM t
  171. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.benchmark_id = beta.benchmark_id AND t.end_date = beta.end_date
  172. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  173. CONTEXT BY t.entity_id, t.benchmark_id
  174. ORDER BY t.entity_id, t.end_date, t.benchmark_id;
  175. return alpha;
  176. }
  177. /*
  178. * Winning Ratio, Tracking Error, Information Ratio
  179. *
  180. * DO WE FOUND A BIG BUG OF JAVA IMPLEMENTATION, WHICH ASSUMES FACTORS OF CURRENT MONTH ARE SAME AS HISTORICAL ONES!
  181. *
  182. * TODO: Information Ratio is way off!
  183. * Not sure how to describe a giant number("inf"), for now 999 is used
  184. */
  185. def cal_benchmark_tracking(ret, benchmarks, bmk_ret) {
  186. t0 = SELECT t.entity_id, t.end_date, t.price_date,
  187. t.ret, bmk.ret AS ret_bmk, cumcount(t.entity_id) AS cnt, (t.ret - bmk.ret) AS exc_ret, bm.benchmark_id
  188. FROM ret t
  189. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  190. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  191. WHERE t.ret > -1
  192. AND bmk.ret > -1
  193. CONTEXT BY t.entity_id, bm.benchmark_id;
  194. t = SELECT entity_id, end_date.cummax() AS end_date, price_date.cummax() AS price_date, price_date.cummin() AS min_date, benchmark_id,
  195. cumcount(iif(exc_ret >= 0, 1, null)) \ cnt AS winrate,
  196. exc_ret.cumstd() AS track_error,
  197. iif(exc_ret.cumstd() == 0, null, exc_ret.cumavg() / exc_ret.cumstd()) AS info
  198. FROM t0 CONTEXT BY entity_id, benchmark_id
  199. ORDER BY entity_id, end_date, benchmark_id;
  200. return t;
  201. }
  202. /*
  203. * Upside/Down Capture Return/Ratio
  204. *
  205. */
  206. def cal_capture_ratio(ret, benchmarks, bmk_ret) {
  207. 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
  208. FROM ret t
  209. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  210. 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
  211. WHERE t.ret > -1
  212. AND bmk.ret >= 0
  213. CONTEXT BY t.entity_id, bm.benchmark_id;
  214. 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
  215. FROM ret t
  216. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  217. 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
  218. WHERE t.ret > -1
  219. AND bmk.ret < 0
  220. CONTEXT BY t.entity_id, bm.benchmark_id;
  221. t = (SELECT * FROM (
  222. SELECT iif(isNull(t1.entity_id), t2.entity_id, t1.entity_id) AS entity_id,
  223. iif(isNull(t1.end_date), t2.end_date, t1.end_date) AS end_date,
  224. iif(isNull(t1.benchmark_id), t2.benchmark_id, t1.benchmark_id) AS benchmark_id,
  225. t1.upside_ret.pow(1 \ t1.bmk_upside_cnt)-1 AS upside_capture_ret,
  226. (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,
  227. t2.downside_ret.pow(1 \ t2.bmk_downside_cnt)-1 AS downside_capture_ret,
  228. (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
  229. 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)
  230. ORDER BY entity_id, benchmark_id, end_date).ffill();
  231. return t;
  232. }
  233. /*
  234. * Sharpe Ratio
  235. * NOTE: Java version is noncompliant-GIPS annulized number
  236. */
  237. def cal_sharpe(ret, std_dev, risk_free) {
  238. sharpe = SELECT t.entity_id, t.end_date, (t.ret - rfr.ret).cumavg() / std.std_dev AS sharpe
  239. FROM ret t
  240. INNER JOIN std_dev std ON t.entity_id = std.entity_id AND t.end_date = std.end_date
  241. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  242. WHERE std.std_dev <> 0
  243. CONTEXT BY t.entity_id;
  244. return sharpe;
  245. }
  246. /*
  247. * Treynor Ratio
  248. */
  249. def cal_treynor(ret, risk_free, beta) {
  250. t = SELECT *, cumcount(entity_id) AS cnt
  251. FROM ret t
  252. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  253. WHERE t.ret > -1
  254. AND rfr.ret > -1
  255. CONTEXT BY t.entity_id;
  256. treynor = SELECT t.entity_id, t.end_date, beta.benchmark_id,
  257. ((1 + t.ret).cumprod().pow(12\iif(t.cnt<12, 12, t.cnt)) - (1 + t.rfr_ret).cumprod().pow(12\iif(t.cnt<12, 12, t.cnt))) / beta.beta AS treynor
  258. FROM t
  259. INNER JOIN beta AS beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  260. CONTEXT BY t.entity_id, beta.benchmark_id;
  261. return treynor;
  262. }
  263. /*
  264. * Jensen's Alpha
  265. * TODO: the result is slightly off
  266. */
  267. def cal_jensen(ret, bmk_ret, risk_free, beta) {
  268. jensen = SELECT t.entity_id, t.end_date, t.ret.cumavg() - rfr.ret.cumavg() - beta.beta * (bmk.ret.cumavg() - rfr.ret.cumavg()) AS jensen, beta.benchmark_id
  269. FROM ret t
  270. INNER JOIN beta beta ON t.entity_id = beta.entity_id AND t.end_date = beta.end_date
  271. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND beta.benchmark_id = bmk.benchmark_id
  272. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  273. CONTEXT BY t.entity_id, beta.benchmark_id;
  274. return jensen;
  275. }
  276. /*
  277. * Modigliani Modigliani Measure (M2)
  278. * NOTE: M2 = sharpe * std(benchmark) + risk_free_rate
  279. * NOTE: Java version is noncompliant-GIPS annulized number
  280. */
  281. def cal_m2(ret, benchmarks, bmk_ret, risk_free) {
  282. 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
  283. FROM ret t
  284. INNER JOIN benchmarks bm ON t.entity_id = bm.entity_id AND t.end_date = bm.end_date
  285. INNER JOIN bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  286. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  287. CONTEXT BY t.entity_id, bm.benchmark_id;
  288. return m2;
  289. }
  290. /*
  291. * Morningstar Return, Morningstar Risk-Adjusted Return
  292. *
  293. * TODO: Tax and loads are NOT taken care of
  294. * TODO: Assume Chinese methodology using 3, 5, 10 as number of traling years
  295. * TODO: need verify with reliable results
  296. *
  297. * NOTE: Morningstar methodology requires monthly return for calculation, so that "12" is hard-coded here
  298. *
  299. *
  300. */
  301. def cal_ms_return(ret, risk_free) {
  302. r = SELECT t.entity_id, t.end_date, t.price_date.cummax() AS price_date, t.price_date.cummin() AS min_date,
  303. ((1 + t.ret)\(1 + rfr.ret)).cumprod().pow(12\(t.end_date.cummax() - t.end_date.cummin()))-1 AS ms_ret_a,
  304. (1 + t.ret).pow(-2).cumavg().pow(-12/2)-1 AS ms_rar_a
  305. FROM ret t
  306. INNER JOIN risk_free rfr ON t.end_date = rfr.end_date
  307. CONTEXT BY t.entity_id;
  308. return r;
  309. }
  310. /*
  311. * Calculation for monthly indicators which need benchmark
  312. * @param ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  313. * @param benchmarks <TABLE>: entity-benchmark mapping table
  314. * @param index_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  315. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  316. *
  317. * @return: indicators table
  318. *
  319. *
  320. * Create 20240904 模仿Java & python代码在Dolphin中实现,具体计算逻辑可能会有不同 Joey
  321. * TODO: some datapoints require more data, we need a way to disable calculation for them
  322. *
  323. */
  324. def cal_indicators_with_benchmark(mutable ret, benchmarks, index_ret, risk_free) {
  325. // sorting for correct first() and last() value
  326. ret.sortBy!(['entity_id', 'price_date'], [1, 1]);
  327. // alpha, beta
  328. alpha_beta = cal_alpha_beta(ret, benchmarks, index_ret, risk_free);
  329. // 胜率、跟踪误差、信息比率
  330. bmk_tracking = cal_benchmark_tracking(ret, benchmarks, index_ret);
  331. // 特雷诺
  332. treynor = cal_treynor(ret, risk_free, alpha_beta);
  333. // 詹森指数
  334. jensen = cal_jensen(ret, index_ret, risk_free, alpha_beta);
  335. // M2
  336. m2 = cal_m2(ret, benchmarks, index_ret, risk_free);
  337. // 上下行捕获率、收益
  338. capture_r = cal_capture_ratio(ret, benchmarks, index_ret);
  339. r = SELECT * FROM bmk_tracking a1
  340. LEFT JOIN alpha_beta ON a1.entity_id = alpha_beta.entity_id AND a1.benchmark_id = alpha_beta.benchmark_id AND a1.end_date = alpha_beta.end_date
  341. LEFT JOIN treynor ON a1.entity_id = treynor.entity_id AND a1.benchmark_id = treynor.benchmark_id AND a1.end_date = treynor.end_date
  342. LEFT JOIN jensen ON a1.entity_id = jensen.entity_id AND a1.benchmark_id = jensen.benchmark_id AND a1.end_date = jensen.end_date
  343. LEFT JOIN m2 ON a1.entity_id = m2.entity_id AND a1.benchmark_id = m2.benchmark_id AND a1.end_date = m2.end_date
  344. LEFT JOIN capture_r ON a1.entity_id = capture_r.entity_id AND a1.benchmark_id = capture_r.benchmark_id AND a1.end_date = capture_r.end_date;
  345. // 年化各数据点
  346. // GIPS RULE: NO annulization for data less than 1 year
  347. plainAnnu = get_annulization_multiple('m');
  348. sqrtAnnu = sqrt(get_annulization_multiple('m'));
  349. r.addColumn(['alpha_a', 'jensen_a', 'track_error_a', 'info_a', 'm2_a'],
  350. [DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  351. UPDATE r
  352. SET alpha_a = alpha * iif(price_date.month() - min_date.month() >= 11, plainAnnu, 1),
  353. jensen_a = jensen * iif(price_date.month() - min_date.month() >= 11, plainAnnu, 1),
  354. track_error_a = track_error * iif(price_date.month() - min_date.month() >= 11, sqrtAnnu, 1),
  355. info_a = info * iif(price_date.month() - min_date.month() >= 11, sqrtAnnu, 1),
  356. m2_a = m2 * iif(price_date.month() - min_date.month() >= 11, plainAnnu, 1);
  357. return r.dropColumns!(['price_date', 'min_date']);
  358. }
  359. /*
  360. * Monthly standard indicator calculation
  361. * @param ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  362. * @param benchmarks <TABLE>: entity-benchmark mapping table
  363. * @param benchmark_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  364. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  365. *
  366. * @return: indicators table
  367. *
  368. *
  369. * Create 20240904 模仿Java & python代码在Dolphin中实现,具体计算逻辑可能会有不同 Joey
  370. * TODO: some datapoints require more data, we need a way to disable calculation for them
  371. *
  372. */
  373. def cal_indicators(mutable ret, benchmarks, benchmark_ret, risk_free) {
  374. // sorting for correct first() and last() value
  375. ret.sortBy!(['entity_id', 'price_date'], [1, 1]);
  376. // 收益、标准差、偏度、峰度、最大回撤、VaR, CVaR、卡玛比率
  377. rtn = cal_basic_performance(ret, 'm');
  378. // 夏普
  379. sharpe = cal_sharpe(ret, rtn, risk_free);
  380. // 整合后的下行标准差、欧米伽、索提诺、卡帕
  381. lpms = cal_omega_sortino_kappa(ret, risk_free);
  382. // 需要基准的指标们
  383. indicator_with_benchmark = cal_indicators_with_benchmark(ret, benchmarks, benchmark_ret, risk_free);
  384. r = SELECT * FROM rtn a1
  385. LEFT JOIN sharpe ON a1.entity_id = sharpe.entity_id AND a1.end_date = sharpe.end_date
  386. LEFT JOIN lpms ON a1.entity_id = lpms.entity_id AND a1.end_date = lpms.end_date
  387. LEFT JOIN indicator_with_benchmark bmk ON a1.entity_id = bmk.entity_id AND a1.end_date = bmk.end_date;
  388. // 年化各数据点
  389. // GIPS RULE: NO annulization for data less than 1 year
  390. plainAnnu = get_annulization_multiple('m');
  391. sqrtAnnu = sqrt(get_annulization_multiple('m'));
  392. r.addColumn(['std_dev_a', 'ds_dev_a', 'sharpe_a', 'sortino_a'],
  393. [DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  394. UPDATE r
  395. SET std_dev_a = std_dev * iif(price_date.month() - min_date.month() >= 11, sqrtAnnu, 1),
  396. ds_dev_a = ds_dev * iif(price_date.month() - min_date.month() >= 11, sqrtAnnu, 1),
  397. sharpe_a = sharpe * iif(price_date.month() - min_date.month() >= 11, sqrtAnnu, 1),
  398. sortino_a = sortino * iif(price_date.month() - min_date.month() >= 11, sqrtAnnu, 1);
  399. return r;
  400. }
  401. /*
  402. * Monthly BFI indicator calculation
  403. * @param ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  404. * @param benchmarks <TABLE>: entity-benchmark mapping table
  405. * @param benchmark_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  406. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  407. *
  408. * @return: BFI indicators table
  409. *
  410. *
  411. * Create 20240914 Joey
  412. *
  413. */
  414. def cal_bfi_indicators(mutable ret, benchmarks, benchmark_ret, risk_free) {
  415. // 需要基准的指标们
  416. r = cal_indicators_with_benchmark(ret, benchmarks, benchmark_ret, risk_free);
  417. return r;
  418. }
  419. /*
  420. * Monthly Morningstar indicator calculation
  421. *
  422. * @param ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  423. * @param benchmarks <USELESS>:
  424. * @param benchmark_ret <USELESS>:
  425. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  426. *
  427. */
  428. def cal_ms_indicators(mutable ret, benchmarks, benchmark_ret, risk_free) {
  429. r = cal_ms_return(ret, risk_free);
  430. return r;
  431. }
  432. /*
  433. * Calculate trailing 6m, ytd, 1y, 2y, 3y, 4y, 5y, 10y and since inception datapoints
  434. *
  435. * @param: func <FUNCTION>: the calculation function
  436. * @param: entity_info <TABLE>: basic information of entity, NEED COLUMNS entity_id, inception_date
  437. * @param benchmarks <TABLE>: entity-benchmark mapping table
  438. * @param: ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  439. * @param: end_day <DATE>: 计算截止日期
  440. * @param bmk_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  441. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  442. * @param periods <BOOL VECTOR>: 是否计算的区间向量,分别对应 incep, ytd, 6m, 1y, 2y, 3y, 4y, 5y, 10y
  443. *
  444. * Example: cal_trailing(
  445. *
  446. */
  447. def cal_trailing(func, entity_info, benchmarks, mutable tb_ret, end_day, bmk_ret, risk_free_rate, periods) {
  448. r_incep = null;
  449. r_ytd = null;
  450. r_6m = null;
  451. r_1y = null;
  452. r_2y = null;
  453. r_3y = null;
  454. r_4y = null;
  455. r_5y = null;
  456. r_10y = null;
  457. // since inception
  458. if(tb_ret.size() > 0 && periods[0] == 1) {
  459. r_incep = func(tb_ret, benchmarks, bmk_ret, risk_free_rate);
  460. }
  461. // ytd
  462. tb_ret_ytd = SELECT * FROM tb_ret WHERE end_date >= end_day.yearBegin().month();
  463. if(tb_ret_ytd.size() > 0 && periods[1] == 1) {
  464. r_ytd = func(tb_ret_ytd, benchmarks, bmk_ret, risk_free_rate);
  465. }
  466. // trailing 6m
  467. tb_ret_6m = SELECT * FROM tb_ret r INNER JOIN entity_info ei ON r.entity_id = ei.entity_id
  468. WHERE r.end_date > end_day.month()-6 AND (end_day.month() - ei.inception_date.month()) >= 6;
  469. if(tb_ret_6m.size() > 0 && periods[2] == 1) {
  470. r_6m = func(tb_ret_6m, benchmarks, bmk_ret, risk_free_rate);
  471. }
  472. // trailing 1y
  473. tb_ret_1y = SELECT * FROM tb_ret r INNER JOIN entity_info ei ON r.entity_id = ei.entity_id
  474. WHERE r.end_date > end_day.month()-12 AND (end_day.month() - ei.inception_date.month()) >= 12;
  475. if(tb_ret_1y.size() > 0 && periods[3] == 1) {
  476. r_1y = func(tb_ret_1y, benchmarks, bmk_ret, risk_free_rate);
  477. }
  478. // trailing 2y
  479. tb_ret_2y = SELECT * FROM tb_ret r INNER JOIN entity_info ei ON r.entity_id = ei.entity_id
  480. WHERE r.end_date > end_day.month()-24 AND (end_day.month() - ei.inception_date.month()) >= 24;
  481. if(tb_ret_2y.size() > 0 && periods[4] == 1) {
  482. r_2y = func(tb_ret_2y, benchmarks, bmk_ret, risk_free_rate);
  483. }
  484. // trailing 3y
  485. tb_ret_3y = SELECT * FROM tb_ret r INNER JOIN entity_info ei ON r.entity_id = ei.entity_id
  486. WHERE r.end_date > end_day.month()-36 AND (end_day.month() - ei.inception_date.month()) >= 36;
  487. if(tb_ret_3y.size() > 0 && periods[5] == 1) {
  488. r_3y = func(tb_ret_3y, benchmarks, bmk_ret, risk_free_rate);
  489. }
  490. // trailing 4y
  491. tb_ret_4y = SELECT * FROM tb_ret r INNER JOIN entity_info ei ON r.entity_id = ei.entity_id
  492. WHERE r.end_date > end_day.month()-48 AND (end_day.month() - ei.inception_date.month()) >= 48;
  493. if(tb_ret_4y.size() > 0 && periods[6] == 1) {
  494. r_4y = func(tb_ret_4y, benchmarks, bmk_ret, risk_free_rate);
  495. }
  496. // trailing 5y
  497. tb_ret_5y = SELECT * FROM tb_ret r INNER JOIN entity_info ei ON r.entity_id = ei.entity_id
  498. WHERE r.end_date > end_day.month()-60 AND (end_day.month() - ei.inception_date.month()) >= 60;
  499. if(tb_ret_5y.size() > 0 && periods[7] == 1) {
  500. r_5y = func(tb_ret_5y, benchmarks, bmk_ret, risk_free_rate);
  501. }
  502. // trailing 10y
  503. tb_ret_10y = SELECT * FROM tb_ret r INNER JOIN entity_info ei ON r.entity_id = ei.entity_id
  504. WHERE r.end_date > end_day.month()-120 AND (end_day.month() - ei.inception_date.month()) >= 120;
  505. if(tb_ret_10y.size() > 0 && periods[8] == 1) {
  506. r_10y = func(tb_ret_10y, benchmarks, bmk_ret, risk_free_rate);
  507. }
  508. return r_incep, r_ytd, r_6m, r_1y, r_2y, r_3y, r_4y, r_5y, r_10y;
  509. }
  510. /*
  511. * Calculate trailing 6m, ytd, 1y, 2y, 3y, 4y, 5y, 10y and since inception standard indicators
  512. *
  513. * @param: entity_info <TABLE>: basic information of entity, NEED COLUMNS entity_id, inception_date
  514. * @param benchmarks <TABLE>: entity-benchmark mapping table
  515. * @param: ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  516. * @param: end_day <DATE>: 计算截止日期
  517. * @param bmk_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  518. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  519. *
  520. */
  521. def cal_trailing_indicators(entity_info, benchmarks, mutable tb_ret, end_day, bmk_ret, risk_free_rate) {
  522. return cal_trailing(cal_indicators, entity_info, benchmarks, tb_ret, end_day, bmk_ret, risk_free_rate, [1,1,1,1,1,1,1,1,1]);
  523. }
  524. /*
  525. * Calculate trailing 6m, ytd, 1y, 2y, 3y, 4y, 5y, 10y and since inception bfi indicators
  526. *
  527. * @param: entity_info <TABLE>: basic information of entity, NEED COLUMNS entity_id, inception_date
  528. * @param benchmarks <TABLE>: entity-benchmark mapping table
  529. * @param: ret <TABLE>: 收益表,NEED COLUMNS entity_id, price_dat, end_date, nav
  530. * @param: end_day <DATE>: 计算截止日期
  531. * @param bmk_ret <TABLE>: historical benchmark return table, NEED COLUMNS fund_id, end_date, ret
  532. * @param risk_free <TABLE>: historical risk free rate table, NEED COLUMNS fund_id, end_date, ret
  533. *
  534. *
  535. */
  536. def cal_trailing_bfi_indicators(entity_info, benchmarks, mutable tb_ret, end_day, bmk_ret, risk_free_rate) {
  537. return cal_trailing(cal_bfi_indicators, entity_info, benchmarks, tb_ret, end_day, bmk_ret, risk_free_rate, [1,1,1,1,1,1,1,1,1]);
  538. }
  539. /*
  540. * Calculate trailing 3y, 5y, 10y Morningstar Return, Risk-Adjested Return and Risk
  541. *
  542. */
  543. def cal_trailing_ms_indicators(entity_info, mutable tb_ret, end_day, risk_free_rate) {
  544. return cal_trailing(cal_ms_indicators, entity_info, , tb_ret, end_day, , risk_free_rate, periods=[0,0,0,0,0,1,0,1,1]);
  545. }
  546. /*
  547. * Calculate fund indicators for one date
  548. *
  549. * @param entity_type <STRING>: MF, HF
  550. * @param fund_ids <STRING>: 逗号和单引号分隔的fund_id
  551. * @param end_day <DATE>: 要计算的日期
  552. * @param isFromNav <BOOL>: 用净值实时计算还是从表中取月收益
  553. * @param isFromSQL <BOOL>: TODO: 从MySQL还是本地DolphinDB取净值/收益数据
  554. *
  555. * @return <DICT TABLE>: ['PBI-INCEP', 'PBI-YTD', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y', 'MS-3Y', 'MS-5Y', 'MS-10Y']
  556. *
  557. * TODO: primary_benchmark_id seems not be used as benchmark, when it is FA00000VNB
  558. *
  559. * Example: cal_fund_indicators('HF', "'HF000004KN','HF000103EU','HF00018WXG'", 2024.06.28, true);
  560. *
  561. */
  562. def cal_fund_indicators(entity_type, fund_ids, end_day, isFromNav) {
  563. very_old_date = 1990.01.01;
  564. start_month = 1990.01M;
  565. fund_info = get_fund_info(fund_ids);
  566. if(fund_info.isVoid() || fund_info.size() == 0) { return null };
  567. fund_info.rename!('fund_id', 'entity_id');
  568. if(isFromNav == true) {
  569. // 从净值开始计算收益
  570. tb_ret = SELECT * FROM cal_fund_monthly_returns(entity_type, fund_ids, true) WHERE price_date <= end_day;
  571. tb_ret.rename!(['fund_id', 'cumulative_nav'], ['entity_id', 'nav']);
  572. } else {
  573. // 从fund_performance表里读月收益
  574. tb_ret = get_monthly_ret('FD', fund_ids, very_old_date, end_day, true);
  575. tb_ret.rename!(['fund_id'], ['entity_id']);
  576. }
  577. // 取基金和基准的对照表
  578. primary_benchmark = SELECT fund_id AS entity_id, end_date, iif(benchmark_id.isNull(), 'IN00000008', benchmark_id) AS benchmark_id
  579. FROM get_fund_primary_benchmark(fund_ids, start_month.temporalFormat('yyyy-MM'), end_day.month().temporalFormat('yyyy-MM')) ;
  580. // 取所有出现的基准月收益
  581. bmk_ret = get_benchmark_return(primary_benchmark, end_day);
  582. risk_free_rate = SELECT fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(very_old_date, end_day);
  583. // 标准的指标
  584. t0 = cal_trailing_indicators(fund_info, primary_benchmark, tb_ret, end_day, bmk_ret, risk_free_rate);
  585. // Morningstar 指标
  586. t1 = cal_trailing_ms_indicators(fund_info, tb_ret, end_day, risk_free_rate);
  587. // PBI stands for "Primary Benchmark Index", MS stands for "MorningStar"
  588. v_table_name = ['PBI-INCEP', 'PBI-YTD', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y', 'MS-3Y', 'MS-5Y', 'MS-10Y'];
  589. return dict(v_table_name, t0 <- t1[5] <- t1[7] <- t1[8]);
  590. }
  591. /*
  592. * Calculate fund BFI indicators for one date
  593. *
  594. * @param entity_type <STRING>: MF, HF
  595. * @param fund_ids <STRING>: 逗号和单引号分隔的fund_id
  596. * @param end_day <DATE>: 要计算的日期
  597. * @param isFromNav <BOOL>: 用净值实时计算还是从表中取月收益
  598. * @param isFromSQL <BOOL>: TODO: 从MySQL还是本地DolphinDB取净值/收益数据
  599. *
  600. * @return <DICT TABLE>: ['BFI-INCEP', 'BFI-YTD', 'BFI-6M', 'BFI-1Y', 'BFI-2Y', 'BFI-3Y', 'BFI-4Y', 'BFI-5Y', 'BFI-10Y']
  601. *
  602. * TODO: primary_benchmark_id seems not be used as benchmark, when it is FA00000VNB
  603. * TODO: intergrate with cal_fund_indicators
  604. *
  605. * Example: cal_fund_bfi_indicators('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", 2024.08.31, true);
  606. *
  607. */
  608. def cal_fund_bfi_indicators(entity_type, fund_ids, end_day, isFromNav) {
  609. very_old_date = 1990.01.01;
  610. start_month = 1990.01M;
  611. fund_info = get_fund_info(fund_ids);
  612. if(fund_info.isVoid() || fund_info.size() == 0) { return null };
  613. fund_info.rename!('fund_id', 'entity_id');
  614. if(isFromNav == true) {
  615. // 从净值开始计算收益
  616. tb_ret = SELECT * FROM cal_fund_monthly_returns(entity_type, fund_ids, true) WHERE price_date <= end_day;
  617. tb_ret.rename!(['fund_id', 'cumulative_nav'], ['entity_id', 'nav']);
  618. } else {
  619. // 从fund_performance表里读月收益
  620. tb_ret = get_monthly_ret('FD', fund_ids, very_old_date, end_day, true);
  621. tb_ret.rename!(['fund_id'], ['entity_id']);
  622. }
  623. // 取基金和基准的对照表
  624. bfi_benchmark = SELECT fund_id AS entity_id, end_date.temporalParse('yyyy-MM') AS end_date, factor_id AS benchmark_id
  625. FROM get_fund_bfi_factors(fund_ids, start_month.temporalFormat('yyyy-MM'), end_day.temporalFormat('yyyy-MM'));
  626. if(bfi_benchmark.isVoid() || bfi_benchmark.size() == 0) { return null; }
  627. bmk_ret = get_benchmark_return(bfi_benchmark, end_day);
  628. risk_free_rate = SELECT fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(very_old_date, end_day);
  629. t0 = cal_trailing_bfi_indicators(fund_info, bfi_benchmark, tb_ret, end_day, bmk_ret, risk_free_rate);
  630. // BFI stands for "Best Fit Index"
  631. v_table_name = ['BFI-INCEP', 'BFI-YTD', 'BFI-6M', 'BFI-1Y', 'BFI-2Y', 'BFI-3Y', 'BFI-4Y', 'BFI-5Y', 'BFI-10Y'];
  632. return dict(v_table_name, t0);
  633. }
  634. /*
  635. * Calculate portfolio indicators for one date
  636. *
  637. * @param portfolio_ids <STRING>: comma-delimited portfolio ids
  638. * @param end_day <DATE>: the date
  639. * @param cal_method <INT>: calculate based on cumulative nav (1) or nav (2)
  640. * @param isFromNav <BOOL>: calculate returns from NAV on-the-fly (true) or get from monthly return table (false)
  641. *
  642. * Example: cal_portfolio_indicators('166002,166114', 2024.08.31, 1, true);
  643. *
  644. */
  645. def cal_portfolio_indicators(portfolio_ids, end_day, cal_method, isFromNav) {
  646. very_old_date = 1990.01.01;
  647. portfolio_info = get_portfolio_info(portfolio_ids);
  648. if(portfolio_info.isVoid() || portfolio_info.size() == 0) { return null };
  649. portfolio_info.rename!('portfolio_id', 'entity_id');
  650. if(isFromNav == true) {
  651. // 从净值开始计算收益
  652. tb_raw_ret = SELECT * FROM cal_portfolio_nav(portfolio_ids, very_old_date, cal_method) WHERE price_date <= end_day;
  653. // funky thing is you can't use "AS" for the grouping columns?
  654. tb_ret = SELECT portfolio_id, price_date.month(), price_date.last() AS price_date, (1+ret).prod()-1 AS ret, nav.last() AS nav
  655. FROM tb_raw_ret
  656. WHERE price_date <= end_day
  657. GROUP BY portfolio_id, price_date.month();
  658. tb_ret.rename!(['portfolio_id', 'month_price_date'], ['entity_id', 'end_date']);
  659. } else {
  660. // 从pf_portfolio_performance表里读月收益
  661. tb_ret = get_monthly_ret('PF', portfolio_ids, very_old_date, end_day, true);
  662. tb_ret.rename!(['portfolio_id'], ['entity_id']);
  663. }
  664. // 沪深300做基准,同SQL保持一致
  665. primary_benchmark = SELECT entity_id, 'IN00000008' AS benchmark_id FROM portfolio_info;
  666. // 取所有出现的基准月收益
  667. bmk_ret = get_benchmark_return(primary_benchmark, end_day);
  668. risk_free_rate = SELECT fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(very_old_date, end_day);
  669. t0 = cal_trailing_indicators(portfolio_info, primary_benchmark, tb_ret, end_day, bmk_ret, risk_free_rate);
  670. v_table_name = ['PBI-INCEP', 'PBI-YTD', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y'];
  671. return dict(v_table_name, t0);
  672. }
  673. /*
  674. * Calculate portfolio bfi indicators for one date
  675. *
  676. * @param portfolio_ids <STRING>: comma-delimited portfolio ids
  677. * @param end_day <DATE>: the date
  678. * @param cal_method <INT>: calculate based on cumulative nav (1) or nav (2)
  679. * @param isFromNav <BOOL>: calculate returns from NAV on-the-fly (true) or get from monthly return table (false)
  680. *
  681. * TODO: intergrate with cal_portfolio_indicators
  682. *
  683. * Example: cal_portfolio_bfi_indicators('166002,166114', 2024.08.31, 1, true);
  684. *
  685. */
  686. def cal_portfolio_bfi_indicators(portfolio_ids, end_day, cal_method, isFromNav) {
  687. very_old_date = 1990.01.01;
  688. start_month = 1990.01M;
  689. portfolio_info = get_portfolio_info(portfolio_ids);
  690. if(portfolio_info.isVoid() || portfolio_info.size() == 0) { return null };
  691. portfolio_info.rename!('portfolio_id', 'entity_id');
  692. if(isFromNav == true) {
  693. // 从净值开始计算收益
  694. tb_raw_ret = SELECT * FROM cal_portfolio_nav(portfolio_ids, very_old_date, cal_method) WHERE price_date <= end_day;
  695. // funky thing is you can't use "AS" for the grouping columns?
  696. tb_ret = SELECT portfolio_id, price_date.month(), price_date.last() AS price_date, (1+ret).prod()-1 AS ret, nav.last() AS nav
  697. FROM tb_raw_ret
  698. WHERE price_date <= end_day
  699. GROUP BY portfolio_id, price_date.month();
  700. tb_ret.rename!(['portfolio_id', 'month_price_date'], ['entity_id', 'end_date']);
  701. } else {
  702. // 从pf_portfolio_performance表里读月收益
  703. tb_ret = get_monthly_ret('PF', portfolio_ids, very_old_date, end_day, true);
  704. tb_ret.rename!(['portfolio_id'], ['entity_id']);
  705. }
  706. // 取组合和基准的对照表
  707. bfi_benchmark = SELECT portfolio_id AS entity_id, end_date, factor_id AS benchmark_id
  708. FROM get_portfolio_bfi_factors(portfolio_ids, start_month.temporalFormat('yyyy-MM'), end_day.temporalFormat('yyyy-MM'));
  709. if(bfi_benchmark.isVoid() || bfi_benchmark.size() == 0) { return null; }
  710. bmk_ret = get_benchmark_return(bfi_benchmark, end_day);
  711. risk_free_rate = SELECT fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(very_old_date, end_day);
  712. t0 = cal_trailing_bfi_indicators(portfolio_info, bfi_benchmark, tb_ret, end_day, bmk_ret, risk_free_rate);
  713. v_table_name = ['PBI-INCEP', 'PBI-YTD', 'PBI-6M', 'PBI-1Y', 'PBI-2Y', 'PBI-3Y', 'PBI-4Y', 'PBI-5Y', 'PBI-10Y'];
  714. return dict(v_table_name, t0);
  715. }