dataPuller.dos 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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_index_info("'IN00000008','IN000002GE'");
  255. *
  256. */
  257. def get_index_info(index_ids) {
  258. s_entity_ids = ids_to_string(index_ids);
  259. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  260. 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
  261. FROM mfdb.indexes_profile fi
  262. WHERE fi.index_id IN (" + s_entity_ids + ")
  263. AND fi.isvalid = 1
  264. ORDER BY fi.index_id"
  265. conn = connect_mysql()
  266. t = odbc::query(conn, s_query)
  267. conn.close()
  268. return t
  269. }
  270. /*
  271. * 取组合有效信息
  272. *
  273. * Example: get_portfolio_info('166002,166114');
  274. *
  275. */
  276. def get_portfolio_info(portfolio_ids) {
  277. s_entity_ids = ids_to_string(portfolio_ids);
  278. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  279. 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
  280. FROM pfdb.`pf_customer_portfolio_map` cpm
  281. INNER JOIN pfdb.cm_user u ON cpm.userid = u.userid
  282. WHERE cpm.id IN (" + portfolio_ids + ")
  283. AND cpm.isvalid = 1
  284. AND u.isvalid = 1
  285. ORDER BY cpm.id"
  286. conn = connect_mysql()
  287. t = odbc::query(conn, s_query)
  288. conn.close()
  289. return t
  290. }
  291. /*
  292. * 取基金组合基础有效信息
  293. *
  294. * Example: get_entity_info('HF', ['HF000004KN','HF000103EU','HF00018WXG']);
  295. * get_entity_info('PF', '166002,166114');
  296. */
  297. def get_entity_info(entity_type, entity_ids) {
  298. t = null;
  299. s_entity_ids = ids_to_string(entity_ids);
  300. if(s_entity_ids == null || s_entity_ids == '') return null;
  301. if(entity_type == 'MF' || entity_type == 'HF') {
  302. t = get_fund_info(s_entity_ids);
  303. t.rename!('fund_id', 'entity_id');
  304. } else if(entity_type == 'PF') {
  305. t = get_portfolio_info(s_entity_ids);
  306. t.rename!('portfolio_id', 'entity_id');
  307. } else if(entity_type IN ['MI', 'FI']) {
  308. t = get_index_info(s_entity_ids);
  309. t.rename!('index_id', 'entity_id');
  310. }
  311. return t;
  312. }
  313. /*
  314. * 取某时间后更新的各基金组合最早净值日期
  315. *
  316. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  317. * @param entity_ids <VECTOR|STRING>: NULL时取全量
  318. * @param update_time <DATETIME>: all updates after this time
  319. * @param isFromMySQL <BOOL>:
  320. *
  321. * Example: get_entity_list_by_nav_updatetime('MF', ['MF00003PW1', 'MF00003PW2'], 2024.09.26, true);
  322. * get_entity_list_by_nav_updatetime('HF', null, 2024.07.19T10:00:00, true);
  323. * get_entity_list_by_nav_updatetime('PF', '166002,166114', 2024.06.20, true);
  324. *
  325. */
  326. def get_entity_list_by_nav_updatetime(entity_type, entity_ids, updatetime, isFromMySQL) {
  327. tmp = get_nav_table_description(entity_type);
  328. s_entity_ids = ids_to_string(entity_ids);
  329. sql_entity_id = '';
  330. if(s_entity_ids != NULL) {
  331. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  332. }
  333. if(isFromMySQL == true) {
  334. nav_table_name = tmp.table_name[0];
  335. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
  336. FROM " + tmp.table_name[0] + "
  337. WHERE isvalid = 1 " +
  338. sql_entity_id + "
  339. AND " + tmp.cumulative_nav_col[0] + " > 0
  340. AND updatetime >= '" + updatetime$STRING + "'
  341. GROUP BY " + tmp.sec_id_col[0] + "
  342. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  343. conn = connect_mysql();
  344. t = odbc::query(conn, s_query);
  345. conn.close();
  346. } else {
  347. //TODO
  348. }
  349. return t
  350. }
  351. /*
  352. * 取私募基金用于月末 fund_performance 表更新的净值
  353. *
  354. * @param fund_ids: 逗号分隔的ID字符串, 每个ID都有''
  355. * @param month_end: 月末日期字符串 YYYY-MM
  356. *
  357. *
  358. */
  359. def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
  360. s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
  361. conn = connect_mysql()
  362. t = odbc::query(conn, s_query)
  363. conn.close()
  364. return t
  365. }
  366. /*
  367. * 取某时间段的基金主基准
  368. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  369. *
  370. * Example: get_fund_primary_benchmark("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  371. */
  372. def get_fund_primary_benchmark(fund_ids, month_start, month_end) {
  373. s_query = "SELECT fund_id, primary_benchmark_id AS benchmark_id, inception_date
  374. FROM mfdb.fund_information
  375. WHERE fund_id IN (" + fund_ids + ")
  376. AND isvalid = 1;";
  377. conn = connect_mysql();
  378. t = odbc::query(conn, s_query);
  379. conn.close();
  380. t.addColumn('end_date', MONTH);
  381. m_start = temporalParse(month_start, 'yyyy-MM');
  382. m_end = temporalParse(month_end, 'yyyy-MM');
  383. tb_end_date = table(m_start..m_end AS end_date);
  384. 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());
  385. }
  386. /*
  387. * 取某时间段的组合主基准
  388. * NOTE: 目前所有Java指标计算组合默认主基准是FA00000VNB,以后很可能会改
  389. *
  390. * Example: get_portfolio_primary_benchmark("166002,166114", '1990-01', '2024-08');
  391. */
  392. def get_portfolio_primary_benchmark(portfolio_ids, month_start, month_end) {
  393. s_query = "SELECT id AS portfolio_id, 'FA00000VNB' AS benchmark_id, inception_date
  394. FROM pfdb.pf_customer_portfolio_map
  395. WHERE id IN (" + portfolio_ids + ")
  396. AND isvalid = 1;";
  397. conn = connect_mysql();
  398. t = odbc::query(conn, s_query);
  399. conn.close();
  400. t.addColumn('end_date', MONTH);
  401. m_start = temporalParse(month_start, 'yyyy-MM');
  402. m_end = temporalParse(month_end, 'yyyy-MM');
  403. tb_end_date = table(m_start..m_end AS end_date);
  404. 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());
  405. }
  406. /*
  407. * 取某时间段的基金组合主基准
  408. *
  409. * NOTE: 指数和因子的”主基准”设置为沪深300
  410. *
  411. * Example: get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  412. * get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
  413. * get_entity_primary_benchmark('MI', ['IN00000008', 'IN0000000M'], '2024-07', '2024-08');
  414. */
  415. def get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
  416. t = table(100:0,
  417. ['entity_id', 'end_date', 'benchmark_id'],
  418. [iif(entity_type == 'PF', INT, SYMBOL), MONTH, SYMBOL]);
  419. s_entity_ids = ids_to_string(entity_ids);
  420. if(s_entity_ids == null || s_entity_ids == '') return null;
  421. if(entity_type == 'MF' || entity_type == 'HF') {
  422. t = get_fund_primary_benchmark(s_entity_ids, month_start, month_end);
  423. t.rename!('fund_id', 'entity_id');
  424. } else if(entity_type == 'PF') {
  425. t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
  426. t.rename!('portfolio_id', 'entity_id');
  427. } else if(entity_type IN ['MI', 'FI', 'FA', 'CI', 'EQ']) {
  428. // 对于指数、因子来说,没有什么基准。但为了指标计算不得不在这里设个假的
  429. t = SELECT entity_id, end_date, 'IN00000008' AS benchmark_id
  430. FROM cj(get_entity_info(entity_type, s_entity_ids), table(temporalParse(month_start, 'yyyy-MM')..temporalParse(month_end, 'yyyy-MM') AS end_date))
  431. WHERE end_date >= iif(inception_date.isNull(), 1990.01M, inception_date.month())
  432. }
  433. return t;
  434. }
  435. /*
  436. * 取某时间段的基金BFI因子
  437. *
  438. * Example: get_fund_bfi_factors("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  439. * get_fund_bfi_factors(['MF00003PW2', 'MF00003PW1', 'MF00003PXO'], '1990-01', '2024-06');
  440. */
  441. def get_fund_bfi_factors(fund_ids, month_start, month_end) {
  442. s_entity_ids = ids_to_string(fund_ids);
  443. if(s_entity_ids == null || s_entity_ids == '') return null;
  444. s_query = "SELECT fund_id, end_date, factor_id
  445. FROM pfdb.pf_fund_factor_bfi_by_category_group
  446. WHERE fund_id IN (" + s_entity_ids + ")
  447. AND end_date >= '" + month_start + "'
  448. AND end_date <= '" + month_end + "'
  449. AND isvalid = 1
  450. ORDER BY fund_id, end_date, factor_id;";
  451. conn = connect_mysql();
  452. t = odbc::query(conn, s_query);
  453. conn.close();
  454. return t;
  455. }
  456. /*
  457. * 取某时间段的组合BFI因子
  458. *
  459. * Example: get_portfolio_bfi_factors("166002,166114", '1900-01', '2024-06');
  460. */
  461. def get_portfolio_bfi_factors(portfolio_ids, month_start, month_end) {
  462. s_query = "SELECT portfolio_id, end_date, factor_id
  463. FROM pfdb.pf_portfolio_factor_bfi_by_category_group
  464. WHERE portfolio_id IN (" + portfolio_ids + ")
  465. AND end_date >= '" + month_start + "'
  466. AND end_date <= '" + month_end + "'
  467. AND isvalid = 1
  468. ORDER BY portfolio_id, end_date, factor_id;";
  469. conn = connect_mysql();
  470. t = odbc::query(conn, s_query);
  471. conn.close();
  472. return t;
  473. }
  474. /*
  475. * 取某时间段的基金组合BFI基准
  476. *
  477. *
  478. * Example: get_entity_bfi_factors('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  479. * get_entity_bfi_factors('PF', [166002,166114], '1990-01', '2024-08');
  480. */
  481. def get_entity_bfi_factors(entity_type, entity_ids, month_start, month_end) {
  482. t = null;
  483. s_entity_ids = ids_to_string(entity_ids);
  484. if(s_entity_ids == null || s_entity_ids == '') return null;
  485. if(entity_type == 'MF' || entity_type == 'HF') {
  486. t = get_fund_bfi_factors(s_entity_ids, month_start, month_end);
  487. t.rename!('fund_id', 'entity_id');
  488. } else if(entity_type == 'PF') {
  489. t = get_portfolio_bfi_factors(s_entity_ids, month_start, month_end);
  490. t.rename!('portfolio_id', 'entity_id');
  491. }
  492. return t;
  493. }
  494. /*
  495. * 取组合交易表
  496. *
  497. *
  498. * Example: get_portfolio_holding_history("166002,364640")
  499. */
  500. def get_portfolio_holding_history(portfolio_ids) {
  501. s_query = "SELECT portfolio_id, holding_date, fund_id, amount, fund_share, ROUND(amount/fund_share, 6) AS nav
  502. FROM pfdb.pf_portfolio_fund_history
  503. WHERE portfolio_id IN (" + portfolio_ids + ")
  504. AND isvalid = 1
  505. ORDER BY portfolio_id, holding_date";
  506. conn = connect_mysql();
  507. t = odbc::query(conn, s_query);
  508. conn.close();
  509. return t;
  510. }
  511. /*
  512. * 取基金证券从某日期后的所有净值及前值
  513. *
  514. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  515. * @param freq <STRING>: m, w, d
  516. * @param json_query <JSON>: [{sec_id:xxx, price_date: yyyy-mm-dd}]
  517. *
  518. */
  519. def get_nav_for_return_calculation(entity_type, freq, json_query) {
  520. s_query = "CALL pfdb.sp_get_nav_after_date('" + entity_type + "', '" + freq + "', '" + json_query + "')";
  521. conn = connect_mysql();
  522. t = odbc::query(conn, s_query);
  523. conn.close();
  524. return t;
  525. }
  526. /*
  527. * 取主基准和BFI的历史月收益率
  528. *
  529. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表
  530. * @param end_day <DATE>: 收益的截止日期
  531. *
  532. * @return <TABLE>: benchmark_id, end_date, ret
  533. *
  534. */
  535. def get_benchmark_return(benchmarks, end_day) {
  536. s_index_ids = '';
  537. s_factor_ids = '';
  538. if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
  539. // 前缀为 IN 的 benchmark id
  540. t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';
  541. s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
  542. // 前缀为 FA 的 benchmark id
  543. t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
  544. s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
  545. // 目前指数的月度业绩存在 fund_performance 表
  546. 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);
  547. // 而因子的月度业绩存在 cm_factor_performance 表
  548. 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);
  549. return t_bmk;
  550. }
  551. /*
  552. * 取某时间区间内的 XXX_indicator 表数据
  553. *
  554. *
  555. */
  556. def get_entity_indicaor(entity_type, entity_ids, start_date, end_date, isFromMySQL) {
  557. t = null;
  558. s_entity_ids = ids_to_string(entity_ids);
  559. if(s_entity_ids == null || s_entity_ids == '') return null;
  560. tmp = get_indicator_table_description(entity_type);
  561. yyyymm_start = start_date.temporalFormat("yyyy-MM")
  562. yyyymm_end = end_date.temporalFormat("yyyy-MM")
  563. if(isFromMySQL == true) {
  564. s_query = "SELECT *
  565. FROM " + tmp.table_name[0] + "
  566. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  567. AND isvalid = 1
  568. AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
  569. ORDER BY " + tmp.sec_id_col[0] + ", end_date";
  570. conn = connect_mysql()
  571. t = odbc::query(conn, s_query)
  572. conn.close()
  573. }
  574. return t
  575. }
  576. /*
  577. * 取持有基金净值更新的组合列表
  578. *
  579. * TODO: 需要跑3分钟,待优化
  580. *
  581. * Example: get_portfolio_list_by_fund_nav_updatetime([166002,166114], 2024.10.28, true);
  582. */
  583. def get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, isFromMySQL) {
  584. t = null;
  585. s_entity_ids = ids_to_string(portfolio_ids);
  586. if(isFromMySQL == true) {
  587. s_query = "CALL pfdb.sp_get_portfolios_to_cal_nav(" + iif(s_entity_ids.isNull(), 'NULL', "'" + s_entity_ids + "'") + ",'" + updatetime + "')";
  588. conn = connect_mysql();
  589. t = odbc::query(conn, s_query);
  590. conn.close();
  591. }
  592. return t
  593. }
  594. /*
  595. * 取Json中指定的组合当日净值
  596. *
  597. * @param s_json <JSON>
  598. *
  599. * Example: get_portfolio_nav_by_date([{"portfolio_id": 166002,"price_date": "2024.10.25"},{"portfolio_id": 166114,"price_date": "2024.03.13"}], true);
  600. */
  601. def get_portfolio_nav_by_date(s_json, isFromMySQL) {
  602. t = null;
  603. if(isFromMySQL == true) {
  604. s_query = "SELECT t.portfolio_id, t.price_date, nav.cumulative_nav
  605. FROM JSON_TABLE ( '" + s_json + "', '$[*]'
  606. COLUMNS ( portfolio_id INT PATH '$.portfolio_id',
  607. price_date DATE PATH '$.price_date' ) ) t
  608. LEFT JOIN pfdb.pf_portfolio_nav nav ON t.portfolio_id = nav.portfolio_id AND t.price_date = nav.price_date;";
  609. conn = connect_mysql();
  610. t = odbc::query(conn, s_query);
  611. conn.close();
  612. }
  613. return t;
  614. }
  615. /*
  616. * 【Morningstar Integration】取某时间后净值更新的公募基金列表
  617. *
  618. * @param entity_ids <STRING|VECTOR>:
  619. * @param update_time <DATETIME>: all updates after this time
  620. *
  621. * TODO: 将 public_nav2 换成 mfdb.public_nav 后,要把 createtime 改成 updatetime
  622. *
  623. * Example: ms_get_fund_list_by_nav_updatetime(['MF00003PW1','MF00003PWC'], 2024.10.26);
  624. */
  625. def ms_get_fund_list_by_nav_updatetime(entity_ids, updatetime) {
  626. s_entity_ids = ids_to_string(entity_ids);
  627. sql_entity_id = '';
  628. if(s_entity_ids != NULL) {
  629. sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  630. }
  631. s_query = "SELECT fund_id AS entity_id, MIN(price_date) AS price_date
  632. FROM raw_db.public_nav2
  633. WHERE isvalid = 1 " +
  634. sql_entity_id + "
  635. AND cumulative_nav > 0
  636. AND createtime >= '" + updatetime$STRING + "'
  637. GROUP BY fund_id
  638. ORDER BY fund_id, price_date";
  639. conn = connect_mysql();
  640. t = odbc::query(conn, s_query);
  641. conn.close();
  642. return t
  643. }
  644. /*
  645. * 【Morningstar Integration】
  646. *
  647. * Example: ms_get_fund_info("'MF00003PW1','MF00003PWC'");
  648. *
  649. */
  650. def ms_get_fund_info(fund_ids) {
  651. s_entity_ids = ids_to_string(fund_ids);
  652. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  653. 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
  654. FROM raw_db.fund_information2 fi
  655. INNER JOIN raw_db.fund_strategy2 fs ON fi.fund_id = fs.fund_id AND fs.isvalid = 1
  656. WHERE fi.fund_id IN (" + s_entity_ids + ")
  657. AND fi.isvalid = 1
  658. ORDER BY fi.fund_id"
  659. conn = connect_mysql()
  660. t = odbc::query(conn, s_query)
  661. conn.close()
  662. return t
  663. }
  664. /*
  665. * 【Morningstar Integration】
  666. *
  667. * Example: ms_get_fund_monthly_nav(['MF00003PW1','MF00003PWC']);
  668. *
  669. */
  670. def ms_get_fund_monthly_nav(fund_ids) {
  671. s_entity_ids = ids_to_string(fund_ids);
  672. if(s_entity_ids == NULL || s_entity_ids == '') return null;
  673. s_query = "SELECT n.fund_id AS entity_id, n.price_date, n.cumulative_nav
  674. FROM raw_db.public_nav2 n INNER JOIN (
  675. SELECT fund_id, max(price_date) AS monthend_date
  676. FROM raw_db.public_nav2
  677. WHERE fund_id IN (" + s_entity_ids + ")
  678. AND isvalid = 1
  679. AND cumulative_nav > 0
  680. GROUP BY fund_id, DATE_FORMAT(price_date, '%Y-%m')
  681. ) t ON n.fund_id = t.fund_id AND n.price_date = t.monthend_date
  682. UNION
  683. SELECT fi.fund_id, fi.inception_date, IFNULL(fi.initial_unit_value, 1)
  684. FROM raw_db.fund_information2 fi
  685. WHERE fi.fund_id IN (" + s_entity_ids + ")
  686. ORDER BY entity_id, price_date;"
  687. conn = connect_mysql()
  688. t = odbc::query(conn, s_query)
  689. conn.close()
  690. return t
  691. }
  692. /*
  693. * 【Morningstar Integration】取某时间段的基金主基准
  694. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  695. *
  696. * Example: ms_get_fund_primary_benchmark("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  697. */
  698. def ms_get_fund_primary_benchmark(fund_ids, month_start, month_end) {
  699. s_query = "SELECT fund_id, primary_benchmark_id AS benchmark_id, inception_date
  700. FROM raw_db.fund_information2
  701. WHERE fund_id IN (" + fund_ids + ")
  702. AND isvalid = 1;";
  703. conn = connect_mysql();
  704. t = odbc::query(conn, s_query);
  705. conn.close();
  706. t.addColumn('end_date', MONTH);
  707. m_start = temporalParse(month_start, 'yyyy-MM');
  708. m_end = temporalParse(month_end, 'yyyy-MM');
  709. tb_end_date = table(m_start..m_end AS end_date);
  710. 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());
  711. }
  712. /*
  713. * 【Morningstar Integration】取某时间段的基金组合主基准
  714. *
  715. *
  716. * Example: ms_get_entity_primary_benchmark('MF', "'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  717. * ms_get_entity_primary_benchmark('PF', [166002,166114], '1990-01', '2024-08');
  718. */
  719. def ms_get_entity_primary_benchmark(entity_type, entity_ids, month_start, month_end) {
  720. t = null;
  721. s_entity_ids = ids_to_string(entity_ids);
  722. if(s_entity_ids == null || s_entity_ids == '') return null;
  723. if(entity_type == 'MF' || entity_type == 'HF') {
  724. t = ms_get_fund_primary_benchmark(s_entity_ids, month_start, month_end);
  725. t.rename!('fund_id', 'entity_id');
  726. } else if(entity_type == 'PF') {
  727. t = get_portfolio_primary_benchmark(s_entity_ids, month_start, month_end);
  728. t.rename!('portfolio_id', 'entity_id');
  729. }
  730. return t;
  731. }
  732. /*
  733. * 【Morningstar Integration】取无风险月度利率
  734. *
  735. * ms_get_risk_free_rate(1990.01.01, today())
  736. */
  737. def ms_get_risk_free_rate(start_date, end_date) {
  738. return get_monthly_ret('MI', "'IN000002EI'", start_date, end_date, true);
  739. }
  740. /*
  741. * 【Morningstar Integration】取某时间段的基金同类平均指数
  742. * NOTE: 目前数据库里只存最新的基准,以后很可能会支持时间序列
  743. *
  744. * Example: ms_get_fund_category_average("'MF00003PW2', 'MF00003PW1', 'MF00003PXO'", '1990-01', '2024-06');
  745. */
  746. def ms_get_fund_category_average(fund_ids, month_start, month_end) {
  747. s_query = "SELECT fi.fund_id, ip.index_id AS benchmark_id, fi.inception_date
  748. FROM raw_db.fund_information2 fi
  749. INNER JOIN mfdb.indexes_profile ip ON ip.index_code = concat('MSMWCA.2.', fi.pub_sub_fund_type)
  750. WHERE fi.fund_id IN (" + fund_ids + ")
  751. AND fi.isvalid = 1
  752. AND ip.isvalid = 1;";
  753. conn = connect_mysql();
  754. t = odbc::query(conn, s_query);
  755. conn.close();
  756. t.addColumn('end_date', MONTH);
  757. m_start = temporalParse(month_start, 'yyyy-MM');
  758. m_end = temporalParse(month_end, 'yyyy-MM');
  759. tb_end_date = table(m_start..m_end AS end_date);
  760. 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());
  761. }