rankingCalculator.dos 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. module fundit::rankingCalculator
  2. use fundit::sqlUtilities
  3. use fundit::operationDataPuller;
  4. use fundit::performanceDataPuller;
  5. use fundit::dataSaver
  6. /*
  7. * 汇集所有参与排名的指标信息
  8. *
  9. */
  10. def get_indicator_info() {
  11. ids = [1,
  12. 2, 6, 9, 10, 11, 12, 21, 50, 52, 59,
  13. 14, 15, 16, 17, 18, 19, 40, 58,
  14. 37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  15. 33, 34, 35, 36,
  16. 66, 53, 54, 55, 56, 57
  17. ];
  18. names = ['ret',
  19. 'maxdrawdown', 'kurtosis', 'skewness', 'stddev', 'alpha', 'beta', 'downsidedev', 'maxdrawdown_months', 'maxdrawdown_recoverymonths', 'winrate',
  20. 'kapparatio', 'treynorratio', 'jensen', 'omegaratio', 'sharperatio', 'sortinoratio_MAR', 'calmarratio', 'sortinoratio',
  21. 'per_con', 'info_ratio', 'var', 'cvar', 'smddvar', 'smddcvar', 'smdd_lpm1', 'smdd_lpm2', 'smdd_downside_dev', 'tracking_error', 'm2',
  22. 'upsidecapture_ret', 'downsidecapture_ret', 'upsidecapture_ratio', 'downsidecapture_ratio',
  23. 'stability', 'jc_stddev', 'gzstyle_stddev', 'gzstrategy_stddev', 'zz_stddev', 'zx_stddev'
  24. ];
  25. is_ASCs = [false,
  26. true, true, false, true, false, false, true, true, true, false,
  27. false, false, false, false, false, false, false, false,
  28. false, false, true, true, true, true, true, true, true, true, false,
  29. false, false, false, true,
  30. true, true, true, true, true, true
  31. ];
  32. return table(names AS name, ids AS id, is_ASCs AS is_ASC);
  33. }
  34. /*
  35. * 自定义百分位计算
  36. *
  37. */
  38. defg perRank(x, is_ASC) {
  39. return (100 * x.rank(ascending=is_ASC, percent=true)).round(0);
  40. }
  41. /*
  42. * 动态生成用于排序的SQL脚本
  43. *
  44. * @param data_table <TABLE>: 指标横表
  45. * @param indicator_table <TABLE>: 指标表,有 id, name, is_ASC 字段
  46. *
  47. * TODO: portfolio, cf, manager, company,
  48. * TODO: bfi & category
  49. *
  50. */
  51. def gen_ranking_sql(data_table, indicator_table) {
  52. ranking = create_entity_indicator_ranking();
  53. ranking_num = create_entity_indicator_ranking_num();
  54. for(indicator in indicator_table) {
  55. // 与 MySQL 不同,这里统一把近4年和成立以来的排名去掉
  56. if(indicator.id == 1)
  57. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  58. else {
  59. v_trailing = ['6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  60. v_missing_trailing = ['1m', '3m'];
  61. }
  62. // 绝对排名和百分位排名
  63. t_ranking = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id']), <indicator.id as indicator_id>,
  64. sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing),
  65. sqlCol(indicator.name + '_' + v_trailing, rank{, indicator.is_ASC}, 'absrank_' + v_trailing),
  66. sqlCol(indicator.name + '_' + v_trailing, perRank{, indicator.is_ASC}, 'perrank_' + v_trailing)
  67. ),
  68. from = data_table,
  69. where = < category_id IS NOT NULL>,
  70. groupBy = sqlCol(['category_id', 'end_date']),
  71. groupFlag = 0 ).eval(); // context by
  72. // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
  73. if(indicator.id != 1) {
  74. v_tmp_col = ['indicator_' + v_missing_trailing, 'absrank_' + v_missing_trailing, 'perrank_' + v_missing_trailing].flatten();
  75. v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(INT, v_missing_trailing.size())].flatten();
  76. t_ranking.addColumn(v_tmp_col, v_tmp_type);
  77. }
  78. t_ranking.reorderColumns!(ranking.colNames());
  79. ranking.tableInsert(t_ranking);
  80. // 平均值、集合数量、各分位的阈值
  81. t_ranking_num = sql(select =(sqlCol(['end_date', 'category_id']),
  82. sqlCol('raise_type', mean, 'raise_type'), <indicator.id as indicator_id>,
  83. sqlCol(indicator.name + '_' + v_trailing, mean, 'avg_' + v_trailing),
  84. sqlCol(indicator.name + '_' + v_trailing, count, 'avg_' + v_trailing + '_cnt'),
  85. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_' + v_trailing),
  86. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_' + v_trailing),
  87. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_' + v_trailing),
  88. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_' + v_trailing),
  89. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_' + v_trailing),
  90. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_' + v_trailing),
  91. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_' + v_trailing),
  92. sqlCol(indicator.name + '_' + v_trailing, iif(indicator.is_ASC, min, max), 'best_' + v_trailing),
  93. sqlCol(indicator.name + '_' + v_trailing, iif(indicator.is_ASC, max, min), 'worst_' + v_trailing)
  94. ),
  95. from = data_table,
  96. where = < category_id IS NOT NULL>,
  97. groupBy = sqlCol(['category_id', 'end_date']),
  98. groupFlag = 1).eval(); // group by
  99. // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
  100. if(indicator.id != 1) {
  101. v_tmp_col = ['avg_' + v_missing_trailing, 'avg_' + v_missing_trailing + '_cnt', 'perrank_percent_5_' + v_missing_trailing,
  102. 'perrank_percent_10_' + v_missing_trailing, 'perrank_percent_25_' + v_missing_trailing,
  103. 'perrank_percent_50_' + v_missing_trailing, 'perrank_percent_75_' + v_missing_trailing,
  104. 'perrank_percent_90_' + v_missing_trailing, 'perrank_percent_95_' + v_missing_trailing,
  105. 'best_' + v_missing_trailing, 'worst_' + v_missing_trailing
  106. ].flatten();
  107. v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  108. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  109. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  110. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  111. take(DOUBLE, v_missing_trailing.size()),take(DOUBLE, v_missing_trailing.size())
  112. ].flatten();
  113. t_ranking_num.addColumn(v_tmp_col, v_tmp_type);
  114. }
  115. t_ranking_num.reorderColumns!(ranking_num.colNames());
  116. ranking_num.tableInsert(t_ranking_num);
  117. }
  118. return ranking, ranking_num;
  119. }
  120. /*
  121. * 运行排名SQL脚本
  122. *
  123. *
  124. */
  125. def run_ranking_sql(ranking_by, mutable data_table, indicator_table) {
  126. // data_table = t
  127. // v_tables = v_ranking_tables
  128. // ranking_by = 'strategy'
  129. ret = array(ANY, 0);
  130. if(ranking_by == 'bfi') {
  131. UPDATE data_table SET category_id = factor_id;
  132. v_ranking = gen_ranking_sql(data_table, indicator_table);
  133. ret.append!(v_ranking[0]); // ranking table
  134. ret.append!(v_ranking[1]); // ranking_num table
  135. } else {
  136. // 策略排名
  137. UPDATE data_table SET category_id = strategy$STRING;
  138. v_ranking = gen_ranking_sql(data_table, indicator_table);
  139. ret.append!(v_ranking[0]); // ranking table
  140. ret.append!(v_ranking[1]); // ranking_num table
  141. // 子策略排名
  142. UPDATE data_table SET category_id = substrategy$STRING;
  143. v_ranking = gen_ranking_sql(data_table, indicator_table);
  144. ret.append!(v_ranking[0]); // ranking table
  145. ret.append!(v_ranking[1]); // ranking_num table
  146. }
  147. return ret;
  148. }
  149. /*
  150. * 为排名做数据准备
  151. *
  152. * TODO: 对少量组合做优化
  153. *
  154. * @return <VECTOR>: 包含两个表,一个指标数据表,一个是指标信息表
  155. *
  156. */
  157. def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
  158. // return
  159. table_desc = get_performance_table_description(entity_type);
  160. tb_data_return = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  161. entity_id_name = table_desc.sec_id_col[0];
  162. // risk
  163. table_desc = get_risk_stats_table_description(entity_type);
  164. tb_data_risk_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  165. // risk adjusted return
  166. table_desc = get_riskadjret_stats_table_description(entity_type);
  167. tb_data_riskadjret_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  168. // others
  169. table_desc = get_indicator_table_description(entity_type);
  170. tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  171. // 做个大宽表
  172. matchingCols = [entity_id_name, 'end_date'];
  173. tb_data = lj(lj(lj(tb_data_return, tb_data_indicator_stats, matchingCols), tb_data_risk_stats, matchingCols), tb_data_riskadjret_stats, matchingCols);
  174. if(ranking_by == 'bfi') {
  175. // 去掉被移到 fund_ty_bfi_bm_indicator 表中的重复字段
  176. v_dups = [38, 48, 11, 12, 59, 16];
  177. v_dup_col = EXEC name + suffix
  178. FROM cj(get_indicator_info(), table(['_6m', '_1y', '_2y', '_3y', '_5y', '_10y', '_ytd'] AS suffix))
  179. WHERE id IN v_dups;
  180. tb_data.dropColumns!(v_dup_col);
  181. // bfi table
  182. table_desc = get_bfi_by_category_group_table_description(entity_type);
  183. tb_bfi = SELECT portfolio_id, end_date, factor_id FROM get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  184. // bfi (as benchmark) indicator
  185. table_desc = get_bfi_indicator_table_description(entity_type);
  186. tb_data_bfi_indicator = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  187. matchingCols2 = [entity_id_name, 'end_date', 'factor_id'];
  188. tb_data = lj(ej(tb_data, tb_bfi, matchingCols), tb_data_bfi_indicator, matchingCols2);
  189. v_indicator_id = [1, // 对应 fund_performance, 取消39(年化收益) 因为没有意义
  190. 41, 42, 49, // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
  191. 2, 6, 9, 10, 21, // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
  192. 14, 15, 17, 18, 40, 58, // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
  193. 11, 12, 16, 33, 34, 35, 36, 38, 48, 59 // 对应 fund_ty_bfi_bm_indicator
  194. ]; // 取消 pf_fund_factor_stability 66 (stabiliy) 因为 dolphin 未计算
  195. // 取消 fund_rbsa_style 53, 54, 55, 56, 57(风格稳定性) 因为 dolphin 未计算
  196. } else {
  197. // upside/downside capture
  198. table_desc = get_capture_style_table_description(entity_type);
  199. tb_data_capture_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  200. tb_data = lj(tb_data, tb_data_capture_stats, matchingCols);
  201. v_indicator_id = [1, // 对应 fund_performance, 取消39(年化收益) 因为没有意义
  202. 38, 41, 42, 48, 49, // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
  203. 2, 6, 9, 10, 11, 12, 21, 59, // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
  204. 14, 15, 16, 17, 18, 40, 58, // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
  205. 33, 34, 35, 36 // 对应 fund_style_stats
  206. ];
  207. }
  208. tb_data.rename!(entity_id_name, 'entity_id');
  209. t = SELECT * FROM entity_info en
  210. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  211. WHERE en.strategy IS NOT NULL;
  212. if(ranking_by == 'bfi')
  213. UPDATE t SET category_id = factor_id;
  214. else if(ranking_by == 'substrategy')
  215. UPDATE t SET category_id = substrategy$STRING;
  216. else
  217. UPDATE t SET category_id = strategy$STRING;
  218. indicator_table = SELECT * FROM get_indicator_info() WHERE id IN v_indicator_id;
  219. return t, indicator_table;
  220. }
  221. /*
  222. * 通用指标排名计算
  223. *
  224. * @param ranking_by <STRING>: strategy, bfi
  225. *
  226. */
  227. def cal_indicator_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
  228. // 当前只对基金做排名, 其它类型参考基金排名做相对排名
  229. if(!(entity_type in ['MF', 'HF'])) return null;
  230. v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
  231. v_ranking_tables = run_ranking_sql(ranking_by, v[0], v[1]);
  232. return v_ranking_tables;
  233. }
  234. /*
  235. * 将源指标表横表变竖表,以方便参考排名计算
  236. *
  237. *
  238. */
  239. def run_transformation_sql(entity_type, data_table, ranking_by, indicator_info) {
  240. // 只有 portfolio_id 是整型,其它的都是字符串
  241. is_id_integer = false;
  242. if(entity_type == 'PF') is_id_integer = true;
  243. tb_ranking = create_entity_indicator_ranking(is_id_integer);
  244. for(indicator in indicator_info) {
  245. // 只有收益需要1m, 3m
  246. if(indicator.id == 1)
  247. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  248. else {
  249. v_trailing = ['6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  250. v_missing = ['1m', '3m'];
  251. }
  252. t = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id']), <indicator.id as indicator_id>,
  253. sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing)
  254. ),
  255. from = data_table
  256. ).eval();
  257. // 给非收益指标补上1m, 3m的三套指标
  258. if(indicator.id != 1 )
  259. {
  260. v_tmp_col = ['indicator_' + v_missing, 'absrank_' + v_missing, 'perrank_' + v_missing].flatten();
  261. v_tmp_type = [take(DOUBLE, v_missing.size()), take(INT, v_missing.size()), take(INT, v_missing.size())].flatten();
  262. t.addColumn(v_tmp_col, v_tmp_type);
  263. }
  264. // 给所有指标补上 absrank 和 perrank 两套指标
  265. v_tmp_col = ['absrank_' + v_trailing, 'perrank_' + v_trailing].flatten();
  266. v_tmp_type = [take(INT, v_trailing.size()), take(INT, v_trailing.size())].flatten();
  267. t.addColumn(v_tmp_col, v_tmp_type);
  268. INSERT INTO tb_ranking
  269. SELECT * FROM (sql(select = sqlCol(tb_ranking.colNames()),
  270. from = t).eval());
  271. }
  272. return tb_ranking;
  273. }
  274. /*
  275. * 将源风险指标表横表变竖表,以方便排名计算
  276. *
  277. *
  278. */
  279. def transform_data_for_ranking (entity_type, entity_info, end_date, ranking_by, isFromMySQL=true) {
  280. if(entity_info.isVoid() || entity_info.size() == 0) return null;
  281. v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
  282. tb_ranking = run_transformation_sql(entity_type, v[0], ranking_by, v[1]);
  283. return tb_ranking;
  284. }
  285. /*
  286. *
  287. * 参考某指定类排名,计算相对排名
  288. *
  289. * @param benchmark_ranking <TABLE>: 被参考的排名表,如公募混合基金
  290. * @param entity_ranking <TABLE>: 被计算的指标表,排名被填充在原表中
  291. * @param isFromMySQL <BOOL>
  292. *
  293. *
  294. * Example: cal_relative_ranking(get_fund_indicator_ranking(NULL, 2024.09M, 102, true),
  295. * transform_risk_stats_for_ranking('PF', get_entity_info('PF', NULL), 2024.09M, true),
  296. * true);
  297. */
  298. def cal_relative_ranking(benchmark_ranking, mutable entity_ranking, isFromMySQL=true) {
  299. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  300. for(tr in v_trailing) {
  301. indicator_val_col = 'indicator_' + tr;
  302. // 乘上100,000 是为了满足 window join 的字段必须是INT或DURATION
  303. tb_tmp = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  304. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int')),
  305. from = entity_ranking,
  306. where = < _$indicator_val_col is not null >,
  307. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  308. ).eval();
  309. tb_tmp2 = sql(select = (sqlCol(['end_date', 'category_id', 'indicator_id']),
  310. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int'),
  311. sqlCol('absrank_' + tr), sqlCol('perrank_' + tr)
  312. ),
  313. from = benchmark_ranking,
  314. where = < _$indicator_val_col is not null >,
  315. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  316. ).eval();
  317. absrank_col = 'absrank_' + tr;
  318. perrank_col = 'perrank_' + tr;
  319. // 用 pwj 来找最接近的排名
  320. tb_tmp_ranking = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  321. sqlCol(indicator_val_col + '_int'),
  322. sqlCol(['absrank_max', 'perrank_max'])),
  323. from = pwj(tb_tmp, tb_tmp2,
  324. window = 0:1,
  325. aggs = [<max(_$absrank_col) as 'absrank_max'>, <max(_$perrank_col) as 'perrank_max'>],
  326. matchingCols = ['end_date', 'category_id', 'indicator_id', indicator_val_col + '_int'])
  327. ).eval();
  328. // 计算的结果填入排名表
  329. sqlUpdate(table = entity_ranking,
  330. updates = [<absrank_max as _$absrank_col>, <perrank_max as _$perrank_col>],
  331. from = <ej(entity_ranking, tb_tmp_ranking, ['entity_id', 'end_date', 'category_id','indicator_id'])>
  332. ).eval();
  333. }
  334. }
  335. /*
  336. * 排名数据入库
  337. *
  338. * @param ranking_by <STRING>: 'strategy', 'bfi'
  339. * @param ranking_tables <VECTOR>: 当 ranking_by = 'strategy' 时包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
  340. * ranking_by = 'bfi' 时包含2个数据表的向量,分别是bfi策略排名,bfi策略排名阈值
  341. */
  342. def save_ranking_tables(ranking_by, ranking_tables) {
  343. if(ranking_tables.isVoid()) return;
  344. entity_id_col = 'fund_id';
  345. if(ranking_by == 'bfi') {
  346. source_table = 'raw_db.pf_fund_bfi_bm_indicator_ranking';
  347. target_table = 'raw_db.pf_fund_bfi_bm_indicator_ranking';
  348. category_id_col = 'factor_id';
  349. } else {
  350. source_table = 'raw_db.pf_fund_indicator_ranking';
  351. target_table = 'raw_db.pf_fund_indicator_ranking';
  352. category_id_col = 'strategy';
  353. }
  354. t = ranking_tables[0];
  355. save_and_sync(t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]), source_table, target_table);
  356. t = ranking_tables[1];
  357. save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
  358. if(ranking_by == 'strategy') {
  359. source_table = source_table.strReplace('_ranking', '_substrategy_ranking');
  360. target_table = target_table.strReplace('_ranking', '_substrategy_ranking');
  361. category_id_col = 'substrategy';
  362. t = ranking_tables[2];
  363. save_and_sync(t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]), source_table, target_table);
  364. t = ranking_tables[3];
  365. save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
  366. }
  367. }
  368. /*
  369. * 参考排名数据入库
  370. *
  371. * @param ranking_tables <TABLE>:
  372. */
  373. def save_relative_ranking_table(entity_type, ranking_table, ranking_by) {
  374. if(ranking_table.isVoid()) return;
  375. source_table = '';
  376. target_table = '';
  377. if(entity_type == 'PF') {
  378. entity_id_col = 'portfolio_id';
  379. if(ranking_by == 'strategy') {
  380. source_table = 'raw_db.pf_portfolio_indicator_ranking';
  381. target_table = 'raw_db.pf_portfolio_indicator_ranking';
  382. } else if(ranking_by == 'substrategy') {save_relative_ranking_table
  383. source_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  384. target_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  385. } else if(ranking_by == 'bfi') {
  386. source_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  387. target_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  388. }
  389. } else if(entity_type == 'CF') {
  390. entity_id_col = 'fund_id';
  391. source_table = 'raw_db.pf_cus_fund_indicator_ranking';
  392. target_table = 'raw_db.pf_cus_fund_indicator_ranking'
  393. }
  394. save_and_sync(ranking_table, source_table, target_table);
  395. }