|
@@ -60,10 +60,15 @@ defg perRank(x, is_ASC) {
|
|
* TODO: bfi & category
|
|
* TODO: bfi & category
|
|
*
|
|
*
|
|
*/
|
|
*/
|
|
-def gen_ranking_sql(data_table, indicator_table) {
|
|
|
|
|
|
+def gen_ranking_sql(entity_type, data_table, indicator_table) {
|
|
|
|
|
|
- ranking = create_entity_indicator_ranking();
|
|
|
|
- ranking_num = create_entity_indicator_ranking_num();
|
|
|
|
|
|
+ ranking = iif(entity_type IN ['PL', 'CO'], create_mc_indicator_ranking(), create_entity_indicator_ranking());
|
|
|
|
+ ranking_num = iif(entity_type IN ['PL', 'CO'], create_mc_indicator_ranking_num(), create_entity_indicator_ranking_num());
|
|
|
|
+
|
|
|
|
+ if(entity_type IN ['PL', 'CO'])
|
|
|
|
+ v_groupby = ['curve_type', 'category_id', 'end_date'];
|
|
|
|
+ else
|
|
|
|
+ v_groupby = ['category_id', 'end_date'];
|
|
|
|
|
|
for(indicator in indicator_table) {
|
|
for(indicator in indicator_table) {
|
|
|
|
|
|
@@ -76,14 +81,14 @@ def gen_ranking_sql(data_table, indicator_table) {
|
|
}
|
|
}
|
|
|
|
|
|
// 绝对排名和百分位排名
|
|
// 绝对排名和百分位排名
|
|
- t_ranking = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id']), <indicator.id as indicator_id>,
|
|
|
|
|
|
+ t_ranking = sql(select = (sqlCol(['entity_id'].join(v_groupby)), <indicator.id as indicator_id>,
|
|
sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing),
|
|
sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing),
|
|
sqlCol(indicator.name + '_' + v_trailing, rank{, indicator.is_ASC}, 'absrank_' + v_trailing),
|
|
sqlCol(indicator.name + '_' + v_trailing, rank{, indicator.is_ASC}, 'absrank_' + v_trailing),
|
|
sqlCol(indicator.name + '_' + v_trailing, perRank{, indicator.is_ASC}, 'perrank_' + v_trailing)
|
|
sqlCol(indicator.name + '_' + v_trailing, perRank{, indicator.is_ASC}, 'perrank_' + v_trailing)
|
|
),
|
|
),
|
|
from = data_table,
|
|
from = data_table,
|
|
where = < category_id IS NOT NULL>,
|
|
where = < category_id IS NOT NULL>,
|
|
- groupBy = sqlCol(['category_id', 'end_date']),
|
|
|
|
|
|
+ groupBy = sqlCol(v_groupby),
|
|
groupFlag = 0 ).eval(); // context by
|
|
groupFlag = 0 ).eval(); // context by
|
|
|
|
|
|
// 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
|
|
// 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
|
|
@@ -99,10 +104,10 @@ def gen_ranking_sql(data_table, indicator_table) {
|
|
t_ranking.reorderColumns!(ranking.colNames());
|
|
t_ranking.reorderColumns!(ranking.colNames());
|
|
ranking.tableInsert(t_ranking);
|
|
ranking.tableInsert(t_ranking);
|
|
|
|
|
|
-
|
|
|
|
// 平均值、集合数量、各分位的阈值
|
|
// 平均值、集合数量、各分位的阈值
|
|
- t_ranking_num = sql(select =(sqlCol(['end_date', 'category_id']),
|
|
|
|
- sqlCol('raise_type', mean, 'raise_type'), <indicator.id as indicator_id>,
|
|
|
|
|
|
+ t_ranking_num = sql(select =(sqlCol(v_groupby),
|
|
|
|
+ iif(entity_type IN ['PL', 'CO'], <indicator.id as indicator_id>, // 取消基金经理/公司的 raise_type 字段
|
|
|
|
+ [sqlCol('raise_type', mean, 'raise_type'), <indicator.id as indicator_id>]),
|
|
sqlCol(indicator.name + '_' + v_trailing, mean, 'avg_' + v_trailing),
|
|
sqlCol(indicator.name + '_' + v_trailing, mean, 'avg_' + v_trailing),
|
|
sqlCol(indicator.name + '_' + v_trailing, count, 'avg_' + v_trailing + '_cnt'),
|
|
sqlCol(indicator.name + '_' + v_trailing, count, 'avg_' + v_trailing + '_cnt'),
|
|
sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_' + v_trailing),
|
|
sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_' + v_trailing),
|
|
@@ -117,7 +122,7 @@ def gen_ranking_sql(data_table, indicator_table) {
|
|
),
|
|
),
|
|
from = data_table,
|
|
from = data_table,
|
|
where = < category_id IS NOT NULL>,
|
|
where = < category_id IS NOT NULL>,
|
|
- groupBy = sqlCol(['category_id', 'end_date']),
|
|
|
|
|
|
+ groupBy = sqlCol(v_groupby),
|
|
groupFlag = 1).eval(); // group by
|
|
groupFlag = 1).eval(); // group by
|
|
|
|
|
|
// 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
|
|
// 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
|
|
@@ -151,10 +156,9 @@ def gen_ranking_sql(data_table, indicator_table) {
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
*/
|
|
-def run_ranking_sql(ranking_by, mutable data_table, indicator_table) {
|
|
|
|
|
|
+def run_ranking_sql(entity_type, ranking_by, mutable data_table, indicator_table) {
|
|
|
|
|
|
// data_table = t
|
|
// data_table = t
|
|
-// v_tables = v_ranking_tables
|
|
|
|
// ranking_by = 'strategy'
|
|
// ranking_by = 'strategy'
|
|
|
|
|
|
ret = array(ANY, 0);
|
|
ret = array(ANY, 0);
|
|
@@ -163,7 +167,7 @@ def run_ranking_sql(ranking_by, mutable data_table, indicator_table) {
|
|
|
|
|
|
UPDATE data_table SET category_id = factor_id;
|
|
UPDATE data_table SET category_id = factor_id;
|
|
|
|
|
|
- v_ranking = gen_ranking_sql(data_table, indicator_table);
|
|
|
|
|
|
+ v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
|
|
|
|
|
|
ret.append!(v_ranking[0]); // ranking table
|
|
ret.append!(v_ranking[0]); // ranking table
|
|
ret.append!(v_ranking[1]); // ranking_num table
|
|
ret.append!(v_ranking[1]); // ranking_num table
|
|
@@ -173,18 +177,22 @@ def run_ranking_sql(ranking_by, mutable data_table, indicator_table) {
|
|
// 策略排名
|
|
// 策略排名
|
|
UPDATE data_table SET category_id = strategy$STRING;
|
|
UPDATE data_table SET category_id = strategy$STRING;
|
|
|
|
|
|
- v_ranking = gen_ranking_sql(data_table, indicator_table);
|
|
|
|
|
|
+ v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
|
|
|
|
|
|
ret.append!(v_ranking[0]); // ranking table
|
|
ret.append!(v_ranking[0]); // ranking table
|
|
ret.append!(v_ranking[1]); // ranking_num table
|
|
ret.append!(v_ranking[1]); // ranking_num table
|
|
|
|
|
|
- // 子策略排名
|
|
|
|
- UPDATE data_table SET category_id = substrategy$STRING;
|
|
|
|
|
|
+ // 基金经理和公司没有子策略排名
|
|
|
|
+ if(! (entity_type IN ['PL', 'CO']) ) {
|
|
|
|
|
|
- v_ranking = gen_ranking_sql(data_table, indicator_table);
|
|
|
|
-
|
|
|
|
- ret.append!(v_ranking[0]); // ranking table
|
|
|
|
- ret.append!(v_ranking[1]); // ranking_num table
|
|
|
|
|
|
+ // 子策略排名
|
|
|
|
+ UPDATE data_table SET category_id = substrategy$STRING;
|
|
|
|
+
|
|
|
|
+ v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
|
|
|
|
+
|
|
|
|
+ ret.append!(v_ranking[0]); // ranking table
|
|
|
|
+ ret.append!(v_ranking[1]); // ranking_num table
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
return ret;
|
|
return ret;
|
|
@@ -215,7 +223,10 @@ def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isF
|
|
tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
|
|
tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
|
|
|
|
|
|
// 做个大宽表
|
|
// 做个大宽表
|
|
- matchingCols = [entity_id_name, 'end_date'];
|
|
|
|
|
|
+ if(entity_type IN ['PL', 'CO'])
|
|
|
|
+ matchingCols = [entity_id_name, 'curve_type', 'strategy', 'end_date'];
|
|
|
|
+ else
|
|
|
|
+ matchingCols = [entity_id_name, 'end_date'];
|
|
tb_data = lj(lj(lj(tb_data_return, tb_data_indicator_stats, matchingCols), tb_data_risk_stats, matchingCols), tb_data_riskadjret_stats, matchingCols);
|
|
tb_data = lj(lj(lj(tb_data_return, tb_data_indicator_stats, matchingCols), tb_data_risk_stats, matchingCols), tb_data_riskadjret_stats, matchingCols);
|
|
|
|
|
|
if(ranking_by == 'bfi') {
|
|
if(ranking_by == 'bfi') {
|
|
@@ -266,9 +277,14 @@ def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isF
|
|
|
|
|
|
tb_data.rename!(entity_id_name, 'entity_id');
|
|
tb_data.rename!(entity_id_name, 'entity_id');
|
|
|
|
|
|
- t = SELECT * FROM entity_info en
|
|
|
|
- INNER JOIN tb_data d ON en.entity_id = d.entity_id
|
|
|
|
- WHERE en.strategy IS NOT NULL;
|
|
|
|
|
|
+ if(entity_type IN ['PL', 'CO'])
|
|
|
|
+ t = SELECT * FROM entity_info en
|
|
|
|
+ INNER JOIN tb_data d ON en.entity_id = d.entity_id AND en.curve_type = d.curve_type AND en.strategy = d.strategy
|
|
|
|
+ WHERE en.strategy IS NOT NULL;
|
|
|
|
+ else
|
|
|
|
+ t = SELECT * FROM entity_info en
|
|
|
|
+ INNER JOIN tb_data d ON en.entity_id = d.entity_id
|
|
|
|
+ WHERE en.strategy IS NOT NULL;
|
|
|
|
|
|
if(ranking_by == 'bfi')
|
|
if(ranking_by == 'bfi')
|
|
UPDATE t SET category_id = factor_id;
|
|
UPDATE t SET category_id = factor_id;
|
|
@@ -292,11 +308,11 @@ def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isF
|
|
def cal_indicator_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
|
|
def cal_indicator_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
|
|
|
|
|
|
// 当前只对基金做排名, 其它类型参考基金排名做相对排名
|
|
// 当前只对基金做排名, 其它类型参考基金排名做相对排名
|
|
- if(!(entity_type in ['MF', 'HF'])) return null;
|
|
|
|
|
|
+ if(!(entity_type in ['MF', 'HF', 'PL', 'CO'])) return null;
|
|
|
|
|
|
v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
|
|
v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
|
|
|
|
|
|
- v_ranking_tables = run_ranking_sql(ranking_by, v[0], v[1]);
|
|
|
|
|
|
+ v_ranking_tables = run_ranking_sql(entity_type, ranking_by, v[0], v[1]);
|
|
|
|
|
|
return v_ranking_tables;
|
|
return v_ranking_tables;
|
|
}
|
|
}
|
|
@@ -438,21 +454,24 @@ def cal_relative_ranking(benchmark_ranking, mutable entity_ranking, isFromMySQL=
|
|
* @param ranking_tables <VECTOR>: 当 ranking_by = 'strategy' 时包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
|
|
* @param ranking_tables <VECTOR>: 当 ranking_by = 'strategy' 时包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
|
|
* ranking_by = 'bfi' 时包含2个数据表的向量,分别是bfi策略排名,bfi策略排名阈值
|
|
* ranking_by = 'bfi' 时包含2个数据表的向量,分别是bfi策略排名,bfi策略排名阈值
|
|
*/
|
|
*/
|
|
-def save_ranking_tables(ranking_by, ranking_tables) {
|
|
|
|
|
|
+def save_ranking_tables(entity_type, ranking_by, ranking_tables) {
|
|
|
|
|
|
if(ranking_tables.isVoid()) return;
|
|
if(ranking_tables.isVoid()) return;
|
|
|
|
|
|
- entity_id_col = 'fund_id';
|
|
|
|
|
|
+ des_strategy = get_indicator_ranking_table_description(entity_type)[0];
|
|
|
|
+ des_bfi = get_indicator_ranking_table_description(entity_type)[0];
|
|
|
|
+
|
|
|
|
+ entity_id_col = des_strategy.sec_id_col;
|
|
|
|
|
|
if(ranking_by == 'bfi') {
|
|
if(ranking_by == 'bfi') {
|
|
|
|
|
|
- source_table = 'raw_db.pf_fund_bfi_bm_indicator_ranking';
|
|
|
|
- target_table = 'raw_db.pf_fund_bfi_bm_indicator_ranking';
|
|
|
|
|
|
+ source_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
|
|
|
|
+ target_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
|
|
category_id_col = 'factor_id';
|
|
category_id_col = 'factor_id';
|
|
|
|
|
|
} else {
|
|
} else {
|
|
- source_table = 'raw_db.pf_fund_indicator_ranking';
|
|
|
|
- target_table = 'raw_db.pf_fund_indicator_ranking';
|
|
|
|
|
|
+ source_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
|
|
|
|
+ target_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
|
|
category_id_col = 'strategy';
|
|
category_id_col = 'strategy';
|
|
}
|
|
}
|
|
|
|
|
|
@@ -462,7 +481,7 @@ def save_ranking_tables(ranking_by, ranking_tables) {
|
|
t = ranking_tables[1];
|
|
t = ranking_tables[1];
|
|
save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
|
|
save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
|
|
|
|
|
|
- if(ranking_by == 'strategy') {
|
|
|
|
|
|
+ if(ranking_by == 'strategy' && entity_type IN ['HF', 'MF']) {
|
|
|
|
|
|
source_table = source_table.strReplace('_ranking', '_substrategy_ranking');
|
|
source_table = source_table.strReplace('_ranking', '_substrategy_ranking');
|
|
target_table = target_table.strReplace('_ranking', '_substrategy_ranking');
|
|
target_table = target_table.strReplace('_ranking', '_substrategy_ranking');
|