operationDataPuller.dos 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. module fundit::operationDataPuller
  2. use fundit::sqlUtilities
  3. /*
  4. * 取所有一级策略
  5. *
  6. */
  7. def get_strategy_list() {
  8. s_query = "SELECT strategy_id, strategy, rasie_type AS raise_type FROM mfdb.d_strategy WHERE isvalid = 1";
  9. conn = connect_mysql()
  10. t = odbc::query(conn, s_query)
  11. conn.close()
  12. return t
  13. }
  14. /*
  15. * 取所有二级策略
  16. *
  17. */
  18. def get_substrategy_list() {
  19. s_query = "SELECT substrategy_id, substrategy, raise_type FROM mfdb.d_substrategy WHERE isvalid = 1";
  20. conn = connect_mysql()
  21. t = odbc::query(conn, s_query)
  22. conn.close()
  23. return t
  24. }
  25. /*
  26. * 取有效基金基本信息
  27. *
  28. * Example: get_fund_info("'HF000004KN','HF00018WXG'");
  29. * get_fund_info(null);
  30. *
  31. */
  32. def get_fund_info(fund_ids) {
  33. s_entity_ids = ids_to_string(fund_ids);
  34. s_entity_sql = iif(s_entity_ids == NULL, '', " AND fi.fund_id IN (" + s_entity_ids + ")");
  35. s_query = "SELECT fi.fund_id, fi.inception_date, fi.primary_benchmark_id AS benchmark_id, IFNULL(fi.initial_unit_value, 1) AS ini_value, fs.strategy, fs.substrategy, fi.raise_type, fi.p_fund_id
  36. FROM mfdb.fund_information fi
  37. INNER JOIN mfdb.fund_strategy fs ON fi.fund_id = fs.fund_id
  38. WHERE fs.isvalid = 1
  39. AND fi.isvalid = 1" +
  40. s_entity_sql + "
  41. ORDER BY fi.fund_id"
  42. conn = connect_mysql()
  43. t = odbc::query(conn, s_query)
  44. conn.close()
  45. return t
  46. }
  47. /*
  48. * 取有效指数基本信息
  49. *
  50. * Example: get_index_info("'IN00000008','IN000002GE'");
  51. * get_index_info(null);
  52. *
  53. */
  54. def get_index_info(index_ids) {
  55. s_entity_ids = ids_to_string(index_ids);
  56. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.index_id IN (" + s_entity_ids + ")");
  57. s_query = "SELECT fi.index_id, fi.inception_date, NULL AS benchmark_id, IFNULL(fi.index_initial_value, 1) AS ini_value, fi.index_code, fi.index_type_id
  58. FROM mfdb.indexes_profile fi
  59. WHERE fi.isvalid = 1" +
  60. s_entity_sql + "
  61. ORDER BY fi.index_id";
  62. conn = connect_mysql();
  63. t = odbc::query(conn, s_query);
  64. conn.close();
  65. return t;
  66. }
  67. /*
  68. * 取组合有效信息
  69. *
  70. * NOTE: portfolio 的 strategy 统一为公募混合基金102, sub_strategy 用 sub_type (哪里维护的?)
  71. *
  72. * Example: get_portfolio_info('166002,166114');
  73. * get_portfolio_info(NULL);
  74. *
  75. */
  76. def get_portfolio_info(portfolio_ids) {
  77. s_entity_ids = ids_to_string(portfolio_ids);
  78. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND cpm.id IN (" + s_entity_ids + ")");
  79. s_query = "SELECT cpm.id AS portfolio_id, cpm.userid, cpm.customer_id, cpm.inception_date, 1 AS ini_value,
  80. cpm.portfolio_source, cpm.portfolio_type, 102 AS strategy, sub_type AS substrategy, u.org_id
  81. FROM pfdb.`pf_customer_portfolio_map` cpm
  82. INNER JOIN pfdb.cm_user u ON cpm.userid = u.userid
  83. WHERE cpm.isvalid = 1
  84. AND u.isvalid = 1" +
  85. s_entity_sql + "
  86. ORDER BY cpm.id"
  87. conn = connect_mysql()
  88. t = odbc::query(conn, s_query)
  89. conn.close()
  90. return t
  91. }
  92. /*
  93. * 取私有基金有效信息
  94. *
  95. * Example: get_cus_fund_info(['CF0000005V','CF000000CE']);
  96. * get_cus_fund_info(NULL);
  97. *
  98. */
  99. def get_cus_fund_info(fund_ids) {
  100. s_entity_ids = ids_to_string(fund_ids);
  101. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.fund_id IN (" + s_entity_ids + ")");
  102. s_query = "SELECT fi.fund_id, fi.userid, fi.inception_date, IFNULL(fi.primary_benchmark_id, 'IN00000008') AS benchmark_id,
  103. IFNULL(initial_unit_value, 1) AS ini_value, raise_type, strategy, substrategy
  104. FROM pfdb.pf_cus_fund_information fi
  105. INNER JOIN pfdb.cm_user u ON fi.userid = u.userid
  106. WHERE fi.isvalid = 1
  107. AND u.isvalid = 1" +
  108. s_entity_sql + "
  109. ORDER BY fi.fund_id"
  110. conn = connect_mysql()
  111. t = odbc::query(conn, s_query)
  112. conn.close()
  113. return t
  114. }
  115. /*
  116. * 取因子有效信息
  117. *
  118. * Example: get_factor_info(['FA00000VNB','FA00000VMJ']);
  119. * get_factor_info(NULL);
  120. *
  121. */
  122. def get_factor_info(fund_ids) {
  123. s_entity_ids = ids_to_string(fund_ids);
  124. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.factor_id IN (" + s_entity_ids + ")");
  125. s_query = "SELECT fi.factor_id, fi.factor_type, fi.inception_date, NULL AS benchmark_id, IFNULL(initial_value, 1) AS ini_value
  126. FROM pfdb.cm_factor_information fi
  127. WHERE fi.isvalid = 1 " +
  128. s_entity_sql + "
  129. ORDER BY fi.factor_id"
  130. conn = connect_mysql()
  131. t = odbc::query(conn, s_query)
  132. conn.close()
  133. return t
  134. }
  135. /*
  136. * 取基金组合基础有效信息
  137. *
  138. * Example: get_entity_info('HF', ['HF000004KN','HF000103EU','HF00018WXG']);
  139. * get_entity_info('PF', '166002,166114');
  140. * get_entity_info('MI', NULL);
  141. */
  142. def get_entity_info(entity_type, entity_ids) {
  143. t = null;
  144. s_entity_ids = ids_to_string(entity_ids);
  145. if(entity_type == 'MF' || entity_type == 'HF') {
  146. t = get_fund_info(s_entity_ids);
  147. t.rename!('fund_id', 'entity_id');
  148. } else if(entity_type == 'PF') {
  149. t = get_portfolio_info(s_entity_ids);
  150. t.rename!('portfolio_id', 'entity_id');
  151. } else if(entity_type IN ['MI', 'FI']) {
  152. t = get_index_info(s_entity_ids);
  153. t.rename!('index_id', 'entity_id');
  154. } else if(entity_type == 'CF') {
  155. t = get_cus_fund_info(s_entity_ids);
  156. t.rename!('fund_id', 'entity_id');
  157. } else if(entity_type == 'FA') {
  158. t = get_factor_info(s_entity_ids);
  159. t.rename!('factor_id', 'entity_id');
  160. }
  161. return t;
  162. }
  163. /*
  164. * 取某时间段的基金主基准
  165. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  166. *
  167. * Example: get_fund_primary_benchmark("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  168. */
  169. def get_fund_primary_benchmark(fund_ids, month_start, month_end) {
  170. s_query = "SELECT fund_id, primary_benchmark_id AS benchmark_id, inception_date
  171. FROM mfdb.fund_information
  172. WHERE fund_id IN (" + fund_ids + ")
  173. AND isvalid = 1;";
  174. conn = connect_mysql();
  175. t = odbc::query(conn, s_query);
  176. conn.close();
  177. t.addColumn('end_date', MONTH);
  178. m_start = temporalParse(month_start, 'yyyy-MM');
  179. m_end = temporalParse(month_end, 'yyyy-MM');
  180. tb_end_date = table(m_start..m_end AS end_date);
  181. return (SELECT t.fund_id, d.end_date, t.benchmark_id FROM t JOIN tb_end_date d WHERE d.end_date >= t.inception_date.month());
  182. }
  183. /*
  184. * 取某时间段的组合主基准
  185. * NOTE: 目前所有Java指标计算组合默认主基准是FA00000VNB,以后很可能会改
  186. *
  187. * Example: get_portfolio_primary_benchmark("166002,166114", '1990-01', '2024-08');
  188. */
  189. def get_portfolio_primary_benchmark(portfolio_ids, month_start, month_end) {
  190. s_query = "SELECT id AS portfolio_id, 'FA00000VNB' AS benchmark_id, inception_date
  191. FROM pfdb.pf_customer_portfolio_map
  192. WHERE id IN (" + portfolio_ids + ")
  193. AND isvalid = 1;";
  194. conn = connect_mysql();
  195. t = odbc::query(conn, s_query);
  196. conn.close();
  197. t.addColumn('end_date', MONTH);
  198. m_start = temporalParse(month_start, 'yyyy-MM');
  199. m_end = temporalParse(month_end, 'yyyy-MM');
  200. tb_end_date = table(m_start..m_end AS end_date);
  201. return (SELECT t.portfolio_id, d.end_date, t.benchmark_id FROM t JOIN tb_end_date d WHERE d.end_date >= t.inception_date.month());
  202. }
  203. /*
  204. * 取某时间段的基金组合主基准
  205. *
  206. * NOTE: 指数和因子的”主基准”设置为沪深300
  207. *
  208. * Example: get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  209. * get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
  210. * get_entity_primary_benchmark('MI', ['IN00000008', 'IN0000000M'], '2024-07', '2024-08');
  211. * get_entity_primary_benchmark('FA', ['FA00000VNB', 'FA00000VMJ'], '2024-01', '2024-08');
  212. */
  213. def get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
  214. t = table(100:0,
  215. ['entity_id', 'end_date', 'benchmark_id'],
  216. [iif(entity_type == 'PF', INT, SYMBOL), MONTH, SYMBOL]);
  217. s_entity_ids = ids_to_string(entity_ids);
  218. if(s_entity_ids == null || s_entity_ids == '') return null;
  219. if(entity_type == 'MF' || entity_type == 'HF') {
  220. t = get_fund_primary_benchmark(s_entity_ids, month_start, month_end);
  221. t.rename!('fund_id', 'entity_id');
  222. } else if(entity_type == 'PF') {
  223. t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
  224. t.rename!('portfolio_id', 'entity_id');
  225. } else if(entity_type IN ['MI', 'FI', 'FA', 'CI', 'EQ']) {
  226. t = SELECT entity_id, end_date, NULL AS benchmark_id
  227. FROM cj(get_entity_info(entity_type, s_entity_ids), table(temporalParse(month_start, 'yyyy-MM')..temporalParse(month_end, 'yyyy-MM') AS end_date))
  228. WHERE end_date >= iif(inception_date.isNull(), 1990.01M, inception_date.month())
  229. }
  230. return t;
  231. }
  232. /*
  233. * 取某时间段的基金组合BFI基准
  234. *
  235. *
  236. * Example: get_entity_bfi_factors('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", 1990.01M, 2024.06M);
  237. * get_entity_bfi_factors('PF', [166002,166114], 1990.01M, 2029.12M, 2024.10.01);
  238. */
  239. def get_entity_bfi_factors(entity_type, entity_ids, month_start, month_end, updatetime=1990.01.01) {
  240. tmp = get_bfi_by_category_group_table_description(entity_type);
  241. s_entity_ids = ids_to_string(entity_ids);
  242. sql_entity_id = '';
  243. if(s_entity_ids != NULL) {
  244. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  245. }
  246. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, end_date, factor_id
  247. FROM " + tmp.table_name[0] + "
  248. WHERE isvalid = 1 " +
  249. sql_entity_id + "
  250. AND end_date >= '" + month_start.temporalFormat('yyyy-MM') + "'
  251. AND end_date <= '" + month_end.temporalFormat('yyyy-MM') + "'
  252. AND updatetime >= '" + updatetime$STRING + "'
  253. ORDER BY " + tmp.sec_id_col[0] + ", end_date, factor_id";
  254. conn = connect_mysql();
  255. t = odbc::query(conn, s_query);
  256. conn.close();
  257. return t;
  258. }
  259. /*
  260. * 取基金-经理关系表
  261. *
  262. * Example:get_fund_manager_mapping(['MF00003PW1', 'MF00003PW2']);
  263. */
  264. def get_fund_manager_mapping(fund_ids) {
  265. t = null;
  266. s_entity_ids = ids_to_string(fund_ids);
  267. s_query = "SELECT fund_id, fund_manager_id, management_start_date, management_end_date
  268. FROM mfdb.fund_manager_mapping
  269. WHERE fund_id IN (" + s_entity_ids + ")
  270. AND isvalid = 1
  271. ORDER BY fund_id, management_start_date, management_end_date;";
  272. conn = connect_mysql();
  273. t = odbc::query(conn, s_query);
  274. conn.close();
  275. return t;
  276. }