performanceDataPuller.dos 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. module fundit::performanceDataPuller
  2. use fundit::sqlUtilities
  3. /*
  4. * 通用取有更新的数据
  5. *
  6. * NOTE: isvalid = 0 的记录也会被返回
  7. *
  8. * Example: get_data_by_updatetime('mfdb.fund_performance_weekly', 2024.11.14T11:10:00, true);
  9. *
  10. */
  11. def get_data_by_updatetime(table_name, updatetime, isFromMySQL=true) {
  12. if(isFromMySQL == true) {
  13. s_query = "SELECT * FROM " + table_name + "
  14. WHERE updatetime >= '" + updatetime + "'";
  15. conn = connect_mysql()
  16. t = odbc::query(conn, s_query)
  17. conn.close()
  18. } else {
  19. }
  20. return t
  21. }
  22. /*
  23. * 取有周收益更新的基金组合列表及其最早日期
  24. *
  25. * @param entity_type <STRING>: MF,HF,MI,FI
  26. * @param entity_ids <VECTOR|STRING>
  27. *
  28. * Example: get_entity_list_by_weekly_return_updatetime('PF', NULL, 2024.11.20, true);
  29. */
  30. def get_entity_list_by_weekly_return_updatetime(entity_type, entity_ids, updatetime, isFromMySQL=true) {
  31. tmp = get_performance_weekly_table_description(entity_type);
  32. s_entity_ids = ids_to_string(entity_ids);
  33. sql_entity_id = '';
  34. if(s_entity_ids != NULL) {
  35. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  36. }
  37. sql_entity_id += iif(entity_type == 'PF', '', " AND " + tmp.sec_id_col[0] + " LIKE '" + tmp.prefix[0] + "%'");
  38. if(isFromMySQL == true) {
  39. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
  40. FROM " + tmp.table_name[0] + "
  41. WHERE isvalid = 1 " +
  42. sql_entity_id + "
  43. AND " + tmp.cumulative_nav_col[0] + " > 0
  44. AND updatetime >= '" + updatetime$STRING + "'
  45. GROUP BY " + tmp.sec_id_col[0] + "
  46. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  47. conn = connect_mysql()
  48. t = odbc::query(conn, s_query)
  49. conn.close()
  50. } else {
  51. }
  52. return t
  53. }
  54. /*
  55. * 取有更新的最早周收益数据日期
  56. *
  57. * @return <DATE>
  58. *
  59. * Example: get_oldest_date_by_weekly_return_updatetime('MF', 2024.11.20, true);
  60. */
  61. def get_oldest_date_by_weekly_return_updatetime(entity_type, updatetime, isFromMySQL=true) {
  62. rt = null;
  63. tmp = get_performance_weekly_table_description(entity_type);
  64. if(tmp.isVoid() || tmp.size() == 0 ) return rt;
  65. sql_entity_id = iif(entity_type == 'PF', '', " AND " + tmp.sec_id_col[0] + " LIKE '" + tmp.prefix[0] + "%'");
  66. if(isFromMySQL == true) {
  67. nav_table_name = tmp.table_name[0];
  68. s_query = "SELECT MIN(price_date) AS price_date
  69. FROM " + tmp.table_name[0] + "
  70. WHERE updatetime >= '" + updatetime$STRING + "'" +
  71. sql_entity_id + "
  72. AND " + tmp.cumulative_nav_col[0] + " > 0
  73. GROUP BY " + tmp.sec_id_col[0];
  74. conn = connect_mysql()
  75. t = odbc::query(conn, s_query)
  76. conn.close()
  77. if(t.isVoid() || t.size() == 0) return rt;
  78. rt = t[0].price_date;
  79. } else {
  80. }
  81. return rt;
  82. }
  83. /*
  84. * 通用取周净值
  85. *
  86. * Example: get_weekly_ret('MF', ['MF00003PW1'], 2024.06.01, today(), true);
  87. * get_weekly_ret('PF', [166002], 2024.06.01, today(), true);
  88. */
  89. def get_weekly_ret(entity_type, entity_ids, start_date=1990.01.01, end_date=2099.12.31, isFromMySQL=true){
  90. s_entity_ids = ids_to_string(entity_ids);
  91. if(s_entity_ids == null || s_entity_ids == '') return null;
  92. tmp = get_performance_weekly_table_description(entity_type);
  93. if(isFromMySQL == true) {
  94. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, year_week, price_date, ret_1w AS ret, " + tmp.cumulative_nav_col[0] + " AS nav
  95. FROM " + tmp.table_name[0] + "
  96. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  97. AND isvalid = 1
  98. AND price_date BETWEEN '" + start_date + "' AND '" + end_date + "'
  99. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  100. conn = connect_mysql()
  101. t = odbc::query(conn, s_query)
  102. conn.close()
  103. } else {
  104. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  105. }
  106. return t
  107. }
  108. /*
  109. * 通用取周收益
  110. *
  111. */
  112. def get_entity_weekly_rets(entity_type, entity_info) {
  113. rets = null;
  114. if(entity_info.isVoid() || entity_info.size() == 0) return rets;
  115. very_old_date = '1990.01.01';
  116. // 简单起见,取整个数据集的最新日期(month-end production时取上月最后一天即可
  117. end_day = entity_info.price_date.max();
  118. return get_weekly_ret(entity_type, entity_info.entity_id, very_old_date, end_day, true);
  119. }
  120. /*
  121. * 通用取月收益
  122. *
  123. * @param entity_type <STRING>:
  124. * @param entity_ids <VECTOR|STRING>:
  125. * @param start_date <DATE>:
  126. * @param end_date <DATE>:
  127. * @param isFromMySQL <BOOL>:
  128. *
  129. *
  130. * Example: get_monthly_ret('HF', ['HF000004KN','HF000103EU','HF00018WXG'], 2000.01.01, 2024.03.01, true);
  131. * get_monthly_ret('PF', [166002], 2000.01.01, 2024.03.01, true);
  132. * get_monthly_ret('PL', ['PL000000NS', 'PL00000ICF'], 2000.01.01, 2024.12.02, true);
  133. */
  134. def get_monthly_ret(entity_type, entity_ids, start_date, end_date, isFromMySQL) {
  135. s_entity_ids = ids_to_string(entity_ids);
  136. if(s_entity_ids == null || s_entity_ids == '') return null;
  137. tmp = get_performance_table_description(entity_type);
  138. yyyymm_start = start_date.temporalFormat("yyyy-MM")
  139. yyyymm_end = end_date.temporalFormat("yyyy-MM")
  140. sql_extra_cols = iif(entity_type in ['PL', 'CO'], "curve_type, strategy, ", "");
  141. if(isFromMySQL == true) {
  142. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, " + sql_extra_cols + "end_date, price_date, ret_1m AS ret, " + tmp.cumulative_nav_col[0] + " AS nav
  143. FROM " + tmp.table_name[0] + "
  144. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  145. AND isvalid = 1
  146. AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
  147. ORDER BY " + tmp.sec_id_col[0] + ", end_date";
  148. conn = connect_mysql()
  149. t = odbc::query(conn, s_query)
  150. conn.close()
  151. } else {
  152. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  153. 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"))
  154. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0], then rename to "entity_id"?
  155. s_where = expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(","))
  156. t = sql(s_col, tb_local, s_where).eval()
  157. }
  158. return t
  159. }
  160. /*
  161. * 取无风险月度利率
  162. *
  163. * get_risk_free_rate(1990.01.01, today())
  164. */
  165. def get_risk_free_rate(start_date, end_date) {
  166. return get_monthly_ret('MI', "'IN0000000M'", start_date, end_date, true);
  167. }
  168. /*
  169. * 取基金最新收益及净值
  170. *
  171. * Example: get_entity_list_by_latest_return_updatetime('HF', ['HF000004KN','HF00018WXG'], 2024.01.01, true)
  172. * get_entity_list_by_latest_return_updatetime('HF', NULL, 2024.11.01, true)
  173. */
  174. def get_entity_list_by_latest_return_updatetime(entity_type, entity_ids, updatetime, isFromMySQL=true) {
  175. tmp = get_latest_performance_table_description(entity_type);
  176. s_entity_ids = ids_to_string(entity_ids);
  177. sql_entity_id = '';
  178. if(s_entity_ids != NULL) {
  179. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  180. }
  181. sql_entity_id += iif(entity_type == 'PF', '', " AND " + tmp.sec_id_col[0] + " LIKE '" + tmp.prefix[0] + "%'");
  182. if(isFromMySQL == true) {
  183. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, price_date, end_date
  184. FROM " + tmp.table_name[0] + "
  185. WHERE isvalid = 1 " +
  186. sql_entity_id + "
  187. AND " + tmp.cumulative_nav_col[0] + " > 0
  188. AND updatetime >= '" + updatetime$STRING + "'
  189. ORDER BY " + tmp.sec_id_col[0];
  190. conn = connect_mysql()
  191. t = odbc::query(conn, s_query)
  192. conn.close()
  193. } else {
  194. tb_local = load_table_from_local("fundit", "mfdb.fund_latest_nav_performance")
  195. s_col = sqlCol("*")
  196. s_where = expr(<fund_id>, in, entity_ids.strReplace("'", "").split(","))
  197. t = sql(s_col, tb_local, s_where).eval()
  198. }
  199. return t
  200. }
  201. /*
  202. * 通用取给定日期之后(含此日期)的净值
  203. *
  204. * @param price_date <DATE/MONTH>
  205. *
  206. *
  207. *
  208. * Example: get_nav_by_price_date('HF', "'HF000004KN','HF00018WXG'", 2024.05.01, true);
  209. * get_nav_by_price_date('MI', "'IN00000008','IN0000000M'", 2024.05.01, true);
  210. * get_nav_by_price_date('PL', NULL, 2024.11.01, true);
  211. */
  212. def get_nav_by_price_date(entity_type, entity_ids, price_date, isFromMySQL) {
  213. s_entity_ids = ids_to_string(entity_ids);
  214. tmp = get_nav_table_description(entity_type);
  215. if(s_entity_ids == null || s_entity_ids == '') sql_entity_id = '';
  216. else sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  217. if(entity_type in ('PL', 'CO')) {
  218. sql_price_date = " AND " + tmp.price_date_col[0] + " >= '" + price_date.month().temporalFormat('yyyy-MM') + "'";
  219. }
  220. else {
  221. sql_price_date = " AND " + tmp.price_date_col[0] + " >= '" + price_date.temporalFormat('yyyy-MM-dd') + "'";
  222. }
  223. if(isFromMySQL == true) {
  224. nav_table_name = tmp.table_name[0];
  225. s_query = "SELECT DISTINCT " + tmp.sec_id_col[0] + " AS entity_id, " + tmp.price_date_col[0] + ", " + tmp.cumulative_nav_col[0] + " AS cumulative_nav, " + tmp.nav_col[0] + " AS nav
  226. FROM " + tmp.table_name[0] + "
  227. WHERE isvalid = 1 " +
  228. sql_entity_id + sql_price_date + "
  229. AND " + tmp.cumulative_nav_col[0] + " > 0
  230. ORDER BY " + tmp.sec_id_col[0] + ", " + tmp.price_date_col[0];
  231. conn = connect_mysql();
  232. t = odbc::query(conn, s_query);
  233. conn.close();
  234. } else {
  235. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  236. s_col = sqlCol("*")
  237. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0]?
  238. s_where = [expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(",")), <price_date >= price_date>]
  239. t = sql(s_col, tb_local, s_where).eval()
  240. }
  241. return t
  242. }
  243. /*
  244. * 取指数因子点位
  245. *
  246. * get_index_nav_by_price_date("'IN00000008','FA00000WKG'", 2024.06.01)
  247. */
  248. def get_index_nav_by_price_date(index_ids, price_date) {
  249. s_query = "SELECT index_id, price_date, close AS cumulative_nav
  250. FROM mfdb.market_indexes
  251. WHERE index_id IN (" + index_ids + ")
  252. AND isvalid = 1
  253. AND close > 0
  254. AND price_date >= '" + price_date + "'
  255. UNION
  256. SELECT index_id AS index_id, price_date, index_value AS cumulative_nav
  257. FROM mfdb.indexes_ty_index
  258. WHERE index_id IN (" + index_ids + ")
  259. AND isvalid = 1
  260. AND index_value > 0
  261. AND price_date >= '" + price_date + "'
  262. UNION
  263. SELECT factor_id AS index_id, price_date, factor_value AS cumulative_nav
  264. FROM pfdb.cm_factor_value
  265. WHERE factor_id IN (" + index_ids + ")
  266. AND isvalid = 1
  267. AND factor_value > 0
  268. AND price_date >= '" + price_date + "'
  269. ORDER BY price_date"
  270. conn = connect_mysql()
  271. t = odbc::query(conn, s_query)
  272. conn.close()
  273. return t
  274. }
  275. /*
  276. * 取某时间后更新的各基金组合最早净值日期
  277. *
  278. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  279. * @param entity_ids <VECTOR|STRING>: NULL时取全量
  280. * @param update_time <DATETIME>: all updates after this time
  281. * @param isFromMySQL <BOOL>:
  282. *
  283. * Example: get_entity_list_by_nav_updatetime('MF', ['MF00003PW1', 'MF00003PW2'], 2024.09.26, true);
  284. * get_entity_list_by_nav_updatetime('HF', null, 2024.07.19T10:00:00, true);
  285. * get_entity_list_by_nav_updatetime('PF', '166002,166114', 2024.06.20, true);
  286. * get_entity_list_by_nav_updatetime('PL', NULL, 2024.10.20, true);
  287. *
  288. */
  289. def get_entity_list_by_nav_updatetime(entity_type, entity_ids, updatetime, isFromMySQL) {
  290. tmp = get_nav_table_description(entity_type);
  291. s_entity_ids = ids_to_string(entity_ids);
  292. sql_entity_id = '';
  293. if(s_entity_ids != NULL) {
  294. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  295. }
  296. sql_extra_keys = iif(entity_type in ('PL', 'CO'), ", curve_type, strategy", "");
  297. if(isFromMySQL == true) {
  298. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id" + sql_extra_keys + ", MIN(" + tmp.price_date_col[0] + ") AS price_date
  299. FROM " + tmp.table_name[0] + "
  300. WHERE isvalid = 1 " +
  301. sql_entity_id + "
  302. AND " + tmp.cumulative_nav_col[0] + " > 0
  303. AND updatetime >= '" + updatetime$STRING + "'
  304. GROUP BY " + tmp.sec_id_col[0] + sql_extra_keys + "
  305. ORDER BY " + tmp.sec_id_col[0] + ", " + tmp.price_date_col[0];
  306. conn = connect_mysql();
  307. t = odbc::query(conn, s_query);
  308. conn.close();
  309. } else {
  310. //TODO
  311. }
  312. return t
  313. }
  314. /*
  315. * 取基金证券从某日期后的所有净值及前值
  316. *
  317. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  318. * @param freq <STRING>: m, w, d
  319. * @param pre_nav_incld <INT>: 0- no pre_nav; 1- pre_nav only; 2- pre_nav + afters
  320. * @param json_query <JSON>: [{sec_id:xxx, price_date: yyyy-mm-dd}]
  321. *
  322. * Example: get_nav_for_return_calculation('MI', 'm', '[{"sec_id": "IN00000008","price_date": "2004-12-31"}, {"sec_id": "IN00000077","price_date": "2003-12-31"}]', 1)
  323. *
  324. */
  325. def get_nav_for_return_calculation(entity_type, freq, json_query, pre_nav_incld=2) {
  326. s_query = "CALL pfdb.sp_get_nav_for_return_cal('" + entity_type + "', '" + freq + "', " + pre_nav_incld + ", '" + json_query + "')";
  327. conn = connect_mysql();
  328. t = odbc::query(conn, s_query);
  329. conn.close();
  330. return t;
  331. }
  332. /*
  333. * 取基金经理/公司从某日期后的所有净值及前值
  334. *
  335. * @param entity_type <STRING>: PL, CO
  336. * @param json_query <JSON>: [{entity_id:xxx, curve_type:1, strategy:0, end_date: yyyy-mm}]
  337. * @param pre_nav_incld <INT>: 0- no pre_nav; 1- pre_nav only; 2- pre_nav + afters
  338. * @param freq <STRING>: m- monthly, w-weekly
  339. *
  340. * Example: get_mc_nav_for_return_calculation('PL', '[{"entity_id":"PL000000NS", "curve_type":"4", "strategy":"0", "effective_date":"2024-07"}]', 2);
  341. * get_mc_nav_for_return_calculation('CO', '[{"entity_id":"CO00000017", "curve_type":"4", "strategy":"0", "effective_date":"202406"}]', 1, 'w');
  342. *
  343. */
  344. def get_mc_nav_for_return_calculation(entity_type, json_query, pre_nav_incld=2, freq='m') {
  345. s_query = "CALL pfdb.sp_get_mc_nav_for_return_cal('" + entity_type + "', '" + freq + "', " + pre_nav_incld + ", '" + json_query + "')";
  346. conn = connect_mysql();
  347. t = odbc::query(conn, s_query);
  348. conn.close();
  349. return t;
  350. }
  351. /*
  352. * 取主基准和BFI的历史月收益率
  353. *
  354. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表
  355. * @param end_day <DATE>: 收益的截止日期
  356. *
  357. * @return <TABLE>: benchmark_id, end_date, ret
  358. *
  359. */
  360. def get_benchmark_return(benchmarks, end_day) {
  361. s_index_ids = '';
  362. s_factor_ids = '';
  363. if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
  364. // 前缀为 IN 的 benchmark id
  365. t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';;
  366. s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
  367. // 前缀为 FA 的 benchmark id
  368. t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
  369. s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
  370. // 目前指数的月度业绩存在 fund_performance 表
  371. 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);
  372. // 而因子的月度业绩存在 cm_factor_performance 表
  373. 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);
  374. return t_bmk;
  375. }
  376. /*
  377. * 将十位标准字符型ID分门别类
  378. *
  379. * @param entity_ids <VECTOR>: vector of ids
  380. * @return <DICT>: entity_type - vector of ids
  381. *
  382. * NOTE: "IN"打头的ID无法判断是市场指数还是图译指数,所以 MI, FI 都会被返回
  383. *
  384. * Example: check_universe(['MF00003PW1','HF12345678','MF00003PW2','IN00000008']);
  385. */
  386. def check_universe (entity_ids) {
  387. v_id = array(STRING);
  388. dic = dict(STRING, ANY);
  389. if(entity_ids.isVoid() || entity_ids.size() == 0) return dic;
  390. for(r in get_entity_id_info()) {
  391. v_id = entity_ids[like(entity_ids, r.prefix + '%') && strlen(entity_ids)==10];
  392. if(v_id.size() > 0) {
  393. dic[r.entity_type] = v_id;
  394. }
  395. }
  396. return dic;
  397. }
  398. /*
  399. * 通用取收益数据
  400. *
  401. * @param entity_type <STRING>:
  402. * @param entity_ids <VECTOR>
  403. * @param freq <STRING>: m, w, d
  404. * @param start_day <DATE>
  405. * @param end_day <DATE>
  406. * @param isFromMySQL <BOOL>
  407. *
  408. * Example: get_entity_return('MF', ['MF00003PW1', 'MF00003PW2'], 'w', 2024.02.01, 2024.09.30, true);
  409. * get_entity_return(NULL, ['FA00000VML', 'FA00000VMM', 'FA00000VMN', 'FA00000VMO', 'IN0000007G'], 'w');
  410. * get_entity_return('PF', [166002], 'm');
  411. */
  412. def get_entity_return(entity_type, entity_ids, freq='m', start_day=1990.01.01, end_day=2099.12.31, isFromMySQL=true) {
  413. t_ret = table(1000:0, ['entity_id', 'effective_date', 'price_date', 'ret', 'nav'], [iif(entity_type=='PF', INT, SYMBOL), STRING, DATE, DOUBLE, DOUBLE]);
  414. d = dict(STRING, ANY);
  415. if(entity_type != null) d[entity_type] = entity_ids;
  416. else
  417. d = check_universe(entity_ids);
  418. for(univ in d.keys()) {
  419. if(freq == 'm') {
  420. t = SELECT entity_id, end_date$STRING AS effective_date, price_date, ret, nav
  421. FROM get_monthly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
  422. t_ret.tableInsert(t);
  423. } else if(freq == 'w') {
  424. t = SELECT entity_id, year_week$STRING AS effective_date, price_date, ret, nav
  425. FROM get_weekly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
  426. t_ret.tableInsert(t);
  427. } else { // 缺省是日收益
  428. s_json = table(d[univ] AS sec_id, take(start_day, d[univ].size()) AS price_date).toStdJson();
  429. // 取基金组合在包括各自的某净值日期的前值及之后的所有净值
  430. tb_nav = get_nav_for_return_calculation(univ, 'd', s_json);
  431. if(!tb_nav.isVoid() && tb_nav.size() > 0) {
  432. tb_nav.rename!('sec_id', 'entity_id').sortBy!(['entity_id', 'price_date'], [1, 1]);
  433. t = SELECT entity_id, price_date$STRING AS effective_date, price_date, cumulative_nav.ratios()-1 AS ret, cumulative_nav AS nav
  434. FROM tb_nav
  435. WHERE price_date <= end_day CONTEXT BY entity_id;
  436. t_ret.tableInsert(t);
  437. }
  438. }
  439. }
  440. return t_ret;
  441. }
  442. /*
  443. * 取持有基金净值更新的组合列表
  444. *
  445. * TODO: 需要跑3分钟,待优化
  446. *
  447. * Example: get_portfolio_list_by_fund_nav_updatetime([166002,166114], 2024.10.28, true);
  448. */
  449. def get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, isFromMySQL) {
  450. t = null;
  451. s_entity_ids = ids_to_string(portfolio_ids);
  452. if(isFromMySQL == true) {
  453. s_query = "CALL pfdb.sp_get_portfolios_to_cal_nav(" + iif(s_entity_ids.isNull(), 'NULL', "'" + s_entity_ids + "'") + ",'" + updatetime + "')";
  454. conn = connect_mysql();
  455. t = odbc::query(conn, s_query);
  456. conn.close();
  457. }
  458. return t
  459. }
  460. /*
  461. * 根据指数净值更新日期,取受影响的合成因子列表
  462. *
  463. * Modify 20241218 去掉 factor_type = 5的限制,支持非BFI因子使用 cm_factor_index_map
  464. *
  465. * Example: get_bfi_factor_list_by_index_nav_updatetime(['FA00000VMH','FA00000VMK','FA00000SMB'], 2024.11.08, true);
  466. * get_bfi_factor_list_by_index_nav_updatetime(NULL, NULL, true);
  467. */
  468. def get_bfi_factor_list_by_index_nav_updatetime(factor_ids, updatetime, isFromMySQL) {
  469. t = null;
  470. s_entity_ids = ids_to_string(factor_ids);
  471. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.factor_id IN (" + s_entity_ids + ")");
  472. sql_updatetime = iif(updatetime == NULL, '', " AND mi.updatetime > '" + updatetime + "'");
  473. if(isFromMySQL == true) {
  474. s_query = "SELECT fi.factor_id, GREATEST(MIN(mi.price_date), fi.inception_date) AS first_cal_date, LEAST(MAX(mi.price_date), CURRENT_DATE) AS latest_cal_date
  475. FROM pfdb.cm_factor_information fi
  476. INNER JOIN pfdb.cm_factor_index_map map ON fi.factor_id = map.factor_id
  477. INNER JOIN mfdb.market_indexes mi ON map.index_id = mi.index_id
  478. WHERE fi.isvalid = 1" +
  479. sql_entity_id + "
  480. AND map.isvalid = 1
  481. AND mi.isvalid = 1
  482. AND mi.close > 0 " +
  483. sql_updatetime + "
  484. GROUP BY fi.factor_id";
  485. conn = connect_mysql();
  486. t = odbc::query(conn, s_query);
  487. conn.close();
  488. }
  489. return t
  490. }
  491. /*
  492. * 取固定权重的组合持仓,包括BFI因子,日再平衡组合等
  493. *
  494. * Example: get_fixed_weight_portfolio_holding('PF', [145589, 170904]);
  495. * get_fixed_weight_portfolio_holding('FA', ['FA00000VMH', 'FA00000VMI']);
  496. */
  497. def get_fixed_weight_portfolio_holding(entity_type, entity_ids) {
  498. t = null;
  499. s_query = '';
  500. s_entity_ids = ids_to_string(entity_ids);
  501. if(entity_type == 'FA') {
  502. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND factor_id IN (" + s_entity_ids + ")");
  503. s_query = "SELECT factor_id AS entity_id, holding_date, index_id AS sec_id, weight, 2 AS entity_type
  504. FROM pfdb.cm_factor_index_map
  505. WHERE isvalid = 1" +
  506. sql_entity_id;
  507. } else if(entity_type == 'PF') {
  508. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND portfolio_id IN (" + s_entity_ids + ")");
  509. s_query = "SELECT portfolio_id AS entity_id, effective_date AS holding_date, entity_id AS sec_id, weight, entity_type
  510. FROM pfdb.pf_portfolio_rebalance_weights
  511. WHERE isvalid = 1" +
  512. sql_entity_id;
  513. }
  514. if( s_query == '') return t;
  515. conn = connect_mysql();
  516. t = odbc::query(conn, s_query);
  517. conn.close();
  518. return t;
  519. }
  520. /*
  521. * 通用取Json中指定的证券当日净值
  522. *
  523. *
  524. * Example: get_entity_nav_by_date('PF', '[{"entity_id": 166002,"price_date": "2024.10.25"},{"entity_id": 166114,"price_date": "2024.03.13"}]', true);
  525. */
  526. def get_entity_nav_by_date(entity_type, s_json, isFromMySQL=true) {
  527. t = null;
  528. desc = get_nav_table_description(entity_type)[0];
  529. if(isFromMySQL == true) {
  530. s_query = "SELECT t.entity_id, nav.price_date, nav." + desc.cumulative_nav_col + " AS cumulative_nav, nav." + desc.nav_col + " AS nav
  531. FROM JSON_TABLE ( '" + s_json + "', '$[*]'
  532. COLUMNS ( entity_id " + iif(entity_type=='PF', 'INT', 'VARCHAR(10)') + " PATH '$.entity_id',
  533. price_date DATE PATH '$.price_date' ) ) t
  534. LEFT JOIN " + desc.table_name + " nav ON t.entity_id = nav." + desc.sec_id_col + " AND t.price_date = nav.price_date
  535. WHERE nav.isvalid = 1
  536. AND nav." + desc.cumulative_nav_col + " > 0
  537. ORDER BY t.entity_id;";
  538. conn = connect_mysql();
  539. t = odbc::query(conn, s_query);
  540. conn.close();
  541. }
  542. return t;
  543. }
  544. /*
  545. * 同步专用:取Json中指定的证券当日净值及isvalid, createtime、updatetime
  546. *
  547. * TODO: need adding index on json table
  548. *
  549. * Example: sync_entity_nav_by_date('MF', '[{"entity_id": "MF00003PW1","price_date": "2024.10.25"},{"entity_id": "MF00003PW2","price_date": "2024.03.13"}]');
  550. * sync_entity_nav_by_date('MI', '[{"entity_id": "IN00000008","price_date": "2000.01.25"}]');
  551. * sync_entity_nav_by_date('PF', '[{"entity_id": 166002,"price_date": "2024.10.25"},{"entity_id": 166114,"price_date": "2024.03.13"}]');
  552. */
  553. def sync_entity_nav_by_date(entity_type, s_json) {
  554. t = null;
  555. s_query = "CALL pfdb.sp_sync_nav_for_dolphin('" + entity_type + "','" + s_json + "');";
  556. conn = connect_mysql();
  557. t = odbc::query(conn, s_query);
  558. conn.close();
  559. return t;
  560. }
  561. /*
  562. * 通用取Json中指定日期后(含当日)的证券收益
  563. *
  564. * @param freq <STRING>: m, w
  565. *
  566. *
  567. * Example: get_entity_ret_by_date('PF', '[{"entity_id": 166002,"price_date": "2024.10.25"},{"entity_id": 166114,"price_date": "2024.03.13"}]', 'm', true);
  568. * get_entity_ret_by_date('MF', '[{"entity_id": "MF00003PW1","price_date": "2023.12.28"}]', 'w', true);
  569. */
  570. def get_entity_ret_by_date(entity_type, s_json, freq, isFromMySQL=true) {
  571. t = null;
  572. if(isFromMySQL == true) {
  573. s_query = "CALL pfdb.sp_get_return_by_json('" + entity_type + "', '" + freq + "', '" + s_json + "');";
  574. conn = connect_mysql();
  575. t = odbc::query(conn, s_query);
  576. conn.close();
  577. }
  578. return t;
  579. }
  580. /*
  581. * 取月度指标表
  582. *
  583. * @param table_name <STRING>: 指标表名
  584. * @param end_date <MONTH>
  585. * @param isFromMySQL <BOOL>
  586. *
  587. * Example: get_monthly_indicator_data('mfdb.fund_performance', 2024.09M, true);
  588. */
  589. def get_monthly_indicator_data(table_name, end_date, isFromMySQL=true) {
  590. t = null;
  591. s_end_date_sql = iif(end_date.isNull(), '', " AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" );
  592. if(isFromMySQL == true) {
  593. s_query = "SELECT * FROM " + table_name + " WHERE isvalid = 1 " + s_end_date_sql;
  594. conn = connect_mysql();
  595. t = odbc::query(conn, s_query);
  596. conn.close();
  597. }
  598. return t;
  599. }
  600. /*
  601. * 取 pf_fund_indicator_ranking 表
  602. *
  603. *
  604. * Example: get_fund_indicator_ranking("'MF00003PW1'", 2024.09M, 102, true);
  605. * get_fund_indicator_ranking(NULL, 2023.09M, [1, 3], true);
  606. */
  607. def get_fund_indicator_ranking(fund_ids, end_date, strategy, isFromMySQL=true) {
  608. t = null;
  609. s_entity_ids = ids_to_string(fund_ids);
  610. sql_entity_id = '';
  611. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  612. s_strategy_ids = ids_to_string(strategy);
  613. sql_strategy_id = '';
  614. if(s_strategy_ids != NULL) sql_strategy_id = " AND strategy IN (" + s_strategy_ids + ")";
  615. if(isFromMySQL == true) {
  616. s_query = "SELECT *
  617. FROM pfdb.pf_fund_indicator_ranking
  618. WHERE isvalid = 1
  619. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  620. sql_strategy_id +
  621. sql_entity_id;
  622. conn = connect_mysql();
  623. t = odbc::query(conn, s_query);
  624. conn.close();
  625. }
  626. return t;
  627. }
  628. /*
  629. * 取 pf_fund_indicator_substrategy_ranking 表
  630. *
  631. *
  632. * Example: get_fund_indicator_substrategy_ranking("'MF00003PW1'", 2024.09M, 23, true);
  633. * get_fund_indicator_substrategy_ranking(NULL, 2023.09M, [22, 23], true);
  634. */
  635. def get_fund_indicator_substrategy_ranking(fund_ids, end_date, substrategy, isFromMySQL=true) {
  636. t = null;
  637. s_entity_ids = ids_to_string(fund_ids);
  638. sql_entity_id = '';
  639. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  640. s_strategy_ids = ids_to_string(substrategy);
  641. sql_strategy_id = '';
  642. if(s_strategy_ids != NULL) sql_strategy_id = " AND substrategy IN (" + s_strategy_ids + ")";
  643. if(isFromMySQL == true) {
  644. s_query = "SELECT *
  645. FROM pfdb.pf_fund_indicator_substrategy_ranking
  646. WHERE isvalid = 1
  647. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  648. sql_strategy_id +
  649. sql_entity_id;
  650. conn = connect_mysql();
  651. t = odbc::query(conn, s_query);
  652. conn.close();
  653. }
  654. return t;
  655. }
  656. /*
  657. * 取 pf_fund_bfi_bm_indicator_ranking 表
  658. *
  659. *
  660. * Example: get_fund_bfi_bm_indicator_ranking("'MF00003PW1'", 2024.09M, NULL, true);
  661. *
  662. */
  663. def get_fund_bfi_bm_indicator_ranking(fund_ids, end_date, factor_ids, isFromMySQL=true) {
  664. t = null;
  665. s_entity_ids = ids_to_string(fund_ids);
  666. sql_entity_id = '';
  667. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  668. s_factor_ids = ids_to_string(factor_ids);
  669. sql_factor_id = '';
  670. if(s_factor_ids != NULL) sql_factor_id = " AND factor_id IN (" + s_factor_ids + ")";
  671. if(isFromMySQL == true) {
  672. s_query = "SELECT *
  673. FROM pfdb.pf_fund_bfi_bm_indicator_ranking
  674. WHERE isvalid = 1
  675. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  676. sql_factor_id +
  677. sql_entity_id;
  678. conn = connect_mysql();
  679. t = odbc::query(conn, s_query);
  680. conn.close();
  681. }
  682. return t;
  683. }
  684. /*
  685. * 取算相关性所需要的指数/因子ID
  686. *
  687. * NOTE: 与 Java 使用的逻辑相同, 在cm_class_asset_index中有当月performance的指数加上 CSI5 和 Cap3Style 的 factor
  688. * 唯一的不同是将历史已经停止维护的老指数筛掉(必须有上月的指数收益)
  689. *
  690. * Example: get_correlation_index_list();
  691. */
  692. def get_correlation_index_list() {
  693. s_query = "SELECT a.fund_id AS entity_id
  694. FROM mfdb.fund_performance AS a
  695. LEFT JOIN pfdb.cm_class_asset_index AS b ON a.fund_id = b.index_id
  696. WHERE a.end_date = LEFT(DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH), 7)
  697. AND a.fund_id LIKE 'IN%'
  698. AND b.isvalid = 1
  699. ORDER BY a.fund_id ASC";
  700. conn = connect_mysql();
  701. t = odbc::query(conn, s_query);
  702. conn.close();
  703. INSERT INTO t VALUES (['FA00000VML','FA00000VMM','FA00000VMN','FA00000VMO','FA00000WKG','FA00000WKH']);
  704. return t;
  705. }
  706. /*
  707. * 取BFI所需要的因子ID
  708. *
  709. * TODO: 大盘风格、小盘风格 (FA00000WKG, FA00000WKH) 没有category_group_id & order_id, 在哪里用?
  710. */
  711. def get_bfi_index_list() {
  712. s_query = "SELECT factor_id, factor_name, category_group_id, maximum_num, order_id
  713. FROM pfdb.cm_factor_information
  714. WHERE isvalid = 1
  715. AND ( factor_type = 5 OR factor_id IN ('FA00000SMB', 'FA00000VMG') )
  716. ORDER BY order_id ASC";
  717. conn = connect_mysql();
  718. t = odbc::query(conn, s_query);
  719. conn.close();
  720. return t;
  721. }
  722. /*
  723. * 取基金分类平均的周收益
  724. *
  725. * TODO: 需要跑1分钟,待优化
  726. *
  727. * Example: get_category_avg_weekly_return('bfi', 2024.09.28, 5, 30, true);
  728. * get_category_avg_weekly_return('strategy', 2014.06.27, 5, 30, true);
  729. */
  730. def get_category_avg_weekly_return(category_type, begin_day, trim_pct=5, min_cnt=30, isFromMySQL=true) {
  731. t = null;
  732. if(isFromMySQL == true) {
  733. s_query = "CALL pfdb.sp_get_category_avg_weekly_return('" + category_type + "', '" + begin_day + "', " + trim_pct + ", " + min_cnt + ")";
  734. conn = connect_mysql();
  735. t = odbc::query(conn, s_query);
  736. conn.close();
  737. }
  738. return t
  739. }
  740. /*
  741. * 取基金经理或公司月收益
  742. *
  743. *
  744. * Example: get_mc_average_return('PL', 'm', '[{"entity_id": "PL000000NS","effective_date": "2024-07"},{"entity_id": "PL00000ICF","effective_date": "2023-12"}]', 0, 1, true);
  745. * get_mc_average_return('CO', 'w', '[{"entity_id": "CO00000017","effective_date": "202407"},{"entity_id": "CO0001003W","effective_date": "202306"}]', 0, 1, true);
  746. */
  747. def get_mc_average_return(entity_type, freq, json_query, trim_pct=0, min_cnt=1, isFromMySQL=true) {
  748. t = null;
  749. if(isFromMySQL == true) {
  750. s_query = "CALL pfdb.sp_get_mc_avg_return('" + entity_type + "', '" + freq + "', '" + json_query + "', " + trim_pct + ", " + min_cnt + ")";
  751. conn = connect_mysql();
  752. t = odbc::query(conn, s_query);
  753. conn.close();
  754. }
  755. return t
  756. }
  757. /*
  758. * 取有基金月收益更新的基金公司列表
  759. *
  760. * NOTE: 月收益表 isvalid = 0 的记录也会被返回
  761. *
  762. * Example: get_company_list_by_fund_updatetime(2024.11.14 11:10:00, 'm', true);
  763. * get_company_list_by_fund_updatetime(2024.12.24 11:10:00, 'w', true);
  764. */
  765. def get_company_list_by_fund_updatetime(updatetime, freq='m', isFromMySQL=true) {
  766. if(isFromMySQL == true) {
  767. if(freq == 'm') {
  768. s_query = "SELECT ci.company_id, MIN(fp.end_date) AS effective_date
  769. FROM mfdb.company_information ci
  770. INNER JOIN mfdb.fund_information fi ON ci.company_id = fi.advisor_id
  771. INNER JOIN mfdb.fund_performance fp ON fi.fund_id = fp.fund_id
  772. WHERE ci.isvalid = 1
  773. AND fi.isvalid = 1
  774. AND fp.updatetime >= '" + updatetime + "'
  775. GROUP BY ci.company_id;";
  776. } else {
  777. s_query = "SELECT ci.company_id, MIN(fp.year_week) AS effective_date
  778. FROM mfdb.company_information ci
  779. INNER JOIN mfdb.fund_information fi ON ci.company_id = fi.advisor_id
  780. INNER JOIN mfdb.fund_performance_weekly fp ON fi.fund_id = fp.fund_id
  781. WHERE ci.isvalid = 1
  782. AND fi.isvalid = 1
  783. AND fp.updatetime >= '" + updatetime + "'
  784. GROUP BY ci.company_id;";
  785. }
  786. conn = connect_mysql();
  787. t = odbc::query(conn, s_query);
  788. conn.close();
  789. } else {
  790. }
  791. return t
  792. }
  793. /*
  794. * 取有基金月收益更新的基金经理列表
  795. *
  796. * NOTE: 月收益表 isvalid = 0 的记录也会被返回
  797. *
  798. * Example: get_manager_list_by_fund_updatetime(2024.11.01, 'm', true);
  799. * get_manager_list_by_fund_updatetime(2024.12.12, 'w', true);
  800. *
  801. */
  802. def get_manager_list_by_fund_updatetime(updatetime, freq='m', isFromMySQL=true) {
  803. if(isFromMySQL == true) {
  804. if(freq == 'm') {
  805. s_query = "SELECT mi.personnel_id AS manager_id, MIN(fp.end_date) AS effective_date
  806. FROM mfdb.personnel_information mi
  807. INNER JOIN mfdb.fund_manager_mapping fmp ON mi.personnel_id = fmp.fund_manager_id
  808. INNER JOIN mfdb.fund_information fi ON fmp.fund_id = fi.fund_id
  809. INNER JOIN mfdb.fund_performance fp ON fi.fund_id = fp.fund_id
  810. WHERE mi.isvalid = 1
  811. AND fmp.isvalid = 1
  812. AND fi.isvalid = 1
  813. AND fp.updatetime >= '" + updatetime + "'
  814. AND fp.price_date <= IFNULL(fmp.management_end_date, CURRENT_DATE)
  815. GROUP BY mi.personnel_id;";
  816. } else {
  817. s_query = "SELECT mi.personnel_id AS manager_id, MIN(fp.year_week) AS effective_date
  818. FROM mfdb.personnel_information mi
  819. INNER JOIN mfdb.fund_manager_mapping fmp ON mi.personnel_id = fmp.fund_manager_id
  820. INNER JOIN mfdb.fund_information fi ON fmp.fund_id = fi.fund_id
  821. INNER JOIN mfdb.fund_performance_weekly fp ON fi.fund_id = fp.fund_id
  822. WHERE mi.isvalid = 1
  823. AND fmp.isvalid = 1
  824. AND fi.isvalid = 1
  825. AND fp.updatetime >= '" + updatetime + "'
  826. AND fp.price_date <= IFNULL(fmp.management_end_date, CURRENT_DATE)
  827. GROUP BY mi.personnel_id;";
  828. }
  829. conn = connect_mysql();;
  830. t = odbc::query(conn, s_query);
  831. conn.close();
  832. } else {
  833. }
  834. return t;
  835. }
  836. /*
  837. * 取基金经理/公司月度业绩表更新的收益日期
  838. *
  839. * Example: get_mc_performance_by_updatetime('PL', 2024.11.01, true);
  840. * get_mc_performance_by_updatetime('CO', 2024.11.01, true);
  841. */
  842. def get_mc_performance_by_updatetime(entity_type, updatetime, isFromMySQL=true) {
  843. des = get_performance_table_description(entity_type)[0];
  844. if(isFromMySQL == true) {
  845. s_query = "SELECT " + des.sec_id_col + " AS entity_id, curve_type, strategy, MIN(fp.end_date) AS end_date, MIN(fp.price_date) AS price_date
  846. FROM " + des.table_name + " fp
  847. WHERE fp.isvalid = 1
  848. AND fp.updatetime >= '" + updatetime + "'
  849. GROUP BY fp. " + des.sec_id_col + ", curve_type, strategy
  850. ORDER BY fp." + des.sec_id_col + ", curve_type, strategy;";
  851. conn = connect_mysql();
  852. t = odbc::query(conn, s_query);
  853. conn.close();
  854. } else {
  855. }
  856. return t;
  857. }