task_monthlyPerformance.dos 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. module fundit::task_monthlyPerformance
  2. use fundit::dataPuller;
  3. use fundit::dataSaver;
  4. use fundit::bfiMatcher;
  5. use fundit::rankingCalculator;
  6. /*
  7. * [定时任务] 计算基金一、二级分类排名并存入数据库
  8. *
  9. * @param entity_type <STRING>: 'MF', 'HF'
  10. * @param end_date <MONTH>:
  11. * @param isFromMySQL <BOOL>: false 时读取dolphin本地的收益及指标表,用于初始化数据
  12. *
  13. *
  14. * Example: CalEntityRankingTask('MF', 2024.09M, true);
  15. */
  16. def CalEntityRankingTask(entity_type, end_date, isFromMySQL=true) {
  17. if(!(entity_type in ['MF', 'HF'])) return NULL;
  18. entity_info = get_entity_info(entity_type, NULL);
  19. v_ranking_tables = cal_indicator_ranking('strategy', entity_type, entity_info, end_date, isFromMySQL);
  20. save_ranking_tables(entity_type, v_ranking_tables);
  21. }
  22. /*
  23. * [定时任务] 计算基金BFI排名并存入数据库
  24. *
  25. * @param entity_type <STRING>: 'MF', 'HF'
  26. * @param end_date <MONTH>:
  27. * @param isFromMySQL <BOOL>: false 时读取dolphin本地的收益及指标表,用于初始化数据
  28. *
  29. *
  30. * Example: CalEntityBfiRankingTask('MF', 2024.09M, true);
  31. */
  32. def CalEntityBfiRankingTask(entity_type, end_date, isFromMySQL=true) {
  33. if(!(entity_type in ['MF', 'HF'])) return NULL;
  34. entity_info = get_entity_info(entity_type, NULL);
  35. v_ranking_tables = cal_indicator_ranking('bfi', entity_type, entity_info, end_date, isFromMySQL);
  36. save_ranking_tables(entity_type, v_ranking_tables);
  37. }
  38. /*
  39. * Private Method: 计算相对排名并存入数据库
  40. *
  41. *
  42. */
  43. def cal_and_save_relative_ranking(entity_type, benchmark_ranking, entity_ranking, ranking_by, isFromMySQL=true) {
  44. // benchmark_ranking= tb_fund_ranking
  45. t_entity_ranking = entity_ranking;
  46. cal_relative_ranking(benchmark_ranking, t_entity_ranking, isFromMySQL);
  47. t_entity_ranking.rename!('category_id', iif(ranking_by=='bfi', 'factor_id', ranking_by));
  48. save_relative_ranking_table(entity_type, t_entity_ranking, ranking_by);
  49. }
  50. /*
  51. *
  52. * [定时任务] 以公募基金为评级参考,计算组合、私有基金收益及指标排名
  53. *
  54. * TODO: customer fund
  55. * TODO: 计算单个组合时总耗时1.5min, 大部分时间用来获取Mysql数据
  56. *
  57. * Example: CalRelativeRankingTask('PF', NULL, 2024.09M, true);
  58. * CalRelativeRankingTask('PF', 143109, 2024.09M, true);
  59. */
  60. def CalRelativeRankingTask(entity_type, entity_ids, end_date, isFromMySQL=true) {
  61. // entity_type = 'PF'
  62. // end_date = 2024.09M
  63. // isFromMySQL = true
  64. // ranking_by = 'bfi'
  65. // entity_ids = 143109
  66. entity_info = get_entity_info(entity_type, entity_ids);
  67. if(entity_type == 'PF')
  68. entity_info = SELECT * FROM entity_info WHERE portfolio_type IN (1, 2) // 1: 用户组合、2:客户真实组合,忽略客户推荐组合、总览综合等虚拟组合
  69. v_ranking_by = ['strategy', 'substrategy', 'bfi'];
  70. // 暂时以公募混合基金为排名参考
  71. for(ranking_by in v_ranking_by) {
  72. if(ranking_by == 'strategy') {
  73. v_category = EXEC DISTINCT strategy FROM entity_info WHERE strategy IS NOT NULL;
  74. tb_fund_ranking = get_fund_indicator_ranking(NULL, end_date, v_category, isFromMySQL);
  75. UPDATE tb_fund_ranking SET category_id = strategy$STRING;
  76. } else if(ranking_by == 'substrategy') {
  77. v_category = EXEC DISTINCT substrategy FROM entity_info WHERE substrategy IS NOT NULL;
  78. tb_fund_ranking = get_fund_indicator_substrategy_ranking(NULL, end_date, v_category, isFromMySQL);
  79. UPDATE tb_fund_ranking SET category_id = substrategy$STRING;
  80. } else if(ranking_by == 'bfi') {
  81. if(entity_ids != NULL || entity_ids != '')
  82. v_category = EXEC DISTINCT factor_id FROM get_portfolio_bfi_factors(entity_ids, end_date.temporalFormat('yyyy-MM'), end_date.temporalFormat('yyyy-MM'));
  83. else
  84. v_category = NULL;
  85. tb_fund_ranking = get_fund_bfi_bm_indicator_ranking(NULL, end_date, v_category, isFromMySQL);
  86. UPDATE tb_fund_ranking SET category_id = factor_id;
  87. }
  88. if(tb_fund_ranking.isVoid() || tb_fund_ranking.size() == 0) return;
  89. entity_ranking = transform_data_for_ranking(entity_type, entity_info, end_date, ranking_by, isFromMySQL);
  90. cal_and_save_relative_ranking(entity_type, tb_fund_ranking, entity_ranking, ranking_by, isFromMySQL);
  91. }
  92. }