rankingCalculator.dos 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. module fundit::rankingCalculator
  2. use fundit::sqlUtilities
  3. use fundit::dataPuller
  4. use fundit::dataSaver
  5. /*
  6. * 计算收益率排名
  7. *
  8. * TODO: 整合入 gen_ranking_sql
  9. */
  10. def cal_ret_ranking(entity_type, entity_info, end_date, isFromMySQL) {
  11. table_desc = get_performance_table_description(entity_type);
  12. tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  13. sec_id_col = table_desc.sec_id_col[0];
  14. tb_data.rename!(sec_id_col, 'entity_id');
  15. tb_strategy = get_strategy_list();
  16. tb_substrategy = get_substrategy_list();
  17. t = SELECT *
  18. FROM entity_info en
  19. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  20. WHERE en.strategy IS NOT NULL
  21. AND (en.entity_id LIKE 'MF%' OR en.entity_id LIKE 'HF%')
  22. // 按照 MySQL 字段建表
  23. t_s = create_entity_indicator_ranking(false);
  24. t_s_num = create_entity_indicator_ranking_num(false);
  25. t_ss = create_entity_indicator_substrategy_ranking(false);
  26. t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
  27. v_tables = [t_s, t_s_num, t_ss, t_ss_num];
  28. v_tables[0] = SELECT entity_id, end_date, strategy, 1 AS indicator_id,
  29. ret_1m AS indicator_1m, ret_1m.rank(false) AS absrank_1m, (ret_1m.rank(false, percent=true)*100).round(0) AS perrank_1m,
  30. ret_3m AS indicator_3m, ret_3m.rank(false) AS absrank_3m, (ret_3m.rank(false, percent=true)*100).round(0) AS perrank_3m,
  31. ret_6m AS indicator_6m, ret_6m.rank(false) AS absrank_6m, (ret_6m.rank(false, percent=true)*100).round(0) AS perrank_6m,
  32. ret_1y AS indicator_1y, ret_1y.rank(false) AS absrank_1y, (ret_1y.rank(false, percent=true)*100).round(0) AS perrank_1y,
  33. ret_2y AS indicator_2y, ret_2y.rank(false) AS absrank_2y, (ret_2y.rank(false, percent=true)*100).round(0) AS perrank_2y,
  34. ret_3y AS indicator_3y, ret_3y.rank(false) AS absrank_3y, (ret_3y.rank(false, percent=true)*100).round(0) AS perrank_3y,
  35. ret_5y AS indicator_5y, ret_5y.rank(false) AS absrank_5y, (ret_5y.rank(false, percent=true)*100).round(0) AS perrank_5y,
  36. ret_10y AS indicator_10y, ret_10y.rank(false) AS absrank_10y, (ret_10y.rank(false, percent=true)*100).round(0) AS perrank_10y,
  37. ret_ytd AS indicator_ytd, ret_ytd.rank(false) AS absrank_ytd, (ret_ytd.rank(false, percent=true)*100).round(0) AS perrank_ytd
  38. FROM t CONTEXT BY strategy, end_date;
  39. v_tables[1] = SELECT t.end_date, t.strategy, s.raise_type[0], 1 AS indicator_id,
  40. ret_1m.mean() AS avg_1m, ret_1m.count() AS avg_1m_cnt, ret_1m.percentile(95) AS perrank_percent_5_1m,
  41. ret_1m.percentile(90) AS perrank_percent_10_1m, ret_1m.percentile(75) AS perrank_percent_25_1m,
  42. ret_1m.percentile(50) AS perrank_percent_50_1m, ret_1m.percentile(25) AS perrank_percent_75_1m,
  43. ret_1m.percentile(10) AS perrank_percent_90_1m, ret_1m.percentile(5) AS perrank_percent_95_1m,
  44. ret_1m.max() AS best_1m, ret_1m.min() AS worst_1m,
  45. ret_3m.mean() AS avg_3m, ret_3m.count() AS avg_3m_cnt, ret_3m.percentile(95) AS perrank_percent_5_3m,
  46. ret_3m.percentile(90) AS perrank_percent_10_3m, ret_3m.percentile(75) AS perrank_percent_25_3m,
  47. ret_3m.percentile(50) AS perrank_percent_50_3m, ret_3m.percentile(25) AS perrank_percent_75_3m,
  48. ret_3m.percentile(10) AS perrank_percent_90_3m, ret_3m.percentile(5) AS perrank_percent_95_3m,
  49. ret_3m.max() AS best_3m, ret_3m.min() AS worst_3m,
  50. ret_6m.mean() AS avg_6m, ret_6m.count() AS avg_6m_cnt, ret_6m.percentile(95) AS perrank_percent_5_6m,
  51. ret_6m.percentile(90) AS perrank_percent_10_6m, ret_6m.percentile(75) AS perrank_percent_25_6m,
  52. ret_6m.percentile(50) AS perrank_percent_50_6m, ret_6m.percentile(25) AS perrank_percent_75_6m,
  53. ret_6m.percentile(10) AS perrank_percent_90_6m, ret_6m.percentile(5) AS perrank_percent_95_6m,
  54. ret_6m.max() AS best_6m, ret_6m.min() AS worst_6m,
  55. ret_1y.mean() AS avg_1y, ret_1y.count() AS avg_1y_cnt, ret_1y.percentile(95) AS perrank_percent_5_1y,
  56. ret_1y.percentile(90) AS perrank_percent_10_1y, ret_1y.percentile(75) AS perrank_percent_25_1y,
  57. ret_1y.percentile(50) AS perrank_percent_50_1y, ret_1y.percentile(25) AS perrank_percent_75_1y,
  58. ret_1y.percentile(10) AS perrank_percent_90_1y, ret_1y.percentile(5) AS perrank_percent_95_1y,
  59. ret_1y.max() AS best_1y, ret_1y.min() AS worst_1y,
  60. ret_2y.mean() AS avg_2y, ret_2y.count() AS avg_2y_cnt, ret_2y.percentile(95) AS perrank_percent_5_2y,
  61. ret_2y.percentile(90) AS perrank_percent_10_2y, ret_2y.percentile(75) AS perrank_percent_25_2y,
  62. ret_2y.percentile(50) AS perrank_percent_50_2y, ret_2y.percentile(25) AS perrank_percent_75_2y,
  63. ret_2y.percentile(10) AS perrank_percent_90_2y, ret_2y.percentile(5) AS perrank_percent_95_2y,
  64. ret_2y.max() AS best_2y, ret_2y.min() AS worst_2y,
  65. ret_3y.mean() AS avg_3y, ret_3y.count() AS avg_3y_cnt, ret_3y.percentile(95) AS perrank_percent_5_3y,
  66. ret_3y.percentile(90) AS perrank_percent_10_3y, ret_3y.percentile(75) AS perrank_percent_25_3y,
  67. ret_3y.percentile(50) AS perrank_percent_50_3y, ret_3y.percentile(25) AS perrank_percent_75_3y,
  68. ret_3y.percentile(10) AS perrank_percent_90_3y, ret_3y.percentile(5) AS perrank_percent_95_3y,
  69. ret_3y.max() AS best_3y, ret_3y.min() AS worst_3y,
  70. ret_5y.mean() AS avg_5y, ret_5y.count() AS avg_5y_cnt, ret_5y.percentile(95) AS perrank_percent_5_5y,
  71. ret_5y.percentile(90) AS perrank_percent_10_5y, ret_5y.percentile(75) AS perrank_percent_25_5y,
  72. ret_5y.percentile(50) AS perrank_percent_50_5y, ret_5y.percentile(25) AS perrank_percent_75_5y,
  73. ret_5y.percentile(10) AS perrank_percent_90_5y, ret_5y.percentile(5) AS perrank_percent_95_5y,
  74. ret_5y.max() AS best_5y, ret_5y.min() AS worst_5y,
  75. ret_10y.mean() AS avg_10y, ret_10y.count() AS avg_10y_cnt, ret_10y.percentile(95) AS perrank_percent_5_10y,
  76. ret_10y.percentile(90) AS perrank_percent_10_10y, ret_10y.percentile(75) AS perrank_percent_25_10y,
  77. ret_10y.percentile(50) AS perrank_percent_50_10y, ret_10y.percentile(25) AS perrank_percent_75_10y,
  78. ret_10y.percentile(10) AS perrank_percent_90_10y, ret_10y.percentile(5) AS perrank_percent_95_10y,
  79. ret_10y.max() AS best_10y, ret_10y.min() AS worst_10y,
  80. ret_ytd.mean() AS avg_ytd, ret_ytd.count() AS avg_ytd_cnt, ret_ytd.percentile(95) AS perrank_percent_5_ytd,
  81. ret_ytd.percentile(90) AS perrank_percent_10_ytd, ret_ytd.percentile(75) AS perrank_percent_25_ytd,
  82. ret_ytd.percentile(50) AS perrank_percent_50_ytd, ret_ytd.percentile(25) AS perrank_percent_75_ytd,
  83. ret_ytd.percentile(10) AS perrank_percent_90_ytd, ret_ytd.percentile(5) AS perrank_percent_95_ytd,
  84. ret_ytd.max() AS best_ytd, ret_ytd.min() AS worst_ytd
  85. FROM t
  86. INNER JOIN tb_strategy s ON t.strategy = s.strategy_id
  87. GROUP BY t.strategy, t.end_date;
  88. v_tables[2] = SELECT entity_id, end_date, substrategy, 1 AS indicator_id,
  89. ret_1m AS indicator_1m, ret_1m.rank(false) AS absrank_1m, (ret_1m.rank(false, percent=true)*100).round(0) AS perrank_1m,
  90. ret_3m AS indicator_3m, ret_3m.rank(false) AS absrank_3m, (ret_3m.rank(false, percent=true)*100).round(0) AS perrank_3m,
  91. ret_6m AS indicator_6m, ret_6m.rank(false) AS absrank_6m, (ret_6m.rank(false, percent=true)*100).round(0) AS perrank_6m,
  92. ret_1y AS indicator_1y, ret_1y.rank(false) AS absrank_1y, (ret_1y.rank(false, percent=true)*100).round(0) AS perrank_1y,
  93. ret_2y AS indicator_2y, ret_2y.rank(false) AS absrank_2y, (ret_2y.rank(false, percent=true)*100).round(0) AS perrank_2y,
  94. ret_3y AS indicator_3y, ret_3y.rank(false) AS absrank_3y, (ret_3y.rank(false, percent=true)*100).round(0) AS perrank_3y,
  95. ret_5y AS indicator_5y, ret_5y.rank(false) AS absrank_5y, (ret_5y.rank(false, percent=true)*100).round(0) AS perrank_5y,
  96. ret_10y AS indicator_10y, ret_10y.rank(false) AS absrank_10y, (ret_10y.rank(false, percent=true)*100).round(0) AS perrank_10y,
  97. ret_ytd AS indicator_ytd, ret_ytd.rank(false) AS absrank_ytd, (ret_ytd.rank(false, percent=true)*100).round(0) AS perrank_ytd
  98. FROM t CONTEXT BY substrategy, end_date;
  99. v_tables[3] = SELECT t.end_date, t.substrategy, s.raise_type[0], 1 AS indicator_id,
  100. ret_1m.mean() AS avg_1m, ret_1m.count() AS avg_1m_cnt, ret_1m.percentile(95) AS perrank_percent_5_1m,
  101. ret_1m.percentile(90) AS perrank_percent_10_1m, ret_1m.percentile(75) AS perrank_percent_25_1m,
  102. ret_1m.percentile(50) AS perrank_percent_50_1m, ret_1m.percentile(25) AS perrank_percent_75_1m,
  103. ret_1m.percentile(10) AS perrank_percent_90_1m, ret_1m.percentile(5) AS perrank_percent_95_1m,
  104. ret_1m.max() AS best_1m, ret_1m.min() AS worst_1m,
  105. ret_3m.mean() AS avg_3m, ret_3m.count() AS avg_3m_cnt, ret_3m.percentile(95) AS perrank_percent_5_3m,
  106. ret_3m.percentile(90) AS perrank_percent_10_3m, ret_3m.percentile(75) AS perrank_percent_25_3m,
  107. ret_3m.percentile(50) AS perrank_percent_50_3m, ret_3m.percentile(25) AS perrank_percent_75_3m,
  108. ret_3m.percentile(10) AS perrank_percent_90_3m, ret_3m.percentile(5) AS perrank_percent_95_3m,
  109. ret_3m.max() AS best_3m, ret_3m.min() AS worst_3m,
  110. ret_6m.mean() AS avg_6m, ret_6m.count() AS avg_6m_cnt, ret_6m.percentile(95) AS perrank_percent_5_6m,
  111. ret_6m.percentile(90) AS perrank_percent_10_6m, ret_6m.percentile(75) AS perrank_percent_25_6m,
  112. ret_6m.percentile(50) AS perrank_percent_50_6m, ret_6m.percentile(25) AS perrank_percent_75_6m,
  113. ret_6m.percentile(10) AS perrank_percent_90_6m, ret_6m.percentile(5) AS perrank_percent_95_6m,
  114. ret_6m.max() AS best_6m, ret_6m.min() AS worst_6m,
  115. ret_1y.mean() AS avg_1y, ret_1y.count() AS avg_1y_cnt, ret_1y.percentile(95) AS perrank_percent_5_1y,
  116. ret_1y.percentile(90) AS perrank_percent_10_1y, ret_1y.percentile(75) AS perrank_percent_25_1y,
  117. ret_1y.percentile(50) AS perrank_percent_50_1y, ret_1y.percentile(25) AS perrank_percent_75_1y,
  118. ret_1y.percentile(10) AS perrank_percent_90_1y, ret_1y.percentile(5) AS perrank_percent_95_1y,
  119. ret_1y.max() AS best_1y, ret_1y.min() AS worst_1y,
  120. ret_2y.mean() AS avg_2y, ret_2y.count() AS avg_2y_cnt, ret_2y.percentile(95) AS perrank_percent_5_2y,
  121. ret_2y.percentile(90) AS perrank_percent_10_2y, ret_2y.percentile(75) AS perrank_percent_25_2y,
  122. ret_2y.percentile(50) AS perrank_percent_50_2y, ret_2y.percentile(25) AS perrank_percent_75_2y,
  123. ret_2y.percentile(10) AS perrank_percent_90_2y, ret_2y.percentile(5) AS perrank_percent_95_2y,
  124. ret_2y.max() AS best_2y, ret_2y.min() AS worst_2y,
  125. ret_3y.mean() AS avg_3y, ret_3y.count() AS avg_3y_cnt, ret_3y.percentile(95) AS perrank_percent_5_3y,
  126. ret_3y.percentile(90) AS perrank_percent_10_3y, ret_3y.percentile(75) AS perrank_percent_25_3y,
  127. ret_3y.percentile(50) AS perrank_percent_50_3y, ret_3y.percentile(25) AS perrank_percent_75_3y,
  128. ret_3y.percentile(10) AS perrank_percent_90_3y, ret_3y.percentile(5) AS perrank_percent_95_3y,
  129. ret_3y.max() AS best_3y, ret_3y.min() AS worst_3y,
  130. ret_5y.mean() AS avg_5y, ret_5y.count() AS avg_5y_cnt, ret_5y.percentile(95) AS perrank_percent_5_5y,
  131. ret_5y.percentile(90) AS perrank_percent_10_5y, ret_5y.percentile(75) AS perrank_percent_25_5y,
  132. ret_5y.percentile(50) AS perrank_percent_50_5y, ret_5y.percentile(25) AS perrank_percent_75_5y,
  133. ret_5y.percentile(10) AS perrank_percent_90_5y, ret_5y.percentile(5) AS perrank_percent_95_5y,
  134. ret_5y.max() AS best_5y, ret_5y.min() AS worst_5y,
  135. ret_10y.mean() AS avg_10y, ret_10y.count() AS avg_10y_cnt, ret_10y.percentile(95) AS perrank_percent_5_10y,
  136. ret_10y.percentile(90) AS perrank_percent_10_10y, ret_10y.percentile(75) AS perrank_percent_25_10y,
  137. ret_10y.percentile(50) AS perrank_percent_50_10y, ret_10y.percentile(25) AS perrank_percent_75_10y,
  138. ret_10y.percentile(10) AS perrank_percent_90_10y, ret_10y.percentile(5) AS perrank_percent_95_10y,
  139. ret_10y.max() AS best_10y, ret_10y.min() AS worst_10y,
  140. ret_ytd.mean() AS avg_ytd, ret_ytd.count() AS avg_ytd_cnt, ret_ytd.percentile(95) AS perrank_percent_5_ytd,
  141. ret_ytd.percentile(90) AS perrank_percent_10_ytd, ret_ytd.percentile(75) AS perrank_percent_25_ytd,
  142. ret_ytd.percentile(50) AS perrank_percent_50_ytd, ret_ytd.percentile(25) AS perrank_percent_75_ytd,
  143. ret_ytd.percentile(10) AS perrank_percent_90_ytd, ret_ytd.percentile(5) AS perrank_percent_95_ytd,
  144. ret_ytd.max() AS best_ytd, ret_ytd.min() AS worst_ytd
  145. FROM t
  146. INNER JOIN tb_substrategy s ON t.substrategy = s.substrategy_id
  147. GROUP BY t.substrategy, t.end_date;
  148. return v_tables;
  149. }
  150. /*
  151. * 自定义百分位计算
  152. *
  153. */
  154. defg perRank(x, is_ASC) {
  155. return (100 * x.rank(ascending=is_ASC, percent=true)).round(0);
  156. }
  157. /*
  158. * 动态生成用于排序的SQL脚本
  159. *
  160. * @param indicator_name <STRING>: 指标字段名
  161. * @param indicator_id <INT>:指标ID
  162. * @param is_ASC <BOOL>: 是否排正序
  163. * @param ranking_by <STRING>: 'strategy', 'substrategy', 'factor', 'catavg'
  164. *
  165. * TODO: bfi & category
  166. *
  167. */
  168. def gen_ranking_sql(data_table, indicator_name, indicator_id, is_ASC, ranking_by) {
  169. // 近1月和近3月排名仅对收益有效,为了满足表结构的要求,需要建立几个”假”字段,并用NULL赋值
  170. t_tmp = table(1000:0, ['indicator_id', 'indicator_1m', 'absrank_1m', 'perrank_1m',
  171. 'indicator_3m', 'absrank_3m', 'perrank_3m'],
  172. [INT, DOUBLE, INT, INT, DOUBLE, INT, INT]);
  173. INSERT INTO t_tmp VALUES (indicator_id, double(NULL), int(NULL), int(NULL), double(NULL), int(NULL), int(NULL));
  174. // 因为 parseExpr 没法将表 data_table 传入,所以用 sql()
  175. t_ranking = sql(select = (sqlCol('entity_id'), sqlCol('end_date'), sqlCol(ranking_by), sqlCol('indicator_id'),
  176. sqlCol('indicator_1m'), sqlCol('absrank_1m'), sqlCol('perrank_1m'),
  177. sqlCol('indicator_3m'), sqlCol('absrank_3m'), sqlCol('perrank_3m'),
  178. // 与 MySQL 不同,这里统一把近4年和成立以来的排名去掉
  179. sqlCol(indicator_name + '_6m',,'indicator_6m'),
  180. sqlCol(indicator_name + '_6m', rank{, is_ASC}, 'absrank_6m'),
  181. sqlCol(indicator_name + '_6m', perRank{, is_ASC}, 'perrank_6m'),
  182. sqlCol(indicator_name + '_1y',,'indicator_1y'),
  183. sqlCol(indicator_name + '_1y', rank{, is_ASC}, 'absrank_1y'),
  184. sqlCol(indicator_name + '_1y', perRank{, is_ASC}, 'perrank_1y'),
  185. sqlCol(indicator_name + '_2y',,'indicator_2y'),
  186. sqlCol(indicator_name + '_2y', rank{, is_ASC}, 'absrank_2y'),
  187. sqlCol(indicator_name + '_2y', perRank{, is_ASC}, 'perrank_2y'),
  188. sqlCol(indicator_name + '_3y',,'indicator_3y'),
  189. sqlCol(indicator_name + '_3y', rank{, is_ASC}, 'absrank_3y'),
  190. sqlCol(indicator_name + '_3y', perRank{, is_ASC}, 'perrank_3y'),
  191. sqlCol(indicator_name + '_5y',,'indicator_5y'),
  192. sqlCol(indicator_name + '_5y', rank{, is_ASC}, 'absrank_5y'),
  193. sqlCol(indicator_name + '_5y', perRank{, is_ASC}, 'perrank_5y'),
  194. sqlCol(indicator_name + '_10y',,'indicator_10y'),
  195. sqlCol(indicator_name + '_10y', rank{, is_ASC}, 'absrank_10y'),
  196. sqlCol(indicator_name + '_10y', perRank{, is_ASC}, 'perrank_10y'),
  197. sqlCol(indicator_name + '_ytd',,'indicator_ytd'),
  198. sqlCol(indicator_name + '_ytd', rank{, is_ASC}, 'absrank_ytd'),
  199. sqlCol(indicator_name + '_ytd', perRank{, is_ASC}, 'perrank_ytd')
  200. ),
  201. from = cj(data_table, t_tmp),
  202. where = <_$ranking_by IS NOT NULL>,
  203. groupBy = (sqlCol(ranking_by), sqlCol('end_date')),
  204. groupFlag = 0 ).eval(); // context by
  205. // 近1月和近3月排名仅对收益有效,为了满足表结构的要求,需要建立几个”假”字段,并用NULL赋值
  206. t_tmp = table(1000:0, ['indicator_id', 'avg_1m', 'avg_1m_cnt', 'perrank_percent_5_1m', 'perrank_percent_10_1m', 'perrank_percent_25_1m',
  207. 'perrank_percent_50_1m', 'perrank_percent_75_1m', 'perrank_percent_90_1m', 'perrank_percent_95_1m', 'best_1m', 'worst_1m',
  208. 'avg_3m', 'avg_3m_cnt', 'perrank_percent_5_3m', 'perrank_percent_10_3m', 'perrank_percent_25_3m',
  209. 'perrank_percent_50_3m', 'perrank_percent_75_3m', 'perrank_percent_90_3m', 'perrank_percent_95_3m', 'best_3m', 'worst_3m'],
  210. [INT, DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE,
  211. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  212. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE,
  213. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  214. INSERT INTO t_tmp VALUES (indicator_id, double(NULL), int(NULL), double(NULL), double(NULL), double(NULL),
  215. double(NULL), double(NULL), double(NULL), double(NULL), double(NULL), double(NULL),
  216. double(NULL), int(NULL), double(NULL), double(NULL), double(NULL),
  217. double(NULL), double(NULL), double(NULL), double(NULL), double(NULL), double(NULL));
  218. t_ranking_num = sql(select = (sqlCol('end_date'), sqlCol(ranking_by), sqlCol('raise_type', mean, 'raise_type'), sqlCol('indicator_id', mean,'indicator_id'),
  219. sqlCol('avg_1m', mean, 'avg_1m'), sqlCol('avg_1m_cnt', mean, 'avg_1m_cnt'),
  220. sqlCol('perrank_percent_5_1m', mean, 'perrank_percent_5_1m'),
  221. sqlCol('perrank_percent_10_1m', mean, 'perrank_percent_10_1m'),
  222. sqlCol('perrank_percent_25_1m', mean, 'perrank_percent_25_1m'),
  223. sqlCol('perrank_percent_50_1m', mean, 'perrank_percent_50_1m'),
  224. sqlCol('perrank_percent_75_1m', mean, 'perrank_percent_75_1m'),
  225. sqlCol('perrank_percent_90_1m', mean, 'perrank_percent_90_1m'),
  226. sqlCol('perrank_percent_95_1m', mean, 'perrank_percent_95_1m'),
  227. sqlCol('best_1m', mean, 'best_1m'), sqlCol('worst_1m', mean, 'worst_1m'),
  228. sqlCol('avg_3m', mean, 'avg_3m'), sqlCol('avg_3m_cnt', mean, 'avg_3m_cnt'),
  229. sqlCol('perrank_percent_5_3m', mean, 'perrank_percent_5_3m'),
  230. sqlCol('perrank_percent_10_3m', mean, 'perrank_percent_10_3m'),
  231. sqlCol('perrank_percent_25_3m', mean, 'perrank_percent_25_3m'),
  232. sqlCol('perrank_percent_50_3m', mean, 'perrank_percent_50_3m'),
  233. sqlCol('perrank_percent_75_3m', mean, 'perrank_percent_75_3m'),
  234. sqlCol('perrank_percent_90_3m', mean, 'perrank_percent_90_3m'),
  235. sqlCol('perrank_percent_95_3m', mean, 'perrank_percent_95_3m'),
  236. sqlCol('best_3m', mean, 'best_3m'), sqlCol('worst_3m', mean, 'worst_3m'),
  237. // 与 MySQL 不同,这里统一把近4年和成立以来的排名去掉
  238. sqlCol(indicator_name + '_6m', mean, 'avg_6m'), sqlCol(indicator_name + '_6m', count, 'avg_6m_cnt'),
  239. sqlCol(indicator_name + '_6m', percentile{, iif(is_ASC, 5, 95)}, 'perrank_percent_5_6m'),
  240. sqlCol(indicator_name + '_6m', percentile{, iif(is_ASC, 10, 90)}, 'perrank_percent_10_6m'),
  241. sqlCol(indicator_name + '_6m', percentile{, iif(is_ASC, 25, 75)}, 'perrank_percent_25_6m'),
  242. sqlCol(indicator_name + '_6m', percentile{, iif(is_ASC, 50, 50)}, 'perrank_percent_50_6m'),
  243. sqlCol(indicator_name + '_6m', percentile{, iif(is_ASC, 75, 25)}, 'perrank_percent_75_6m'),
  244. sqlCol(indicator_name + '_6m', percentile{, iif(is_ASC, 90, 10)}, 'perrank_percent_90_6m'),
  245. sqlCol(indicator_name + '_6m', percentile{, iif(is_ASC, 95, 5)}, 'perrank_percent_95_6m'),
  246. sqlCol(indicator_name + '_6m', iif(is_ASC, min, max), 'best_6m'),
  247. sqlCol(indicator_name + '_6m', iif(is_ASC, max, min), 'worst_6m'),
  248. sqlCol(indicator_name + '_1y', mean, 'avg_1y'), sqlCol(indicator_name + '_1y', count, 'avg_1y_cnt'),
  249. sqlCol(indicator_name + '_1y', percentile{, iif(is_ASC, 5, 95)}, 'perrank_percent_5_1y'),
  250. sqlCol(indicator_name + '_1y', percentile{, iif(is_ASC, 10, 90)}, 'perrank_percent_10_1y'),
  251. sqlCol(indicator_name + '_1y', percentile{, iif(is_ASC, 25, 75)}, 'perrank_percent_25_1y'),
  252. sqlCol(indicator_name + '_1y', percentile{, iif(is_ASC, 50, 50)}, 'perrank_percent_50_1y'),
  253. sqlCol(indicator_name + '_1y', percentile{, iif(is_ASC, 75, 25)}, 'perrank_percent_75_1y'),
  254. sqlCol(indicator_name + '_1y', percentile{, iif(is_ASC, 90, 10)}, 'perrank_percent_90_1y'),
  255. sqlCol(indicator_name + '_1y', percentile{, iif(is_ASC, 95, 5)}, 'perrank_percent_95_1y'),
  256. sqlCol(indicator_name + '_1y', iif(is_ASC, min, max), 'best_1y'),
  257. sqlCol(indicator_name + '_1y', iif(is_ASC, max, min), 'worst_1y'),
  258. sqlCol(indicator_name + '_2y', mean, 'avg_2y'), sqlCol(indicator_name + '_2y', count, 'avg_2y_cnt'),
  259. sqlCol(indicator_name + '_2y', percentile{, iif(is_ASC, 5, 95)}, 'perrank_percent_5_2y'),
  260. sqlCol(indicator_name + '_2y', percentile{, iif(is_ASC, 10, 90)}, 'perrank_percent_10_2y'),
  261. sqlCol(indicator_name + '_2y', percentile{, iif(is_ASC, 25, 75)}, 'perrank_percent_25_2y'),
  262. sqlCol(indicator_name + '_2y', percentile{, iif(is_ASC, 50, 50)}, 'perrank_percent_50_2y'),
  263. sqlCol(indicator_name + '_2y', percentile{, iif(is_ASC, 75, 25)}, 'perrank_percent_75_2y'),
  264. sqlCol(indicator_name + '_2y', percentile{, iif(is_ASC, 90, 10)}, 'perrank_percent_90_2y'),
  265. sqlCol(indicator_name + '_2y', percentile{, iif(is_ASC, 95, 5)}, 'perrank_percent_95_2y'),
  266. sqlCol(indicator_name + '_2y', iif(is_ASC, min, max), 'best_2y'),
  267. sqlCol(indicator_name + '_2y', iif(is_ASC, max, min), 'worst_2y'),
  268. sqlCol(indicator_name + '_3y', mean, 'avg_3y'), sqlCol(indicator_name + '_3y', count, 'avg_3y_cnt'),
  269. sqlCol(indicator_name + '_3y', percentile{, iif(is_ASC, 5, 95)}, 'perrank_percent_5_3y'),
  270. sqlCol(indicator_name + '_3y', percentile{, iif(is_ASC, 10, 90)}, 'perrank_percent_10_3y'),
  271. sqlCol(indicator_name + '_3y', percentile{, iif(is_ASC, 25, 75)}, 'perrank_percent_25_3y'),
  272. sqlCol(indicator_name + '_3y', percentile{, iif(is_ASC, 50, 50)}, 'perrank_percent_50_3y'),
  273. sqlCol(indicator_name + '_3y', percentile{, iif(is_ASC, 75, 25)}, 'perrank_percent_75_3y'),
  274. sqlCol(indicator_name + '_3y', percentile{, iif(is_ASC, 90, 10)}, 'perrank_percent_90_3y'),
  275. sqlCol(indicator_name + '_3y', percentile{, iif(is_ASC, 95, 5)}, 'perrank_percent_95_3y'),
  276. sqlCol(indicator_name + '_3y', iif(is_ASC, min, max), 'best_3y'),
  277. sqlCol(indicator_name + '_3y', iif(is_ASC, max, min), 'worst_3y'),
  278. sqlCol(indicator_name + '_5y', mean, 'avg_5y'), sqlCol(indicator_name + '_5y', count, 'avg_5y_cnt'),
  279. sqlCol(indicator_name + '_5y', percentile{, iif(is_ASC, 5, 95)}, 'perrank_percent_5_5y'),
  280. sqlCol(indicator_name + '_5y', percentile{, iif(is_ASC, 10, 90)}, 'perrank_percent_10_5y'),
  281. sqlCol(indicator_name + '_5y', percentile{, iif(is_ASC, 25, 75)}, 'perrank_percent_25_5y'),
  282. sqlCol(indicator_name + '_5y', percentile{, iif(is_ASC, 50, 50)}, 'perrank_percent_50_5y'),
  283. sqlCol(indicator_name + '_5y', percentile{, iif(is_ASC, 75, 25)}, 'perrank_percent_75_5y'),
  284. sqlCol(indicator_name + '_5y', percentile{, iif(is_ASC, 90, 10)}, 'perrank_percent_90_5y'),
  285. sqlCol(indicator_name + '_5y', percentile{, iif(is_ASC, 95, 5)}, 'perrank_percent_95_5y'),
  286. sqlCol(indicator_name + '_5y', iif(is_ASC, min, max), 'best_5y'),
  287. sqlCol(indicator_name + '_5y', iif(is_ASC, max, min), 'worst_5y'),
  288. sqlCol(indicator_name + '_10y', mean, 'avg_10y'), sqlCol(indicator_name + '_10y', count, 'avg_10y_cnt'),
  289. sqlCol(indicator_name + '_10y', percentile{, iif(is_ASC, 5, 95)}, 'perrank_percent_5_10y'),
  290. sqlCol(indicator_name + '_10y', percentile{, iif(is_ASC, 10, 90)}, 'perrank_percent_10_10y'),
  291. sqlCol(indicator_name + '_10y', percentile{, iif(is_ASC, 25, 75)}, 'perrank_percent_25_10y'),
  292. sqlCol(indicator_name + '_10y', percentile{, iif(is_ASC, 50, 50)}, 'perrank_percent_50_10y'),
  293. sqlCol(indicator_name + '_10y', percentile{, iif(is_ASC, 75, 25)}, 'perrank_percent_75_10y'),
  294. sqlCol(indicator_name + '_10y', percentile{, iif(is_ASC, 90, 10)}, 'perrank_percent_90_10y'),
  295. sqlCol(indicator_name + '_10y', percentile{, iif(is_ASC, 95, 5)}, 'perrank_percent_95_10y'),
  296. sqlCol(indicator_name + '_10y', iif(is_ASC, min, max), 'best_10y'),
  297. sqlCol(indicator_name + '_10y', iif(is_ASC, max, min), 'worst_10y'),
  298. sqlCol(indicator_name + '_ytd', mean, 'avg_ytd'), sqlCol(indicator_name + '_ytd', count, 'avg_ytd_cnt'),
  299. sqlCol(indicator_name + '_ytd', percentile{, iif(is_ASC, 5, 95)}, 'perrank_percent_5_ytd'),
  300. sqlCol(indicator_name + '_ytd', percentile{, iif(is_ASC, 10, 90)}, 'perrank_percent_10_ytd'),
  301. sqlCol(indicator_name + '_ytd', percentile{, iif(is_ASC, 25, 75)}, 'perrank_percent_25_ytd'),
  302. sqlCol(indicator_name + '_ytd', percentile{, iif(is_ASC, 50, 50)}, 'perrank_percent_50_ytd'),
  303. sqlCol(indicator_name + '_ytd', percentile{, iif(is_ASC, 75, 25)}, 'perrank_percent_75_ytd'),
  304. sqlCol(indicator_name + '_ytd', percentile{, iif(is_ASC, 90, 10)}, 'perrank_percent_90_ytd'),
  305. sqlCol(indicator_name + '_ytd', percentile{, iif(is_ASC, 95, 5)}, 'perrank_percent_95_ytd'),
  306. sqlCol(indicator_name + '_ytd', iif(is_ASC, min, max), 'best_ytd'),
  307. sqlCol(indicator_name + '_ytd', iif(is_ASC, max, min), 'worst_ytd')
  308. ),
  309. from = cj(data_table, t_tmp),
  310. where = <_$ranking_by IS NOT NULL>,
  311. groupBy = (sqlCol(ranking_by), sqlCol('end_date')),
  312. groupFlag = 1).eval(); // group by
  313. return t_ranking, t_ranking_num;
  314. }
  315. /*
  316. * 运行排名SQL脚本
  317. *
  318. * NOTE: 没有用 parseExpr 来生成动态脚本的原因是数据表无法传入
  319. */
  320. def run_ranking_sql(data_table, indicator_name, indicator_id, is_ASC, mutable v_tables) {
  321. tb_strategy_ranking = gen_ranking_sql(data_table, indicator_name, indicator_id, is_ASC, 'strategy')[0];
  322. v_tables[0].tableInsert(tb_strategy_ranking);
  323. tb_strategy_ranking_num = gen_ranking_sql(data_table, indicator_name, indicator_id, is_ASC, 'strategy')[1];
  324. v_tables[1].tableInsert(tb_strategy_ranking_num);
  325. tb_substrategy_ranking = gen_ranking_sql(data_table, indicator_name, indicator_id, is_ASC, 'substrategy')[0];
  326. v_tables[2].tableInsert(tb_substrategy_ranking);
  327. tb_substrategy_ranking_num = gen_ranking_sql(data_table, indicator_name, indicator_id, is_ASC, 'substrategy')[1];
  328. v_tables[3].tableInsert(tb_substrategy_ranking_num);
  329. }
  330. /*
  331. * 计算风险指标排名
  332. *
  333. *
  334. */
  335. def cal_risk_ranking(entity_type, entity_info, end_date, isFromMySQL) {
  336. table_desc = get_risk_stats_table_description(entity_type);
  337. tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  338. sec_id_col = table_desc.sec_id_col[0];
  339. tb_data.rename!(sec_id_col, 'entity_id');
  340. t = SELECT *
  341. FROM entity_info en
  342. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  343. WHERE en.strategy IS NOT NULL;
  344. // 按照 MySQL 字段建表
  345. t_s = create_entity_indicator_ranking(false);
  346. t_s_num = create_entity_indicator_ranking_num(false);
  347. t_ss = create_entity_indicator_substrategy_ranking(false);
  348. t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
  349. v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
  350. // 最大回撤
  351. run_ranking_sql(t, 'maxdrawdown', 2, true, v_ranking_tables);
  352. // 峰度
  353. run_ranking_sql(t, 'kurtosis', 6, true, v_ranking_tables);
  354. // 偏度
  355. run_ranking_sql(t, 'skewness', 9, false, v_ranking_tables);
  356. // 标准差
  357. run_ranking_sql(t, 'stddev', 10, true, v_ranking_tables);
  358. // Alpha
  359. run_ranking_sql(t, 'alpha', 11, false, v_ranking_tables);
  360. // Beta
  361. run_ranking_sql(t, 'beta', 12, false, v_ranking_tables);
  362. // 下行标准差
  363. run_ranking_sql(t, 'downsidedev', 21, true, v_ranking_tables);
  364. // 月最大回撤 dolphin 未计算
  365. // run_ranking_sql(t, 'maxdrawdown_months', 50, true, v_ranking_tables);
  366. // 最大回撤修复月份数 dolphin 未计算
  367. //run_ranking_sql(t, 'maxdrawdown_recoverymonths', 52, true, v_ranking_tables);
  368. // 胜率
  369. run_ranking_sql(t, 'winrate', 59, false, v_ranking_tables);
  370. return v_ranking_tables;
  371. }
  372. /*
  373. * 计算风险调整收益指标排名
  374. *
  375. *
  376. */
  377. def cal_risk_adj_return_ranking(entity_type, entity_info, end_date, isFromMySQL) {
  378. table_desc = get_riskadjret_stats_table_description(entity_type);
  379. tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  380. sec_id_col = table_desc.sec_id_col[0];
  381. tb_data.rename!(sec_id_col, 'entity_id');
  382. t = SELECT *
  383. FROM entity_info en
  384. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  385. WHERE en.strategy IS NOT NULL;
  386. // 按照 MySQL 字段建表
  387. t_s = create_entity_indicator_ranking(false);
  388. t_s_num = create_entity_indicator_ranking_num(false);
  389. t_ss = create_entity_indicator_substrategy_ranking(false);
  390. t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
  391. v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
  392. // Kappa ratio
  393. run_ranking_sql(t, 'kapparatio', 14, false, v_ranking_tables);
  394. // Treynor ratio
  395. run_ranking_sql(t, 'treynorratio', 15, false, v_ranking_tables);
  396. // Jensen
  397. run_ranking_sql(t, 'jensen', 16, false, v_ranking_tables);
  398. // Omega ratio
  399. run_ranking_sql(t, 'omegaratio', 17, false, v_ranking_tables);
  400. // Sharpe ratio
  401. run_ranking_sql(t, 'sharperatio', 18, false, v_ranking_tables);
  402. // MAR Sortino ratio dolphin 未计算
  403. //run_ranking_sql(t, 'sortinoratio_MAR', 19, false, v_ranking_tables);
  404. // Calmar ratio
  405. run_ranking_sql(t, 'calmarratio', 40, false, v_ranking_tables);
  406. // Sortino ratio
  407. run_ranking_sql(t, 'sortinoratio', 58, false, v_ranking_tables);
  408. return v_ranking_tables;
  409. }
  410. /*
  411. * 计算杂项指标排名
  412. *
  413. *
  414. */
  415. def cal_other_indicator_ranking(entity_type, entity_info, end_date, isFromMySQL) {
  416. table_desc = get_indicator_table_description(entity_type);
  417. tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  418. sec_id_col = table_desc.sec_id_col[0];
  419. tb_data.rename!(sec_id_col, 'entity_id');
  420. t = SELECT *
  421. FROM entity_info en
  422. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  423. WHERE en.strategy IS NOT NULL;
  424. // 按照 MySQL 字段建表
  425. t_s = create_entity_indicator_ranking(false);
  426. t_s_num = create_entity_indicator_ranking_num(false);
  427. t_ss = create_entity_indicator_substrategy_ranking(false);
  428. t_ss_num = create_entity_indicator_substrategy_ranking_num(false);
  429. v_ranking_tables = [t_s, t_s_num, t_ss, t_ss_num];
  430. // 风格一致性 dolphin 未计算
  431. //run_ranking_sql(t, 'per_con', 37, false, v_ranking_tables);
  432. // Information ratio
  433. run_ranking_sql(t, 'info_ratio', 38, false, v_ranking_tables);
  434. // Value at Risk
  435. run_ranking_sql(t, 'var', 41, true, v_ranking_tables);
  436. // Conditional Value at Risk
  437. run_ranking_sql(t, 'cvar', 42, true, v_ranking_tables);
  438. // SMDD 模型下的 VaR dolphin 未计算
  439. //run_ranking_sql(t, 'smddvar', 43, true, v_ranking_tables);
  440. // SMDD 模型下的 CVaR dolphin 未计算
  441. //run_ranking_sql(t, 'smddcvar', 44, true, v_ranking_tables);
  442. // SMDD 模型下的 LPM1 dolphin 未计算
  443. //run_ranking_sql(t, 'smdd_lpm1', 45, true, v_ranking_tables);
  444. // SMDD 模型下的 LPM2 dolphin 未计算
  445. //run_ranking_sql(t, 'smdd_lpm2', 46, true, v_ranking_tables);
  446. // SMDD 模型下的下行风险 dolphin 未计算
  447. //run_ranking_sql(t, 'smdd_downside_dev', 47, true, v_ranking_tables);
  448. // 跟踪误差
  449. run_ranking_sql(t, 'tracking_error', 48, true, v_ranking_tables);
  450. // M2
  451. run_ranking_sql(t, 'm2', 49, false, v_ranking_tables);
  452. return v_ranking_tables;
  453. }
  454. /*
  455. * 将源指标表横表变竖表,以方便排名计算
  456. *
  457. *
  458. */
  459. def transform_data_for_ranking(entity_type, data_table, ranking_by, indicator_info) {
  460. v_trailing = ['6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  461. // 只有 portfolio_id 是整型,其它的都是字符串
  462. is_id_integer = false;
  463. if(entity_type == 'PF') is_id_integer = true;
  464. if(ranking_by == 'strategy')
  465. tb_ranking = create_entity_indicator_ranking(is_id_integer).rename!(ranking_by, 'category_id');
  466. else if(ranking_by == 'substrategy')
  467. tb_ranking = create_entity_indicator_substrategy_ranking(is_id_integer).rename!(ranking_by, 'category_id');
  468. else if(ranking_by == 'factor_id')
  469. tb_ranking = NULL;
  470. for(indicator in indicator_info) {
  471. t = sql(select = (sqlCol('entity_id'), sqlCol('end_date'), sqlCol('category_id'),
  472. sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing)
  473. ),
  474. from = data_table
  475. ).eval();
  476. t.join!(table(take(indicator.id, t.size()) AS indicator_id,
  477. take(double(NULL), t.size()) AS indicator_1m,
  478. take(int(NULL), t.size()) AS absrank_1m,
  479. take(int(NULL), t.size()) AS perrank_1m,
  480. take(double(NULL), t.size()) AS indicator_3m,
  481. take(int(NULL), t.size()) AS absrank_3m,
  482. take(int(NULL), t.size()) AS perrank_3m,
  483. take(int(NULL), t.size()) AS absrank_6m,
  484. take(int(NULL), t.size()) AS perrank_6m,
  485. take(int(NULL), t.size()) AS absrank_1y,
  486. take(int(NULL), t.size()) AS perrank_1y,
  487. take(int(NULL), t.size()) AS absrank_2y,
  488. take(int(NULL), t.size()) AS perrank_2y,
  489. take(int(NULL), t.size()) AS absrank_3y,
  490. take(int(NULL), t.size()) AS perrank_3y,
  491. take(int(NULL), t.size()) AS absrank_5y,
  492. take(int(NULL), t.size()) AS perrank_5y,
  493. take(int(NULL), t.size()) AS absrank_10y,
  494. take(int(NULL), t.size()) AS perrank_10y,
  495. take(int(NULL), t.size()) AS absrank_ytd,
  496. take(int(NULL), t.size()) AS perrank_ytd)
  497. );
  498. INSERT INTO tb_ranking
  499. SELECT * FROM (sql(select = sqlCol(tb_ranking.colNames()),
  500. from = t).eval());
  501. }
  502. return tb_ranking;
  503. }
  504. /*
  505. * 将源风险指标表横表变竖表,以方便排名计算
  506. *
  507. * TODO: 一直缺 portfolio bfi indicator 计算!mysql 里的 pf_fund_bfi_bm_indicator_ranking 是错的...
  508. */
  509. def transform_risk_stats_for_ranking (entity_type, entity_info, end_date, ranking_by, isFromMySQL=true) {
  510. table_desc = get_risk_stats_table_description(entity_type);
  511. tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  512. sec_id_col = table_desc.sec_id_col[0];
  513. tb_data.rename!(sec_id_col, 'entity_id');
  514. data_table = SELECT * FROM entity_info en
  515. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  516. WHERE en.strategy IS NOT NULL
  517. data_table.dropColumns!('id');
  518. data_table.rename!(ranking_by, 'category_id');
  519. // 目前SQL排名的指标
  520. v_indicator_name = ['maxdrawdown', 'kurtosis', 'skewness', 'stddev', 'alpha', 'beta', 'downsidedev', 'maxdrawdown_months', 'maxdrawdown_recoverymonths', 'winrate'];
  521. v_indicator_id = [2, 6, 9, 10, 11, 12, 21, 50, 52, 59];
  522. v_is_ASC = [true, true, false, true, false, false, true, true, true, false];
  523. t_indicator = table(v_indicator_name AS name, v_indicator_id AS id, v_is_ASC AS is_ASC);
  524. tb_ranking = transform_data_for_ranking(entity_type, data_table, ranking_by, t_indicator).rename!('category_id', ranking_by);
  525. return tb_ranking;
  526. }
  527. /*
  528. * 将源风险调整指标表横表变竖表,以方便排名计算
  529. *
  530. *
  531. */
  532. def transform_risk_adj_ret_stats_for_ranking (entity_type, entity_info, end_date, ranking_by, isFromMySQL=true) {
  533. table_desc = get_riskadjret_stats_table_description(entity_type);
  534. tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  535. sec_id_col = table_desc.sec_id_col[0];
  536. tb_data.rename!(sec_id_col, 'entity_id');
  537. data_table = SELECT * FROM entity_info en
  538. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  539. WHERE en.strategy IS NOT NULL
  540. data_table.dropColumns!('id');
  541. data_table.rename!(ranking_by, 'category_id');
  542. // 目前SQL排名的指标
  543. v_indicator_name = ['kapparatio', 'treynorratio', 'jensen', 'omegaratio', 'sharperatio', 'sortinoratio_MAR', 'calmarratio', 'sortinoratio'];
  544. v_indicator_id = [14, 15, 16, 17, 18, 19, 40, 58];
  545. v_is_ASC = [false, false, false, false, false, false, false, false];
  546. t_indicator = table(v_indicator_name AS name, v_indicator_id AS id, v_is_ASC AS is_ASC);
  547. tb_ranking = transform_data_for_ranking(entity_type, data_table, ranking_by, t_indicator).rename!('category_id', ranking_by);
  548. return tb_ranking;
  549. }
  550. /*
  551. * 将源杂项风险指标表横表变竖表,以方便排名计算
  552. *
  553. *
  554. */
  555. def transform_other_indicator_for_ranking (entity_type, entity_info, end_date, ranking_by, isFromMySQL=true) {
  556. table_desc = get_indicator_table_description(entity_type);
  557. tb_data = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  558. sec_id_col = table_desc.sec_id_col[0];
  559. tb_data.rename!(sec_id_col, 'entity_id');
  560. data_table = SELECT * FROM entity_info en
  561. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  562. WHERE en.strategy IS NOT NULL
  563. data_table.dropColumns!('id');
  564. data_table.rename!(ranking_by, 'category_id');
  565. // 目前SQL排名的指标
  566. v_indicator_name = ['per_con', 'info_ratio', 'var', 'cvar', 'smddvar', 'smddcvar', 'smdd_lpm1', 'smdd_lpm2', 'smdd_downside_dev', 'tracking_error', 'm2'];
  567. v_indicator_id = [37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 49];
  568. v_is_ASC = [false, false, true, true, true, true, true, true, true, true, false];
  569. t_indicator = table(v_indicator_name AS name, v_indicator_id AS id, v_is_ASC AS is_ASC);
  570. tb_ranking = transform_data_for_ranking(entity_type, data_table, ranking_by, t_indicator).rename!('category_id', ranking_by);
  571. return tb_ranking;
  572. }
  573. /*
  574. *
  575. * 参考某指定类排名,计算相对排名
  576. *
  577. * @param benchmark_ranking <TABLE>: 被参考的排名表,如公募混合基金
  578. * @param entity_ranking <TABLE>: 被计算的指标表,排名被填充在原表中
  579. * @param isFromMySQL <BOOL>
  580. *
  581. *
  582. * Example: cal_relative_ranking(get_fund_indicator_ranking(NULL, 2024.09M, 102, true),
  583. * transform_risk_stats_for_ranking('PF', get_entity_info('PF', NULL), 2024.09M, true),
  584. * true);
  585. */
  586. def cal_relative_ranking(benchmark_ranking, mutable entity_ranking, isFromMySQL=true) {
  587. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  588. for(tr in v_trailing) {
  589. indicator_val_col = 'indicator_' + tr;
  590. // 乘上100,000 是为了满足 window join 的字段必须是INT或DURATION
  591. tb_tmp = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  592. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int')),
  593. from = entity_ranking,
  594. where = < _$indicator_val_col is not null >,
  595. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  596. ).eval();
  597. tb_tmp2 = sql(select = (sqlCol(['end_date', 'category_id', 'indicator_id']),
  598. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int'),
  599. sqlCol('absrank_' + tr), sqlCol('perrank_' + tr)
  600. ),
  601. from = benchmark_ranking,
  602. where = < _$indicator_val_col is not null >,
  603. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  604. ).eval();
  605. absrank_col = 'absrank_' + tr;
  606. perrank_col = 'perrank_' + tr;
  607. // 用 pwj 来找最接近的排名
  608. tb_tmp_ranking = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  609. sqlCol(indicator_val_col + '_int'),
  610. sqlCol(['absrank_max', 'perrank_max'])),
  611. from = pwj(tb_tmp, tb_tmp2,
  612. window = 0:1,
  613. aggs = [<max(_$absrank_col) as 'absrank_max'>, <max(_$perrank_col) as 'perrank_max'>],
  614. matchingCols = ['end_date', 'category_id', 'indicator_id', indicator_val_col + '_int'])
  615. ).eval();
  616. // 计算的结果填入排名表
  617. sqlUpdate(table = entity_ranking,
  618. updates = [<absrank_max as _$absrank_col>, <perrank_max as _$perrank_col>],
  619. from = <ej(entity_ranking, tb_tmp_ranking, ['entity_id', 'end_date', 'category_id','indicator_id'])>
  620. ).eval();
  621. }
  622. }
  623. /*
  624. * 排名数据入库
  625. *
  626. * @param ranking_tables <VECTOR>: 包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
  627. */
  628. def save_ranking_tables(entity_type, ranking_tables) {
  629. if(ranking_tables.isVoid()) return;
  630. source_table = '';
  631. target_table = '';
  632. if(entity_type IN ['MF', 'HF']) {
  633. entity_id_col = 'fund_id';
  634. source_table = 'raw_db.pf_fund_indicator_ranking';
  635. target_table = 'raw_db.pf_fund_indicator_ranking'
  636. }
  637. ranking_tables[0].rename!('entity_id', entity_id_col);
  638. save_and_sync(ranking_tables[0], source_table, target_table);
  639. save_and_sync(ranking_tables[1], source_table + '_num', target_table + '_num');
  640. ranking_tables[2].rename!('entity_id', entity_id_col);
  641. save_and_sync(ranking_tables[2], source_table.replace!('_ranking', '_substrategy_ranking'), target_table.replace!('_ranking', '_substrategy_ranking'));
  642. save_and_sync(ranking_tables[3], source_table + '_num', target_table + '_num');
  643. }
  644. /*
  645. * 参考排名数据入库
  646. *
  647. * @param ranking_tables <TABLE>:
  648. */
  649. def save_relative_ranking_table(entity_type, ranking_table, ranking_by) {
  650. if(ranking_table.isVoid()) return;
  651. source_table = '';
  652. target_table = '';
  653. if(entity_type == 'PF') {
  654. entity_id_col = 'portfolio_id';
  655. if(ranking_by == 'strategy') {
  656. source_table = 'raw_db.pf_portfolio_indicator_ranking';
  657. target_table = 'raw_db.pf_portfolio_indicator_ranking';
  658. } else if(ranking_by == 'substrategy') {
  659. source_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  660. target_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  661. } else if(ranking_by == 'factor_id') {
  662. source_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  663. target_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  664. }
  665. } else if(entity_type == 'CF') {
  666. entity_id_col = 'fund_id';
  667. source_table = 'raw_db.pf_cus_fund_indicator_ranking';
  668. target_table = 'raw_db.pf_cus_fund_indicator_ranking'
  669. }
  670. save_and_sync(ranking_table, source_table, target_table);
  671. }