task_monthlyPerformance.dos 4.3 KB

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