dataPuller.dos 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. module fundit::dataPuller
  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_index_weekly_rets("'FA00000WKG','FA00000WKH','IN0000007G'", 1990.01.01, today());
  29. * get_index_weekly_rets("'IN0000000M'", 1990.01.01, 2024.10.31);
  30. */
  31. def get_index_weekly_rets(index_ids, start_date, end_date) {
  32. s_start_date = iif(start_date.isNull(), "", " AND price_date >= '" + start_date$STRING + "'")
  33. s_end_date = iif(end_date.isNull(), "", " AND price_date <= '" + end_date$STRING + "'")
  34. s_query = "SELECT factor_id AS index_id, year_week, price_date, factor_value AS cumulative_nav, ret_1w
  35. FROM pfdb.cm_factor_performance_weekly
  36. WHERE isvalid = 1
  37. AND factor_id IN (" + index_ids + ")" +
  38. s_start_date +
  39. s_end_date + "
  40. AND ret_1w IS NOT NULL
  41. UNION
  42. SELECT fund_id AS index_id, year_week, price_date, cumulative_nav, ret_1w
  43. FROM mfdb.fund_performance_weekly
  44. WHERE isvalid = 1
  45. AND fund_id IN (" + index_ids + ")" +
  46. s_start_date +
  47. s_end_date + "
  48. AND ret_1w IS NOT NULL
  49. ORDER BY year_week"
  50. conn = connect_mysql()
  51. t = odbc::query(conn, s_query)
  52. conn.close()
  53. return t
  54. }
  55. /*
  56. * 取基金周收益
  57. *
  58. *
  59. * get_fund_weekly_rets("'MF00003TMH','MF00003UQM'", 1990.01.01, null, true)
  60. */
  61. def get_fund_weekly_rets(fund_ids, start_date, end_date, isFromMySQL) {
  62. s_fund_id = iif(fund_ids.isNull(), "", " AND fund_id IN (" + fund_ids + ")")
  63. s_start_date = iif(start_date.isNull(), "", " AND price_date >= '" + start_date$STRING + "'")
  64. s_end_date = iif(end_date.isNull(), "", " AND price_date <= '" + end_date$STRING + "'")
  65. s_query = "SELECT fund_id, year_week, price_date, cumulative_nav, ret_1w
  66. FROM mfdb.fund_performance_weekly
  67. WHERE isvalid = 1 " +
  68. s_fund_id +
  69. s_start_date +
  70. s_end_date + "
  71. AND ret_1w IS NOT NULL
  72. ORDER BY fund_id, year_week"
  73. conn = connect_mysql()
  74. t = odbc::query(conn, s_query)
  75. conn.close()
  76. return t
  77. }
  78. /*
  79. * 通用取周收益
  80. *
  81. */
  82. def get_entity_weekly_rets(entity_type, entity_info) {
  83. rets = null;
  84. if(entity_info.isVoid() || entity_info.size() == 0) return rets;
  85. very_old_date = '1990.01.01';
  86. // 简单起见,取整个数据集的最新日期(month-end production时取上月最后一天即可
  87. end_day = entity_info.price_date.max();
  88. s_entity_ids = ids_to_string(entity_info.entity_id);
  89. if(entity_type == 'HF' || entity_type == 'MF') {
  90. rets = get_fund_weekly_rets(s_entity_ids, very_old_date, end_day, true);
  91. } else if(entity_type == 'MI' || entity_type == 'FA') {
  92. rets = get_index_weekly_rets(s_entity_ids, very_old_date, end_day);
  93. }
  94. return rets;
  95. }
  96. /*
  97. * 取组合周收益
  98. * TODO: 增加从本地取数据的功能
  99. *
  100. *
  101. * get_portfolio_weekly_rets("166002,364640", 1990.01.01, today(), true)
  102. */
  103. def get_portfolio_weekly_rets(portfolio_ids, start_date, end_date, isFromMySQL) {
  104. s_portfolio_id = iif(portfolio_ids.isNull(), "", " AND portfolio_id IN (" + portfolio_ids + ")")
  105. s_query = "SELECT portfolio_id, year_week, price_date, cumulative_nav, ret_1w
  106. FROM pfdb.pf_portfolio_performance_weekly
  107. WHERE isvalid = 1 " +
  108. s_portfolio_id + "
  109. AND ret_1w IS NOT NULL
  110. AND price_date BETWEEN '" + start_date$STRING + "' AND '" + end_date$STRING + "'
  111. ORDER BY portfolio_id, year_week"
  112. conn = connect_mysql()
  113. t = odbc::query(conn, s_query)
  114. conn.close()
  115. return t
  116. }
  117. /*
  118. * 通用取月收益
  119. *
  120. * @param entity_type <STRING>:
  121. * @param entity_ids <VECTOR|STRING>:
  122. * @param start_date <DATE>:
  123. * @param end_date <DATE>:
  124. * @param isFromMySQL <BOOL>:
  125. *
  126. *
  127. * Example: get_monthly_ret('HF', ['HF000004KN','HF000103EU','HF00018WXG'], 2000.01.01, 2024.03.01, true);
  128. */
  129. def get_monthly_ret(entity_type, entity_ids, start_date, end_date, isFromMySQL) {
  130. s_entity_ids = ids_to_string(entity_ids);
  131. if(s_entity_ids == null || s_entity_ids == '') return null;
  132. tmp = get_performance_table_description(entity_type);
  133. yyyymm_start = start_date.temporalFormat("yyyy-MM")
  134. yyyymm_end = end_date.temporalFormat("yyyy-MM")
  135. if(isFromMySQL == true) {
  136. 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
  137. FROM " + tmp.table_name[0] + "
  138. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  139. AND isvalid = 1
  140. AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
  141. ORDER BY " + tmp.sec_id_col[0] + ", end_date";
  142. conn = connect_mysql()
  143. t = odbc::query(conn, s_query)
  144. conn.close()
  145. } else {
  146. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  147. 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"))
  148. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0], then rename to "entity_id"?
  149. s_where = expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(","))
  150. t = sql(s_col, tb_local, s_where).eval()
  151. }
  152. return t
  153. }
  154. /*
  155. * 取无风险月度利率
  156. *
  157. * get_risk_free_rate(1990.01.01, today())
  158. */
  159. def get_risk_free_rate(start_date, end_date) {
  160. return get_monthly_ret('MI', "'IN0000000M'", start_date, end_date, true);
  161. }
  162. /*
  163. * 取基金最新收益及净值
  164. *
  165. * get_fund_latest_nav_performance("'HF000004KN','HF00018WXG'")
  166. */
  167. def get_fund_latest_nav_performance(fund_ids, isFromMySQL) {
  168. if(isFromMySQL == true) {
  169. s_query = "SELECT *
  170. FROM mfdb.fund_latest_nav_performance
  171. WHERE fund_id IN (" + fund_ids + ")
  172. AND isvalid = 1
  173. ORDER BY fund_id"
  174. conn = connect_mysql()
  175. t = odbc::query(conn, s_query)
  176. conn.close()
  177. } else {
  178. tb_local = load_table_from_local("fundit", "mfdb.fund_latest_nav_performance")
  179. s_col = sqlCol("*")
  180. s_where = expr(<fund_id>, in, fund_ids.strReplace("'", "").split(","))
  181. t = sql(s_col, tb_local, s_where).eval()
  182. }
  183. return t
  184. }
  185. /*
  186. * 通用取净值
  187. *
  188. *
  189. * Create: 202408 Joey
  190. * TODO: add isvalid and nav > 0 for local version
  191. *
  192. *
  193. * Example: get_nav_by_price_date('HF', "'HF000004KN','HF00018WXG'", 2024.05.01, true);
  194. * get_nav_by_price_date('MI', "'IN00000008','IN0000000M'", 2024.05.01, true);
  195. */
  196. def get_nav_by_price_date(entity_type, entity_ids, price_date, isFromMySQL) {
  197. s_entity_ids = ids_to_string(entity_ids);
  198. if(s_entity_ids == null || s_entity_ids == '') return null;
  199. tmp = get_nav_table_description(entity_type);
  200. if(isFromMySQL == true) {
  201. nav_table_name = tmp.table_name[0];
  202. 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
  203. FROM " + tmp.table_name[0] + "
  204. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  205. AND isvalid = 1
  206. AND " + tmp.cumulative_nav_col[0] + " > 0
  207. AND price_date >= '" + price_date$STRING + "'
  208. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  209. conn = connect_mysql();
  210. t = odbc::query(conn, s_query);
  211. conn.close();
  212. } else {
  213. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  214. s_col = sqlCol("*")
  215. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0]?
  216. s_where = [expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(",")), <price_date >= price_date>]
  217. t = sql(s_col, tb_local, s_where).eval()
  218. }
  219. return t
  220. }
  221. /*
  222. * 取指数因子点位
  223. *
  224. * get_index_nav_by_price_date("'IN00000008','FA00000WKG'", 2024.06.01)
  225. */
  226. def get_index_nav_by_price_date(index_ids, price_date) {
  227. s_query = "SELECT index_id, price_date, close AS cumulative_nav
  228. FROM mfdb.market_indexes
  229. WHERE index_id IN (" + index_ids + ")
  230. AND isvalid = 1
  231. AND close > 0
  232. AND price_date >= '" + price_date + "'
  233. UNION
  234. SELECT index_id AS index_id, price_date, index_value AS cumulative_nav
  235. FROM mfdb.indexes_ty_index
  236. WHERE index_id IN (" + index_ids + ")
  237. AND isvalid = 1
  238. AND index_value > 0
  239. AND price_date >= '" + price_date + "'
  240. UNION
  241. SELECT factor_id AS index_id, price_date, factor_value AS cumulative_nav
  242. FROM pfdb.cm_factor_value
  243. WHERE factor_id IN (" + index_ids + ")
  244. AND isvalid = 1
  245. AND factor_value > 0
  246. AND price_date >= '" + price_date + "'
  247. ORDER BY price_date"
  248. conn = connect_mysql()
  249. t = odbc::query(conn, s_query)
  250. conn.close()
  251. return t
  252. }
  253. /*
  254. * 取有效基金基本信息
  255. *
  256. * Example: get_fund_info("'HF000004KN','HF00018WXG'");
  257. * get_fund_info(null);
  258. *
  259. */
  260. def get_fund_info(fund_ids) {
  261. s_entity_ids = ids_to_string(fund_ids);
  262. s_entity_sql = iif(s_entity_ids == NULL, '', " AND fi.fund_id IN (" + s_entity_ids + ")");
  263. 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
  264. FROM mfdb.fund_information fi
  265. INNER JOIN mfdb.fund_strategy fs ON fi.fund_id = fs.fund_id
  266. WHERE fs.isvalid = 1
  267. AND fi.isvalid = 1" +
  268. s_entity_sql + "
  269. ORDER BY fi.fund_id"
  270. conn = connect_mysql()
  271. t = odbc::query(conn, s_query)
  272. conn.close()
  273. return t
  274. }
  275. /*
  276. * 取有效指数基本信息
  277. *
  278. * Example: get_index_info("'IN00000008','IN000002GE'");
  279. * get_index_info(null);
  280. *
  281. */
  282. def get_index_info(index_ids) {
  283. s_entity_ids = ids_to_string(index_ids);
  284. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.index_id IN (" + s_entity_ids + ")");
  285. 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
  286. FROM mfdb.indexes_profile fi
  287. WHERE fi.isvalid = 1" +
  288. s_entity_sql + "
  289. ORDER BY fi.index_id";
  290. conn = connect_mysql();
  291. t = odbc::query(conn, s_query);
  292. conn.close();
  293. return t;
  294. }
  295. /*
  296. * 取组合有效信息
  297. *
  298. * NOTE: portfolio 的 strategy 统一为公募混合基金102, sub_strategy 用 sub_type (哪里维护的?)
  299. *
  300. * Example: get_portfolio_info('166002,166114');
  301. * get_portfolio_info(NULL);
  302. *
  303. */
  304. def get_portfolio_info(portfolio_ids) {
  305. s_entity_ids = ids_to_string(portfolio_ids);
  306. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND cpm.id IN (" + s_entity_ids + ")");
  307. 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
  308. FROM pfdb.`pf_customer_portfolio_map` cpm
  309. INNER JOIN pfdb.cm_user u ON cpm.userid = u.userid
  310. WHERE cpm.isvalid = 1
  311. AND u.isvalid = 1" +
  312. s_entity_sql + "
  313. ORDER BY cpm.id"
  314. conn = connect_mysql()
  315. t = odbc::query(conn, s_query)
  316. conn.close()
  317. return t
  318. }
  319. /*
  320. * 取组合有效信息
  321. *
  322. * Example: get_cus_fund_info(['CF0000005V','CF000000CE']);
  323. * get_cus_fund_info(NULL);
  324. *
  325. */
  326. def get_cus_fund_info(fund_ids) {
  327. s_entity_ids = ids_to_string(fund_ids);
  328. s_entity_sql = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.fund_id IN (" + s_entity_ids + ")");
  329. s_query = "SELECT fi.fund_id, fi.userid, fi.inception_date, IFNULL(fi.primary_benchmark_id, 'IN00000008') AS benchmark_id,
  330. IFNULL(initial_unit_value, 1) AS ini_value, raise_type, strategy, substrategy
  331. FROM pfdb.pf_cus_fund_information fi
  332. INNER JOIN pfdb.cm_user u ON fi.userid = u.userid
  333. WHERE fi.isvalid = 1
  334. AND u.isvalid = 1" +
  335. s_entity_sql + "
  336. ORDER BY fi.fund_id"
  337. conn = connect_mysql()
  338. t = odbc::query(conn, s_query)
  339. conn.close()
  340. return t
  341. }
  342. /*
  343. * 取基金组合基础有效信息
  344. *
  345. * Example: get_entity_info('HF', ['HF000004KN','HF000103EU','HF00018WXG']);
  346. * get_entity_info('PF', '166002,166114');
  347. * get_entity_info('MI', NULL);
  348. */
  349. def get_entity_info(entity_type, entity_ids) {
  350. t = null;
  351. s_entity_ids = ids_to_string(entity_ids);
  352. if(entity_type == 'MF' || entity_type == 'HF') {
  353. t = get_fund_info(s_entity_ids);
  354. t.rename!('fund_id', 'entity_id');
  355. } else if(entity_type == 'PF') {
  356. t = get_portfolio_info(s_entity_ids);
  357. t.rename!('portfolio_id', 'entity_id');
  358. } else if(entity_type IN ['MI', 'FI']) {
  359. t = get_index_info(s_entity_ids);
  360. t.rename!('index_id', 'entity_id');
  361. } else if(entity_type == 'CF') {
  362. t = get_cus_fund_info(s_entity_ids);
  363. t.rename!('fund_id', 'entity_id');
  364. }
  365. return t;
  366. }
  367. /*
  368. * 取某时间后更新的各基金组合最早净值日期
  369. *
  370. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  371. * @param entity_ids <VECTOR|STRING>: NULL时取全量
  372. * @param update_time <DATETIME>: all updates after this time
  373. * @param isFromMySQL <BOOL>:
  374. *
  375. * Example: get_entity_list_by_nav_updatetime('MF', ['MF00003PW1', 'MF00003PW2'], 2024.09.26, true);
  376. * get_entity_list_by_nav_updatetime('HF', null, 2024.07.19T10:00:00, true);
  377. * get_entity_list_by_nav_updatetime('PF', '166002,166114', 2024.06.20, true);
  378. *
  379. */
  380. def get_entity_list_by_nav_updatetime(entity_type, entity_ids, updatetime, isFromMySQL) {
  381. tmp = get_nav_table_description(entity_type);
  382. s_entity_ids = ids_to_string(entity_ids);
  383. sql_entity_id = '';
  384. if(s_entity_ids != NULL) {
  385. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  386. }
  387. if(isFromMySQL == true) {
  388. nav_table_name = tmp.table_name[0];
  389. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
  390. FROM " + tmp.table_name[0] + "
  391. WHERE isvalid = 1 " +
  392. sql_entity_id + "
  393. AND " + tmp.cumulative_nav_col[0] + " > 0
  394. AND updatetime >= '" + updatetime$STRING + "'
  395. GROUP BY " + tmp.sec_id_col[0] + "
  396. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  397. conn = connect_mysql();
  398. t = odbc::query(conn, s_query);
  399. conn.close();
  400. } else {
  401. //TODO
  402. }
  403. return t
  404. }
  405. /*
  406. * 取私募基金用于月末 fund_performance 表更新的净值
  407. *
  408. * @param fund_ids: 逗号分隔的ID字符串, 每个ID都有''
  409. * @param month_end: 月末日期字符串 YYYY-MM
  410. *
  411. *
  412. */
  413. def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
  414. s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
  415. conn = connect_mysql()
  416. t = odbc::query(conn, s_query)
  417. conn.close()
  418. return t
  419. }
  420. /*
  421. * 取某时间段的基金主基准
  422. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  423. *
  424. * Example: get_fund_primary_benchmark("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  425. */
  426. def get_fund_primary_benchmark(fund_ids, month_start, month_end) {
  427. s_query = "SELECT fund_id, primary_benchmark_id AS benchmark_id, inception_date
  428. FROM mfdb.fund_information
  429. WHERE fund_id IN (" + fund_ids + ")
  430. AND isvalid = 1;";
  431. conn = connect_mysql();
  432. t = odbc::query(conn, s_query);
  433. conn.close();
  434. t.addColumn('end_date', MONTH);
  435. m_start = temporalParse(month_start, 'yyyy-MM');
  436. m_end = temporalParse(month_end, 'yyyy-MM');
  437. tb_end_date = table(m_start..m_end AS end_date);
  438. 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());
  439. }
  440. /*
  441. * 取某时间段的组合主基准
  442. * NOTE: 目前所有Java指标计算组合默认主基准是FA00000VNB,以后很可能会改
  443. *
  444. * Example: get_portfolio_primary_benchmark("166002,166114", '1990-01', '2024-08');
  445. */
  446. def get_portfolio_primary_benchmark(portfolio_ids, month_start, month_end) {
  447. s_query = "SELECT id AS portfolio_id, 'FA00000VNB' AS benchmark_id, inception_date
  448. FROM pfdb.pf_customer_portfolio_map
  449. WHERE id IN (" + portfolio_ids + ")
  450. AND isvalid = 1;";
  451. conn = connect_mysql();
  452. t = odbc::query(conn, s_query);
  453. conn.close();
  454. t.addColumn('end_date', MONTH);
  455. m_start = temporalParse(month_start, 'yyyy-MM');
  456. m_end = temporalParse(month_end, 'yyyy-MM');
  457. tb_end_date = table(m_start..m_end AS end_date);
  458. 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());
  459. }
  460. /*
  461. * 取某时间段的基金组合主基准
  462. *
  463. * NOTE: 指数和因子的”主基准”设置为沪深300
  464. *
  465. * Example: get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  466. * get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
  467. * get_entity_primary_benchmark('MI', ['IN00000008', 'IN0000000M'], '2024-07', '2024-08');
  468. */
  469. def get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
  470. t = table(100:0,
  471. ['entity_id', 'end_date', 'benchmark_id'],
  472. [iif(entity_type == 'PF', INT, SYMBOL), MONTH, SYMBOL]);
  473. s_entity_ids = ids_to_string(entity_ids);
  474. if(s_entity_ids == null || s_entity_ids == '') return null;
  475. if(entity_type == 'MF' || entity_type == 'HF') {
  476. t = get_fund_primary_benchmark(s_entity_ids, month_start, month_end);
  477. t.rename!('fund_id', 'entity_id');
  478. } else if(entity_type == 'PF') {
  479. t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
  480. t.rename!('portfolio_id', 'entity_id');
  481. } else if(entity_type IN ['MI', 'FI', 'FA', 'CI', 'EQ']) {
  482. // 对于指数、因子来说,没有什么基准。但为了指标计算不得不在这里设个假的
  483. t = SELECT entity_id, end_date, 'IN00000008' AS benchmark_id
  484. FROM cj(get_entity_info(entity_type, s_entity_ids), table(temporalParse(month_start, 'yyyy-MM')..temporalParse(month_end, 'yyyy-MM') AS end_date))
  485. WHERE end_date >= iif(inception_date.isNull(), 1990.01M, inception_date.month())
  486. }
  487. return t;
  488. }
  489. /*
  490. * 取某时间段的基金BFI因子
  491. *
  492. * Example: get_fund_bfi_factors("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  493. * get_fund_bfi_factors(['MF00003PW2', 'MF00003PW1', 'MF00003PXO'], '1990-01', '2024-06');
  494. */
  495. def get_fund_bfi_factors(fund_ids, month_start, month_end) {
  496. s_entity_ids = ids_to_string(fund_ids);
  497. if(s_entity_ids == null || s_entity_ids == '') return null;
  498. s_query = "SELECT fund_id, end_date, factor_id
  499. FROM pfdb.pf_fund_factor_bfi_by_category_group
  500. WHERE fund_id IN (" + s_entity_ids + ")
  501. AND end_date >= '" + month_start + "'
  502. AND end_date <= '" + month_end + "'
  503. AND isvalid = 1
  504. ORDER BY fund_id, end_date, factor_id;";
  505. conn = connect_mysql();
  506. t = odbc::query(conn, s_query);
  507. conn.close();
  508. return t;
  509. }
  510. /*
  511. * 取某时间段的组合BFI因子
  512. *
  513. * Example: get_portfolio_bfi_factors("166002,166114", '1900-01', '2024-06');
  514. */
  515. def get_portfolio_bfi_factors(portfolio_ids, month_start, month_end) {
  516. s_query = "SELECT portfolio_id, end_date, factor_id
  517. FROM pfdb.pf_portfolio_factor_bfi_by_category_group
  518. WHERE portfolio_id IN (" + portfolio_ids + ")
  519. AND end_date >= '" + month_start + "'
  520. AND end_date <= '" + month_end + "'
  521. AND isvalid = 1
  522. ORDER BY portfolio_id, end_date, factor_id;";
  523. conn = connect_mysql();
  524. t = odbc::query(conn, s_query);
  525. conn.close();
  526. return t;
  527. }
  528. /*
  529. * 取某时间段的基金组合BFI基准
  530. *
  531. *
  532. * Example: get_entity_bfi_factors('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  533. * get_entity_bfi_factors('PF', [166002,166114], '1990-01', '2024-08');
  534. */
  535. def get_entity_bfi_factors(entity_type, entity_ids, month_start, month_end) {
  536. t = null;
  537. s_entity_ids = ids_to_string(entity_ids);
  538. if(s_entity_ids == null || s_entity_ids == '') return null;
  539. if(entity_type == 'MF' || entity_type == 'HF') {
  540. t = get_fund_bfi_factors(s_entity_ids, month_start, month_end);
  541. t.rename!('fund_id', 'entity_id');
  542. } else if(entity_type == 'PF') {
  543. t = get_portfolio_bfi_factors(s_entity_ids, month_start, month_end);
  544. t.rename!('portfolio_id', 'entity_id');
  545. }
  546. return t;
  547. }
  548. /*
  549. * 取组合交易表
  550. *
  551. *
  552. * Example: get_portfolio_holding_history("166002,364640")
  553. */
  554. def get_portfolio_holding_history(portfolio_ids) {
  555. s_query = "SELECT portfolio_id, holding_date, fund_id, amount, fund_share, ROUND(amount/fund_share, 6) AS nav
  556. FROM pfdb.pf_portfolio_fund_history
  557. WHERE portfolio_id IN (" + portfolio_ids + ")
  558. AND isvalid = 1
  559. ORDER BY portfolio_id, holding_date";
  560. conn = connect_mysql();
  561. t = odbc::query(conn, s_query);
  562. conn.close();
  563. return t;
  564. }
  565. /*
  566. * 取基金证券从某日期后的所有净值及前值
  567. *
  568. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  569. * @param freq <STRING>: m, w, d
  570. * @param json_query <JSON>: [{sec_id:xxx, price_date: yyyy-mm-dd}]
  571. *
  572. */
  573. def get_nav_for_return_calculation(entity_type, freq, json_query) {
  574. s_query = "CALL pfdb.sp_get_nav_after_date('" + entity_type + "', '" + freq + "', '" + json_query + "')";
  575. conn = connect_mysql();
  576. t = odbc::query(conn, s_query);
  577. conn.close();
  578. return t;
  579. }
  580. /*
  581. * 取主基准和BFI的历史月收益率
  582. *
  583. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表
  584. * @param end_day <DATE>: 收益的截止日期
  585. *
  586. * @return <TABLE>: benchmark_id, end_date, ret
  587. *
  588. */
  589. def get_benchmark_return(benchmarks, end_day) {
  590. s_index_ids = '';
  591. s_factor_ids = '';
  592. if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
  593. // 前缀为 IN 的 benchmark id
  594. t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';
  595. s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
  596. // 前缀为 FA 的 benchmark id
  597. t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
  598. s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
  599. // 目前指数的月度业绩存在 fund_performance 表
  600. 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);
  601. // 而因子的月度业绩存在 cm_factor_performance 表
  602. 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);
  603. return t_bmk;
  604. }
  605. /*
  606. * 取持有基金净值更新的组合列表
  607. *
  608. * TODO: 需要跑3分钟,待优化
  609. *
  610. * Example: get_portfolio_list_by_fund_nav_updatetime([166002,166114], 2024.10.28, true);
  611. */
  612. def get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, isFromMySQL) {
  613. t = null;
  614. s_entity_ids = ids_to_string(portfolio_ids);
  615. if(isFromMySQL == true) {
  616. s_query = "CALL pfdb.sp_get_portfolios_to_cal_nav(" + iif(s_entity_ids.isNull(), 'NULL', "'" + s_entity_ids + "'") + ",'" + updatetime + "')";
  617. conn = connect_mysql();
  618. t = odbc::query(conn, s_query);
  619. conn.close();
  620. }
  621. return t
  622. }
  623. /*
  624. * 取Json中指定的组合当日净值
  625. *
  626. * @param s_json <JSON>
  627. *
  628. * Example: get_portfolio_nav_by_date([{"portfolio_id": 166002,"price_date": "2024.10.25"},{"portfolio_id": 166114,"price_date": "2024.03.13"}], true);
  629. */
  630. def get_portfolio_nav_by_date(s_json, isFromMySQL) {
  631. t = null;
  632. if(isFromMySQL == true) {
  633. s_query = "SELECT t.portfolio_id, t.price_date, nav.cumulative_nav
  634. FROM JSON_TABLE ( '" + s_json + "', '$[*]'
  635. COLUMNS ( portfolio_id INT PATH '$.portfolio_id',
  636. price_date DATE PATH '$.price_date' ) ) t
  637. LEFT JOIN pfdb.pf_portfolio_nav nav ON t.portfolio_id = nav.portfolio_id AND t.price_date = nav.price_date;";
  638. conn = connect_mysql();
  639. t = odbc::query(conn, s_query);
  640. conn.close();
  641. }
  642. return t;
  643. }
  644. /*
  645. * 取月度指标表
  646. *
  647. * @param table_name <STRING>: 指标表名
  648. * @param end_date <MONTH>
  649. * @param isFromMySQL <BOOL>
  650. *
  651. * Example: get_monthly_indicator_data('mfdb.fund_performance', 2024.09M, true);
  652. */
  653. def get_monthly_indicator_data(table_name, end_date, isFromMySQL=true) {
  654. t = null;
  655. s_end_date_sql = iif(end_date.isNull(), '', " AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" );
  656. if(isFromMySQL == true) {
  657. s_query = "SELECT * FROM " + table_name + " WHERE isvalid = 1 " + s_end_date_sql;
  658. conn = connect_mysql();
  659. t = odbc::query(conn, s_query);
  660. conn.close();
  661. }
  662. return t;
  663. }
  664. /*
  665. * 取 pf_fund_indicator_ranking 表
  666. *
  667. *
  668. * Example: get_fund_indicator_ranking("'MF00003PW1'", 2024.09M, 102, true);
  669. * get_fund_indicator_ranking(NULL, 2023.09M, [1, 3], true);
  670. */
  671. def get_fund_indicator_ranking(fund_ids, end_date, strategy, isFromMySQL=true) {
  672. t = null;
  673. s_entity_ids = ids_to_string(fund_ids);
  674. sql_entity_id = '';
  675. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  676. s_strategy_ids = ids_to_string(strategy);
  677. sql_strategy_id = '';
  678. if(s_strategy_ids != NULL) sql_strategy_id = " AND strategy IN (" + s_strategy_ids + ")";
  679. if(isFromMySQL == true) {
  680. s_query = "SELECT *
  681. FROM pfdb.pf_fund_indicator_ranking
  682. WHERE isvalid = 1
  683. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  684. sql_strategy_id +
  685. sql_entity_id;
  686. conn = connect_mysql();
  687. t = odbc::query(conn, s_query);
  688. conn.close();
  689. }
  690. return t;
  691. }
  692. /*
  693. * 取 pf_fund_indicator_substrategy_ranking 表
  694. *
  695. *
  696. * Example: get_fund_indicator_substrategy_ranking("'MF00003PW1'", 2024.09M, 23, true);
  697. * get_fund_indicator_substrategy_ranking(NULL, 2023.09M, [22, 23], true);
  698. */
  699. def get_fund_indicator_substrategy_ranking(fund_ids, end_date, substrategy, isFromMySQL=true) {
  700. t = null;
  701. s_entity_ids = ids_to_string(fund_ids);
  702. sql_entity_id = '';
  703. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  704. s_strategy_ids = ids_to_string(substrategy);
  705. sql_strategy_id = '';
  706. if(s_strategy_ids != NULL) sql_strategy_id = " AND substrategy IN (" + s_strategy_ids + ")";
  707. if(isFromMySQL == true) {
  708. s_query = "SELECT *
  709. FROM pfdb.pf_fund_indicator_substrategy_ranking
  710. WHERE isvalid = 1
  711. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  712. sql_strategy_id +
  713. sql_entity_id;
  714. conn = connect_mysql();
  715. t = odbc::query(conn, s_query);
  716. conn.close();
  717. }
  718. return t;
  719. }
  720. /*
  721. * 取 pf_fund_bfi_bm_indicator_ranking 表
  722. *
  723. *
  724. * Example: get_fund_bfi_bm_indicator_ranking("'MF00003PW1'", 2024.09M, NULL, true);
  725. *
  726. */
  727. def get_fund_bfi_bm_indicator_ranking(fund_ids, end_date, factor_ids, isFromMySQL=true) {
  728. t = null;
  729. s_entity_ids = ids_to_string(fund_ids);
  730. sql_entity_id = '';
  731. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  732. s_factor_ids = ids_to_string(factor_ids);
  733. sql_factor_id = '';
  734. if(s_factor_ids != NULL) sql_factor_id = " AND factor_id IN (" + s_factor_ids + ")";
  735. if(isFromMySQL == true) {
  736. s_query = "SELECT *
  737. FROM pfdb.pf_fund_bfi_bm_indicator_ranking
  738. WHERE isvalid = 1
  739. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  740. sql_factor_id +
  741. sql_entity_id;
  742. conn = connect_mysql();
  743. t = odbc::query(conn, s_query);
  744. conn.close();
  745. }
  746. return t;
  747. }
  748. /*
  749. * 【Morningstar Integration】取某时间后净值更新的公募基金列表
  750. *
  751. * @param entity_ids <STRING|VECTOR>:
  752. * @param update_time <DATETIME>: all updates after this time
  753. *
  754. * TODO: 将 public_nav2 换成 mfdb.public_nav 后,要把 createtime 改成 updatetime
  755. *
  756. * Example: ms_get_fund_list_by_nav_updatetime(['MF00003PW1','MF00003PWC'], 2024.10.26);
  757. */
  758. def ms_get_fund_list_by_nav_updatetime(entity_ids, updatetime) {
  759. s_entity_ids = ids_to_string(entity_ids);
  760. sql_entity_id = '';
  761. if(s_entity_ids != NULL) {
  762. sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  763. }
  764. s_query = "SELECT fund_id AS entity_id, MIN(price_date) AS price_date
  765. FROM raw_db.public_nav2
  766. WHERE isvalid = 1 " +
  767. sql_entity_id + "
  768. AND cumulative_nav > 0
  769. AND createtime >= '" + updatetime$STRING + "'
  770. GROUP BY fund_id
  771. ORDER BY fund_id, price_date";
  772. conn = connect_mysql();
  773. t = odbc::query(conn, s_query);
  774. conn.close();
  775. return t
  776. }
  777. /*
  778. * 【Morningstar Integration】
  779. *
  780. * Example: ms_get_fund_info("'MF00003PW1','MF00003PWC'");
  781. *
  782. */
  783. def ms_get_fund_info(fund_ids) {
  784. s_entity_ids = ids_to_string(fund_ids);
  785. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  786. 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
  787. FROM raw_db.fund_information2 fi
  788. INNER JOIN raw_db.fund_strategy2 fs ON fi.fund_id = fs.fund_id AND fs.isvalid = 1
  789. WHERE fi.fund_id IN (" + s_entity_ids + ")
  790. AND fi.isvalid = 1
  791. ORDER BY fi.fund_id"
  792. conn = connect_mysql()
  793. t = odbc::query(conn, s_query)
  794. conn.close()
  795. return t
  796. }
  797. /*
  798. * 【Morningstar Integration】
  799. *
  800. * Example: ms_get_fund_monthly_nav(['MF00003PW1','MF00003PWC']);
  801. *
  802. */
  803. def ms_get_fund_monthly_nav(fund_ids) {
  804. s_entity_ids = ids_to_string(fund_ids);
  805. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  806. s_query = "SELECT n.fund_id AS entity_id, n.price_date, n.cumulative_nav
  807. FROM raw_db.public_nav2 n INNER JOIN (
  808. SELECT fund_id, max(price_date) AS monthend_date
  809. FROM raw_db.public_nav2
  810. WHERE fund_id IN (" + s_entity_ids + ")
  811. AND isvalid = 1
  812. AND cumulative_nav > 0
  813. GROUP BY fund_id, DATE_FORMAT(price_date, '%Y-%m')
  814. ) t ON n.fund_id = t.fund_id AND n.price_date = t.monthend_date
  815. UNION
  816. SELECT fi.fund_id, fi.inception_date, IFNULL(fi.initial_unit_value, 1)
  817. FROM raw_db.fund_information2 fi
  818. WHERE fi.fund_id IN (" + s_entity_ids + ")
  819. ORDER BY entity_id, price_date;"
  820. conn = connect_mysql()
  821. t = odbc::query(conn, s_query)
  822. conn.close()
  823. return t
  824. }
  825. /*
  826. * 【Morningstar Integration】取某时间段的基金主基准
  827. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  828. *
  829. * Example: ms_get_fund_primary_benchmark("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  830. */
  831. def ms_get_fund_primary_benchmark(fund_ids, month_start, month_end) {
  832. s_query = "SELECT fund_id, primary_benchmark_id AS benchmark_id, inception_date
  833. FROM raw_db.fund_information2
  834. WHERE fund_id IN (" + fund_ids + ")
  835. AND isvalid = 1;";
  836. conn = connect_mysql();
  837. t = odbc::query(conn, s_query);
  838. conn.close();
  839. t.addColumn('end_date', MONTH);
  840. m_start = temporalParse(month_start, 'yyyy-MM');
  841. m_end = temporalParse(month_end, 'yyyy-MM');
  842. tb_end_date = table(m_start..m_end AS end_date);
  843. 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());
  844. }
  845. /*
  846. * 【Morningstar Integration】取某时间段的基金组合主基准
  847. *
  848. *
  849. * Example: ms_get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  850. * ms_get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
  851. */
  852. def ms_get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
  853. t = null;
  854. s_entity_ids = ids_to_string(entity_ids);
  855. if(s_entity_ids == null || s_entity_ids == '') return null;
  856. if(entity_type == 'MF' || entity_type == 'HF') {
  857. t = ms_get_fund_primary_benchmark(s_entity_ids, month_start, month_end);
  858. t.rename!('fund_id', 'entity_id');
  859. } else if(entity_type == 'PF') {
  860. t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
  861. t.rename!('portfolio_id', 'entity_id');
  862. }
  863. return t;
  864. }
  865. /*
  866. * 【Morningstar Integration】取无风险月度利率
  867. *
  868. * ms_get_risk_free_rate(1990.01.01, today())
  869. */
  870. def ms_get_risk_free_rate(start_date, end_date) {
  871. return get_monthly_ret('MI', "'IN000002EI'", start_date, end_date, true);
  872. }
  873. /*
  874. * 【Morningstar Integration】取某时间段的基金同类平均指数
  875. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  876. *
  877. * Example: ms_get_fund_category_average("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  878. */
  879. def ms_get_fund_category_average(fund_ids, month_start, month_end) {
  880. s_query = "SELECT fi.fund_id, ip.index_id AS benchmark_id, fi.inception_date
  881. FROM raw_db.fund_information2 fi
  882. INNER JOIN mfdb.indexes_profile ip ON ip.index_code = concat('MSMWCA.2.', fi.pub_sub_fund_type)
  883. WHERE fi.fund_id IN (" + fund_ids + ")
  884. AND fi.isvalid = 1
  885. AND ip.isvalid = 1;";
  886. conn = connect_mysql();
  887. t = odbc::query(conn, s_query);
  888. conn.close();
  889. t.addColumn('end_date', MONTH);
  890. m_start = temporalParse(month_start, 'yyyy-MM');
  891. m_end = temporalParse(month_end, 'yyyy-MM');
  892. tb_end_date = table(m_start..m_end AS end_date);
  893. 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());
  894. }