dataPuller.dos 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. module fundit::dataPuller
  2. /*
  3. * MySQL 连接,使用前应确保 loadPlugin("ODBC")已经被运行过
  4. *
  5. * Create 20240711 使用ODBC连接MySQL数据库 Joey
  6. *
  7. */
  8. def connect_mysql() {
  9. // 阿里云的mysql被魔改过,当前DolphinDB无法支持MySQL插件,只能用ODBC
  10. // loadPlugin("ODBC")
  11. // conn = odbc::connect("Driver={MySQL ODBC 9.0 UNICODE Driver};Server=funditdb-dev.mysql.rds.aliyuncs.com;Database=mfdb;User=pf_user;Password=MzBlMDA0OG", "MySQL")
  12. // 使用Windows的ODBC数据源事先设置号的连接
  13. // conn = odbc::connect("Dsn=FunditDB-mfdb")
  14. conn = odbc::connect("Dsn=FunditDB-dev-mfdb")
  15. // t = odbc::query(conn, "SELECT * FROM pfdb.pf_portfolio_nav LIMIT 100")
  16. return conn
  17. }
  18. /*
  19. * 取本地数据库
  20. *
  21. * get_local_database("fundit", "mfdb")
  22. */
  23. def get_local_database(server_name, db_name) {
  24. db = database(directory="D:/Program Files/DolphinDB/server/database/" + server_name + "/" + db_name + "/")
  25. return db
  26. }
  27. /*
  28. * 读本地dolphindb数据表
  29. *
  30. * load_table_from_local("fundit", mfdb.fund_performance")
  31. */
  32. def load_table_from_local(server_name, table_name) {
  33. db = get_local_database(server_name, table_name.split(".")[0])
  34. return loadTable(db, table_name.split(".")[1])
  35. }
  36. /*
  37. * 存数据表到mySQL或本地dolphindb,原数据会被替代!
  38. *
  39. * save_table(tb_fund_performance, "mfdb.fund_performance", false)
  40. */
  41. def save_table(tb, table_name, isToMySQL) {
  42. if(isToMySQL == true) {
  43. tb.addColumn("creatorid" "createtime" "updaterid" "updatetime" "isvalid", [INT, DATETIME, INT, DATETIME, INT])
  44. UPDATE tb SET creatorid = 888888, createtime = now(), updaterid = null, updatetime = null, isvalid = 1
  45. conn = connect_mysql()
  46. odbc::execute(conn, "TRUNCATE TABLE " + table_name + "_dolphin")
  47. odbc::append(conn, tb, table_name + "_dolphin", false)
  48. conn.close()
  49. } else {
  50. db = get_local_database("fundit", table_name.split(".")[0])
  51. saveTable(db, tb, table_name.split(".")[1])
  52. }
  53. }
  54. /*
  55. * 取指数周收益
  56. *
  57. * get_index_weekly_rets("'FA00000WKG','FA00000WKH','IN0000007G'", 1990.01.01, today())
  58. */
  59. def get_index_weekly_rets(index_ids, start_date, end_date) {
  60. s_start_date = iif(start_date.isNull(), "", " AND price_date >= '" + start_date$STRING + "'")
  61. s_end_date = iif(end_date.isNull(), "", " AND price_date <= '" + end_date$STRING + "'")
  62. s_query = "SELECT factor_id AS index_id, year_week, price_date, factor_value AS cumulative_nav, ret_1w
  63. FROM pfdb.cm_factor_performance_weekly
  64. WHERE isvalid = 1
  65. AND factor_id IN (" + index_ids + ")" +
  66. s_start_date +
  67. s_end_date + "
  68. AND ret_1w IS NOT NULL
  69. UNION
  70. SELECT fund_id AS index_id, year_week, price_date, cumulative_nav, ret_1w
  71. FROM mfdb.fund_performance_weekly
  72. WHERE isvalid = 1
  73. AND fund_id IN (" + index_ids + ")" +
  74. s_start_date +
  75. s_end_date + "
  76. AND ret_1w IS NOT NULL
  77. ORDER BY year_week"
  78. conn = connect_mysql()
  79. t = odbc::query(conn, s_query)
  80. conn.close()
  81. return t
  82. }
  83. /*
  84. * 取基金周收益
  85. *
  86. *
  87. * get_fund_weekly_rets("'MF00003TMH','MF00003UQM'", 1990.01.01, null, true)
  88. */
  89. def get_fund_weekly_rets(fund_ids, start_date, end_date, isFromMySQL) {
  90. s_fund_id = iif(fund_ids.isNull(), "", " AND fund_id IN (" + fund_ids + ")")
  91. s_start_date = iif(start_date.isNull(), "", " AND price_date >= '" + start_date$STRING + "'")
  92. s_end_date = iif(end_date.isNull(), "", " AND price_date <= '" + end_date$STRING + "'")
  93. s_query = "SELECT fund_id, year_week, price_date, cumulative_nav, ret_1w
  94. FROM mfdb.fund_performance_weekly
  95. WHERE isvalid = 1 " +
  96. s_fund_id +
  97. s_start_date +
  98. s_end_date + "
  99. AND ret_1w IS NOT NULL
  100. ORDER BY fund_id, year_week"
  101. conn = connect_mysql()
  102. t = odbc::query(conn, s_query)
  103. conn.close()
  104. return t
  105. }
  106. /*
  107. * 取组合周收益
  108. * TODO: 增加从本地取数据的功能
  109. *
  110. *
  111. * get_portfolio_weekly_rets("166002,364640", 1990.01.01, today(), true)
  112. */
  113. def get_portfolio_weekly_rets(portfolio_ids, start_date, end_date, isFromMySQL) {
  114. s_portfolio_id = iif(portfolio_ids.isNull(), "", " AND portfolio_id IN (" + portfolio_ids + ")")
  115. s_query = "SELECT portfolio_id, year_week, price_date, cumulative_nav, ret_1w
  116. FROM pfdb.pf_portfolio_performance_weekly
  117. WHERE isvalid = 1 " +
  118. s_portfolio_id + "
  119. AND ret_1w IS NOT NULL
  120. AND price_date BETWEEN '" + start_date$STRING + "' AND '" + end_date$STRING + "'
  121. ORDER BY portfolio_id, year_week"
  122. conn = connect_mysql()
  123. t = odbc::query(conn, s_query)
  124. conn.close()
  125. return t
  126. }
  127. /*
  128. * 取公私募基金月收益
  129. *
  130. * get_fund_monthly_ret("'MF00003PW1','MF00003PW1'", 1990.01.01, today(), true)
  131. */
  132. def get_fund_monthly_ret(fund_ids, start_date, end_date, isFromMySQL) {
  133. yyyymm_start = start_date.temporalFormat("yyyy-MM")
  134. yyyymm_end = end_date.temporalFormat("yyyy-MM")
  135. if(isFromMySQL == true) {
  136. ret_table_name = "mfdb.fund_performance"
  137. s_query = "SELECT fund_id, end_date, price_date, ret_1m AS ret, cumulative_nav AS nav, ret_ytd_a, ret_incep_a
  138. FROM " + ret_table_name + "
  139. WHERE fund_id IN (" + fund_ids + ")
  140. AND isvalid = 1
  141. AND cumulative_nav > 0
  142. AND end_date BETWEEN '" + yyyymm_start + "' AND '" + yyyymm_end + "'
  143. ORDER BY fund_id, end_date"
  144. conn = connect_mysql()
  145. t = odbc::query(conn, s_query)
  146. conn.close()
  147. } else {
  148. tb_local = load_table_from_local("fundit", "mfdb.fund_performance")
  149. s_col = (sqlCol("fund_id"), sqlCol("end_date"), sqlColAlias(<ret_1m>, "ret"), sqlColAlias(<cumulative_nav>, "nav"), sqlCol("ret_ytd_a"), sqlCol("ret_incep_a"))
  150. s_where = expr(<fund_id>, in, fund_ids.strReplace("'", "").split(","))
  151. t = sql(s_col, tb_local, s_where).eval()
  152. }
  153. return t
  154. }
  155. /*
  156. * 取无风险月度利率
  157. *
  158. * get_risk_free_rate("IN0000000M", 1990.01.01, today())
  159. */
  160. def get_risk_free_rate(index_id, start_date, end_date) {
  161. return get_fund_monthly_ret(index_id, start_date, end_date, true)
  162. }
  163. /*
  164. * 取基金最新收益及净值
  165. *
  166. * get_fund_latest_nav_performance("'HF000004KN','HF00018WXG'")
  167. */
  168. def get_fund_latest_nav_performance(fund_ids, isFromMySQL) {
  169. if(isFromMySQL == true) {
  170. s_query = "SELECT *
  171. FROM mfdb.fund_latest_nav_performance
  172. WHERE fund_id IN (" + fund_ids + ")
  173. AND isvalid = 1
  174. ORDER BY fund_id"
  175. conn = connect_mysql()
  176. t = odbc::query(conn, s_query)
  177. conn.close()
  178. } else {
  179. tb_local = load_table_from_local("fundit", "mfdb.fund_latest_nav_performance")
  180. s_col = sqlCol("*")
  181. s_where = expr(<fund_id>, in, fund_ids.strReplace("'", "").split(","))
  182. t = sql(s_col, tb_local, s_where).eval()
  183. }
  184. return t
  185. }
  186. /*
  187. * 取私募基金净值
  188. *
  189. *
  190. *
  191. * Create: 202408 Joey
  192. * TODO: add isvalid and nav > 0 for local version
  193. *
  194. *
  195. * Example: get_hedge_fund_nav_by_price_date("'HF000004KN','HF00018WXG'", 2024.05.01, true)
  196. */
  197. def get_hedge_fund_nav_by_price_date(fund_ids, price_date, isFromMySQL) {
  198. s_fund_ids = '';
  199. // 判断输入的 fund_ids 是字符串标量还是向量
  200. if ( fund_ids.form() == 0 ) {
  201. s_fund_ids = fund_ids;
  202. } else {
  203. s_fund_ids = "'" + fund_ids.concat("','") + "'";
  204. }
  205. if(isFromMySQL == true) {
  206. nav_table_name = "mfdb.nav"
  207. s_query = "SELECT fund_id, price_date, cumulative_nav
  208. FROM " + nav_table_name + "
  209. WHERE fund_id IN (" + s_fund_ids + ")
  210. AND isvalid = 1
  211. AND cumulative_nav > 0
  212. AND price_date >= '" + price_date$STRING + "'
  213. ORDER BY fund_id, price_date"
  214. conn = connect_mysql()
  215. t = odbc::query(conn, s_query)
  216. conn.close()
  217. } else {
  218. tb_local = load_table_from_local("fundit", "mfdb.nav")
  219. s_col = sqlCol("*")
  220. s_where = [expr(<fund_id>, in, fund_ids.strReplace("'", "").split(",")), <price_date >= price_date>]
  221. t = sql(s_col, tb_local, s_where).eval()
  222. }
  223. return t
  224. }
  225. /*
  226. * 取指数因子点位
  227. *
  228. * get_index_nav_by_price_date("'IN00000008','FA00000WKG'", 2024.06.01)
  229. */
  230. def get_index_nav_by_price_date(index_ids, price_date) {
  231. s_query = "SELECT index_id, price_date, close AS cumulative_nav
  232. FROM mfdb.market_indexes
  233. WHERE index_id IN (" + index_ids + ")
  234. AND isvalid = 1
  235. AND close > 0
  236. AND price_date >= '" + price_date + "'
  237. UNION
  238. SELECT index_id AS index_id, price_date, index_value AS cumulative_nav
  239. FROM mfdb.indexes_ty_index
  240. WHERE index_id IN (" + index_ids + ")
  241. AND isvalid = 1
  242. AND index_value > 0
  243. AND price_date >= '" + price_date + "'
  244. UNION
  245. SELECT factor_id AS index_id, price_date, factor_value AS cumulative_nav
  246. FROM pfdb.cm_factor_value
  247. WHERE factor_id IN (" + index_ids + ")
  248. AND isvalid = 1
  249. AND factor_value > 0
  250. AND price_date >= '" + price_date + "'
  251. ORDER BY price_date"
  252. conn = connect_mysql()
  253. t = odbc::query(conn, s_query)
  254. conn.close()
  255. return t
  256. }
  257. /*
  258. * 取有效基金基本信息
  259. *
  260. * get_fund_info("'HF000004KN','HF00018WXG'")
  261. */
  262. def get_fund_info(fund_ids) {
  263. s_query = "SELECT fi.fund_id, fi.inception_date, fi.primary_benchmark_id AS benchmark_id, IFNULL(fi.initial_unit_value, 1) AS ini_value, fs.strategy, fs.substrategy
  264. FROM mfdb.fund_information fi
  265. INNER JOIN mfdb.fund_strategy fs ON fi.fund_id = fs.fund_id AND fs.isvalid = 1
  266. WHERE fi.fund_id IN (" + fund_ids + ")
  267. AND fi.isvalid = 1
  268. ORDER BY fi.fund_id"
  269. conn = connect_mysql()
  270. t = odbc::query(conn, s_query)
  271. conn.close()
  272. return t
  273. }
  274. /*
  275. * 取私募基金净值更新信息, 返回基金及其净值更新的最早净值日期
  276. *
  277. * @param fund_ids: fund_id STRING VECTOR
  278. * @param update_time: all updates after this time
  279. *
  280. * Example: get_fund_list_by_nav_updatetime(null, 2024.07.19T10:00:00)
  281. *
  282. */
  283. def get_fund_list_by_nav_updatetime(fund_ids, updatetime) {
  284. s_fund_sql = '';
  285. // 这里要用 isVoid, 因为 isNull对向量返回的是布尔向量
  286. if (! isVoid(fund_ids)){
  287. s_fund_ids = fund_ids.concat("','");
  288. s_fund_sql = " AND fi.fund_id IN ('" + s_fund_ids + "')";
  289. }
  290. s_query = "SELECT fi.fund_id, MIN(nav.price_date) AS price_date,
  291. fi.inception_date, fi.primary_benchmark_id AS benchmark_id, IFNULL(fi.initial_unit_value, 1) AS ini_value
  292. FROM mfdb.fund_information fi
  293. INNER JOIN mfdb.nav ON fi.fund_id = nav.fund_id
  294. WHERE fi.isvalid = 1" +
  295. s_fund_sql + "
  296. AND nav.cumulative_nav > 0
  297. AND nav.updatetime >= '" + updatetime + "'
  298. GROUP BY fi.fund_id
  299. ORDER BY fi.fund_id"
  300. conn = connect_mysql()
  301. t = odbc::query(conn, s_query)
  302. conn.close()
  303. return t
  304. }
  305. /*
  306. * 存私募基金净值到本地dolphindb
  307. *
  308. * save_hedge_fund_nav_to_local(tb_nav)
  309. */
  310. def save_hedge_fund_nav_to_local(tb_nav) {
  311. save_table(tb_nav, "mfdb.nav", false)
  312. }
  313. /*
  314. * 存私募基金净值到本地dolphindb
  315. *
  316. * get_portfolio_holding_history("166002,364640")
  317. */
  318. def get_portfolio_holding_history(portfolio_ids) {
  319. s_query = "SELECT portfolio_id, holding_date, fund_id, amount, fund_share, ROUND(amount/fund_share, 6) as nav
  320. FROM pfdb.pf_portfolio_fund_history
  321. WHERE portfolio_id IN (" + portfolio_ids + ")
  322. AND isvalid = 1
  323. ORDER BY portfolio_id, holding_date"
  324. conn = connect_mysql()
  325. t = odbc::query(conn, s_query)
  326. conn.close()
  327. return t
  328. }
  329. /*
  330. * 取私募基金用于月末 fund_performance 表更新的净值
  331. *
  332. * @param fund_ids: 逗号分隔的ID字符串, 每个ID都有''
  333. * @param month_end: 月末日期字符串 YYYY-MM
  334. *
  335. *
  336. */
  337. def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
  338. s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
  339. conn = connect_mysql()
  340. t = odbc::query(conn, s_query)
  341. conn.close()
  342. return t
  343. }