dataPuller.dos 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. module fundit::dataPuller
  2. use fundit::sqlUtilities
  3. /*
  4. * 取指数周收益
  5. *
  6. * get_index_weekly_rets("'FA00000WKG','FA00000WKH','IN0000007G'", 1990.01.01, today())
  7. */
  8. def get_index_weekly_rets(index_ids, start_date, end_date) {
  9. s_start_date = iif(start_date.isNull(), "", " AND price_date >= '" + start_date$STRING + "'")
  10. s_end_date = iif(end_date.isNull(), "", " AND price_date <= '" + end_date$STRING + "'")
  11. s_query = "SELECT factor_id AS index_id, year_week, price_date, factor_value AS cumulative_nav, ret_1w
  12. FROM pfdb.cm_factor_performance_weekly
  13. WHERE isvalid = 1
  14. AND factor_id IN (" + index_ids + ")" +
  15. s_start_date +
  16. s_end_date + "
  17. AND ret_1w IS NOT NULL
  18. UNION
  19. SELECT fund_id AS index_id, year_week, price_date, cumulative_nav, ret_1w
  20. FROM mfdb.fund_performance_weekly
  21. WHERE isvalid = 1
  22. AND fund_id IN (" + index_ids + ")" +
  23. s_start_date +
  24. s_end_date + "
  25. AND ret_1w IS NOT NULL
  26. ORDER BY year_week"
  27. conn = connect_mysql()
  28. t = odbc::query(conn, s_query)
  29. conn.close()
  30. return t
  31. }
  32. /*
  33. * 取基金周收益
  34. *
  35. *
  36. * get_fund_weekly_rets("'MF00003TMH','MF00003UQM'", 1990.01.01, null, true)
  37. */
  38. def get_fund_weekly_rets(fund_ids, start_date, end_date, isFromMySQL) {
  39. s_fund_id = iif(fund_ids.isNull(), "", " AND fund_id IN (" + fund_ids + ")")
  40. s_start_date = iif(start_date.isNull(), "", " AND price_date >= '" + start_date$STRING + "'")
  41. s_end_date = iif(end_date.isNull(), "", " AND price_date <= '" + end_date$STRING + "'")
  42. s_query = "SELECT fund_id, year_week, price_date, cumulative_nav, ret_1w
  43. FROM mfdb.fund_performance_weekly
  44. WHERE isvalid = 1 " +
  45. s_fund_id +
  46. s_start_date +
  47. s_end_date + "
  48. AND ret_1w IS NOT NULL
  49. ORDER BY fund_id, 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. def get_entity_weekly_rets(entity_type, entity_info) {
  60. rets = null;
  61. if(entity_info.isVoid() || entity_info.size() == 0) return rets;
  62. very_old_date = '1990.01.01';
  63. // 简单起见,取整个数据集的最新日期(month-end production时取上月最后一天即可
  64. end_day = entity_info.price_date.max();
  65. s_entity_ids = ids_to_string(entity_info.entity_id);
  66. if(entity_type == 'HF' || entity_type == 'MF') {
  67. rets = get_fund_weekly_rets(s_entity_ids, very_old_date, end_day, true);
  68. } else if(entity_type == 'MI' || entity_type == 'FA') {
  69. rets = get_index_weekly_rets(s_entity_ids, very_old_date, end_day);
  70. }
  71. return rets;
  72. }
  73. /*
  74. * 取组合周收益
  75. * TODO: 增加从本地取数据的功能
  76. *
  77. *
  78. * get_portfolio_weekly_rets("166002,364640", 1990.01.01, today(), true)
  79. */
  80. def get_portfolio_weekly_rets(portfolio_ids, start_date, end_date, isFromMySQL) {
  81. s_portfolio_id = iif(portfolio_ids.isNull(), "", " AND portfolio_id IN (" + portfolio_ids + ")")
  82. s_query = "SELECT portfolio_id, year_week, price_date, cumulative_nav, ret_1w
  83. FROM pfdb.pf_portfolio_performance_weekly
  84. WHERE isvalid = 1 " +
  85. s_portfolio_id + "
  86. AND ret_1w IS NOT NULL
  87. AND price_date BETWEEN '" + start_date$STRING + "' AND '" + end_date$STRING + "'
  88. ORDER BY portfolio_id, year_week"
  89. conn = connect_mysql()
  90. t = odbc::query(conn, s_query)
  91. conn.close()
  92. return t
  93. }
  94. /*
  95. * 通用取月收益
  96. *
  97. * @param entity_type <STRING>:
  98. * @param entity_ids <VECTOR|STRING>:
  99. * @param start_date <DATE>:
  100. * @param end_date <DATE>:
  101. * @param isFromMySQL <BOOL>:
  102. *
  103. *
  104. * Example: get_monthly_ret('HF', ['HF000004KN','HF000103EU','HF00018WXG'], 2000.01.01, 2024.03.01, true);
  105. */
  106. def get_monthly_ret(entity_type, entity_ids, start_date, end_date, isFromMySQL) {
  107. s_entity_ids = ids_to_string(entity_ids);
  108. if(s_entity_ids == null || s_entity_ids == '') return null;
  109. tmp = get_performance_table_description(entity_type);
  110. yyyymm_start = start_date.temporalFormat("yyyy-MM")
  111. yyyymm_end = end_date.temporalFormat("yyyy-MM")
  112. if(isFromMySQL == true) {
  113. 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
  114. FROM " + tmp.table_name[0] + "
  115. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  116. AND isvalid = 1
  117. AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
  118. ORDER BY " + tmp.sec_id_col[0] + ", end_date";
  119. conn = connect_mysql()
  120. t = odbc::query(conn, s_query)
  121. conn.close()
  122. } else {
  123. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  124. 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"))
  125. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0], then rename to "entity_id"?
  126. s_where = expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(","))
  127. t = sql(s_col, tb_local, s_where).eval()
  128. }
  129. return t
  130. }
  131. /*
  132. * 取无风险月度利率
  133. *
  134. * get_risk_free_rate(1990.01.01, today())
  135. */
  136. def get_risk_free_rate(start_date, end_date) {
  137. return get_monthly_ret('MI', "'IN0000000M'", start_date, end_date, true);
  138. }
  139. /*
  140. * 取基金最新收益及净值
  141. *
  142. * get_fund_latest_nav_performance("'HF000004KN','HF00018WXG'")
  143. */
  144. def get_fund_latest_nav_performance(fund_ids, isFromMySQL) {
  145. if(isFromMySQL == true) {
  146. s_query = "SELECT *
  147. FROM mfdb.fund_latest_nav_performance
  148. WHERE fund_id IN (" + fund_ids + ")
  149. AND isvalid = 1
  150. ORDER BY fund_id"
  151. conn = connect_mysql()
  152. t = odbc::query(conn, s_query)
  153. conn.close()
  154. } else {
  155. tb_local = load_table_from_local("fundit", "mfdb.fund_latest_nav_performance")
  156. s_col = sqlCol("*")
  157. s_where = expr(<fund_id>, in, fund_ids.strReplace("'", "").split(","))
  158. t = sql(s_col, tb_local, s_where).eval()
  159. }
  160. return t
  161. }
  162. /*
  163. * 通用取净值
  164. *
  165. *
  166. * Create: 202408 Joey
  167. * TODO: add isvalid and nav > 0 for local version
  168. *
  169. *
  170. * Example: get_nav_by_price_date('HF', "'HF000004KN','HF00018WXG'", 2024.05.01, true);
  171. * get_nav_by_price_date('MI', "'IN00000008','IN0000000M'", 2024.05.01, true);
  172. */
  173. def get_nav_by_price_date(entity_type, entity_ids, price_date, isFromMySQL) {
  174. s_entity_ids = ids_to_string(entity_ids);
  175. if(s_entity_ids == null || s_entity_ids == '') return null;
  176. tmp = get_nav_table_description(entity_type);
  177. if(isFromMySQL == true) {
  178. nav_table_name = tmp.table_name[0];
  179. 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
  180. FROM " + tmp.table_name[0] + "
  181. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  182. AND isvalid = 1
  183. AND " + tmp.cumulative_nav_col[0] + " > 0
  184. AND price_date >= '" + price_date$STRING + "'
  185. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  186. conn = connect_mysql();
  187. t = odbc::query(conn, s_query);
  188. conn.close();
  189. } else {
  190. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  191. s_col = sqlCol("*")
  192. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0]?
  193. s_where = [expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(",")), <price_date >= price_date>]
  194. t = sql(s_col, tb_local, s_where).eval()
  195. }
  196. return t
  197. }
  198. /*
  199. * 取指数因子点位
  200. *
  201. * get_index_nav_by_price_date("'IN00000008','FA00000WKG'", 2024.06.01)
  202. */
  203. def get_index_nav_by_price_date(index_ids, price_date) {
  204. s_query = "SELECT index_id, price_date, close AS cumulative_nav
  205. FROM mfdb.market_indexes
  206. WHERE index_id IN (" + index_ids + ")
  207. AND isvalid = 1
  208. AND close > 0
  209. AND price_date >= '" + price_date + "'
  210. UNION
  211. SELECT index_id AS index_id, price_date, index_value AS cumulative_nav
  212. FROM mfdb.indexes_ty_index
  213. WHERE index_id IN (" + index_ids + ")
  214. AND isvalid = 1
  215. AND index_value > 0
  216. AND price_date >= '" + price_date + "'
  217. UNION
  218. SELECT factor_id AS index_id, price_date, factor_value AS cumulative_nav
  219. FROM pfdb.cm_factor_value
  220. WHERE factor_id IN (" + index_ids + ")
  221. AND isvalid = 1
  222. AND factor_value > 0
  223. AND price_date >= '" + price_date + "'
  224. ORDER BY price_date"
  225. conn = connect_mysql()
  226. t = odbc::query(conn, s_query)
  227. conn.close()
  228. return t
  229. }
  230. /*
  231. * 取有效基金基本信息
  232. *
  233. * Example: get_fund_info("'HF000004KN','HF00018WXG'");
  234. * get_fund_info(['HF000004KN','HF00018WXG']);
  235. *
  236. */
  237. def get_fund_info(fund_ids) {
  238. s_entity_ids = ids_to_string(fund_ids);
  239. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  240. 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
  241. FROM mfdb.fund_information fi
  242. INNER JOIN mfdb.fund_strategy fs ON fi.fund_id = fs.fund_id AND fs.isvalid = 1
  243. WHERE fi.fund_id IN (" + s_entity_ids + ")
  244. AND fi.isvalid = 1
  245. ORDER BY fi.fund_id"
  246. conn = connect_mysql()
  247. t = odbc::query(conn, s_query)
  248. conn.close()
  249. return t
  250. }
  251. /*
  252. * 取组合有效信息
  253. *
  254. * Example: get_portfolio_info('166002,166114');
  255. *
  256. */
  257. def get_portfolio_info(portfolio_ids) {
  258. s_entity_ids = ids_to_string(portfolio_ids);
  259. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  260. s_query = "SELECT cpm.id AS portfolio_id, cpm.userid, cpm.customer_id, cpm.inception_date, cpm.portfolio_source, cpm.portfolio_type
  261. FROM pfdb.`pf_customer_portfolio_map` cpm
  262. INNER JOIN pfdb.cm_user u ON cpm.userid = u.userid
  263. WHERE cpm.id IN (" + portfolio_ids + ")
  264. AND cpm.isvalid = 1
  265. AND u.isvalid = 1
  266. ORDER BY cpm.id"
  267. conn = connect_mysql()
  268. t = odbc::query(conn, s_query)
  269. conn.close()
  270. return t
  271. }
  272. /*
  273. * 取基金组合基础有效信息
  274. *
  275. * Example: get_entity_info('HF', ['HF000004KN','HF000103EU','HF00018WXG']);
  276. * get_entity_info('PF', '166002,166114');
  277. */
  278. def get_entity_info(entity_type, entity_ids) {
  279. t = null;
  280. s_entity_ids = ids_to_string(entity_ids);
  281. if(s_entity_ids == null || s_entity_ids == '') return null;
  282. if(entity_type == 'MF' || entity_type == 'HF') {
  283. t = get_fund_info(s_entity_ids);
  284. t.rename!('fund_id', 'entity_id');
  285. } else if(entity_type == 'PF') {
  286. t = get_portfolio_info(s_entity_ids);
  287. t.rename!('portfolio_id', 'entity_id');
  288. }
  289. return t;
  290. }
  291. /*
  292. * 取某时间后更新的各基金组合最早净值日期
  293. *
  294. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  295. * @param entity_ids <VECTOR|STRING>: NULL时取全量
  296. * @param update_time <DATETIME>: all updates after this time
  297. * @param isFromMySQL <BOOL>:
  298. *
  299. * Example: get_entity_list_by_nav_updatetime('MF', ['MF00003PW1', 'MF00003PW2'], 2024.09.26, true);
  300. * get_entity_list_by_nav_updatetime('HF', null, 2024.07.19T10:00:00, true);
  301. * get_entity_list_by_nav_updatetime('PF', '166002,166114', 2024.06.20, true);
  302. *
  303. */
  304. def get_entity_list_by_nav_updatetime(entity_type, entity_ids, updatetime, isFromMySQL) {
  305. tmp = get_nav_table_description(entity_type);
  306. s_entity_ids = ids_to_string(entity_ids);
  307. sql_entity_id = '';
  308. if(s_entity_ids != NULL) {
  309. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  310. }
  311. if(isFromMySQL == true) {
  312. nav_table_name = tmp.table_name[0];
  313. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
  314. FROM " + tmp.table_name[0] + "
  315. WHERE isvalid = 1 " +
  316. sql_entity_id + "
  317. AND " + tmp.cumulative_nav_col[0] + " > 0
  318. AND updatetime >= '" + updatetime$STRING + "'
  319. GROUP BY " + tmp.sec_id_col[0] + "
  320. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  321. conn = connect_mysql();
  322. t = odbc::query(conn, s_query);
  323. conn.close();
  324. } else {
  325. //TODO
  326. }
  327. return t
  328. }
  329. /*
  330. * 取私募基金用于月末 fund_performance 表更新的净值
  331. *
  332. * @param fund_ids: 逗号分隔的ID字符串, 每个ID都有''
  333. * @param month_end: 月末日期字符串 YYYY-MM
  334. *
  335. *
  336. */
  337. def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
  338. s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
  339. conn = connect_mysql()
  340. t = odbc::query(conn, s_query)
  341. conn.close()
  342. return t
  343. }
  344. /*
  345. * 取某时间段的基金主基准
  346. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  347. *
  348. * Example: get_fund_primary_benchmark("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  349. */
  350. def get_fund_primary_benchmark(fund_ids, month_start, month_end) {
  351. s_query = "SELECT fund_id, primary_benchmark_id AS benchmark_id, inception_date
  352. FROM mfdb.fund_information
  353. WHERE fund_id IN (" + fund_ids + ")
  354. AND isvalid = 1;";
  355. conn = connect_mysql();
  356. t = odbc::query(conn, s_query);
  357. conn.close();
  358. t.addColumn('end_date', MONTH);
  359. m_start = temporalParse(month_start, 'yyyy-MM');
  360. m_end = temporalParse(month_end, 'yyyy-MM');
  361. tb_end_date = table(m_start..m_end AS end_date);
  362. 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());
  363. }
  364. /*
  365. * 取某时间段的组合主基准
  366. * NOTE: 目前所有组合默认主基准是沪深300,以后很可能会改
  367. *
  368. * Example: get_portfolio_primary_benchmark("166002,166114", '1990-01', '2024-08');
  369. */
  370. def get_portfolio_primary_benchmark(portfolio_ids, month_start, month_end) {
  371. s_query = "SELECT id AS portfolio_id, 'IN00000008' AS benchmark_id, inception_date
  372. FROM pfdb.pf_customer_portfolio_map
  373. WHERE id IN (" + portfolio_ids + ")
  374. AND isvalid = 1;";
  375. conn = connect_mysql();
  376. t = odbc::query(conn, s_query);
  377. conn.close();
  378. t.addColumn('end_date', MONTH);
  379. m_start = temporalParse(month_start, 'yyyy-MM');
  380. m_end = temporalParse(month_end, 'yyyy-MM');
  381. tb_end_date = table(m_start..m_end AS end_date);
  382. 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());
  383. }
  384. /*
  385. * 取某时间段的基金组合主基准
  386. *
  387. *
  388. * Example: get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  389. * get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
  390. */
  391. def get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
  392. t = null;
  393. s_entity_ids = ids_to_string(entity_ids);
  394. if(s_entity_ids == null || s_entity_ids == '') return null;
  395. if(entity_type == 'MF' || entity_type == 'HF') {
  396. t = get_fund_primary_benchmark(s_entity_ids, month_start, month_end);
  397. t.rename!('fund_id', 'entity_id');
  398. } else if(entity_type == 'PF') {
  399. t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
  400. t.rename!('portfolio_id', 'entity_id');
  401. }
  402. return t;
  403. }
  404. /*
  405. * 取某时间段的基金BFI因子
  406. *
  407. * Example: get_fund_bfi_factors("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  408. * get_fund_bfi_factors(['MF00003PW2', 'MF00003PW1', 'MF00003PXO'], '1990-01', '2024-06');
  409. */
  410. def get_fund_bfi_factors(fund_ids, month_start, month_end) {
  411. s_entity_ids = ids_to_string(fund_ids);
  412. if(s_entity_ids == null || s_entity_ids == '') return null;
  413. s_query = "SELECT fund_id, end_date, factor_id
  414. FROM pfdb.pf_fund_factor_bfi_by_category_group
  415. WHERE fund_id IN (" + s_entity_ids + ")
  416. AND end_date >= '" + month_start + "'
  417. AND end_date <= '" + month_end + "'
  418. AND isvalid = 1
  419. ORDER BY fund_id, end_date, factor_id;";
  420. conn = connect_mysql();
  421. t = odbc::query(conn, s_query);
  422. conn.close();
  423. return t;
  424. }
  425. /*
  426. * 取某时间段的组合BFI因子
  427. *
  428. * Example: get_portfolio_bfi_factors("166002,166114", '1900-01', '2024-06');
  429. */
  430. def get_portfolio_bfi_factors(portfolio_ids, month_start, month_end) {
  431. s_query = "SELECT portfolio_id, end_date, factor_id
  432. FROM pfdb.pf_portfolio_factor_bfi_by_category_group
  433. WHERE portfolio_id IN (" + portfolio_ids + ")
  434. AND end_date >= '" + month_start + "'
  435. AND end_date <= '" + month_end + "'
  436. AND isvalid = 1
  437. ORDER BY portfolio_id, end_date, factor_id;";
  438. conn = connect_mysql();
  439. t = odbc::query(conn, s_query);
  440. conn.close();
  441. return t;
  442. }
  443. /*
  444. * 取某时间段的基金组合BFI基准
  445. *
  446. *
  447. * Example: get_entity_bfi_factors('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  448. * get_entity_bfi_factors('PF', [166002,166114], '1990-01', '2024-08');
  449. */
  450. def get_entity_bfi_factors(entity_type, entity_ids, month_start, month_end) {
  451. t = null;
  452. s_entity_ids = ids_to_string(entity_ids);
  453. if(s_entity_ids == null || s_entity_ids == '') return null;
  454. if(entity_type == 'MF' || entity_type == 'HF') {
  455. t = get_fund_bfi_factors(s_entity_ids, month_start, month_end);
  456. t.rename!('fund_id', 'entity_id');
  457. } else if(entity_type == 'PF') {
  458. t = get_portfolio_bfi_factors(s_entity_ids, month_start, month_end);
  459. t.rename!('portfolio_id', 'entity_id');
  460. }
  461. return t;
  462. }
  463. /*
  464. * 取组合交易表
  465. *
  466. *
  467. * Example: get_portfolio_holding_history("166002,364640")
  468. */
  469. def get_portfolio_holding_history(portfolio_ids) {
  470. s_query = "SELECT portfolio_id, holding_date, fund_id, amount, fund_share, ROUND(amount/fund_share, 6) AS nav
  471. FROM pfdb.pf_portfolio_fund_history
  472. WHERE portfolio_id IN (" + portfolio_ids + ")
  473. AND isvalid = 1
  474. ORDER BY portfolio_id, holding_date";
  475. conn = connect_mysql();
  476. t = odbc::query(conn, s_query);
  477. conn.close();
  478. return t;
  479. }
  480. /*
  481. * 取基金证券从某日期后的所有净值及前值
  482. *
  483. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  484. * @param freq <STRING>: m, w, d
  485. * @param json_query <JSON>: [{sec_id:xxx, price_date: yyyy-mm-dd}]
  486. *
  487. */
  488. def get_nav_for_return_calculation(entity_type, freq, json_query) {
  489. s_query = "CALL pfdb.sp_get_nav_after_date('" + entity_type + "', '" + freq + "', '" + json_query + "')";
  490. conn = connect_mysql();
  491. t = odbc::query(conn, s_query);
  492. conn.close();
  493. return t;
  494. }
  495. /*
  496. * 取主基准和BFI的历史月收益率
  497. *
  498. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表
  499. * @param end_day <DATE>: 收益的截止日期
  500. *
  501. * @return <TABLE>: benchmark_id, end_date, ret
  502. *
  503. */
  504. def get_benchmark_return(benchmarks, end_day) {
  505. s_index_ids = '';
  506. s_factor_ids = '';
  507. if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
  508. // 前缀为 IN 的 benchmark id
  509. t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';
  510. s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
  511. // 前缀为 FA 的 benchmark id
  512. t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
  513. s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
  514. // 目前指数的月度业绩存在 fund_performance 表
  515. 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);
  516. // 而因子的月度业绩存在 cm_factor_performance 表
  517. 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);
  518. return t_bmk;
  519. }