123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- module fundit::sqlUtilities
-
- /*
- * MySQL dev server 连接,使用前应确保 loadPlugin("ODBC")已经被运行过
- *
- * Create 20240711 使用ODBC连接MySQL数据库 Joey
- *
- */
- def connect_mysql(user='pf_user') {
- // 阿里云的mysql被魔改过,当前DolphinDB无法支持MySQL插件,只能用ODBC
- // loadPlugin("ODBC")
- // conn = odbc::connect("Driver={MySQL ODBC 9.0 UNICODE Driver};Server=funditdb-dev.mysql.rds.aliyuncs.com;Database=mfdb;User=pf_user;Password=MzBlMDA0OG", "MySQL")
- // 使用Windows的ODBC数据源事先设置号的连接
- // conn = odbc::connect("Dsn=FunditDB-mfdb")
- s = "Dsn=FunditDB-dev-" + user;
- conn = odbc::connect(s);
- // t = odbc::query(conn, "SELECT * FROM pfdb.pf_portfolio_nav LIMIT 100")
- return conn;
- }
- /*
- * 取本地数据库
- *
- * get_local_database("fundit", "mfdb")
- */
- def get_local_database(server_name, db_name) {
- db = database(directory="D:/Program Files/DolphinDB/server/database/" + server_name + "/" + db_name + "/")
-
- return db
- }
- /*
- * 读本地dolphindb数据表
- *
- * load_table_from_local("fundit", mfdb.fund_performance")
- */
- def load_table_from_local(server_name, table_name) {
- db = get_local_database(server_name, table_name.split(".")[0])
-
- return loadTable(db, table_name.split(".")[1])
- }
- /*
- * 未知形态的id转为MySQL需要的的逗号分隔字符串
- *
- * Example: ids_to_string("'a','b','c'");
- * ids_to_string(['a', NULL, 'c']);
- * ids_to_string([1,2,3]);
- * ids_to_string(12);
- * ids_to_string('').isNull();
- */
- def ids_to_string(ids) {
- s_ids = '';
- if(ids.isVoid()) return s_ids;
- // 输入的 ids 是字符串标量
- if (ids.form() == 0) {
- s_ids = (ids$STRING).trim();
- // 输入的 ids 是字符串向量
- } else if(ids.form() == 1) {
- if(ids.type() == 4) // INTEGER
- s_ids = ids.concat(",").trim();
- else // STRING
- s_ids = "'" + ids.concat("','").trim() + "'";
- // 缺省返回空
- } else {
- s_ids = NULL;
- }
- return s_ids;
- }
- /*
- * 返回ID的规律(前缀)
- *
- */
- def get_entity_id_info() {
- tmp = table(10:0, ['entity_type', 'prefix'], [STRING, STRING]);
- // 公募,私募,私有基金,股票,市场指数,图译指数,图译因子,人物,公司
- INSERT INTO tmp VALUES (`MF`HF`CF`EQ`MI`FI`CI`FA`PL`CO, `MF`HF`CF`EQ`IN`IN`CI`FA`PL`CO);
- return tmp;
- }
- /*
- * 返回标准RBSA的几组指数集
- *
- */
- def get_rbsa_index() {
- d = dict(STRING, ANY);
- d['AS0000005Q'] = ['IN00000008', 'IN00000077','IN0000007G', 'IN0000009M'];
- d['BondType'] = ['IN0000007A', 'IN0000007G','IN0000008J', 'IN000002CM'];
- d['Cap3Style'] = ['FA00000WKG', 'FA00000WKH','IN0000007G'];
- d['CNI7Style'] = ['IN0000000S', 'IN0000000T','IN0000000U', 'IN0000000V', 'IN0000000W', 'IN0000000X', 'IN0000007G'];
- d['CSI11'] = ['IN0000000Y', 'IN0000000Z','IN00000010', 'IN00000011', 'IN00000012', 'IN00000013', 'IN00000014', 'IN00000015', 'IN00000016', 'IN00000017', 'IN0000007G'];
- d['CSI5'] = ['FA00000VML', 'FA00000VMM','FA00000VMN', 'FA00000VMO', 'IN0000007G'];
- d['Large4Assets'] = ['IN00000008', 'IN00000077','IN0000007G', 'IN0000009M'];
- return d;
- }
- /*
- * 根据不同类型的主体返回其净值表的表名、字段名和ID前两位特征字符
- */
- def get_nav_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'nav_col', 'prefix'],
- [STRING, STRING, STRING, STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.nav', 'mfdb.public_nav', 'pfdb.pf_cus_fund_nav', 'mfdb.stock_price', 'mfdb.market_indexes', 'mfdb.indexes_ty_index', 'pfdb.cm_udf_index_nav', 'pfdb.cm_factor_value', 'pfdb.pf_portfolio_nav'],
- ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'index_id', 'index_id', 'index_id', 'factor_id', 'portfolio_id'],
- ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
- ['nav', 'nav', 'nav', 'nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
- ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
-
- }
- /*
- * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
- */
- def get_performance_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
- [STRING, STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF', 'PL', 'CO'],
- ['mfdb.fund_performance', 'mfdb.fund_performance', 'pfdb.pf_cus_fund_performance', 'mfdb.stock_performance', 'mfdb.fund_performance', 'mfdb.fund_performance', 'pfdb.cm_udf_index_performance', 'pfdb.cm_factor_performance', 'pfdb.pf_portfolio_performance', 'mfdb.manager_performance', 'mfdb.company_performance'],
- ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id', 'manager_id', 'company_id'],
- ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
-
- }
- /*
- * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
- */
- def get_performance_weekly_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
- [STRING, STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_performance_weekly', 'mfdb.fund_performance_weekly', 'pfdb.pf_cus_fund_performance_weekly', 'mfdb.stock_performance_weekly', 'mfdb.fund_performance_weekly', 'mfdb.fund_performance_weekly', 'pfdb.cm_udf_index_performance_weekly', 'pfdb.cm_factor_performance_weekly', 'pfdb.pf_portfolio_performance_weekly'],
- ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
- ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
-
- }
- /*
- * 根据不同类型的主体返回其杂项指标的表名、字段名
- *
- * Example: get_indicator_table_description('HF');
- */
- def get_indicator_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_indicator', 'mfdb.fund_indicator', 'pfdb.pf_cus_fund_indicator', 'mfdb.fund_indicator', 'mfdb.fund_indicator', 'pfdb.cm_udf_index_indicator', 'pfdb.cm_factor_indicator', 'pfdb.pf_portfolio_indicator'],
- ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * 根据不同类型的主体返回其风险指标的表名、字段名
- *
- * Example: get_risk_stats_table_description('HF');
- */
- def get_risk_stats_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_risk_stats', 'mfdb.fund_risk_stats', 'pfdb.pf_cus_fund_risk_stats', 'mfdb.fund_risk_stats', 'mfdb.fund_risk_stats', 'pfdb.cm_udf_index_risk_stats', 'pfdb.cm_factor_risk_stats', 'pfdb.pf_portfolio_risk_stats'],
- ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * 根据不同类型的主体返回其风险调整收益指标的表名、字段名
- *
- * Example: get_riskadjret_stats_table_description('HF');
- */
- def get_riskadjret_stats_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_riskadjret_stats', 'mfdb.fund_riskadjret_stats', 'pfdb.pf_cus_fund_riskadjret_stats', 'mfdb.fund_riskadjret_stats', 'mfdb.fund_riskadjret_stats', 'pfdb.cm_udf_index_riskadjret_stats', 'pfdb.cm_factor_riskadjret_stats', 'pfdb.pf_portfolio_riskadjret_stats'],
- ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * 根据不同类型的主体返回其杂项指标的表名、字段名
- *
- * Example: get_capture_style_table_description('MF');
- */
- def get_capture_style_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_style_stats', 'mfdb.fund_style_stats', 'pfdb.pf_cus_fund_style_stats', 'mfdb.fund_style_stats', 'mfdb.fund_style_stats', 'pfdb.cm_udf_index_style_stats', 'pfdb.cm_factor_style_stats', 'pfdb.pf_portfolio_style_stats'],
- ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * 根据不同类型的主体返回其BFI指标的表名、字段名
- *
- * Example: get_risk_stats_table_description('HF');
- */
- def get_bfi_indicator_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_ty_bfi_bm_indicator', 'mfdb.fund_ty_bfi_bm_indicator', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_ty_bfi_bm_indicator'],
- ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * 根据不同类型的主体返回其有效BFI因子的表名、字段名
- *
- * Example: get_bfi_by_category_group_table_description('HF');
- */
- def get_bfi_by_category_group_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合, 经理
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF', 'MG'],
- ['pfdb.pf_fund_factor_bfi_by_category_group', 'pfdb.pf_fund_factor_bfi_by_category_group', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_factor_bfi_by_category_group', 'pf_manager_factor_bfi_by_category_group'],
- ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id', 'mamanger_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * Annulized multiple
- */
- def get_annulization_multiple(freq) {
- ret = 1;
-
- if (freq == 'd') {
- ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
- } else if (freq == 'w') {
- ret = 52;
- } else if (freq == 'm') {
- ret = 12;
- } else if (freq == 'q') {
- ret = 4;
- } else if (freq == 's') {
- ret = 2;
- } else if (freq == 'a') {
- ret = 1;
- }
-
- return ret;
- }
|