123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- module fundit::performanceDataPuller
- use fundit::sqlUtilities
- /*
- * 通用取有更新的数据
- *
- * NOTE: isvalid = 0 的记录也会被返回
- *
- * Example: get_data_by_updatetime('mfdb.fund_performance_weekly', 2024.11.14T11:10:00, true);
- *
- */
- def get_data_by_updatetime(table_name, updatetime, isFromMySQL=true) {
- if(isFromMySQL == true) {
- s_query = "SELECT * FROM " + table_name + "
- WHERE updatetime >= '" + updatetime + "'";
-
- conn = connect_mysql()
-
- t = odbc::query(conn, s_query)
-
- conn.close()
- } else {
-
-
- }
- return t
- }
- def get_entity_list_by_weekly_return_updatetime(entity_type, entity_ids, updatetime, isFromMySQL=true) {
- tmp = get_performance_weekly_table_description(entity_type);
- s_entity_ids = ids_to_string(entity_ids);
- sql_entity_id = '';
- if(s_entity_ids != NULL) {
- sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
- }
- if(isFromMySQL == true) {
- nav_table_name = tmp.table_name[0];
-
- s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
- FROM " + tmp.table_name[0] + "
- WHERE isvalid = 1 " +
- sql_entity_id + "
- AND " + tmp.cumulative_nav_col[0] + " > 0
- AND updatetime >= '" + updatetime$STRING + "'
- GROUP BY " + tmp.sec_id_col[0] + "
- ORDER BY " + tmp.sec_id_col[0] + ", price_date";
-
- conn = connect_mysql()
-
- t = odbc::query(conn, s_query)
-
- conn.close()
- } else {
-
-
- }
- return t
- }
- def get_weekly_ret(entity_type, entity_ids, start_date=1990.01.01, end_date=2099.12.31, isFromMySQL=true){
- s_entity_ids = ids_to_string(entity_ids);
- if(s_entity_ids == null || s_entity_ids == '') return null;
- tmp = get_performance_weekly_table_description(entity_type);
- if(isFromMySQL == true) {
- s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, year_week, price_date, ret_1w AS ret, " + tmp.cumulative_nav_col[0] + " AS nav
- FROM " + tmp.table_name[0] + "
- WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
- AND isvalid = 1
- AND price_date BETWEEN '" + start_date + "' AND '" + end_date + "'
- ORDER BY " + tmp.sec_id_col[0] + ", price_date";
-
- conn = connect_mysql()
-
- t = odbc::query(conn, s_query)
-
- conn.close()
- } else {
-
- tb_local = load_table_from_local("fundit", tmp.table_name[0])
-
- }
- return t
- }
- def get_entity_weekly_rets(entity_type, entity_info) {
- rets = null;
- if(entity_info.isVoid() || entity_info.size() == 0) return rets;
- very_old_date = '1990.01.01';
-
- end_day = entity_info.price_date.max();
- return get_weekly_ret(entity_type, entity_info.entity_id, very_old_date, end_day, true);
- }
- def get_monthly_ret(entity_type, entity_ids, start_date, end_date, isFromMySQL) {
- s_entity_ids = ids_to_string(entity_ids);
- if(s_entity_ids == null || s_entity_ids == '') return null;
- tmp = get_performance_table_description(entity_type);
- yyyymm_start = start_date.temporalFormat("yyyy-MM")
- yyyymm_end = end_date.temporalFormat("yyyy-MM")
- if(isFromMySQL == true) {
- s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, end_date, price_date, ret_1m AS ret, " + tmp.cumulative_nav_col[0] + " AS nav
- FROM " + tmp.table_name[0] + "
- WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
- AND isvalid = 1
- AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
- ORDER BY " + tmp.sec_id_col[0] + ", end_date";
-
- conn = connect_mysql()
-
- t = odbc::query(conn, s_query)
-
- conn.close()
- } else {
-
- tb_local = load_table_from_local("fundit", tmp.table_name[0])
- s_col = (sqlCol(tmp.sec_id_col[0]), sqlCol("end_date"), sqlColAlias(<ret_1m>, "ret"), sqlColAlias(<cumulative_nav>, "nav"), sqlCol("ret_ytd_a"), sqlCol("ret_incep_a"))
-
- s_where = expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(","))
-
- t = sql(s_col, tb_local, s_where).eval()
-
- }
- return t
- }
- def get_risk_free_rate(start_date, end_date) {
-
- return get_monthly_ret('MI', "'IN0000000M'", start_date, end_date, true);
- }
- def get_fund_latest_nav_performance(fund_ids, isFromMySQL) {
- if(isFromMySQL == true) {
-
- s_query = "SELECT *
- FROM mfdb.fund_latest_nav_performance
- WHERE fund_id IN (" + fund_ids + ")
- AND isvalid = 1
- ORDER BY fund_id"
- conn = connect_mysql()
-
- t = odbc::query(conn, s_query)
-
- conn.close()
- } else {
- tb_local = load_table_from_local("fundit", "mfdb.fund_latest_nav_performance")
- s_col = sqlCol("*")
- s_where = expr(<fund_id>, in, fund_ids.strReplace("'", "").split(","))
-
- t = sql(s_col, tb_local, s_where).eval()
- }
- return t
- }
- def get_nav_by_price_date(entity_type, entity_ids, price_date, isFromMySQL) {
- s_entity_ids = ids_to_string(entity_ids);
- if(s_entity_ids == null || s_entity_ids == '') return null;
- tmp = get_nav_table_description(entity_type);
- if(isFromMySQL == true) {
- nav_table_name = tmp.table_name[0];
-
- s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, price_date, " + tmp.cumulative_nav_col[0] + " AS cumulative_nav, " + tmp.nav_col[0] + " AS nav
- FROM " + tmp.table_name[0] + "
- WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
- AND isvalid = 1
- AND " + tmp.cumulative_nav_col[0] + " > 0
- AND price_date >= '" + price_date$STRING + "'
- ORDER BY " + tmp.sec_id_col[0] + ", price_date";
-
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- } else {
-
- tb_local = load_table_from_local("fundit", tmp.table_name[0])
- s_col = sqlCol("*")
-
- s_where = [expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(",")), <price_date >= price_date>]
-
- t = sql(s_col, tb_local, s_where).eval()
-
- }
- return t
- }
- def get_index_nav_by_price_date(index_ids, price_date) {
- s_query = "SELECT index_id, price_date, close AS cumulative_nav
- FROM mfdb.market_indexes
- WHERE index_id IN (" + index_ids + ")
- AND isvalid = 1
- AND close > 0
- AND price_date >= '" + price_date + "'
- UNION
- SELECT index_id AS index_id, price_date, index_value AS cumulative_nav
- FROM mfdb.indexes_ty_index
- WHERE index_id IN (" + index_ids + ")
- AND isvalid = 1
- AND index_value > 0
- AND price_date >= '" + price_date + "'
- UNION
- SELECT factor_id AS index_id, price_date, factor_value AS cumulative_nav
- FROM pfdb.cm_factor_value
- WHERE factor_id IN (" + index_ids + ")
- AND isvalid = 1
- AND factor_value > 0
- AND price_date >= '" + price_date + "'
- ORDER BY price_date"
- conn = connect_mysql()
- t = odbc::query(conn, s_query)
- conn.close()
- return t
- }
- def get_entity_list_by_nav_updatetime(entity_type, entity_ids, updatetime, isFromMySQL) {
- tmp = get_nav_table_description(entity_type);
- s_entity_ids = ids_to_string(entity_ids);
- sql_entity_id = '';
- if(s_entity_ids != NULL) {
- sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
- }
- if(isFromMySQL == true) {
- nav_table_name = tmp.table_name[0];
-
- s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
- FROM " + tmp.table_name[0] + "
- WHERE isvalid = 1 " +
- sql_entity_id + "
- AND " + tmp.cumulative_nav_col[0] + " > 0
- AND updatetime >= '" + updatetime$STRING + "'
- GROUP BY " + tmp.sec_id_col[0] + "
- ORDER BY " + tmp.sec_id_col[0] + ", price_date";
-
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- } else {
-
- }
- return t
- }
- def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
-
- s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
- conn = connect_mysql()
- t = odbc::query(conn, s_query)
- conn.close()
- return t
- }
- def get_nav_for_return_calculation(entity_type, freq, json_query) {
- s_query = "CALL pfdb.sp_get_nav_after_date('" + entity_type + "', '" + freq + "', '" + json_query + "')";
- conn = connect_mysql();
- t = odbc::query(conn, s_query);
- conn.close();
- return t;
- }
- def get_benchmark_return(benchmarks, end_day) {
- s_index_ids = '';
- s_factor_ids = '';
- if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
-
- t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';;
- s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
-
- t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
- s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
-
- t_bmk = SELECT entity_id AS benchmark_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_monthly_ret('MI', s_index_ids, 1990.01.01, end_day, true);
-
- INSERT INTO t_bmk SELECT entity_id AS factor_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_monthly_ret('FA', s_factor_ids, 1990.01.01, end_day, true);
- return t_bmk;
- }
- def check_universe (entity_ids) {
- v_id = array(STRING);
- dic = dict(STRING, ANY);
- if(entity_ids.isVoid() || entity_ids.size() == 0) return dic;
- for(r in get_entity_id_info()) {
- v_id = entity_ids[like(entity_ids, r.prefix + '%') && strlen(entity_ids)==10];
- if(v_id.size() > 0) {
- dic[r.entity_type] = v_id;
- }
- }
- return dic;
- }
- def get_entity_return(entity_type, entity_ids, freq='m', start_day=1990.01.01, end_day=2099.12.31, isFromMySQL=true) {
- t_ret = table(1000:0, ['entity_id', 'effective_date', 'price_date', 'ret', 'nav'], [iif(entity_type=='PF', INT, SYMBOL), STRING, DATE, DOUBLE, DOUBLE]);
- d = dict(STRING, ANY);
- if(entity_type != null) d[entity_type] = entity_ids;
- else
- d = check_universe(entity_ids);
- for(univ in d.keys()) {
- if(freq == 'm') {
-
- t = SELECT entity_id, end_date$STRING AS effective_date, price_date, ret, nav
- FROM get_monthly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
-
- t_ret.tableInsert(t);
-
- } else if(freq == 'w') {
-
- t = SELECT entity_id, year_week$STRING AS effective_date, price_date, ret, nav
- FROM get_weekly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
-
- t_ret.tableInsert(t);
-
- } else {
-
- s_json = table(d[univ] AS sec_id, take(start_day, d[univ].size()) AS price_date).toStdJson();
-
-
- tb_nav = get_nav_for_return_calculation(univ, 'd', s_json);
- if(!tb_nav.isVoid() && tb_nav.size() > 0) {
-
- tb_nav.rename!('sec_id', 'entity_id').sortBy!(['entity_id', 'price_date'], [1, 1]);
-
- t = SELECT entity_id, price_date$STRING AS effective_date, price_date, cumulative_nav.ratios()-1 AS ret, cumulative_nav AS nav
- FROM tb_nav
- WHERE price_date <= end_day CONTEXT BY entity_id;
- t_ret.tableInsert(t);
- }
- }
- }
- return t_ret;
-
- }
- def get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, isFromMySQL) {
- t = null;
- s_entity_ids = ids_to_string(portfolio_ids);
- if(isFromMySQL == true) {
- s_query = "CALL pfdb.sp_get_portfolios_to_cal_nav(" + iif(s_entity_ids.isNull(), 'NULL', "'" + s_entity_ids + "'") + ",'" + updatetime + "')";
- conn = connect_mysql();
- t = odbc::query(conn, s_query);
- conn.close();
- }
- return t
- }
- def get_bfi_factor_list_by_index_nav_updatetime(factor_ids, updatetime, isFromMySQL) {
- t = null;
- s_entity_ids = ids_to_string(factor_ids);
- sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.factor_id IN (" + s_entity_ids + ")");
- sql_updatetime = iif(updatetime == NULL, '', " AND mi.updatetime > '" + updatetime + "'");
- if(isFromMySQL == true) {
- s_query = "SELECT fi.factor_id, GREATEST(MIN(mi.price_date), fi.inception_date) AS first_cal_date, LEAST(MAX(mi.price_date), CURRENT_DATE) AS latest_cal_date
- FROM pfdb.cm_factor_information fi
- INNER JOIN pfdb.cm_factor_index_map map ON fi.factor_id = map.factor_id
- INNER JOIN mfdb.market_indexes mi ON map.index_id = mi.index_id
- WHERE fi.isvalid = 1
- AND fi.factor_type = 5" +
- sql_entity_id + "
- AND map.isvalid = 1
- AND mi.isvalid = 1
- AND mi.close > 0 " +
- sql_updatetime + "
- GROUP BY fi.factor_id";
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- }
- return t
- }
- def get_fixed_weight_portfolio_holding(entity_type, entity_ids) {
- t = null;
- s_query = '';
- s_entity_ids = ids_to_string(entity_ids);
- if(entity_type == 'FA') {
- sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND factor_id IN (" + s_entity_ids + ")");
-
- s_query = "SELECT factor_id AS entity_id, holding_date, index_id AS sec_id, weight, 2 AS entity_type
- FROM pfdb.cm_factor_index_map
- WHERE isvalid = 1" +
- sql_entity_id;
- } else if(entity_type == 'PF') {
- sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND portfolio_id IN (" + s_entity_ids + ")");
-
- s_query = "SELECT portfolio_id AS entity_id, effective_date AS holding_date, entity_id AS sec_id, weight, entity_type
- FROM pfdb.pf_portfolio_rebalance_weights
- WHERE isvalid = 1" +
- sql_entity_id;
-
- }
- if( s_query == '') return t;
- conn = connect_mysql();
- t = odbc::query(conn, s_query);
- conn.close();
- return t;
- }
- def get_entity_nav_by_date(entity_type, s_json, isFromMySQL=true) {
- t = null;
- desc = get_nav_table_description(entity_type)[0];
- if(isFromMySQL == true) {
- s_query = "SELECT t.entity_id, t.price_date, nav." + desc.nav_col + " AS cumulative_nav
- FROM JSON_TABLE ( '" + s_json + "', '$[*]'
- COLUMNS ( entity_id " + iif(entity_type=='PF', 'INT', 'VARCHAR(10)') + " PATH '$.entity_id',
- price_date DATE PATH '$.price_date' ) ) t
- LEFT JOIN " + desc.table_name + " nav ON t.entity_id = nav." + desc.sec_id_col + " AND t.price_date = nav.price_date;";
- conn = connect_mysql();
- t = odbc::query(conn, s_query);
-
- conn.close();
- }
- return t;
-
- }
- def get_monthly_indicator_data(table_name, end_date, isFromMySQL=true) {
- t = null;
- s_end_date_sql = iif(end_date.isNull(), '', " AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" );
- if(isFromMySQL == true) {
- s_query = "SELECT * FROM " + table_name + " WHERE isvalid = 1 " + s_end_date_sql;
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- }
- return t;
- }
- def get_fund_indicator_ranking(fund_ids, end_date, strategy, isFromMySQL=true) {
- t = null;
- s_entity_ids = ids_to_string(fund_ids);
- sql_entity_id = '';
- if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
- s_strategy_ids = ids_to_string(strategy);
- sql_strategy_id = '';
- if(s_strategy_ids != NULL) sql_strategy_id = " AND strategy IN (" + s_strategy_ids + ")";
- if(isFromMySQL == true) {
- s_query = "SELECT *
- FROM pfdb.pf_fund_indicator_ranking
- WHERE isvalid = 1
- AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
- sql_strategy_id +
- sql_entity_id;
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- }
- return t;
- }
- def get_fund_indicator_substrategy_ranking(fund_ids, end_date, substrategy, isFromMySQL=true) {
- t = null;
- s_entity_ids = ids_to_string(fund_ids);
- sql_entity_id = '';
- if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
- s_strategy_ids = ids_to_string(substrategy);
- sql_strategy_id = '';
- if(s_strategy_ids != NULL) sql_strategy_id = " AND substrategy IN (" + s_strategy_ids + ")";
- if(isFromMySQL == true) {
- s_query = "SELECT *
- FROM pfdb.pf_fund_indicator_substrategy_ranking
- WHERE isvalid = 1
- AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
- sql_strategy_id +
- sql_entity_id;
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- }
- return t;
- }
- def get_fund_bfi_bm_indicator_ranking(fund_ids, end_date, factor_ids, isFromMySQL=true) {
- t = null;
- s_entity_ids = ids_to_string(fund_ids);
- sql_entity_id = '';
- if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
- s_factor_ids = ids_to_string(factor_ids);
- sql_factor_id = '';
- if(s_factor_ids != NULL) sql_factor_id = " AND factor_id IN (" + s_factor_ids + ")";
- if(isFromMySQL == true) {
- s_query = "SELECT *
- FROM pfdb.pf_fund_bfi_bm_indicator_ranking
- WHERE isvalid = 1
- AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
- sql_factor_id +
- sql_entity_id;
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- }
- return t;
- }
- def get_correlation_index_list() {
- s_query = "SELECT a.fund_id AS entity_id
- FROM mfdb.fund_performance AS a
- LEFT JOIN pfdb.cm_class_asset_index AS b ON a.fund_id = b.index_id
- WHERE a.end_date = LEFT(CURRENT_DATE, 7)
- AND a.fund_id LIKE 'IN%'
- AND b.isvalid = 1
- ORDER BY a.fund_id ASC";
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- INSERT INTO t VALUES (['FA00000VML','FA00000VMM','FA00000VMN','FA00000VMO','FA00000WKG','FA00000WKH']);
- return t;
- }
- def get_bfi_index_list() {
- s_query = "SELECT factor_id, factor_name, category_group_id, maximum_num, order_id
- FROM pfdb.cm_factor_information
- WHERE isvalid = 1
- AND ( factor_type = 5 OR factor_id IN ('FA00000SMB', 'FA00000VMG') )
- ORDER BY order_id ASC";
- conn = connect_mysql();
-
- t = odbc::query(conn, s_query);
-
- conn.close();
- return t;
- }
|