performanceDataPuller.dos 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. */
  133. def get_monthly_ret(entity_type, entity_ids, start_date, end_date, isFromMySQL) {
  134. s_entity_ids = ids_to_string(entity_ids);
  135. if(s_entity_ids == null || s_entity_ids == '') return null;
  136. tmp = get_performance_table_description(entity_type);
  137. yyyymm_start = start_date.temporalFormat("yyyy-MM")
  138. yyyymm_end = end_date.temporalFormat("yyyy-MM")
  139. if(isFromMySQL == true) {
  140. 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
  141. FROM " + tmp.table_name[0] + "
  142. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  143. AND isvalid = 1
  144. AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
  145. ORDER BY " + tmp.sec_id_col[0] + ", end_date";
  146. conn = connect_mysql()
  147. t = odbc::query(conn, s_query)
  148. conn.close()
  149. } else {
  150. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  151. 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"))
  152. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0], then rename to "entity_id"?
  153. s_where = expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(","))
  154. t = sql(s_col, tb_local, s_where).eval()
  155. }
  156. return t
  157. }
  158. /*
  159. * 取无风险月度利率
  160. *
  161. * get_risk_free_rate(1990.01.01, today())
  162. */
  163. def get_risk_free_rate(start_date, end_date) {
  164. return get_monthly_ret('MI', "'IN0000000M'", start_date, end_date, true);
  165. }
  166. /*
  167. * 取基金最新收益及净值
  168. *
  169. * Example: get_entity_list_by_latest_return_updatetime('HF', ['HF000004KN','HF00018WXG'], 2024.01.01, true)
  170. * get_entity_list_by_latest_return_updatetime('HF', NULL, 2024.11.01, true)
  171. */
  172. def get_entity_list_by_latest_return_updatetime(entity_type, entity_ids, updatetime, isFromMySQL=true) {
  173. tmp = get_latest_performance_table_description(entity_type);
  174. s_entity_ids = ids_to_string(entity_ids);
  175. sql_entity_id = '';
  176. if(s_entity_ids != NULL) {
  177. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  178. }
  179. sql_entity_id += iif(entity_type == 'PF', '', " AND " + tmp.sec_id_col[0] + " LIKE '" + tmp.prefix[0] + "%'");
  180. if(isFromMySQL == true) {
  181. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, price_date, end_date
  182. FROM " + tmp.table_name[0] + "
  183. WHERE isvalid = 1 " +
  184. sql_entity_id + "
  185. AND " + tmp.cumulative_nav_col[0] + " > 0
  186. AND updatetime >= '" + updatetime$STRING + "'
  187. ORDER BY " + tmp.sec_id_col[0];
  188. conn = connect_mysql()
  189. t = odbc::query(conn, s_query)
  190. conn.close()
  191. } else {
  192. tb_local = load_table_from_local("fundit", "mfdb.fund_latest_nav_performance")
  193. s_col = sqlCol("*")
  194. s_where = expr(<fund_id>, in, entity_ids.strReplace("'", "").split(","))
  195. t = sql(s_col, tb_local, s_where).eval()
  196. }
  197. return t
  198. }
  199. /*
  200. * 通用取给定日期之后(含此日期)的净值
  201. *
  202. *
  203. * Create: 202408 Joey
  204. * TODO: add isvalid and nav > 0 for local version
  205. *
  206. *
  207. * Example: get_nav_by_price_date('HF', "'HF000004KN','HF00018WXG'", 2024.05.01, true);
  208. * get_nav_by_price_date('MI', "'IN00000008','IN0000000M'", 2024.05.01, true);
  209. */
  210. def get_nav_by_price_date(entity_type, entity_ids, price_date, isFromMySQL) {
  211. s_entity_ids = ids_to_string(entity_ids);
  212. if(s_entity_ids == null || s_entity_ids == '') return null;
  213. tmp = get_nav_table_description(entity_type);
  214. if(isFromMySQL == true) {
  215. nav_table_name = tmp.table_name[0];
  216. 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
  217. FROM " + tmp.table_name[0] + "
  218. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  219. AND isvalid = 1
  220. AND " + tmp.cumulative_nav_col[0] + " > 0
  221. AND price_date >= '" + price_date$STRING + "'
  222. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  223. conn = connect_mysql();
  224. t = odbc::query(conn, s_query);
  225. conn.close();
  226. } else {
  227. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  228. s_col = sqlCol("*")
  229. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0]?
  230. s_where = [expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(",")), <price_date >= price_date>]
  231. t = sql(s_col, tb_local, s_where).eval()
  232. }
  233. return t
  234. }
  235. /*
  236. * 取指数因子点位
  237. *
  238. * get_index_nav_by_price_date("'IN00000008','FA00000WKG'", 2024.06.01)
  239. */
  240. def get_index_nav_by_price_date(index_ids, price_date) {
  241. s_query = "SELECT index_id, price_date, close AS cumulative_nav
  242. FROM mfdb.market_indexes
  243. WHERE index_id IN (" + index_ids + ")
  244. AND isvalid = 1
  245. AND close > 0
  246. AND price_date >= '" + price_date + "'
  247. UNION
  248. SELECT index_id AS index_id, price_date, index_value AS cumulative_nav
  249. FROM mfdb.indexes_ty_index
  250. WHERE index_id IN (" + index_ids + ")
  251. AND isvalid = 1
  252. AND index_value > 0
  253. AND price_date >= '" + price_date + "'
  254. UNION
  255. SELECT factor_id AS index_id, price_date, factor_value AS cumulative_nav
  256. FROM pfdb.cm_factor_value
  257. WHERE factor_id IN (" + index_ids + ")
  258. AND isvalid = 1
  259. AND factor_value > 0
  260. AND price_date >= '" + price_date + "'
  261. ORDER BY price_date"
  262. conn = connect_mysql()
  263. t = odbc::query(conn, s_query)
  264. conn.close()
  265. return t
  266. }
  267. /*
  268. * 取某时间后更新的各基金组合最早净值日期
  269. *
  270. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  271. * @param entity_ids <VECTOR|STRING>: NULL时取全量
  272. * @param update_time <DATETIME>: all updates after this time
  273. * @param isFromMySQL <BOOL>:
  274. *
  275. * Example: get_entity_list_by_nav_updatetime('MF', ['MF00003PW1', 'MF00003PW2'], 2024.09.26, true);
  276. * get_entity_list_by_nav_updatetime('HF', null, 2024.07.19T10:00:00, true);
  277. * get_entity_list_by_nav_updatetime('PF', '166002,166114', 2024.06.20, true);
  278. *
  279. */
  280. def get_entity_list_by_nav_updatetime(entity_type, entity_ids, updatetime, isFromMySQL) {
  281. tmp = get_nav_table_description(entity_type);
  282. s_entity_ids = ids_to_string(entity_ids);
  283. sql_entity_id = '';
  284. if(s_entity_ids != NULL) {
  285. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  286. }
  287. if(isFromMySQL == true) {
  288. nav_table_name = tmp.table_name[0];
  289. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
  290. FROM " + tmp.table_name[0] + "
  291. WHERE isvalid = 1 " +
  292. sql_entity_id + "
  293. AND " + tmp.cumulative_nav_col[0] + " > 0
  294. AND updatetime >= '" + updatetime$STRING + "'
  295. GROUP BY " + tmp.sec_id_col[0] + "
  296. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  297. conn = connect_mysql();
  298. t = odbc::query(conn, s_query);
  299. conn.close();
  300. } else {
  301. //TODO
  302. }
  303. return t
  304. }
  305. /*
  306. * 取基金证券从某日期后的所有净值及前值
  307. *
  308. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  309. * @param freq <STRING>: m, w, d
  310. * @param pre_nav_incld <INT>: 0- no pre_nav; 1- pre_nav only; 2- pre_nav + afters
  311. * @param json_query <JSON>: [{sec_id:xxx, price_date: yyyy-mm-dd}]
  312. *
  313. * 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)
  314. *
  315. */
  316. def get_nav_for_return_calculation(entity_type, freq, json_query, pre_nav_incld=2) {
  317. s_query = "CALL pfdb.sp_get_nav_for_return_cal('" + entity_type + "', '" + freq + "', " + pre_nav_incld + ", '" + json_query + "')";
  318. conn = connect_mysql();
  319. t = odbc::query(conn, s_query);
  320. conn.close();
  321. return t;
  322. }
  323. /*
  324. * 取基金经理/公司从某日期后的所有净值及前值
  325. *
  326. * @param entity_type <STRING>: PL, CO
  327. * @param pre_nav_incld <INT>: 0- no pre_nav; 1- pre_nav only; 2- pre_nav + afters
  328. * @param json_query <JSON>: [{entity_id:xxx, curve_type:1, strategy:0, end_date: yyyy-mm}]
  329. *
  330. * Example: get_mc_nav_for_return_calculation('PL', '[{"entity_id":"PL000000NS", "curve_type":"4", "strategy":"0", "end_date":"2024-07"}]', 2);
  331. * get_mc_nav_for_return_calculation('CO', '[{"entity_id":"CO00000017", "curve_type":"4", "strategy":"0", "end_date":"2024-07"}]', 1);
  332. *
  333. */
  334. def get_mc_nav_for_return_calculation(entity_type, json_query, pre_nav_incld=2) {
  335. s_query = "CALL pfdb.sp_get_mc_nav_for_return_cal('" + entity_type + "', " + pre_nav_incld + ", '" + json_query + "')";
  336. conn = connect_mysql();
  337. t = odbc::query(conn, s_query);
  338. conn.close();
  339. return t;
  340. }
  341. /*
  342. * 取主基准和BFI的历史月收益率
  343. *
  344. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表
  345. * @param end_day <DATE>: 收益的截止日期
  346. *
  347. * @return <TABLE>: benchmark_id, end_date, ret
  348. *
  349. */
  350. def get_benchmark_return(benchmarks, end_day) {
  351. s_index_ids = '';
  352. s_factor_ids = '';
  353. if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
  354. // 前缀为 IN 的 benchmark id
  355. t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';;
  356. s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
  357. // 前缀为 FA 的 benchmark id
  358. t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
  359. s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
  360. // 目前指数的月度业绩存在 fund_performance 表
  361. 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);
  362. // 而因子的月度业绩存在 cm_factor_performance 表
  363. 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);
  364. return t_bmk;
  365. }
  366. /*
  367. * 将十位标准字符型ID分门别类
  368. *
  369. * @param entity_ids <VECTOR>: vector of ids
  370. * @return <DICT>: entity_type - vector of ids
  371. *
  372. * NOTE: "IN"打头的ID无法判断是市场指数还是图译指数,所以 MI, FI 都会被返回
  373. *
  374. * Example: check_universe(['MF00003PW1','HF12345678','MF00003PW2','IN00000008']);
  375. */
  376. def check_universe (entity_ids) {
  377. v_id = array(STRING);
  378. dic = dict(STRING, ANY);
  379. if(entity_ids.isVoid() || entity_ids.size() == 0) return dic;
  380. for(r in get_entity_id_info()) {
  381. v_id = entity_ids[like(entity_ids, r.prefix + '%') && strlen(entity_ids)==10];
  382. if(v_id.size() > 0) {
  383. dic[r.entity_type] = v_id;
  384. }
  385. }
  386. return dic;
  387. }
  388. /*
  389. * 通用取收益数据
  390. *
  391. * @param entity_type <STRING>:
  392. * @param entity_ids <VECTOR>
  393. * @param freq <STRING>: m, w, d
  394. * @param start_day <DATE>
  395. * @param end_day <DATE>
  396. * @param isFromMySQL <BOOL>
  397. *
  398. * Example: get_entity_return('MF', ['MF00003PW1', 'MF00003PW2'], 'w', 2024.02.01, 2024.09.30, true);
  399. * get_entity_return(NULL, ['FA00000VML', 'FA00000VMM', 'FA00000VMN', 'FA00000VMO', 'IN0000007G'], 'w');
  400. * get_entity_return('PF', [166002], 'm');
  401. */
  402. def get_entity_return(entity_type, entity_ids, freq='m', start_day=1990.01.01, end_day=2099.12.31, isFromMySQL=true) {
  403. t_ret = table(1000:0, ['entity_id', 'effective_date', 'price_date', 'ret', 'nav'], [iif(entity_type=='PF', INT, SYMBOL), STRING, DATE, DOUBLE, DOUBLE]);
  404. d = dict(STRING, ANY);
  405. if(entity_type != null) d[entity_type] = entity_ids;
  406. else
  407. d = check_universe(entity_ids);
  408. for(univ in d.keys()) {
  409. if(freq == 'm') {
  410. t = SELECT entity_id, end_date$STRING AS effective_date, price_date, ret, nav
  411. FROM get_monthly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
  412. t_ret.tableInsert(t);
  413. } else if(freq == 'w') {
  414. t = SELECT entity_id, year_week$STRING AS effective_date, price_date, ret, nav
  415. FROM get_weekly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
  416. t_ret.tableInsert(t);
  417. } else { // 缺省是日收益
  418. s_json = table(d[univ] AS sec_id, take(start_day, d[univ].size()) AS price_date).toStdJson();
  419. // 取基金组合在包括各自的某净值日期的前值及之后的所有净值
  420. tb_nav = get_nav_for_return_calculation(univ, 'd', s_json);
  421. if(!tb_nav.isVoid() && tb_nav.size() > 0) {
  422. tb_nav.rename!('sec_id', 'entity_id').sortBy!(['entity_id', 'price_date'], [1, 1]);
  423. t = SELECT entity_id, price_date$STRING AS effective_date, price_date, cumulative_nav.ratios()-1 AS ret, cumulative_nav AS nav
  424. FROM tb_nav
  425. WHERE price_date <= end_day CONTEXT BY entity_id;
  426. t_ret.tableInsert(t);
  427. }
  428. }
  429. }
  430. return t_ret;
  431. }
  432. /*
  433. * 取持有基金净值更新的组合列表
  434. *
  435. * TODO: 需要跑3分钟,待优化
  436. *
  437. * Example: get_portfolio_list_by_fund_nav_updatetime([166002,166114], 2024.10.28, true);
  438. */
  439. def get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, isFromMySQL) {
  440. t = null;
  441. s_entity_ids = ids_to_string(portfolio_ids);
  442. if(isFromMySQL == true) {
  443. s_query = "CALL pfdb.sp_get_portfolios_to_cal_nav(" + iif(s_entity_ids.isNull(), 'NULL', "'" + s_entity_ids + "'") + ",'" + updatetime + "')";
  444. conn = connect_mysql();
  445. t = odbc::query(conn, s_query);
  446. conn.close();
  447. }
  448. return t
  449. }
  450. /*
  451. * 根据指数净值更新日期,取受影响的BFI因子列表
  452. *
  453. * Example: get_bfi_factor_list_by_index_nav_updatetime(['FA00000VMH','FA00000VMK'], 2024.11.08, true);
  454. * get_bfi_factor_list_by_index_nav_updatetime(NULL, NULL, true);
  455. */
  456. def get_bfi_factor_list_by_index_nav_updatetime(factor_ids, updatetime, isFromMySQL) {
  457. t = null;
  458. s_entity_ids = ids_to_string(factor_ids);
  459. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.factor_id IN (" + s_entity_ids + ")");
  460. sql_updatetime = iif(updatetime == NULL, '', " AND mi.updatetime > '" + updatetime + "'");
  461. if(isFromMySQL == true) {
  462. 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
  463. FROM pfdb.cm_factor_information fi
  464. INNER JOIN pfdb.cm_factor_index_map map ON fi.factor_id = map.factor_id
  465. INNER JOIN mfdb.market_indexes mi ON map.index_id = mi.index_id
  466. WHERE fi.isvalid = 1
  467. AND fi.factor_type = 5" + // 5: bfi factor
  468. sql_entity_id + "
  469. AND map.isvalid = 1
  470. AND mi.isvalid = 1
  471. AND mi.close > 0 " +
  472. sql_updatetime + "
  473. GROUP BY fi.factor_id";
  474. conn = connect_mysql();
  475. t = odbc::query(conn, s_query);
  476. conn.close();
  477. }
  478. return t
  479. }
  480. /*
  481. * 取固定权重的组合持仓,包括BFI因子,日再平衡组合等
  482. *
  483. * Example: get_fixed_weight_portfolio_holding('PF', [145589, 170904]);
  484. * get_fixed_weight_portfolio_holding('FA', ['FA00000VMH', 'FA00000VMI']);
  485. */
  486. def get_fixed_weight_portfolio_holding(entity_type, entity_ids) {
  487. t = null;
  488. s_query = '';
  489. s_entity_ids = ids_to_string(entity_ids);
  490. if(entity_type == 'FA') {
  491. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND factor_id IN (" + s_entity_ids + ")");
  492. s_query = "SELECT factor_id AS entity_id, holding_date, index_id AS sec_id, weight, 2 AS entity_type
  493. FROM pfdb.cm_factor_index_map
  494. WHERE isvalid = 1" +
  495. sql_entity_id;
  496. } else if(entity_type == 'PF') {
  497. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND portfolio_id IN (" + s_entity_ids + ")");
  498. s_query = "SELECT portfolio_id AS entity_id, effective_date AS holding_date, entity_id AS sec_id, weight, entity_type
  499. FROM pfdb.pf_portfolio_rebalance_weights
  500. WHERE isvalid = 1" +
  501. sql_entity_id;
  502. }
  503. if( s_query == '') return t;
  504. conn = connect_mysql();
  505. t = odbc::query(conn, s_query);
  506. conn.close();
  507. return t;
  508. }
  509. /*
  510. * 通用取Json中指定的证券当日净值
  511. *
  512. *
  513. * Example: get_entity_nav_by_date('PF', '[{"entity_id": 166002,"price_date": "2024.10.25"},{"entity_id": 166114,"price_date": "2024.03.13"}]', true);
  514. */
  515. def get_entity_nav_by_date(entity_type, s_json, isFromMySQL=true) {
  516. t = null;
  517. desc = get_nav_table_description(entity_type)[0];
  518. if(isFromMySQL == true) {
  519. s_query = "SELECT t.entity_id, nav.price_date, nav." + desc.cumulative_nav_col + " AS cumulative_nav, nav." + desc.nav_col + " AS nav
  520. FROM JSON_TABLE ( '" + s_json + "', '$[*]'
  521. COLUMNS ( entity_id " + iif(entity_type=='PF', 'INT', 'VARCHAR(10)') + " PATH '$.entity_id',
  522. price_date DATE PATH '$.price_date' ) ) t
  523. LEFT JOIN " + desc.table_name + " nav ON t.entity_id = nav." + desc.sec_id_col + " AND t.price_date = nav.price_date
  524. WHERE nav.isvalid = 1
  525. AND nav." + desc.cumulative_nav_col + " > 0
  526. ORDER BY t.entity_id;";
  527. conn = connect_mysql();
  528. t = odbc::query(conn, s_query);
  529. conn.close();
  530. }
  531. return t;
  532. }
  533. /*
  534. * 通用取Json中指定日期后(含当日)的证券收益
  535. *
  536. * @param freq <STRING>: m, w
  537. *
  538. *
  539. * 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);
  540. * get_entity_ret_by_date('MF', '[{"entity_id": "MF00003PW1","price_date": "2023.12.28"}]', 'w', true);
  541. */
  542. def get_entity_ret_by_date(entity_type, s_json, freq, isFromMySQL=true) {
  543. t = null;
  544. if(isFromMySQL == true) {
  545. s_query = "CALL pfdb.sp_get_return_by_json('" + entity_type + "', '" + freq + "', '" + s_json + "');";
  546. conn = connect_mysql();
  547. t = odbc::query(conn, s_query);
  548. conn.close();
  549. }
  550. return t;
  551. }
  552. /*
  553. * 取月度指标表
  554. *
  555. * @param table_name <STRING>: 指标表名
  556. * @param end_date <MONTH>
  557. * @param isFromMySQL <BOOL>
  558. *
  559. * Example: get_monthly_indicator_data('mfdb.fund_performance', 2024.09M, true);
  560. */
  561. def get_monthly_indicator_data(table_name, end_date, isFromMySQL=true) {
  562. t = null;
  563. s_end_date_sql = iif(end_date.isNull(), '', " AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" );
  564. if(isFromMySQL == true) {
  565. s_query = "SELECT * FROM " + table_name + " WHERE isvalid = 1 " + s_end_date_sql;
  566. conn = connect_mysql();
  567. t = odbc::query(conn, s_query);
  568. conn.close();
  569. }
  570. return t;
  571. }
  572. /*
  573. * 取 pf_fund_indicator_ranking 表
  574. *
  575. *
  576. * Example: get_fund_indicator_ranking("'MF00003PW1'", 2024.09M, 102, true);
  577. * get_fund_indicator_ranking(NULL, 2023.09M, [1, 3], true);
  578. */
  579. def get_fund_indicator_ranking(fund_ids, end_date, strategy, isFromMySQL=true) {
  580. t = null;
  581. s_entity_ids = ids_to_string(fund_ids);
  582. sql_entity_id = '';
  583. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  584. s_strategy_ids = ids_to_string(strategy);
  585. sql_strategy_id = '';
  586. if(s_strategy_ids != NULL) sql_strategy_id = " AND strategy IN (" + s_strategy_ids + ")";
  587. if(isFromMySQL == true) {
  588. s_query = "SELECT *
  589. FROM pfdb.pf_fund_indicator_ranking
  590. WHERE isvalid = 1
  591. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  592. sql_strategy_id +
  593. sql_entity_id;
  594. conn = connect_mysql();
  595. t = odbc::query(conn, s_query);
  596. conn.close();
  597. }
  598. return t;
  599. }
  600. /*
  601. * 取 pf_fund_indicator_substrategy_ranking 表
  602. *
  603. *
  604. * Example: get_fund_indicator_substrategy_ranking("'MF00003PW1'", 2024.09M, 23, true);
  605. * get_fund_indicator_substrategy_ranking(NULL, 2023.09M, [22, 23], true);
  606. */
  607. def get_fund_indicator_substrategy_ranking(fund_ids, end_date, substrategy, 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(substrategy);
  613. sql_strategy_id = '';
  614. if(s_strategy_ids != NULL) sql_strategy_id = " AND substrategy IN (" + s_strategy_ids + ")";
  615. if(isFromMySQL == true) {
  616. s_query = "SELECT *
  617. FROM pfdb.pf_fund_indicator_substrategy_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_bfi_bm_indicator_ranking 表
  630. *
  631. *
  632. * Example: get_fund_bfi_bm_indicator_ranking("'MF00003PW1'", 2024.09M, NULL, true);
  633. *
  634. */
  635. def get_fund_bfi_bm_indicator_ranking(fund_ids, end_date, factor_ids, 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_factor_ids = ids_to_string(factor_ids);
  641. sql_factor_id = '';
  642. if(s_factor_ids != NULL) sql_factor_id = " AND factor_id IN (" + s_factor_ids + ")";
  643. if(isFromMySQL == true) {
  644. s_query = "SELECT *
  645. FROM pfdb.pf_fund_bfi_bm_indicator_ranking
  646. WHERE isvalid = 1
  647. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  648. sql_factor_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. * 取算相关性所需要的指数/因子ID
  658. *
  659. * NOTE: 与 Java 使用的逻辑相同, 在cm_class_asset_index中有当月performance的指数加上 CSI5 和 Cap3Style 的 factor
  660. * 唯一的不同是将历史已经停止维护的老指数筛掉(必须有当前月份的指数收益)
  661. *
  662. * Example: get_correlation_index_list();
  663. */
  664. def get_correlation_index_list() {
  665. s_query = "SELECT a.fund_id AS entity_id
  666. FROM mfdb.fund_performance AS a
  667. LEFT JOIN pfdb.cm_class_asset_index AS b ON a.fund_id = b.index_id
  668. WHERE a.end_date = LEFT(CURRENT_DATE, 7)
  669. AND a.fund_id LIKE 'IN%'
  670. AND b.isvalid = 1
  671. ORDER BY a.fund_id ASC";
  672. conn = connect_mysql();
  673. t = odbc::query(conn, s_query);
  674. conn.close();
  675. INSERT INTO t VALUES (['FA00000VML','FA00000VMM','FA00000VMN','FA00000VMO','FA00000WKG','FA00000WKH']);
  676. return t;
  677. }
  678. /*
  679. * 取BFI所需要的因子ID
  680. *
  681. * TODO: 大盘风格、小盘风格 (FA00000WKG, FA00000WKH) 没有category_group_id & order_id, 在哪里用?
  682. */
  683. def get_bfi_index_list() {
  684. s_query = "SELECT factor_id, factor_name, category_group_id, maximum_num, order_id
  685. FROM pfdb.cm_factor_information
  686. WHERE isvalid = 1
  687. AND ( factor_type = 5 OR factor_id IN ('FA00000SMB', 'FA00000VMG') )
  688. ORDER BY order_id ASC";
  689. conn = connect_mysql();
  690. t = odbc::query(conn, s_query);
  691. conn.close();
  692. return t;
  693. }
  694. /*
  695. * 取基金分类平均的周收益
  696. *
  697. * TODO: 需要跑1分钟,待优化
  698. *
  699. * Example: get_category_avg_weekly_return('bfi', 2024.09.28, 5, 30, true);
  700. * get_category_avg_weekly_return('strategy', 2014.06.27, 5, 30, true);
  701. */
  702. def get_category_avg_weekly_return(category_type, begin_day, trim_pct=5, min_cnt=30, isFromMySQL=true) {
  703. t = null;
  704. if(isFromMySQL == true) {
  705. s_query = "CALL pfdb.sp_get_category_avg_weekly_return('" + category_type + "', '" + begin_day + "', " + trim_pct + ", " + min_cnt + ")";
  706. conn = connect_mysql();
  707. t = odbc::query(conn, s_query);
  708. conn.close();
  709. }
  710. return t
  711. }
  712. /*
  713. * 取基金经理或公司月收益
  714. *
  715. *
  716. * Example: get_mc_monthly_return('manager', '[{"entity_id": "PL000000NS","end_date": "2024-07"},{"entity_id": "PL00000ICF","end_date": "2023-12"}]', 0, 1, true);
  717. * get_mc_monthly_return('company', '[{"entity_id": "CO00000017","end_date": "2024-07"},{"entity_id": "CO0001003W","end_date": "2023-06"}]', 0, 1, true);
  718. */
  719. def get_mc_monthly_return(entity_type, json_query, trim_pct=0, min_cnt=1, isFromMySQL=true) {
  720. t = null;
  721. if(isFromMySQL == true) {
  722. s_query = "CALL pfdb.sp_get_mc_avg_monthly_return('" + entity_type + "', '" + json_query + "', " + trim_pct + ", " + min_cnt + ")";
  723. conn = connect_mysql();
  724. t = odbc::query(conn, s_query);
  725. conn.close();
  726. }
  727. return t
  728. }
  729. /*
  730. * 取有基金月收益更新的基金公司列表
  731. *
  732. * NOTE: 月收益表 isvalid = 0 的记录也会被返回
  733. *
  734. * Example: get_company_list_by_fund_updatetime(2024.11.14 11:10:00, true);
  735. *
  736. */
  737. def get_company_list_by_fund_updatetime(updatetime, isFromMySQL=true) {
  738. if(isFromMySQL == true) {
  739. s_query = "SELECT ci.company_id, MIN(fp.end_date) AS end_date
  740. FROM mfdb.company_information ci
  741. INNER JOIN mfdb.fund_information fi ON ci.company_id = fi.advisor_id
  742. INNER JOIN mfdb.fund_performance fp ON fi.fund_id = fp.fund_id
  743. WHERE ci.isvalid = 1
  744. AND fi.isvalid = 1
  745. AND fp.updatetime >= '" + updatetime + "'
  746. GROUP BY ci.company_id;";
  747. conn = connect_mysql()
  748. t = odbc::query(conn, s_query)
  749. conn.close()
  750. } else {
  751. }
  752. return t
  753. }
  754. /*
  755. * 取有基金月收益更新的基金经理列表
  756. *
  757. * NOTE: 月收益表 isvalid = 0 的记录也会被返回
  758. *
  759. * Example: get_manager_list_by_fund_updatetime(2024.11.01, true);
  760. *
  761. */
  762. def get_manager_list_by_fund_updatetime(updatetime, isFromMySQL=true) {
  763. if(isFromMySQL == true) {
  764. s_query = "SELECT mi.personnel_id AS manager_id, MIN(fp.end_date) AS end_date
  765. FROM mfdb.personnel_information mi
  766. INNER JOIN mfdb.fund_manager_mapping fmp ON mi.personnel_id = fmp.fund_manager_id
  767. INNER JOIN mfdb.fund_information fi ON fmp.fund_id = fi.fund_id
  768. INNER JOIN mfdb.fund_performance fp ON fi.fund_id = fp.fund_id
  769. WHERE mi.isvalid = 1
  770. AND fmp.isvalid = 1
  771. AND fi.isvalid = 1
  772. AND fp.updatetime >= '" + updatetime + "'
  773. AND fp.end_date <= DATE_FORMAT(IFNULL(fmp.management_end_date, CURRENT_DATE), '%y-%m')
  774. GROUP BY mi.personnel_id;";
  775. conn = connect_mysql()
  776. t = odbc::query(conn, s_query)
  777. conn.close()
  778. } else {
  779. }
  780. return t
  781. }