performanceDataPuller.dos 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. * Example:
  26. */
  27. def get_entity_list_by_weekly_return_updatetime(entity_type, entity_ids, updatetime, isFromMySQL=true) {
  28. tmp = get_performance_weekly_table_description(entity_type);
  29. s_entity_ids = ids_to_string(entity_ids);
  30. sql_entity_id = '';
  31. if(s_entity_ids != NULL) {
  32. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  33. }
  34. if(isFromMySQL == true) {
  35. nav_table_name = tmp.table_name[0];
  36. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
  37. FROM " + tmp.table_name[0] + "
  38. WHERE isvalid = 1 " +
  39. sql_entity_id + "
  40. AND " + tmp.cumulative_nav_col[0] + " > 0
  41. AND updatetime >= '" + updatetime$STRING + "'
  42. GROUP BY " + tmp.sec_id_col[0] + "
  43. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  44. conn = connect_mysql()
  45. t = odbc::query(conn, s_query)
  46. conn.close()
  47. } else {
  48. }
  49. return t
  50. }
  51. /*
  52. * 通用取周净值
  53. *
  54. * Example: get_weekly_ret('MF', ['MF00003PW1'], 2024.06.01, today(), true);
  55. * get_weekly_ret('PF', [166002], 2024.06.01, today(), true);
  56. */
  57. def get_weekly_ret(entity_type, entity_ids, start_date=1990.01.01, end_date=2099.12.31, isFromMySQL=true){
  58. s_entity_ids = ids_to_string(entity_ids);
  59. if(s_entity_ids == null || s_entity_ids == '') return null;
  60. tmp = get_performance_weekly_table_description(entity_type);
  61. if(isFromMySQL == true) {
  62. 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
  63. FROM " + tmp.table_name[0] + "
  64. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  65. AND isvalid = 1
  66. AND price_date BETWEEN '" + start_date + "' AND '" + end_date + "'
  67. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  68. conn = connect_mysql()
  69. t = odbc::query(conn, s_query)
  70. conn.close()
  71. } else {
  72. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  73. }
  74. return t
  75. }
  76. /*
  77. * 通用取周收益
  78. *
  79. */
  80. def get_entity_weekly_rets(entity_type, entity_info) {
  81. rets = null;
  82. if(entity_info.isVoid() || entity_info.size() == 0) return rets;
  83. very_old_date = '1990.01.01';
  84. // 简单起见,取整个数据集的最新日期(month-end production时取上月最后一天即可
  85. end_day = entity_info.price_date.max();
  86. return get_weekly_ret(entity_type, entity_info.entity_id, very_old_date, end_day, true);
  87. }
  88. /*
  89. * 通用取月收益
  90. *
  91. * @param entity_type <STRING>:
  92. * @param entity_ids <VECTOR|STRING>:
  93. * @param start_date <DATE>:
  94. * @param end_date <DATE>:
  95. * @param isFromMySQL <BOOL>:
  96. *
  97. *
  98. * Example: get_monthly_ret('HF', ['HF000004KN','HF000103EU','HF00018WXG'], 2000.01.01, 2024.03.01, true);
  99. * get_monthly_ret('PF', [166002], 2000.01.01, 2024.03.01, true);
  100. */
  101. def get_monthly_ret(entity_type, entity_ids, start_date, end_date, isFromMySQL) {
  102. s_entity_ids = ids_to_string(entity_ids);
  103. if(s_entity_ids == null || s_entity_ids == '') return null;
  104. tmp = get_performance_table_description(entity_type);
  105. yyyymm_start = start_date.temporalFormat("yyyy-MM")
  106. yyyymm_end = end_date.temporalFormat("yyyy-MM")
  107. if(isFromMySQL == true) {
  108. 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
  109. FROM " + tmp.table_name[0] + "
  110. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  111. AND isvalid = 1
  112. AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
  113. ORDER BY " + tmp.sec_id_col[0] + ", end_date";
  114. conn = connect_mysql()
  115. t = odbc::query(conn, s_query)
  116. conn.close()
  117. } else {
  118. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  119. 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"))
  120. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0], then rename to "entity_id"?
  121. s_where = expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(","))
  122. t = sql(s_col, tb_local, s_where).eval()
  123. }
  124. return t
  125. }
  126. /*
  127. * 取无风险月度利率
  128. *
  129. * get_risk_free_rate(1990.01.01, today())
  130. */
  131. def get_risk_free_rate(start_date, end_date) {
  132. return get_monthly_ret('MI', "'IN0000000M'", start_date, end_date, true);
  133. }
  134. /*
  135. * 取基金最新收益及净值
  136. *
  137. * get_fund_latest_nav_performance("'HF000004KN','HF00018WXG'")
  138. */
  139. def get_fund_latest_nav_performance(fund_ids, isFromMySQL) {
  140. if(isFromMySQL == true) {
  141. s_query = "SELECT *
  142. FROM mfdb.fund_latest_nav_performance
  143. WHERE fund_id IN (" + fund_ids + ")
  144. AND isvalid = 1
  145. ORDER BY fund_id"
  146. conn = connect_mysql()
  147. t = odbc::query(conn, s_query)
  148. conn.close()
  149. } else {
  150. tb_local = load_table_from_local("fundit", "mfdb.fund_latest_nav_performance")
  151. s_col = sqlCol("*")
  152. s_where = expr(<fund_id>, in, fund_ids.strReplace("'", "").split(","))
  153. t = sql(s_col, tb_local, s_where).eval()
  154. }
  155. return t
  156. }
  157. /*
  158. * 通用取净值
  159. *
  160. *
  161. * Create: 202408 Joey
  162. * TODO: add isvalid and nav > 0 for local version
  163. *
  164. *
  165. * Example: get_nav_by_price_date('HF', "'HF000004KN','HF00018WXG'", 2024.05.01, true);
  166. * get_nav_by_price_date('MI', "'IN00000008','IN0000000M'", 2024.05.01, true);
  167. */
  168. def get_nav_by_price_date(entity_type, entity_ids, price_date, isFromMySQL) {
  169. s_entity_ids = ids_to_string(entity_ids);
  170. if(s_entity_ids == null || s_entity_ids == '') return null;
  171. tmp = get_nav_table_description(entity_type);
  172. if(isFromMySQL == true) {
  173. nav_table_name = tmp.table_name[0];
  174. 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
  175. FROM " + tmp.table_name[0] + "
  176. WHERE " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")
  177. AND isvalid = 1
  178. AND " + tmp.cumulative_nav_col[0] + " > 0
  179. AND price_date >= '" + price_date$STRING + "'
  180. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  181. conn = connect_mysql();
  182. t = odbc::query(conn, s_query);
  183. conn.close();
  184. } else {
  185. tb_local = load_table_from_local("fundit", tmp.table_name[0])
  186. s_col = sqlCol("*")
  187. // TODO: how to make the "fund_id" dynamicly decided by tmp.sec_id_col[0]?
  188. s_where = [expr(<fund_id>, in, s_entity_ids.strReplace("'", "").split(",")), <price_date >= price_date>]
  189. t = sql(s_col, tb_local, s_where).eval()
  190. }
  191. return t
  192. }
  193. /*
  194. * 取指数因子点位
  195. *
  196. * get_index_nav_by_price_date("'IN00000008','FA00000WKG'", 2024.06.01)
  197. */
  198. def get_index_nav_by_price_date(index_ids, price_date) {
  199. s_query = "SELECT index_id, price_date, close AS cumulative_nav
  200. FROM mfdb.market_indexes
  201. WHERE index_id IN (" + index_ids + ")
  202. AND isvalid = 1
  203. AND close > 0
  204. AND price_date >= '" + price_date + "'
  205. UNION
  206. SELECT index_id AS index_id, price_date, index_value AS cumulative_nav
  207. FROM mfdb.indexes_ty_index
  208. WHERE index_id IN (" + index_ids + ")
  209. AND isvalid = 1
  210. AND index_value > 0
  211. AND price_date >= '" + price_date + "'
  212. UNION
  213. SELECT factor_id AS index_id, price_date, factor_value AS cumulative_nav
  214. FROM pfdb.cm_factor_value
  215. WHERE factor_id IN (" + index_ids + ")
  216. AND isvalid = 1
  217. AND factor_value > 0
  218. AND price_date >= '" + price_date + "'
  219. ORDER BY price_date"
  220. conn = connect_mysql()
  221. t = odbc::query(conn, s_query)
  222. conn.close()
  223. return t
  224. }
  225. /*
  226. * 取某时间后更新的各基金组合最早净值日期
  227. *
  228. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  229. * @param entity_ids <VECTOR|STRING>: NULL时取全量
  230. * @param update_time <DATETIME>: all updates after this time
  231. * @param isFromMySQL <BOOL>:
  232. *
  233. * Example: get_entity_list_by_nav_updatetime('MF', ['MF00003PW1', 'MF00003PW2'], 2024.09.26, true);
  234. * get_entity_list_by_nav_updatetime('HF', null, 2024.07.19T10:00:00, true);
  235. * get_entity_list_by_nav_updatetime('PF', '166002,166114', 2024.06.20, true);
  236. *
  237. */
  238. def get_entity_list_by_nav_updatetime(entity_type, entity_ids, updatetime, isFromMySQL) {
  239. tmp = get_nav_table_description(entity_type);
  240. s_entity_ids = ids_to_string(entity_ids);
  241. sql_entity_id = '';
  242. if(s_entity_ids != NULL) {
  243. sql_entity_id = " AND " + tmp.sec_id_col[0] + " IN (" + s_entity_ids + ")";
  244. }
  245. if(isFromMySQL == true) {
  246. nav_table_name = tmp.table_name[0];
  247. s_query = "SELECT " + tmp.sec_id_col[0] + " AS entity_id, MIN(price_date) AS price_date
  248. FROM " + tmp.table_name[0] + "
  249. WHERE isvalid = 1 " +
  250. sql_entity_id + "
  251. AND " + tmp.cumulative_nav_col[0] + " > 0
  252. AND updatetime >= '" + updatetime$STRING + "'
  253. GROUP BY " + tmp.sec_id_col[0] + "
  254. ORDER BY " + tmp.sec_id_col[0] + ", price_date";
  255. conn = connect_mysql();
  256. t = odbc::query(conn, s_query);
  257. conn.close();
  258. } else {
  259. //TODO
  260. }
  261. return t
  262. }
  263. /*
  264. * 取私募基金用于月末 fund_performance 表更新的净值
  265. *
  266. * @param fund_ids: 逗号分隔的ID字符串, 每个ID都有''
  267. * @param month_end: 月末日期字符串 YYYY-MM
  268. *
  269. *
  270. */
  271. def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
  272. s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
  273. conn = connect_mysql()
  274. t = odbc::query(conn, s_query)
  275. conn.close()
  276. return t
  277. }
  278. /*
  279. * 取基金证券从某日期后的所有净值及前值
  280. *
  281. * @param entity_type <STRING>: MF, HF, EQ, CF, MI, TI, CI, FA, PF
  282. * @param freq <STRING>: m, w, d
  283. * @param json_query <JSON>: [{sec_id:xxx, price_date: yyyy-mm-dd}]
  284. *
  285. */
  286. def get_nav_for_return_calculation(entity_type, freq, json_query) {
  287. s_query = "CALL pfdb.sp_get_nav_after_date('" + entity_type + "', '" + freq + "', '" + json_query + "')";
  288. conn = connect_mysql();
  289. t = odbc::query(conn, s_query);
  290. conn.close();
  291. return t;
  292. }
  293. /*
  294. * 取主基准和BFI的历史月收益率
  295. *
  296. * @param benchmarks <TABLE>: entity-benchmark 的对应关系表
  297. * @param end_day <DATE>: 收益的截止日期
  298. *
  299. * @return <TABLE>: benchmark_id, end_date, ret
  300. *
  301. */
  302. def get_benchmark_return(benchmarks, end_day) {
  303. s_index_ids = '';
  304. s_factor_ids = '';
  305. if(benchmarks.isVoid() || benchmarks.size() == 0) { return null; }
  306. // 前缀为 IN 的 benchmark id
  307. t_index_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'IN%';
  308. s_index_ids = iif(isVoid(t_index_id), "", "'" + t_index_id.benchmark_id.concat("','") + "'");
  309. // 前缀为 FA 的 benchmark id
  310. t_factor_id = SELECT DISTINCT benchmark_id FROM benchmarks WHERE benchmark_id LIKE 'FA%';
  311. s_factor_ids = iif(isVoid(t_factor_id), "", "'" + t_factor_id.benchmark_id.concat("','") + "'");
  312. // 目前指数的月度业绩存在 fund_performance 表
  313. 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);
  314. // 而因子的月度业绩存在 cm_factor_performance 表
  315. 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);
  316. return t_bmk;
  317. }
  318. /*
  319. * 将十位标准字符型ID分门别类
  320. *
  321. * @param entity_ids <VECTOR>: vector of ids
  322. * @return <DICT>: entity_type - vector of ids
  323. *
  324. * NOTE: "IN"打头的ID无法判断是市场指数还是图译指数,所以 MI, FI 都会被返回
  325. *
  326. * Example: check_universe(['MF00003PW1','HF12345678','MF00003PW2','IN00000008']);
  327. */
  328. def check_universe (entity_ids) {
  329. v_id = array(STRING);
  330. dic = dict(STRING, ANY);
  331. if(entity_ids.isVoid() || entity_ids.size() == 0) return dic;
  332. for(r in get_entity_id_info()) {
  333. v_id = entity_ids[like(entity_ids, r.prefix + '%') && strlen(entity_ids)==10];
  334. if(v_id.size() > 0) {
  335. dic[r.entity_type] = v_id;
  336. }
  337. }
  338. return dic;
  339. }
  340. /*
  341. * 通用取收益数据
  342. *
  343. * @param entity_type <STRING>:
  344. * @param entity_ids <VECTOR>
  345. * @param freq <STRING>: m, w, d
  346. * @param start_day <DATE>
  347. * @param end_day <DATE>
  348. * @param isFromMySQL <BOOL>
  349. *
  350. * Example: get_entity_return('MF', ['MF00003PW1', 'MF00003PW2'], 'w', 2024.02.01, 2024.09.30, true);
  351. * get_entity_return(NULL, ['FA00000VML', 'FA00000VMM', 'FA00000VMN', 'FA00000VMO', 'IN0000007G'], 'w');
  352. * get_entity_return('PF', [166002], 'm');
  353. */
  354. def get_entity_return(entity_type, entity_ids, freq='m', start_day=1990.01.01, end_day=2099.12.31, isFromMySQL=true) {
  355. t_ret = table(1000:0, ['entity_id', 'effective_date', 'price_date', 'ret', 'nav'], [iif(entity_type=='PF', INT, SYMBOL), STRING, DATE, DOUBLE, DOUBLE]);
  356. d = dict(STRING, ANY);
  357. if(entity_type != null) d[entity_type] = entity_ids;
  358. else
  359. d = check_universe(entity_ids);
  360. for(univ in d.keys()) {
  361. if(freq == 'm') {
  362. t = SELECT entity_id, end_date$STRING AS effective_date, price_date, ret, nav
  363. FROM get_monthly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
  364. t_ret.tableInsert(t);
  365. } else if(freq == 'w') {
  366. t = SELECT entity_id, year_week$STRING AS effective_date, price_date, ret, nav
  367. FROM get_weekly_ret(univ, d[univ], start_day, end_day, isFromMySQL);
  368. t_ret.tableInsert(t);
  369. } else { // 缺省是日收益
  370. s_json = table(d[univ] AS sec_id, take(start_day, d[univ].size()) AS price_date).toStdJson();
  371. // 取基金组合在包括各自的某净值日期的前值及之后的所有净值
  372. tb_nav = get_nav_for_return_calculation(univ, 'd', s_json);
  373. if(!tb_nav.isVoid() && tb_nav.size() > 0) {
  374. tb_nav.rename!('sec_id', 'entity_id').sortBy!(['entity_id', 'price_date'], [1, 1]);
  375. t = SELECT entity_id, price_date$STRING AS effective_date, price_date, cumulative_nav.ratios()-1 AS ret, cumulative_nav AS nav
  376. FROM tb_nav
  377. WHERE price_date <= end_day CONTEXT BY entity_id;
  378. t_ret.tableInsert(t);
  379. }
  380. }
  381. }
  382. return t_ret;
  383. }
  384. /*
  385. * 取持有基金净值更新的组合列表
  386. *
  387. * TODO: 需要跑3分钟,待优化
  388. *
  389. * Example: get_portfolio_list_by_fund_nav_updatetime([166002,166114], 2024.10.28, true);
  390. */
  391. def get_portfolio_list_by_fund_nav_updatetime(portfolio_ids, updatetime, isFromMySQL) {
  392. t = null;
  393. s_entity_ids = ids_to_string(portfolio_ids);
  394. if(isFromMySQL == true) {
  395. s_query = "CALL pfdb.sp_get_portfolios_to_cal_nav(" + iif(s_entity_ids.isNull(), 'NULL', "'" + s_entity_ids + "'") + ",'" + updatetime + "')";
  396. conn = connect_mysql();
  397. t = odbc::query(conn, s_query);
  398. conn.close();
  399. }
  400. return t
  401. }
  402. /*
  403. * 根据指数净值更新日期,取受影响的BFI因子列表
  404. *
  405. * Example: get_bfi_factor_list_by_index_nav_updatetime(['FA00000VMH','FA00000VMK'], 2024.11.08, true);
  406. * get_bfi_factor_list_by_index_nav_updatetime(NULL, NULL, true);
  407. */
  408. def get_bfi_factor_list_by_index_nav_updatetime(factor_ids, updatetime, isFromMySQL) {
  409. t = null;
  410. s_entity_ids = ids_to_string(factor_ids);
  411. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND fi.factor_id IN (" + s_entity_ids + ")");
  412. sql_updatetime = iif(updatetime == NULL, '', " AND mi.updatetime > '" + updatetime + "'");
  413. if(isFromMySQL == true) {
  414. 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
  415. FROM pfdb.cm_factor_information fi
  416. INNER JOIN pfdb.cm_factor_index_map map ON fi.factor_id = map.factor_id
  417. INNER JOIN mfdb.market_indexes mi ON map.index_id = mi.index_id
  418. WHERE fi.isvalid = 1
  419. AND fi.factor_type = 5" + // 5: bfi factor
  420. sql_entity_id + "
  421. AND map.isvalid = 1
  422. AND mi.isvalid = 1
  423. AND mi.close > 0 " +
  424. sql_updatetime + "
  425. GROUP BY fi.factor_id";
  426. conn = connect_mysql();
  427. t = odbc::query(conn, s_query);
  428. conn.close();
  429. }
  430. return t
  431. }
  432. /*
  433. * 取固定权重的组合持仓,包括BFI因子,日再平衡组合等
  434. *
  435. * Example: get_fixed_weight_portfolio_holding('PF', [145589, 170904]);
  436. * get_fixed_weight_portfolio_holding('FA', ['FA00000VMH', 'FA00000VMI']);
  437. */
  438. def get_fixed_weight_portfolio_holding(entity_type, entity_ids) {
  439. t = null;
  440. s_query = '';
  441. s_entity_ids = ids_to_string(entity_ids);
  442. if(entity_type == 'FA') {
  443. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND factor_id IN (" + s_entity_ids + ")");
  444. s_query = "SELECT factor_id AS entity_id, holding_date, index_id AS sec_id, weight, 2 AS entity_type
  445. FROM pfdb.cm_factor_index_map
  446. WHERE isvalid = 1" +
  447. sql_entity_id;
  448. } else if(entity_type == 'PF') {
  449. sql_entity_id = iif(s_entity_ids == NULL || s_entity_ids == '', '', " AND portfolio_id IN (" + s_entity_ids + ")");
  450. s_query = "SELECT portfolio_id AS entity_id, effective_date AS holding_date, entity_id AS sec_id, weight, entity_type
  451. FROM pfdb.pf_portfolio_rebalance_weights
  452. WHERE isvalid = 1" +
  453. sql_entity_id;
  454. }
  455. if( s_query == '') return t;
  456. conn = connect_mysql();
  457. t = odbc::query(conn, s_query);
  458. conn.close();
  459. return t;
  460. }
  461. /*
  462. * 通用取Json中指定的证券当日净值
  463. *
  464. *
  465. * Example: get_entity_nav_by_date('PF', '[{"entity_id": 166002,"price_date": "2024.10.25"},{"entity_id": 166114,"price_date": "2024.03.13"}]', true);
  466. */
  467. def get_entity_nav_by_date(entity_type, s_json, isFromMySQL=true) {
  468. t = null;
  469. desc = get_nav_table_description(entity_type)[0];
  470. if(isFromMySQL == true) {
  471. s_query = "SELECT t.entity_id, t.price_date, nav." + desc.nav_col + " AS cumulative_nav
  472. FROM JSON_TABLE ( '" + s_json + "', '$[*]'
  473. COLUMNS ( entity_id " + iif(entity_type=='PF', 'INT', 'VARCHAR(10)') + " PATH '$.entity_id',
  474. price_date DATE PATH '$.price_date' ) ) t
  475. LEFT JOIN " + desc.table_name + " nav ON t.entity_id = nav." + desc.sec_id_col + " AND t.price_date = nav.price_date;";
  476. conn = connect_mysql();
  477. t = odbc::query(conn, s_query);
  478. conn.close();
  479. }
  480. return t;
  481. }
  482. /*
  483. * 取月度指标表
  484. *
  485. * @param table_name <STRING>: 指标表名
  486. * @param end_date <MONTH>
  487. * @param isFromMySQL <BOOL>
  488. *
  489. * Example: get_monthly_indicator_data('mfdb.fund_performance', 2024.09M, true);
  490. */
  491. def get_monthly_indicator_data(table_name, end_date, isFromMySQL=true) {
  492. t = null;
  493. s_end_date_sql = iif(end_date.isNull(), '', " AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" );
  494. if(isFromMySQL == true) {
  495. s_query = "SELECT * FROM " + table_name + " WHERE isvalid = 1 " + s_end_date_sql;
  496. conn = connect_mysql();
  497. t = odbc::query(conn, s_query);
  498. conn.close();
  499. }
  500. return t;
  501. }
  502. /*
  503. * 取 pf_fund_indicator_ranking 表
  504. *
  505. *
  506. * Example: get_fund_indicator_ranking("'MF00003PW1'", 2024.09M, 102, true);
  507. * get_fund_indicator_ranking(NULL, 2023.09M, [1, 3], true);
  508. */
  509. def get_fund_indicator_ranking(fund_ids, end_date, strategy, isFromMySQL=true) {
  510. t = null;
  511. s_entity_ids = ids_to_string(fund_ids);
  512. sql_entity_id = '';
  513. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  514. s_strategy_ids = ids_to_string(strategy);
  515. sql_strategy_id = '';
  516. if(s_strategy_ids != NULL) sql_strategy_id = " AND strategy IN (" + s_strategy_ids + ")";
  517. if(isFromMySQL == true) {
  518. s_query = "SELECT *
  519. FROM pfdb.pf_fund_indicator_ranking
  520. WHERE isvalid = 1
  521. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  522. sql_strategy_id +
  523. sql_entity_id;
  524. conn = connect_mysql();
  525. t = odbc::query(conn, s_query);
  526. conn.close();
  527. }
  528. return t;
  529. }
  530. /*
  531. * 取 pf_fund_indicator_substrategy_ranking 表
  532. *
  533. *
  534. * Example: get_fund_indicator_substrategy_ranking("'MF00003PW1'", 2024.09M, 23, true);
  535. * get_fund_indicator_substrategy_ranking(NULL, 2023.09M, [22, 23], true);
  536. */
  537. def get_fund_indicator_substrategy_ranking(fund_ids, end_date, substrategy, isFromMySQL=true) {
  538. t = null;
  539. s_entity_ids = ids_to_string(fund_ids);
  540. sql_entity_id = '';
  541. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  542. s_strategy_ids = ids_to_string(substrategy);
  543. sql_strategy_id = '';
  544. if(s_strategy_ids != NULL) sql_strategy_id = " AND substrategy IN (" + s_strategy_ids + ")";
  545. if(isFromMySQL == true) {
  546. s_query = "SELECT *
  547. FROM pfdb.pf_fund_indicator_substrategy_ranking
  548. WHERE isvalid = 1
  549. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  550. sql_strategy_id +
  551. sql_entity_id;
  552. conn = connect_mysql();
  553. t = odbc::query(conn, s_query);
  554. conn.close();
  555. }
  556. return t;
  557. }
  558. /*
  559. * 取 pf_fund_bfi_bm_indicator_ranking 表
  560. *
  561. *
  562. * Example: get_fund_bfi_bm_indicator_ranking("'MF00003PW1'", 2024.09M, NULL, true);
  563. *
  564. */
  565. def get_fund_bfi_bm_indicator_ranking(fund_ids, end_date, factor_ids, isFromMySQL=true) {
  566. t = null;
  567. s_entity_ids = ids_to_string(fund_ids);
  568. sql_entity_id = '';
  569. if(s_entity_ids != NULL) sql_entity_id = " AND fund_id IN (" + s_entity_ids + ")";
  570. s_factor_ids = ids_to_string(factor_ids);
  571. sql_factor_id = '';
  572. if(s_factor_ids != NULL) sql_factor_id = " AND factor_id IN (" + s_factor_ids + ")";
  573. if(isFromMySQL == true) {
  574. s_query = "SELECT *
  575. FROM pfdb.pf_fund_bfi_bm_indicator_ranking
  576. WHERE isvalid = 1
  577. AND end_date = '" + end_date.temporalFormat('yyyy-MM') + "'" +
  578. sql_factor_id +
  579. sql_entity_id;
  580. conn = connect_mysql();
  581. t = odbc::query(conn, s_query);
  582. conn.close();
  583. }
  584. return t;
  585. }