dataSaver.dos 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. module fundit::dataSaver
  2. use fundit::sqlUtilities
  3. /*
  4. * 存数据表到mySQL或本地dolphindb,原数据会被替代!
  5. *
  6. * save_table(tb_fund_performance, "raw_db.fund_performance", false)
  7. */
  8. def save_table(tb, table_name, isToMySQL) {
  9. if(isToMySQL == true) {
  10. conn = connect_mysql('raw_db');
  11. odbc::execute(conn, "TRUNCATE TABLE " + table_name + "_dolphin")
  12. odbc::append(conn, tb, table_name + "_dolphin", false)
  13. conn.close()
  14. } else {
  15. db = get_local_database("fundit", table_name.split(".")[0])
  16. saveTable(db, tb, table_name.split(".")[1])
  17. }
  18. }
  19. /*
  20. * 存私募基金净值到本地dolphindb
  21. *
  22. * save_hedge_fund_nav_to_local(tb_nav)
  23. */
  24. def save_hedge_fund_nav_to_local(tb_nav) {
  25. save_table(tb_nav, "mfdb.nav", false)
  26. }
  27. /*
  28. * 将数据存回MySQL并同步至正式表
  29. *
  30. */
  31. def save_and_sync(table, source_table_name, target_table_name) {
  32. save_table(table, source_table_name, true);
  33. s_query = "CALL raw_db.sp_sync_table_from_dolphin('" + source_table_name + "_dolphin', '" + target_table_name + "');"
  34. conn = connect_mysql('raw_db');
  35. odbc::execute(conn, s_query);
  36. conn.close();
  37. }
  38. /*
  39. * 建表 XXXX_performance
  40. */
  41. def create_entity_performance() {
  42. return table(1000:0,
  43. ['entity_id', 'end_date', 'price_date', 'cumulative_nav',
  44. 'ret_1m', 'ret_1m_a', 'ret_3m', 'ret_3m_a', 'ret_6m', 'ret_6m_a',
  45. 'ret_1y', 'ret_1y_a', 'ret_2y', 'ret_2y_a', 'ret_3y', 'ret_3y_a', 'ret_4y', 'ret_4y_a',
  46. 'ret_5y', 'ret_5y_a', 'ret_10y', 'ret_10y_a', 'ret_ytd', 'ret_ytd_a', 'ret_incep', 'ret_incep_a'],
  47. [SYMBOL, MONTH, DATE, DOUBLE,
  48. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  49. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  50. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  51. }
  52. /*
  53. * 建表 XXX_indicator
  54. */
  55. def create_entity_indicator() {
  56. return table(1000:0,
  57. ['entity_id', 'end_date',
  58. 'info_ratio_6m', 'm2_6m', 'tracking_error_6m',
  59. 'info_ratio_1y', 'm2_1y', 'tracking_error_1y',
  60. 'info_ratio_2y', 'm2_2y', 'tracking_error_2y', 'var_2y', 'cvar_2y',
  61. 'info_ratio_3y', 'm2_3y', 'tracking_error_3y', 'var_3y', 'cvar_3y',
  62. 'info_ratio_4y', 'm2_4y', 'tracking_error_4y', 'var_4y', 'cvar_4y',
  63. 'info_ratio_5y', 'm2_5y', 'tracking_error_5y', 'var_5y', 'cvar_5y',
  64. 'info_ratio_10y', 'm2_10y', 'tracking_error_10y', 'var_10y', 'cvar_10y',
  65. 'info_ratio_ytd', 'm2_ytd', 'tracking_error_ytd',
  66. 'info_ratio_incep', 'm2_incep','tracking_error_incep', 'var_incep', 'cvar_incep'],
  67. [SYMBOL, MONTH,
  68. DOUBLE, DOUBLE, DOUBLE,
  69. DOUBLE, DOUBLE, DOUBLE,
  70. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  71. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  72. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  73. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  74. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  75. DOUBLE, DOUBLE, DOUBLE,
  76. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  77. }
  78. /*
  79. * 建表 XXX_risk_stats
  80. *
  81. * NOTE: mfdb.fund_risk_stats 中 maxdrawdown_6m 和 maxdrawdown_ytd 因不明原因分别是 6m_maxdrawdown 和 ytd_maxdrawdown 的虚拟列!
  82. */
  83. def create_entity_risk_stats() {
  84. return table(1000:0,
  85. ['entity_id', 'end_date',
  86. 'stddev_6m', 'downsidedev_6m', 'alpha_6m', 'winrate_6m', 'beta_6m', 'skewness_6m', 'kurtosis_6m', 'worstmonth_6m', 'maxdrawdown_6m',
  87. 'stddev_1y', 'downsidedev_1y', 'alpha_1y', 'winrate_1y', 'beta_1y', 'skewness_1y', 'kurtosis_1y', 'worstmonth_1y', 'maxdrawdown_1y',
  88. 'stddev_2y', 'downsidedev_2y', 'alpha_2y', 'winrate_2y', 'beta_2y', 'skewness_2y', 'kurtosis_2y', 'worstmonth_2y', 'maxdrawdown_2y',
  89. 'stddev_3y', 'downsidedev_3y', 'alpha_3y', 'winrate_3y', 'beta_3y', 'skewness_3y', 'kurtosis_3y', 'worstmonth_3y', 'maxdrawdown_3y',
  90. 'stddev_4y', 'downsidedev_4y', 'alpha_4y', 'winrate_4y', 'beta_4y', 'skewness_4y', 'kurtosis_4y', 'worstmonth_4y', 'maxdrawdown_4y',
  91. 'stddev_5y', 'downsidedev_5y', 'alpha_5y', 'winrate_5y', 'beta_5y', 'skewness_5y', 'kurtosis_5y', 'worstmonth_5y', 'maxdrawdown_5y',
  92. 'stddev_10y', 'downsidedev_10y','alpha_10y', 'winrate_10y', 'beta_10y', 'skewness_10y', 'kurtosis_10y', 'worstmonth_10y', 'maxdrawdown_10y',
  93. 'stddev_ytd', 'downsidedev_ytd', 'alpha_ytd', 'winrate_ytd', 'beta_ytd', 'skewness_ytd', 'kurtosis_ytd', 'worstmonth_ytd', 'maxdrawdown_ytd',
  94. 'stddev_incep', 'downsidedev_incep', 'alpha_incep', 'winrate_incep', 'beta_incep', 'skewness_incep', 'kurtosis_incep', 'worstmonth_incep', 'maxdrawdown_incep'],
  95. [SYMBOL, MONTH,
  96. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  97. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  98. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  99. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  100. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  101. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  102. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  103. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  104. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  105. }
  106. /*
  107. * 建表 XXX_riskadjret_stats
  108. */
  109. def create_entity_riskadjret_stats() {
  110. return table(1000:0,
  111. ['entity_id', 'end_date',
  112. 'sharperatio_6m', 'sortinoratio_6m', 'treynorratio_6m', 'jensen_6m', 'calmarratio_6m', 'omegaratio_6m', 'kapparatio_6m',
  113. 'sharperatio_1y', 'sortinoratio_1y', 'treynorratio_1y', 'jensen_1y', 'calmarratio_1y', 'omegaratio_1y', 'kapparatio_1y',
  114. 'sharperatio_2y', 'sortinoratio_2y', 'treynorratio_2y', 'jensen_2y', 'calmarratio_2y', 'omegaratio_2y', 'kapparatio_2y',
  115. 'sharperatio_3y', 'sortinoratio_3y', 'treynorratio_3y', 'jensen_3y', 'calmarratio_3y', 'omegaratio_3y', 'kapparatio_3y',
  116. 'sharperatio_4y', 'sortinoratio_4y', 'treynorratio_4y', 'jensen_4y', 'calmarratio_4y', 'omegaratio_4y', 'kapparatio_4y',
  117. 'sharperatio_5y', 'sortinoratio_5y', 'treynorratio_5y', 'jensen_5y', 'calmarratio_5y', 'omegaratio_5y', 'kapparatio_5y',
  118. 'sharperatio_10y', 'sortinoratio_10y', 'treynorratio_10y', 'jensen_10y', 'calmarratio_10y', 'omegaratio_10y', 'kapparatio_10y',
  119. 'sharperatio_ytd', 'sortinoratio_ytd', 'treynorratio_ytd', 'jensen_ytd', 'calmarratio_ytd', 'omegaratio_ytd', 'kapparatio_ytd',
  120. 'sharperatio_incep', 'sortinoratio_incep', 'treynorratio_incep', 'jensen_incep', 'calmarratio_incep', 'omegaratio_incep', 'kapparatio_incep'],
  121. [SYMBOL, MONTH,
  122. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  123. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  124. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  125. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  126. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  127. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  128. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  129. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  130. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  131. }
  132. /*
  133. * 建表 XXX_style_stats
  134. */
  135. def create_entity_style_stats() {
  136. return table(1000:0,
  137. ['entity_id', 'end_date',
  138. 'upsidecapture_ret_6m', 'downsidecapture_ret_6m', 'upsidecapture_ratio_6m', 'downsidecapture_ratio_6m',
  139. 'upsidecapture_ret_1y', 'downsidecapture_ret_1y', 'upsidecapture_ratio_1y', 'downsidecapture_ratio_1y',
  140. 'upsidecapture_ret_2y', 'downsidecapture_ret_2y', 'upsidecapture_ratio_2y', 'downsidecapture_ratio_2y',
  141. 'upsidecapture_ret_3y', 'downsidecapture_ret_3y', 'upsidecapture_ratio_3y', 'downsidecapture_ratio_3y',
  142. 'upsidecapture_ret_4y', 'downsidecapture_ret_4y', 'upsidecapture_ratio_4y', 'downsidecapture_ratio_4y',
  143. 'upsidecapture_ret_5y', 'downsidecapture_ret_5y', 'upsidecapture_ratio_5y', 'downsidecapture_ratio_5y',
  144. 'upsidecapture_ret_10y', 'downsidecapture_ret_10y', 'upsidecapture_ratio_10y', 'downsidecapture_ratio_10y',
  145. 'upsidecapture_ret_ytd', 'downsidecapture_ret_ytd', 'upsidecapture_ratio_ytd', 'downsidecapture_ratio_ytd',
  146. 'upsidecapture_ret_incep', 'downsidecapture_ret_incep', 'upsidecapture_ratio_incep', 'downsidecapture_ratio_incep'],
  147. [SYMBOL, MONTH,
  148. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  149. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  150. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  151. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  152. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  153. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  154. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  155. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  156. DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  157. }
  158. /*
  159. * 建表 xxx_performance_weekly
  160. */
  161. def create_entity_performance_weekly() {
  162. return table(1000:0,
  163. ['entity_id', 'year_week', 'end_year', 'week_of_year', 'price_date', 'cumulative_nav', 'ret_1w'],
  164. [SYMBOL, STRING, STRING, SHORT, DATE, DOUBLE, DOUBLE]);
  165. }
  166. /*
  167. * 建表 xxx_latest_performance
  168. */
  169. def create_entity_latest_performance() {
  170. return table(1000:0,
  171. ['entity_id', 'end_date', 'price_date', 'pre_price_date', 'nav', 'cumulative_nav',
  172. 'net_value_change', 'ret_1d', 'ret_1w', 'ret_1m', 'ret_3m', 'ret_6m',
  173. 'ret_1y', 'ret_2y', 'ret_3y', 'ret_4y', 'ret_5y', 'ret_10y', 'ret_ytd', 'ret_incep', 'ret_incep_a', 'ret_incep_a_all', 'ret_incep_a_gips',
  174. 'maxdrawdown_1m', 'maxdrawdown_3m', 'maxdrawdown_1y', 'maxdrawdown_incep', 'calmarratio_incep',
  175. 'ret_1y_a', 'ret_2y_a', 'ret_3y_a', 'ret_4y_a', 'ret_5y_a', 'ret_10y_a'],
  176. [SYMBOL, STRING, DATE, DATE, DOUBLE, DOUBLE,
  177. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  178. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  179. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  180. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  181. }
  182. /*
  183. * 建表 xxx_index_coe
  184. *
  185. */
  186. def create_entity_index_coe() {
  187. return table(1000:0,
  188. ['entity_id', 'end_date', 'coe_1y', 'coe_3y', 'coe_5y'],
  189. [SYMBOL, STRING, DOUBLE, DOUBLE, DOUBLE]);
  190. }
  191. /*
  192. * 根据 mysql 表改动某些字段
  193. */
  194. def chg_columns_for_mysql(mutable tb_mysql, id_col_name) {
  195. tb_mysql.rename!('entity_id', id_col_name);
  196. // 将 dolphinDB 的 MONTH 换成 MySQL 的 YYYY-MM 格式
  197. v_end_date = EXEC end_date.temporalFormat('yyyy-MM') FROM tb_mysql;
  198. tb_mysql.replaceColumn!('end_date', v_end_date);
  199. }