rankingCalculator.dos 21 KB

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