task_monthlyPerformance.dos 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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>: XXXX.YYM
  11. * @param isFromMySQL <BOOL>: false 时读取dolphin本地的收益及指标表,用于初始化数据
  12. *
  13. * NOTE: 在假设公募和私募分类互相独立成立的前提下,之用跑一次即可计算公募和私募的排名
  14. * TODO: BFI ranking
  15. *
  16. * Example: cal_entity_ranking('MF', 2024.09M, true);
  17. */
  18. def CalEntityRanking(entity_type, end_date, isFromMySQL=true) {
  19. /*
  20. entity_type = 'MF';
  21. end_date = 2024.09M;
  22. isFromMySQL = true;
  23. */
  24. entity_info = get_entity_info(entity_type, NULL);
  25. // 收益
  26. v_ranking_tables = cal_ret_ranking(entity_type, entity_info, end_date, isFromMySQL);
  27. save_ranking_tables(entity_type, v_ranking_tables);
  28. // 风险相关的指标
  29. v_ranking_tables = cal_risk_ranking(entity_type, entity_info, end_date, isFromMySQL);
  30. save_ranking_tables(entity_type, v_ranking_tables);
  31. // 风险调整收益指标
  32. v_ranking_tables = cal_risk_adj_return_ranking(entity_type, entity_info, end_date, isFromMySQL);
  33. save_ranking_tables(entity_type, v_ranking_tables);
  34. // 杂项指标
  35. v_ranking_tables = cal_other_indicator_ranking(entity_type, entity_info, end_date, isFromMySQL);
  36. save_ranking_tables(entity_type, v_ranking_tables);
  37. /*
  38. v_tables = NULL;
  39. if(entity_type IN ['MF', 'HF', 'MI', 'FI']) {
  40. v_tables = ['mfdb.fund_performance', 'mfdb.fund_risk_stats', 'mfdb.fund_riskadjret_stats', 'mfdb.fund_indicator', 'mfdb.fund_style_stats'];
  41. } else if(entity_type = 'PF') {
  42. v_tables = ['pfdb.pf_portfolio_performance', 'pfdb.pf_portfolio_risk_stats', 'pfdb.pf_portfolio_riskadjret_stats', 'pfdb.pf_portfolio_indicator', 'pfdb.pf_portfolio_style_stats'];
  43. } else if(entity_type = 'CF') {
  44. v_tables = ['pfdb.pf_cus_fund_performance', 'pfdb.pf_cus_fund_risk_stats', 'pfdb.pf_cus_fund_riskadjret_stats', 'pfdb.pf_cus_fund_indicator', 'pfdb.pf_cus_fund_style_stats'];
  45. } else if(entity_type = 'FA') {
  46. v_tables = ['pfdb.cm_factor_performance', 'pfdb.cm_factor_risk_stats', 'pfdb.cm_factor_riskadjret_stats', 'pfdb.cm_factor_indicator', 'pfdb.cm_factor_style_stats'];
  47. } else if(entity_type = 'CI') {
  48. v_tables = ['pfdb.cm_udf_index_performance', 'pfdb.cm_udf_index_risk_stats', 'pfdb.cm_udf_index_riskadjret_stats', 'pfdb.cm_udf_index_indicator', 'pfdb.cm_udf_index_style_stats'];
  49. }
  50. if(v_tables.isNull()) return null;
  51. for(table in v_tables) {
  52. tb_data = get_monthly_indicator_data(table, end_date, isFromMySQL);
  53. }
  54. */
  55. }
  56. /*
  57. *
  58. * 以公募基金为评级参考,计算组合、私有基金收益及指标排名
  59. *
  60. *
  61. * Example: CalRelativeRanking('PF', 2024.09M, true);
  62. */
  63. def CalRelativeRanking(entity_type, end_date, isFromMySQL=true) {
  64. entity_info = get_entity_info(entity_type, NULL);
  65. if(entity_type == 'PF')
  66. entity_info = SELECT * FROM entity_info WHERE portfolio_type IN (1, 2) // 1: 用户组合、2:客户真实组合,忽略客户推荐组合、总览综合等虚拟组合
  67. v_ranking_by = ['strategy', 'substrategy'/*, 'factor_id'*/];
  68. // 暂时以公募混合基金为排名参考
  69. for(ranking_by in v_ranking_by) {
  70. if(ranking_by == 'strategy') {
  71. v_category = EXEC DISTINCT strategy FROM entity_info WHERE strategy IS NOT NULL;
  72. tb_fund_ranking = get_fund_indicator_ranking(NULL, end_date, v_category, true)
  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, true)
  76. } else if(ranking_by == 'bfi') {
  77. tb_fund_ranking = null;
  78. }
  79. if(tb_fund_ranking == null) return;
  80. tb_fund_ranking.rename!(ranking_by, 'category_id');
  81. // 收益
  82. v_indicator_id = [1];
  83. benchmark_ranking = SELECT * FROM tb_fund_ranking WHERE indicator_id IN v_indicator_id;
  84. // 风险指标
  85. v_indicator_id = [2, 6, 9, 10, 11, 12, 21, 50, 52, 59];
  86. benchmark_ranking = SELECT * FROM tb_fund_ranking WHERE indicator_id IN v_indicator_id;
  87. entity_ranking = transform_risk_stats_for_ranking(entity_type, entity_info, end_date, ranking_by, isFromMySQL).rename!(ranking_by, 'category_id');
  88. cal_relative_ranking(benchmark_ranking, entity_ranking, isFromMySQL);
  89. entity_ranking.rename!('category_id', ranking_by);
  90. save_relative_ranking_table(entity_type, entity_ranking, ranking_by);
  91. // 风险调整收益指标
  92. v_indicator_id = [14, 15, 16, 17, 18, 19, 40, 58];
  93. benchmark_ranking = SELECT * FROM tb_fund_ranking WHERE indicator_id IN v_indicator_id;
  94. entity_ranking = transform_risk_adj_ret_stats_for_ranking(entity_type, entity_info, end_date, ranking_by, isFromMySQL).rename!(ranking_by, 'category_id');
  95. cal_relative_ranking(benchmark_ranking, entity_ranking, isFromMySQL);
  96. entity_ranking.rename!('category_id', ranking_by);
  97. save_relative_ranking_table(entity_type, entity_ranking, ranking_by);
  98. // 其它风险指标
  99. v_indicator_id = [37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 49];
  100. benchmark_ranking = SELECT * FROM tb_fund_ranking WHERE indicator_id IN v_indicator_id;
  101. entity_ranking = transform_other_indicator_for_ranking(entity_type, entity_info, end_date, ranking_by, isFromMySQL).rename!(ranking_by, 'category_id');
  102. cal_relative_ranking(benchmark_ranking, entity_ranking, isFromMySQL);
  103. entity_ranking.rename!('category_id', ranking_by);
  104. save_relative_ranking_table(entity_type, entity_ranking, ranking_by);
  105. }
  106. // SELECT * FROM entity_ranking WHERE entity_id = 143109
  107. }