task_monthlyPerformance.dos 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. *
  56. * Example: CalRelativeRanking('PF', 2024.09M, true);
  57. */
  58. def CalRelativeRanking(entity_type, end_date, isFromMySQL=true) {
  59. // entity_type = 'PF'
  60. // end_date = 2024.09M
  61. // isFromMySQL = true
  62. // ranking_by = 'bfi'
  63. entity_info = get_entity_info(entity_type, NULL);
  64. if(entity_type == 'PF')
  65. entity_info = SELECT * FROM entity_info WHERE portfolio_type IN (1, 2) // 1: 用户组合、2:客户真实组合,忽略客户推荐组合、总览综合等虚拟组合
  66. v_ranking_by = ['strategy', 'substrategy', 'bfi'];
  67. // 暂时以公募混合基金为排名参考
  68. for(ranking_by in v_ranking_by) {
  69. if(ranking_by == 'strategy') {
  70. v_category = EXEC DISTINCT strategy FROM entity_info WHERE strategy IS NOT NULL;
  71. tb_fund_ranking = get_fund_indicator_ranking(NULL, end_date, v_category, isFromMySQL);
  72. UPDATE tb_fund_ranking SET category_id = strategy$STRING;
  73. } else if(ranking_by == 'substrategy') {
  74. v_category = EXEC DISTINCT substrategy FROM entity_info WHERE substrategy IS NOT NULL;
  75. tb_fund_ranking = get_fund_indicator_substrategy_ranking(NULL, end_date, v_category, isFromMySQL);
  76. UPDATE tb_fund_ranking SET category_id = substrategy$STRING;
  77. } else if(ranking_by == 'bfi') {
  78. tb_fund_ranking = get_fund_bfi_bm_indicator_ranking(NULL, end_date, NULL, isFromMySQL);
  79. UPDATE tb_fund_ranking SET category_id = factor_id;
  80. }
  81. if(tb_fund_ranking.isVoid() || tb_fund_ranking.size() == 0) return;
  82. entity_ranking = transform_data_for_ranking(entity_type, entity_info, end_date, ranking_by, isFromMySQL);
  83. cal_and_save_relative_ranking(entity_type, tb_fund_ranking, entity_ranking, ranking_by, isFromMySQL);
  84. }
  85. // SELECT * FROM entity_ranking WHERE entity_id = 143109
  86. }