dataPuller.dos 20 KB

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