task_fundPerformance.dos 12 KB

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