task_monthlyPerformance.dos 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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'
  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'
  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. * TODO: customer fund
  57. * TODO: 计算单个组合时总耗时1.5min, 大部分时间用来获取Mysql数据
  58. *
  59. * Example: CalRelativeRankingTask('PF', NULL, 2024.09M, true);
  60. * CalRelativeRankingTask('PF', 143109, 2024.09M, true);
  61. */
  62. def CalRelativeRankingTask(entity_type, entity_ids, end_date, isFromMySQL=true) {
  63. // entity_type = 'PF'
  64. // end_date = 2024.09M
  65. // isFromMySQL = true
  66. // ranking_by = 'bfi'
  67. // entity_ids = 143109
  68. entity_info = get_entity_info(entity_type, entity_ids);
  69. if(entity_type == 'PF')
  70. entity_info = SELECT * FROM entity_info WHERE portfolio_type IN (1, 2) // 1: 用户组合、2:客户真实组合,忽略客户推荐组合、总览综合等虚拟组合
  71. v_ranking_by = ['strategy', 'substrategy', 'bfi'];
  72. // 暂时以公募混合基金为排名参考
  73. for(ranking_by in v_ranking_by) {
  74. if(ranking_by == 'strategy') {
  75. v_category = EXEC DISTINCT strategy FROM entity_info WHERE strategy IS NOT NULL;
  76. tb_fund_ranking = get_fund_indicator_ranking(NULL, end_date, v_category, isFromMySQL);
  77. UPDATE tb_fund_ranking SET category_id = strategy$STRING;
  78. } else if(ranking_by == 'substrategy') {
  79. v_category = EXEC DISTINCT substrategy FROM entity_info WHERE substrategy IS NOT NULL;
  80. tb_fund_ranking = get_fund_indicator_substrategy_ranking(NULL, end_date, v_category, isFromMySQL);
  81. UPDATE tb_fund_ranking SET category_id = substrategy$STRING;
  82. } else if(ranking_by == 'bfi') {
  83. if(entity_ids != NULL || entity_ids != '')
  84. v_category = EXEC DISTINCT factor_id FROM get_entity_bfi_factors(entity_type, entity_ids, end_date, end_date);
  85. else
  86. v_category = NULL;
  87. tb_fund_ranking = get_fund_bfi_bm_indicator_ranking(NULL, end_date, v_category, isFromMySQL);
  88. UPDATE tb_fund_ranking SET category_id = factor_id;
  89. }
  90. if(tb_fund_ranking.isVoid() || tb_fund_ranking.size() == 0) return;
  91. entity_ranking = transform_data_for_ranking(entity_type, entity_info, end_date, ranking_by, isFromMySQL);
  92. cal_and_save_relative_ranking(entity_type, tb_fund_ranking, entity_ranking, ranking_by, isFromMySQL);
  93. }
  94. }