operationDataPuller.dos 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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, cpm.portfolio_source, cpm.portfolio_type, 102 AS strategy, sub_type AS substrategy
  80. FROM pfdb.`pf_customer_portfolio_map` cpm
  81. INNER JOIN pfdb.cm_user u ON cpm.userid = u.userid
  82. WHERE cpm.isvalid = 1
  83. AND u.isvalid = 1" +
  84. s_entity_sql + "
  85. ORDER BY cpm.id"
  86. conn = connect_mysql()
  87. t = odbc::query(conn, s_query)
  88. conn.close()
  89. return t
  90. }
  91. /*
  92. * 取组合有效信息
  93. *
  94. * Example: get_cus_fund_info(['CF0000005V','CF000000CE']);
  95. * get_cus_fund_info(NULL);
  96. *
  97. */
  98. def get_cus_fund_info(fund_ids) {
  99. s_entity_ids = ids_to_string(fund_ids);
  100. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.fund_id IN (" + s_entity_ids + ")");
  101. s_query = "SELECT fi.fund_id, fi.userid, fi.inception_date, IFNULL(fi.primary_benchmark_id, 'IN00000008') AS benchmark_id,
  102. IFNULL(initial_unit_value, 1) AS ini_value, raise_type, strategy, substrategy
  103. FROM pfdb.pf_cus_fund_information fi
  104. INNER JOIN pfdb.cm_user u ON fi.userid = u.userid
  105. WHERE fi.isvalid = 1
  106. AND u.isvalid = 1" +
  107. s_entity_sql + "
  108. ORDER BY fi.fund_id"
  109. conn = connect_mysql()
  110. t = odbc::query(conn, s_query)
  111. conn.close()
  112. return t
  113. }
  114. /*
  115. * 取基金组合基础有效信息
  116. *
  117. * Example: get_entity_info('HF', ['HF000004KN','HF000103EU','HF00018WXG']);
  118. * get_entity_info('PF', '166002,166114');
  119. * get_entity_info('MI', NULL);
  120. */
  121. def get_entity_info(entity_type, entity_ids) {
  122. t = null;
  123. s_entity_ids = ids_to_string(entity_ids);
  124. if(entity_type == 'MF' || entity_type == 'HF') {
  125. t = get_fund_info(s_entity_ids);
  126. t.rename!('fund_id', 'entity_id');
  127. } else if(entity_type == 'PF') {
  128. t = get_portfolio_info(s_entity_ids);
  129. t.rename!('portfolio_id', 'entity_id');
  130. } else if(entity_type IN ['MI', 'FI']) {
  131. t = get_index_info(s_entity_ids);
  132. t.rename!('index_id', 'entity_id');
  133. } else if(entity_type == 'CF') {
  134. t = get_cus_fund_info(s_entity_ids);
  135. t.rename!('fund_id', 'entity_id');
  136. }
  137. return t;
  138. }
  139. /*
  140. * 取某时间段的基金主基准
  141. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  142. *
  143. * Example: get_fund_primary_benchmark("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  144. */
  145. def get_fund_primary_benchmark(fund_ids, month_start, month_end) {
  146. s_query = "SELECT fund_id, primary_benchmark_id AS benchmark_id, inception_date
  147. FROM mfdb.fund_information
  148. WHERE fund_id IN (" + fund_ids + ")
  149. AND isvalid = 1;";
  150. conn = connect_mysql();
  151. t = odbc::query(conn, s_query);
  152. conn.close();
  153. t.addColumn('end_date', MONTH);
  154. m_start = temporalParse(month_start, 'yyyy-MM');
  155. m_end = temporalParse(month_end, 'yyyy-MM');
  156. tb_end_date = table(m_start..m_end AS end_date);
  157. 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());
  158. }
  159. /*
  160. * 取某时间段的组合主基准
  161. * NOTE: 目前所有Java指标计算组合默认主基准是FA00000VNB,以后很可能会改
  162. *
  163. * Example: get_portfolio_primary_benchmark("166002,166114", '1990-01', '2024-08');
  164. */
  165. def get_portfolio_primary_benchmark(portfolio_ids, month_start, month_end) {
  166. s_query = "SELECT id AS portfolio_id, 'FA00000VNB' AS benchmark_id, inception_date
  167. FROM pfdb.pf_customer_portfolio_map
  168. WHERE id IN (" + portfolio_ids + ")
  169. AND isvalid = 1;";
  170. conn = connect_mysql();
  171. t = odbc::query(conn, s_query);
  172. conn.close();
  173. t.addColumn('end_date', MONTH);
  174. m_start = temporalParse(month_start, 'yyyy-MM');
  175. m_end = temporalParse(month_end, 'yyyy-MM');
  176. tb_end_date = table(m_start..m_end AS end_date);
  177. 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());
  178. }
  179. /*
  180. * 取某时间段的基金组合主基准
  181. *
  182. * NOTE: 指数和因子的”主基准”设置为沪深300
  183. *
  184. * Example: get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  185. * get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
  186. * get_entity_primary_benchmark('MI', ['IN00000008', 'IN0000000M'], '2024-07', '2024-08');
  187. */
  188. def get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
  189. t = table(100:0,
  190. ['entity_id', 'end_date', 'benchmark_id'],
  191. [iif(entity_type == 'PF', INT, SYMBOL), MONTH, SYMBOL]);
  192. s_entity_ids = ids_to_string(entity_ids);
  193. if(s_entity_ids == null || s_entity_ids == '') return null;
  194. if(entity_type == 'MF' || entity_type == 'HF') {
  195. t = get_fund_primary_benchmark(s_entity_ids, month_start, month_end);
  196. t.rename!('fund_id', 'entity_id');
  197. } else if(entity_type == 'PF') {
  198. t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
  199. t.rename!('portfolio_id', 'entity_id');
  200. } else if(entity_type IN ['MI', 'FI', 'FA', 'CI', 'EQ']) {
  201. // 对于指数、因子来说,没有什么基准。但为了指标计算不得不在这里设个假的
  202. t = SELECT entity_id, end_date, 'IN00000008' AS benchmark_id
  203. FROM cj(get_entity_info(entity_type, s_entity_ids), table(temporalParse(month_start, 'yyyy-MM')..temporalParse(month_end, 'yyyy-MM') AS end_date))
  204. WHERE end_date >= iif(inception_date.isNull(), 1990.01M, inception_date.month())
  205. }
  206. return t;
  207. }
  208. /*
  209. * 取某时间段的基金组合BFI基准
  210. *
  211. *
  212. * Example: get_entity_bfi_factors('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", 1990.01M, 2024.06M);
  213. * get_entity_bfi_factors('PF', [166002,166114], 1990.01M, 2029.12M, 2024.10.01);
  214. */
  215. def get_entity_bfi_factors(entity_type, entity_ids, month_start, month_end, updatetime=1990.01.01) {
  216. tmp = get_bfi_by_category_group_table_description(entity_type);
  217. s_entity_ids = ids_to_string(entity_ids);
  218. sql_entity_id = '';
  219. if(s_entity_ids != NULL) {
  220. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  221. }
  222. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, end_date, factor_id
  223. FROM " + tmp.table_name[0] + "
  224. WHERE isvalid = 1 " +
  225. sql_entity_id + "
  226. AND end_date >= '" + month_start.temporalFormat('yyyy-MM') + "'
  227. AND end_date <= '" + month_end.temporalFormat('yyyy-MM') + "'
  228. AND updatetime >= '" + updatetime$STRING + "'
  229. ORDER BY " + tmp.sec_id_col[0] + ", end_date, factor_id";
  230. conn = connect_mysql();
  231. t = odbc::query(conn, s_query);
  232. conn.close();
  233. return t;
  234. }
  235. /*
  236. * 取基金-经理关系表
  237. *
  238. * Example:get_fund_manager_mapping(['MF00003PW1', 'MF00003PW2']);
  239. */
  240. def get_fund_manager_mapping(fund_ids) {
  241. t = null;
  242. s_entity_ids = ids_to_string(fund_ids);
  243. s_query = "SELECT fund_id, fund_manager_id, management_start_date, management_end_date
  244. FROM mfdb.fund_manager_mapping
  245. WHERE fund_id IN (" + s_entity_ids + ")
  246. AND isvalid = 1
  247. ORDER BY fund_id, management_start_date, management_end_date;";
  248. conn = connect_mysql();
  249. t = odbc::query(conn, s_query);
  250. conn.close();
  251. return t;
  252. }