task_portfolioPerformance.dos 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. module fundit::task_portfolioPerformance
  2. use fundit::dataPuller;
  3. use fundit::dataSaver;
  4. use fundit::navCalculator;
  5. use fundit::returnCalculator;
  6. use fundit::indicatorCalculator;
  7. /*
  8. * 计算组合历史净值(不存数据库)
  9. *
  10. * @param portfolio_ids <STRING|VECTOR>: 组合IDS,为空时跑全集(但不建议,因为可能会很吃内存)
  11. * @param updatetime <DATETIME>: 持仓证券净值更新时间,忽略时跑全历史
  12. *
  13. * @return <TABLE>: portfolio_id, price_date, ret, nav
  14. *
  15. * Example:calPortfolioNAV([143109, 145041]);
  16. * calPortfolioNAV([143109, 145041], 2024.10.28);
  17. */
  18. def calPortfolioNAV(portfolio_ids, updatetime=1900.01.01) {
  19. // portfolio_ids=[364743, 364744];
  20. // updatetime=1900.01.01;
  21. port_info = get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, true);
  22. tb_nav = cal_portfolio_nav(port_info);
  23. return tb_nav;
  24. }
  25. /*
  26. * 计算组合历史收益和指标(不存数据库)
  27. *
  28. * @param navs <TABLE>: NEED COLUMNS portfolio_id, price_date, ret, nav
  29. *
  30. * @return <DICTIONARY>:
  31. *
  32. * Example:calPortfolioPerformance(calPortfolioNAV([143109, 145041]));
  33. */
  34. def calPortfolioPerformance(navs) {
  35. if(navs.isVoid() || navs.size() == 0) return;
  36. tb_navs = navs;
  37. tb_navs.rename!(['portfolio_id'], ['entity_id']);
  38. port_ids = EXEC DISTINCT entity_id from tb_navs;
  39. port_info = get_entity_info('PF', port_ids);
  40. // 这个函数会根据情况加入成立日当月的初始净值,比直接用navs表中可能带的ret更方便
  41. tb_navs.rename!('nav', 'cumulative_nav');
  42. tb_month_ret = cal_monthly_returns_by_nav(port_info, tb_navs);
  43. tb_month_ret.rename!('cumulative_nav', 'nav');
  44. indicators = cal_monthly_indicators('PF', 'PBI', tb_month_ret);
  45. return indicators;
  46. }
  47. /*
  48. * 计算组合净值并存入数据库
  49. *
  50. * TODO: release 时改变同步目标表为正式表
  51. */
  52. def cal_and_save_portfolio_nav(cal_portfolio_info) {
  53. rt = '';
  54. // 准备类似MySQL结构的数据表
  55. tb_portfolio_nav = create_entity_nav();
  56. // 分批跑
  57. i = 0;
  58. batch_size = 1000;
  59. all_portfolio_id = EXEC DISTINCT portfolio_id FROM cal_portfolio_info;
  60. do { // 先把净值算出来存入数据库,落袋为安
  61. portfolio_info = SELECT * FROM cal_portfolio_info
  62. WHERE portfolio_id IN all_portfolio_id[i : min(all_portfolio_id.size(), i+batch_size)];
  63. if(portfolio_info.isVoid() || portfolio_info.size() == 0) break;
  64. // 30 sec
  65. tb_ret = cal_portfolio_nav(portfolio_info);
  66. INSERT INTO tb_portfolio_nav SELECT portfolio_id$STRING, price_date, nav FROM tb_ret;
  67. i += batch_size;
  68. } while (i <= cal_portfolio_info.size());
  69. if(! tb_portfolio_nav.isVoid() && tb_portfolio_nav.size() > 0) {
  70. // save data to MySQL (12 sec)
  71. try {
  72. tb_portfolio_nav.rename!('entity_id', 'portfolio_id');
  73. save_and_sync(tb_portfolio_nav, 'raw_db.pf_portfolio_nav', 'raw_db.pf_portfolio_nav');
  74. } catch(ex) {
  75. //TODO: Log errors
  76. rt = ex;
  77. }
  78. }
  79. return rt;
  80. }
  81. /*
  82. * 计算组合标准指标并存入数据库
  83. *
  84. * TODO: release 时改变同步目标表为正式表
  85. */
  86. def cal_and_save_portfolio_indicators(cal_portfolio_info) {
  87. rt = '';
  88. // 准备类似MySQL结构的数据表
  89. tb_portfolio_performance = create_entity_performance(true);
  90. tb_portfolio_indicator = create_entity_indicator(true);
  91. tb_portfolio_risk_stats = create_entity_risk_stats(true);
  92. tb_portfolio_riskadjret_stats = create_entity_riskadjret_stats(true);
  93. tb_portfolio_style_stats = create_entity_style_stats(true);
  94. tb_portfolio_performance_weekly = create_entity_performance_weekly(true);
  95. tb_portfolio_latest_performance = create_entity_latest_performance(true);
  96. // 分批跑
  97. i = 0;
  98. batch_size = 1000;
  99. all_portfolio_id = EXEC DISTINCT portfolio_id FROM cal_portfolio_info;
  100. do {
  101. cal_port = SELECT * FROM cal_portfolio_info
  102. WHERE portfolio_id IN all_portfolio_id[i : min(all_portfolio_id.size(), i+batch_size)];
  103. if(cal_port.isVoid() || cal_port.size() == 0) break;
  104. // 取数据库月度净值及前值 5 sec
  105. s_json = (SELECT portfolio_id, 1900.01.01 AS price_date FROM cal_port GROUP BY portfolio_id).rename!('portfolio_id', 'sec_id').toStdJson();
  106. tb_monthly_nav = get_nav_for_return_calculation('PF', 'm', s_json);
  107. // 把 portfolio id 字段从字符串换回整型,不然后面Join table的时候会出错
  108. v_portfolio_id = tb_monthly_nav.sec_id$INT;
  109. tb_monthly_nav.replaceColumn!('sec_id', v_portfolio_id);
  110. tb_monthly_nav.dropColumns!('nav').rename!(['sec_id', 'cumulative_nav'], ['portfolio_id', 'nav']);
  111. // 计算各标准指标
  112. indicators = calPortfolioPerformance(tb_monthly_nav);
  113. // 仿照MySQL的表结构准备好记录 (1s)
  114. port_info = (SELECT portfolio_id, start_cal_date.min() AS price_date FROM cal_port GROUP BY portfolio_id).rename!('portfolio_id', 'entity_id');
  115. generate_entity_performance(port_info, indicators, true, tb_portfolio_performance);
  116. generate_entity_indicator(port_info, indicators, true, tb_portfolio_indicator);
  117. generate_entity_risk_stats(port_info, indicators, true, tb_portfolio_risk_stats);
  118. generate_entity_riskadjret_stats(port_info, indicators, true, tb_portfolio_riskadjret_stats);
  119. generate_entity_style_stats(port_info, indicators, true, tb_portfolio_style_stats);
  120. // 计算周收益 (49s)
  121. port_info = SELECT * FROM ej(port_info, get_entity_info('PF', all_portfolio_id[i : min(all_portfolio_id.size(), i+batch_size)]), 'entity_id')
  122. rets_w = cal_weekly_returns('PF', port_info);
  123. if(! rets_w.isVoid() && rets_w.size() > 0) {
  124. // 把 portfolio id 字段从字符串换回整型,不然后面Join table的时候会出错
  125. v_portfolio_id = rets_w.entity_id$INT;
  126. rets_w.replaceColumn!('entity_id', v_portfolio_id);
  127. generate_entity_performance_weekly(port_info, rets_w, true, tb_portfolio_performance_weekly);
  128. }
  129. // 计算最新收益 (23s)
  130. perf_latest = cal_latest_performance('PF', port_info, true);
  131. if(! perf_latest.isVoid() && perf_latest.size() > 0) {
  132. generate_entity_latest_performance(port_info, perf_latest, true, tb_portfolio_latest_performance);
  133. }
  134. i += batch_size;
  135. } while (i <= cal_portfolio_info.size());
  136. if(! tb_portfolio_performance.isVoid() && tb_portfolio_performance.size() > 0) {
  137. // save data to MySQL
  138. try {
  139. chg_columns_for_mysql(tb_portfolio_performance, 'portfolio_id');
  140. save_and_sync(tb_portfolio_performance, 'raw_db.pf_portfolio_performance', 'raw_db.pf_portfolio_performance');
  141. chg_columns_for_mysql(tb_portfolio_indicator, 'portfolio_id');
  142. save_and_sync(tb_portfolio_indicator, 'raw_db.pf_portfolio_indicator', 'raw_db.pf_portfolio_indicator');
  143. chg_columns_for_mysql(tb_portfolio_risk_stats, 'portfolio_id');
  144. save_and_sync(tb_portfolio_risk_stats, 'raw_db.pf_portfolio_risk_stats', 'raw_db.pf_portfolio_risk_stats');
  145. chg_columns_for_mysql(tb_portfolio_riskadjret_stats, 'portfolio_id');
  146. save_and_sync(tb_portfolio_riskadjret_stats, 'raw_db.pf_portfolio_riskadjret_stats', 'raw_db.pf_portfolio_riskadjret_stats');
  147. chg_columns_for_mysql(tb_portfolio_style_stats, 'portfolio_id');
  148. save_and_sync(tb_portfolio_style_stats, 'raw_db.pf_portfolio_style_stats', 'raw_db.pf_portfolio_style_stats');
  149. save_and_sync(tb_portfolio_performance_weekly, 'raw_db.pf_portfolio_performance_weekly', 'raw_db.pf_portfolio_performance_weekly');
  150. save_and_sync(tb_portfolio_latest_performance, 'raw_db.pf_portfolio_latest_performance', 'raw_db.pf_portfolio_latest_performance');
  151. } catch(ex) {
  152. //TODO: Log errors
  153. rt = ex;
  154. }
  155. }
  156. return rt;
  157. }
  158. /*
  159. * [定时任务]批量计算组合净值、收益及指标
  160. *
  161. * @param updatetime <DATETIME>: 持仓证券净值更新时间,忽略时跑全历史
  162. *
  163. *
  164. * Example: CalPortfolioPerformanceTask(2024.10.28);
  165. */
  166. def CalPortfolioPerformanceTask(updatetime=1900.01.01) {
  167. rt = '';
  168. // 3 min
  169. tb_cal_ports = get_portfolio_list_by_fund_nav_updatetime(NULL, updatetime, true);
  170. if(tb_cal_ports.isVoid() || tb_cal_ports.size() == 0) return;
  171. // 26 min
  172. rt = cal_and_save_portfolio_nav(tb_cal_ports);
  173. // 9 min
  174. rt = rt + '; ' + cal_and_save_portfolio_indicators(tb_cal_ports);
  175. return rt;
  176. }