task_fundPerformance.dos 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. module fundit::task_fundPerformance
  2. use fundit::dataPuller;
  3. use fundit::dataSaver;
  4. use fundit::returnCalculator;
  5. use fundit::indicatorCalculator;
  6. /*
  7. * [定时任务]:最新净值触发的业绩指标计算
  8. *
  9. * @param entityType <STRING>: 'MF', 'HF'...
  10. * @param date <DATETIME>: 净值更新时间, 为空时缺省为当前时间-1天;为1990.01.01或更早日期时代表初始化,指标会被存入本地数据库
  11. *
  12. * NOTE: 与Java不同的是当月indicator计算每日触发,不必等到Month-end production
  13. *
  14. * Example: calFundPerformanceTask('MF', 2024.10.28);
  15. * calFundPerformanceTask('MI', 2024.10.28);
  16. */
  17. def calFundPerformanceTask(entityType, date) {
  18. rt = '';
  19. if(!(entityType IN ['MF', 'HF', 'MI', 'FI'])) return null;
  20. if(date.isNothing() || date.isNull())
  21. end_day = temporalAdd(now(), -1d);
  22. else
  23. end_day = date;
  24. // 取有最新净值变动的基金列表 (1s)
  25. tb_cal_funds = get_entity_list_by_nav_updatetime(entityType, NULL, end_day, true);
  26. if(tb_cal_funds.isVoid() || tb_cal_funds.size() == 0 ) return;
  27. // 按照 MySQL 建好各表
  28. tb_fund_performance = create_entity_performance();
  29. tb_fund_indicator = create_entity_indicator();
  30. tb_fund_risk_stats = create_entity_risk_stats();
  31. tb_fund_riskadjret_stats = create_entity_riskadjret_stats();
  32. tb_fund_style_stats = create_entity_style_stats();
  33. tb_fund_performance_weekly = create_entity_performance_weekly();
  34. tb_fund_latest_performance = create_entity_latest_performance();
  35. // 分批跑
  36. i = 0;
  37. batch_size = 1000;
  38. do {
  39. funds = tb_cal_funds[i : min(tb_cal_funds.size(), i+batch_size)];
  40. if(funds.isVoid() || funds.size() == 0) break;
  41. // 200ms
  42. fund_info = SELECT entity_id, price_date, inception_date, benchmark_id, ini_value
  43. FROM ej(funds, get_entity_info(entityType, funds.entity_id), 'entity_id');
  44. // 计算月收益 (12s)
  45. rets = mix_monthly_returns(entityType, fund_info);
  46. if(!rets.isVoid() && rets.size() > 0) {
  47. // 计算月度指标 (56s)
  48. rets.rename!('cumulative_nav', 'nav');
  49. indicators = cal_monthly_indicators(entityType, 'PBI', rets);
  50. // 仿照MySQL的表结构准备好记录 (1s)
  51. generate_entity_performance(fund_info, indicators, true, tb_fund_performance);
  52. generate_entity_indicator(fund_info, indicators, true, tb_fund_indicator);
  53. generate_entity_risk_stats(fund_info, indicators, true, tb_fund_risk_stats);
  54. generate_entity_riskadjret_stats(fund_info, indicators, true, tb_fund_riskadjret_stats);
  55. generate_entity_style_stats(fund_info, indicators, true, tb_fund_style_stats);
  56. }
  57. // 计算周收益 (8s)
  58. rets_w = cal_weekly_returns(entityType, fund_info);
  59. if(! rets_w.isVoid() && rets_w.size() > 0) {
  60. generate_entity_performance_weekly(fund_info, rets_w, true, tb_fund_performance_weekly);
  61. }
  62. // 计算最新收益 (69s)
  63. perf_latest = cal_latest_performance(entityType, fund_info, true);
  64. if(! perf_latest.isVoid() && perf_latest.size() > 0) {
  65. generate_entity_latest_performance(fund_info, perf_latest, true, tb_fund_latest_performance);
  66. }
  67. i += batch_size;
  68. // } while (i < batch_size);
  69. } while (i <= tb_cal_funds.size());
  70. if(! tb_fund_performance.isVoid() && tb_fund_performance.size() > 0) {
  71. // save data to MySQL (13s)
  72. try {
  73. chg_columns_for_mysql(tb_fund_performance, 'fund_id');
  74. save_and_sync(tb_fund_performance, 'raw_db.fund_performance', 'raw_db.fund_performance');
  75. chg_columns_for_mysql(tb_fund_indicator, 'fund_id');
  76. save_and_sync(tb_fund_indicator, 'raw_db.fund_indicator', 'raw_db.fund_indicator');
  77. chg_columns_for_mysql(tb_fund_risk_stats, 'fund_id');
  78. // mfdb.fund_performance 表中 maxdrawdown_6m & maxdrawdown_ytd 是虚拟列,这里用数据列顺序强行写入真实列 6m_maxdrawdown & ytd_maxdrawdown (DolphinDB 不允许字段名以数字开头)
  79. save_and_sync(tb_fund_risk_stats, 'raw_db.fund_risk_stats', 'raw_db.fund_risk_stats');
  80. chg_columns_for_mysql(tb_fund_riskadjret_stats, 'fund_id');
  81. save_and_sync(tb_fund_riskadjret_stats, 'raw_db.fund_riskadjret_stats', 'raw_db.fund_riskadjret_stats');
  82. chg_columns_for_mysql(tb_fund_style_stats, 'fund_id');
  83. save_and_sync(tb_fund_style_stats, 'raw_db.fund_style_stats', 'raw_db.fund_style_stats');
  84. save_and_sync(tb_fund_performance_weekly, 'raw_db.fund_performance_weekly', 'raw_db.fund_performance_weekly');
  85. save_and_sync(tb_fund_latest_performance, 'raw_db.fund_latest_performance', 'raw_db.fund_latest_nav_performance');
  86. // 数据初始化时将指标存入本地,做排名之用
  87. if(end_day <= 1990.01.01) {
  88. save_table(tb_fund_performance, 'raw_db.fund_performance', false);
  89. save_table(tb_fund_indicator, 'raw_db.fund_indicator', false);
  90. save_table(tb_fund_risk_stats, 'raw_db.fund_risk_stats', false);
  91. save_table(tb_fund_riskadjret_stats, 'raw_db.fund_riskadjret_stats', false);
  92. save_table(tb_fund_style_stats, 'raw_db.fund_style_stats', false);
  93. }
  94. } catch(ex) {
  95. //TODO: Log errors
  96. rt = ex;
  97. }
  98. }
  99. return rt;
  100. }
  101. /*
  102. * 【临时】用于数据初始化:只计算收益
  103. *
  104. * @param entityType <STRING>: 'MF', 'HF'...
  105. * @param date <DATETIME>: 净值更新时间
  106. *
  107. */
  108. def ms_calFundReturns() {
  109. rt = '';
  110. very_old_date = 1990.01.01;
  111. // 取基金列表 (27s)
  112. tb_cal_funds = ms_get_fund_list_by_nav_updatetime(NULL, very_old_date);
  113. if(tb_cal_funds.isVoid() || tb_cal_funds.size() == 0 ) return;
  114. tb_fund_performance = create_entity_performance();
  115. tb_fund_indicator = create_entity_indicator();
  116. tb_fund_risk_stats = create_entity_risk_stats();
  117. tb_fund_riskadjret_stats = create_entity_riskadjret_stats();
  118. tb_fund_style_stats = create_entity_style_stats();
  119. tb_fund_performance_weekly = create_entity_performance_weekly();
  120. tb_fund_latest_performance = create_entity_latest_performance();
  121. // 分批跑
  122. i = 0;
  123. batch_size = 1000;
  124. do {
  125. funds = tb_cal_funds[i : min(tb_cal_funds.size(), i+batch_size)];
  126. if(funds.isVoid() || funds.size() == 0) break;
  127. // 200ms
  128. fund_info = SELECT entity_id, price_date, inception_date, benchmark_id, ini_value
  129. FROM ej(funds, ms_get_fund_info(funds.entity_id), 'entity_id', 'fund_id');
  130. // 计算月收益 (19s)
  131. tb_nav = ms_get_fund_monthly_nav(fund_info.entity_id);
  132. rets = cal_monthly_returns_by_nav(fund_info, tb_nav);
  133. if(!rets.isVoid() && rets.size() > 0) {
  134. // 计算月度指标 (67s)
  135. rets.rename!('cumulative_nav', 'nav');
  136. indicators = cal_monthly_indicators('MF', 'PBI', rets);
  137. // 仿照MySQL的表结构准备好记录 (1s)
  138. generate_entity_performance(fund_info, indicators, true, tb_fund_performance);
  139. generate_entity_indicator(fund_info, indicators, true, tb_fund_indicator);
  140. generate_entity_risk_stats(fund_info, indicators, true, tb_fund_risk_stats);
  141. generate_entity_riskadjret_stats(fund_info, indicators, true, tb_fund_riskadjret_stats);
  142. generate_entity_style_stats(fund_info, indicators, true, tb_fund_style_stats);
  143. }
  144. // 计算周收益 (49s)
  145. rets_w = cal_weekly_returns('MF', fund_info);
  146. if(! rets_w.isVoid() && rets_w.size() > 0) {
  147. generate_entity_performance_weekly(fund_info, rets_w, true, tb_fund_performance_weekly);
  148. }
  149. // 计算最新收益 (23s)
  150. perf_latest = cal_latest_performance('MF', fund_info, true);
  151. if(! perf_latest.isVoid() && perf_latest.size() > 0) {
  152. generate_entity_latest_performance(fund_info, perf_latest, true, tb_fund_latest_performance);
  153. }
  154. i += batch_size;
  155. // } while (i < batch_size);
  156. } while (i <= tb_cal_funds.size());
  157. if(! tb_fund_performance.isVoid() && tb_fund_performance.size() > 0) {
  158. // save data to MySQL (26m)
  159. try {
  160. chg_columns_for_mysql(tb_fund_performance, 'fund_id');
  161. save_and_sync(tb_fund_performance, 'raw_db.fund_performance', 'raw_db.fund_performance');
  162. chg_columns_for_mysql(tb_fund_indicator, 'fund_id');
  163. save_and_sync(tb_fund_indicator, 'raw_db.fund_indicator', 'raw_db.fund_indicator');
  164. chg_columns_for_mysql(tb_fund_risk_stats, 'fund_id');
  165. // mfdb.fund_performance 表中 maxdrawdown_6m & maxdrawdown_ytd 是虚拟列,这里用数据列顺序强行写入真实列 6m_maxdrawdown & ytd_maxdrawdown (DolphinDB 不允许字段名以数字开头)
  166. save_and_sync(tb_fund_risk_stats, 'raw_db.fund_risk_stats', 'raw_db.fund_risk_stats');
  167. chg_columns_for_mysql(tb_fund_riskadjret_stats, 'fund_id');
  168. save_and_sync(tb_fund_riskadjret_stats, 'raw_db.fund_riskadjret_stats', 'raw_db.fund_riskadjret_stats');
  169. chg_columns_for_mysql(tb_fund_style_stats, 'fund_id');
  170. save_and_sync(tb_fund_style_stats, 'raw_db.fund_style_stats', 'raw_db.fund_style_stats');
  171. save_and_sync(tb_fund_performance_weekly, 'raw_db.fund_performance_weekly', 'raw_db.fund_performance_weekly');
  172. save_and_sync(tb_fund_latest_performance, 'raw_db.fund_latest_performance', 'raw_db.fund_latest_nav_performance');
  173. } catch(ex) {
  174. //TODO: Log errors
  175. rt = ex;
  176. }
  177. }
  178. return rt;
  179. }
  180. /*
  181. * 实验性质的API
  182. *
  183. *
  184. */
  185. def calFundIndexCorrelation(entityType, date) {
  186. if(find(['HF', 'MF'], entityType) < 0) return null;
  187. // 取有最新净值变动的基金列表 (1s)
  188. tb_cal_funds = get_entity_list_by_nav_updatetime(entityType, NULL, date, true);
  189. if(tb_cal_funds.isVoid() || tb_cal_funds.size() == 0 ) return;
  190. // tb_fund_index_coe = create_entity_index_coe();
  191. // (7m)
  192. coe = cal_entity_index_coe(entityType, tb_cal_funds[0:1000]);
  193. return coe;
  194. }