dataSaver.dos 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. module fundit::dataSaver
  2. use fundit::sqlUtilities
  3. /*
  4. * 存数据表到mySQL或本地dolphindb,原数据表会被TRUNCATE
  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. //local_table = loadTable(db, table_name.split(".")[1]);
  17. saveTable(db, tb, table_name.split(".")[1]);
  18. }
  19. }
  20. /*
  21. * 【临时】 用于将dolphin table 存到 mysql
  22. *
  23. */
  24. def save_table2(tb, table_name, isCreateTable) {
  25. tb.addColumn(['creatorid', 'createtime', 'updaterid', 'updatetime'], [INT, DATETIME, INT, DATETIME]);
  26. conn = connect_mysql('raw_db');
  27. odbc::append(conn, tb, table_name , isCreateTable, false);
  28. conn.close()
  29. }
  30. /*
  31. * 存私募基金净值到本地dolphindb
  32. *
  33. * save_hedge_fund_nav_to_local(tb_nav)
  34. */
  35. def save_hedge_fund_nav_to_local(tb_nav) {
  36. save_table(tb_nav, "mfdb.nav", false)
  37. }
  38. /*
  39. * 将数据存到本地,之后传回MySQL并同步至正式表
  40. *
  41. *
  42. */
  43. def save_and_sync(table, source_table_name, target_table_name) {
  44. save_table(table, source_table_name, true);
  45. t_table_name = iif(target_table_name.isNothing(), source_table_name, target_table_name);
  46. s_query = "CALL raw_db.sp_sync_table_from_dolphin('" + source_table_name + "_dolphin', '" + t_table_name + "');"
  47. conn = connect_mysql('raw_db');
  48. odbc::execute(conn, s_query);
  49. conn.close();
  50. }
  51. /*
  52. * 建表 XXXX_nav
  53. */
  54. def create_entity_nav(is_id_integer=false) {
  55. return table(1000:0,
  56. ['entity_id', 'price_date', 'cumulative_nav'],
  57. [iif(is_id_integer, INT, SYMBOL), DATE, DOUBLE]);
  58. }
  59. /*
  60. * 建公司/经理净值表 XXXX_fitted_curve
  61. */
  62. def create_mc_fitted_curve() {
  63. return table(1000:0,
  64. ['entity_id', 'curve_type', 'strategy', 'end_date', 'cumulative_nav', 'fund_num'],
  65. [SYMBOL, INT, INT, STRING, DOUBLE, INT]);
  66. }
  67. /*
  68. * 建公司/经理业绩表 xxx_performance
  69. *
  70. */
  71. def create_mc_performance() {
  72. return table(1000:0,
  73. ['entity_id', 'curve_type', 'strategy', 'end_date', 'price_date', 'cumulative_nav',
  74. 'ret_1m', 'ret_1m_a', 'ret_3m', 'ret_3m_a', 'ret_6m', 'ret_6m_a',
  75. 'ret_1y', 'ret_1y_a', 'ret_2y', 'ret_2y_a', 'ret_3y', 'ret_3y_a', 'ret_4y', 'ret_4y_a',
  76. 'ret_5y', 'ret_5y_a', 'ret_10y', 'ret_10y_a', 'ret_ytd', 'ret_ytd_a', 'ret_incep', 'ret_incep_a'],
  77. [SYMBOL, INT, INT, MONTH, DATE, DOUBLE,
  78. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  79. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  80. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  81. }
  82. /*
  83. * 建表 XXXX_performance
  84. */
  85. def create_entity_performance(is_id_integer=false) {
  86. return table(1000:0,
  87. ['entity_id', 'end_date', 'price_date', 'cumulative_nav',
  88. 'ret_1m', 'ret_1m_a', 'ret_3m', 'ret_3m_a', 'ret_6m', 'ret_6m_a',
  89. 'ret_1y', 'ret_1y_a', 'ret_2y', 'ret_2y_a', 'ret_3y', 'ret_3y_a', 'ret_4y', 'ret_4y_a',
  90. 'ret_5y', 'ret_5y_a', 'ret_10y', 'ret_10y_a', 'ret_ytd', 'ret_ytd_a', 'ret_incep', 'ret_incep_a'],
  91. [iif(is_id_integer, INT, SYMBOL), MONTH, DATE, DOUBLE,
  92. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  93. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  94. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  95. }
  96. /*
  97. * 建表 XXX_indicator
  98. */
  99. def create_entity_indicator(is_id_integer=false) {
  100. return table(1000:0,
  101. ['entity_id', 'end_date',
  102. 'info_ratio_6m', 'm2_6m', 'tracking_error_6m',
  103. 'info_ratio_1y', 'm2_1y', 'tracking_error_1y',
  104. 'info_ratio_2y', 'm2_2y', 'tracking_error_2y', 'var_2y', 'cvar_2y',
  105. 'info_ratio_3y', 'm2_3y', 'tracking_error_3y', 'var_3y', 'cvar_3y',
  106. 'info_ratio_4y', 'm2_4y', 'tracking_error_4y', 'var_4y', 'cvar_4y',
  107. 'info_ratio_5y', 'm2_5y', 'tracking_error_5y', 'var_5y', 'cvar_5y',
  108. 'info_ratio_10y', 'm2_10y', 'tracking_error_10y', 'var_10y', 'cvar_10y',
  109. 'info_ratio_ytd', 'm2_ytd', 'tracking_error_ytd',
  110. 'info_ratio_incep', 'm2_incep','tracking_error_incep', 'var_incep', 'cvar_incep'],
  111. [iif(is_id_integer, INT, SYMBOL), MONTH,
  112. DOUBLE, DOUBLE, DOUBLE,
  113. DOUBLE, DOUBLE, DOUBLE,
  114. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  115. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  116. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  117. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  118. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  119. DOUBLE, DOUBLE, DOUBLE,
  120. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  121. }
  122. /*
  123. * 建表 XXX_risk_stats
  124. *
  125. * NOTE: mfdb.fund_risk_stats 中 maxdrawdown_6m 和 maxdrawdown_ytd 因不明原因分别是 6m_maxdrawdown 和 ytd_maxdrawdown 的虚拟列!
  126. */
  127. def create_entity_risk_stats(is_id_integer=false) {
  128. return table(1000:0,
  129. ['entity_id', 'end_date',
  130. 'stddev_6m', 'downsidedev_6m', 'alpha_6m', 'winrate_6m', 'beta_6m', 'skewness_6m', 'kurtosis_6m', 'worstmonth_6m', 'maxdrawdown_6m',
  131. 'stddev_1y', 'downsidedev_1y', 'alpha_1y', 'winrate_1y', 'beta_1y', 'skewness_1y', 'kurtosis_1y', 'worstmonth_1y', 'maxdrawdown_1y',
  132. 'stddev_2y', 'downsidedev_2y', 'alpha_2y', 'winrate_2y', 'beta_2y', 'skewness_2y', 'kurtosis_2y', 'worstmonth_2y', 'maxdrawdown_2y',
  133. 'stddev_3y', 'downsidedev_3y', 'alpha_3y', 'winrate_3y', 'beta_3y', 'skewness_3y', 'kurtosis_3y', 'worstmonth_3y', 'maxdrawdown_3y',
  134. 'stddev_4y', 'downsidedev_4y', 'alpha_4y', 'winrate_4y', 'beta_4y', 'skewness_4y', 'kurtosis_4y', 'worstmonth_4y', 'maxdrawdown_4y',
  135. 'stddev_5y', 'downsidedev_5y', 'alpha_5y', 'winrate_5y', 'beta_5y', 'skewness_5y', 'kurtosis_5y', 'worstmonth_5y', 'maxdrawdown_5y',
  136. 'stddev_10y', 'downsidedev_10y','alpha_10y', 'winrate_10y', 'beta_10y', 'skewness_10y', 'kurtosis_10y', 'worstmonth_10y', 'maxdrawdown_10y',
  137. 'stddev_ytd', 'downsidedev_ytd', 'alpha_ytd', 'winrate_ytd', 'beta_ytd', 'skewness_ytd', 'kurtosis_ytd', 'worstmonth_ytd', 'maxdrawdown_ytd',
  138. 'stddev_incep', 'downsidedev_incep', 'alpha_incep', 'winrate_incep', 'beta_incep', 'skewness_incep', 'kurtosis_incep', 'worstmonth_incep', 'maxdrawdown_incep'],
  139. [iif(is_id_integer, INT, SYMBOL), MONTH,
  140. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  141. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  142. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  143. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  144. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  145. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  146. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  147. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  148. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  149. }
  150. /*
  151. * 建表 XXX_riskadjret_stats
  152. */
  153. def create_entity_riskadjret_stats(is_id_integer=false) {
  154. return table(1000:0,
  155. ['entity_id', 'end_date',
  156. 'sharperatio_6m', 'sortinoratio_6m', 'treynorratio_6m', 'jensen_6m', 'calmarratio_6m', 'omegaratio_6m', 'kapparatio_6m',
  157. 'sharperatio_1y', 'sortinoratio_1y', 'treynorratio_1y', 'jensen_1y', 'calmarratio_1y', 'omegaratio_1y', 'kapparatio_1y',
  158. 'sharperatio_2y', 'sortinoratio_2y', 'treynorratio_2y', 'jensen_2y', 'calmarratio_2y', 'omegaratio_2y', 'kapparatio_2y',
  159. 'sharperatio_3y', 'sortinoratio_3y', 'treynorratio_3y', 'jensen_3y', 'calmarratio_3y', 'omegaratio_3y', 'kapparatio_3y',
  160. 'sharperatio_4y', 'sortinoratio_4y', 'treynorratio_4y', 'jensen_4y', 'calmarratio_4y', 'omegaratio_4y', 'kapparatio_4y',
  161. 'sharperatio_5y', 'sortinoratio_5y', 'treynorratio_5y', 'jensen_5y', 'calmarratio_5y', 'omegaratio_5y', 'kapparatio_5y',
  162. 'sharperatio_10y', 'sortinoratio_10y', 'treynorratio_10y', 'jensen_10y', 'calmarratio_10y', 'omegaratio_10y', 'kapparatio_10y',
  163. 'sharperatio_ytd', 'sortinoratio_ytd', 'treynorratio_ytd', 'jensen_ytd', 'calmarratio_ytd', 'omegaratio_ytd', 'kapparatio_ytd',
  164. 'sharperatio_incep', 'sortinoratio_incep', 'treynorratio_incep', 'jensen_incep', 'calmarratio_incep', 'omegaratio_incep', 'kapparatio_incep'],
  165. [iif(is_id_integer, INT, SYMBOL), MONTH,
  166. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  167. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  168. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  169. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  170. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  171. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  172. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  173. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  174. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  175. }
  176. /*
  177. * 建表 XXX_style_stats
  178. */
  179. def create_entity_style_stats(is_id_integer=false) {
  180. return table(1000:0,
  181. ['entity_id', 'end_date',
  182. 'upsidecapture_ret_6m', 'downsidecapture_ret_6m', 'upsidecapture_ratio_6m', 'downsidecapture_ratio_6m',
  183. 'upsidecapture_ret_1y', 'downsidecapture_ret_1y', 'upsidecapture_ratio_1y', 'downsidecapture_ratio_1y',
  184. 'upsidecapture_ret_2y', 'downsidecapture_ret_2y', 'upsidecapture_ratio_2y', 'downsidecapture_ratio_2y',
  185. 'upsidecapture_ret_3y', 'downsidecapture_ret_3y', 'upsidecapture_ratio_3y', 'downsidecapture_ratio_3y',
  186. 'upsidecapture_ret_4y', 'downsidecapture_ret_4y', 'upsidecapture_ratio_4y', 'downsidecapture_ratio_4y',
  187. 'upsidecapture_ret_5y', 'downsidecapture_ret_5y', 'upsidecapture_ratio_5y', 'downsidecapture_ratio_5y',
  188. 'upsidecapture_ret_10y', 'downsidecapture_ret_10y', 'upsidecapture_ratio_10y', 'downsidecapture_ratio_10y',
  189. 'upsidecapture_ret_ytd', 'downsidecapture_ret_ytd', 'upsidecapture_ratio_ytd', 'downsidecapture_ratio_ytd',
  190. 'upsidecapture_ret_incep', 'downsidecapture_ret_incep', 'upsidecapture_ratio_incep', 'downsidecapture_ratio_incep'],
  191. [iif(is_id_integer, INT, SYMBOL), MONTH,
  192. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  193. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  194. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  195. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  196. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  197. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  198. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  199. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  200. DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  201. }
  202. /*
  203. * 建表 XXX_bfi_bm_indicator
  204. */
  205. def create_entity_bfi_indicator(is_id_integer=false) {
  206. return table(1000:0,
  207. ['entity_id', 'end_date', 'factor_id',
  208. 'upsidecapture_ret_6m', 'downsidecapture_ret_6m', 'upsidecapture_ratio_6m', 'downsidecapture_ratio_6m',
  209. 'alpha_6m', 'winrate_6m', 'beta_6m', 'info_ratio_6m', 'tracking_error_6m', 'jensen_6m',
  210. 'upsidecapture_ret_1y', 'downsidecapture_ret_1y', 'upsidecapture_ratio_1y', 'downsidecapture_ratio_1y',
  211. 'alpha_1y', 'winrate_1y', 'beta_1y', 'info_ratio_1y', 'tracking_error_1y', 'jensen_1y',
  212. 'upsidecapture_ret_2y', 'downsidecapture_ret_2y', 'upsidecapture_ratio_2y', 'downsidecapture_ratio_2y',
  213. 'alpha_2y', 'winrate_2y', 'beta_2y', 'info_ratio_2y', 'tracking_error_2y', 'jensen_2y',
  214. 'upsidecapture_ret_3y', 'downsidecapture_ret_3y', 'upsidecapture_ratio_3y', 'downsidecapture_ratio_3y',
  215. 'alpha_3y', 'winrate_3y', 'beta_3y', 'info_ratio_3y', 'tracking_error_3y', 'jensen_3y',
  216. 'upsidecapture_ret_4y', 'downsidecapture_ret_4y', 'upsidecapture_ratio_4y', 'downsidecapture_ratio_4y',
  217. 'alpha_4y', 'winrate_4y', 'beta_4y', 'info_ratio_4y', 'tracking_error_4y', 'jensen_4y',
  218. 'upsidecapture_ret_5y', 'downsidecapture_ret_5y', 'upsidecapture_ratio_5y', 'downsidecapture_ratio_5y',
  219. 'alpha_5y', 'winrate_5y', 'beta_5y', 'info_ratio_5y', 'tracking_error_5y', 'jensen_5y',
  220. 'upsidecapture_ret_10y', 'downsidecapture_ret_10y', 'upsidecapture_ratio_10y', 'downsidecapture_ratio_10y',
  221. 'alpha_10y', 'winrate_10y', 'beta_10y', 'info_ratio_10y', 'tracking_error_10y', 'jensen_10y',
  222. 'upsidecapture_ret_ytd', 'downsidecapture_ret_ytd', 'upsidecapture_ratio_ytd', 'downsidecapture_ratio_ytd',
  223. 'alpha_ytd', 'winrate_ytd', 'beta_ytd', 'info_ratio_ytd', 'tracking_error_ytd', 'jensen_ytd',
  224. 'upsidecapture_ret_incep', 'downsidecapture_ret_incep', 'upsidecapture_ratio_incep', 'downsidecapture_ratio_incep',
  225. 'alpha_incep', 'winrate_incep', 'beta_incep', 'info_ratio_incep', 'tracking_error_incep', 'jensen_incep'],
  226. [iif(is_id_integer, INT, SYMBOL), MONTH, SYMBOL,
  227. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  228. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  229. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  230. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  231. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  232. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  233. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  234. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  235. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  236. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  237. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  238. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  239. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  240. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  241. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  242. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  243. DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  244. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE] );
  245. }
  246. /*
  247. * 建表 xxx_performance_weekly
  248. */
  249. def create_entity_performance_weekly(is_id_integer=false) {
  250. return table(1000:0,
  251. ['entity_id', 'year_week', 'end_year', 'week_of_year', 'price_date', 'cumulative_nav', 'ret_1w'],
  252. [iif(is_id_integer, INT, SYMBOL), STRING, STRING, SHORT, DATE, DOUBLE, DOUBLE]);
  253. }
  254. /*
  255. * 建表 xxx_latest_performance
  256. */
  257. def create_entity_latest_performance(is_id_integer=false) {
  258. return table(1000:0,
  259. ['entity_id', 'end_date', 'price_date', 'pre_price_date', 'nav', 'cumulative_nav',
  260. 'net_value_change', 'ret_1d', 'ret_1w', 'ret_1m', 'ret_3m', 'ret_6m',
  261. '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',
  262. 'maxdrawdown_1m', 'maxdrawdown_3m', 'maxdrawdown_1y', 'maxdrawdown_incep', 'calmarratio_incep',
  263. 'ret_1y_a', 'ret_2y_a', 'ret_3y_a', 'ret_4y_a', 'ret_5y_a', 'ret_10y_a'],
  264. [iif(is_id_integer, INT, SYMBOL), STRING, DATE, DATE, DOUBLE, DOUBLE,
  265. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  266. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  267. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  268. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  269. }
  270. /*
  271. * 建表 xxx_index_coe
  272. *
  273. */
  274. def create_entity_index_coe(is_id_integer=false) {
  275. return table(1000:0,
  276. ['entity_id', 'end_date', 'index_id',
  277. 'coe_1y', 'coe_3y', 'coe_5y', //'info_ratio_1y', 'info_ratio_3y', 'info_ratio_5y',
  278. 't_value_1y', 't_value_3y', 't_value_5y', 'beta_1y', 'beta_3y', 'beta_5y'],
  279. [iif(is_id_integer, INT, SYMBOL), MONTH, SYMBOL,
  280. DOUBLE, DOUBLE, DOUBLE, //DOUBLE, DOUBLE, DOUBLE,
  281. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  282. }
  283. /*
  284. * 建表 xxx_index_coe
  285. *
  286. */
  287. def create_mc_index_coe(is_id_integer=false) {
  288. return table(1000:0,
  289. ['entity_id', 'curve_type', 'strategy', 'end_date', 'index_id',
  290. 'coe_1y', 'coe_3y', 'coe_5y', //'info_ratio_1y', 'info_ratio_3y', 'info_ratio_5y',
  291. 't_value_1y', 't_value_3y', 't_value_5y', 'beta_1y', 'beta_3y', 'beta_5y'],
  292. [SYMBOL, INT, INT, MONTH, SYMBOL,
  293. DOUBLE, DOUBLE, DOUBLE, //DOUBLE, DOUBLE, DOUBLE,
  294. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
  295. }
  296. /*
  297. * 建表 xxx_factor_bfi_max_r2
  298. *
  299. */
  300. def create_entity_bfi_max_r2(is_id_integer=false) {
  301. return table(1000:0,
  302. ['entity_id', 'end_date', 'factor_id', 'coe', 'r2', 'performance_flag', 'rz_portrait'],
  303. [iif(is_id_integer, INT, SYMBOL), MONTH, SYMBOL, DOUBLE, DOUBLE, STRING, STRING]);
  304. }
  305. /*
  306. * 建表 xxx_factor_bfi
  307. *
  308. */
  309. def create_entity_factor_bfi(is_id_integer=false) {
  310. return table(1000:0,
  311. ['entity_id', 'end_date', 'factor_id',
  312. 'coe', 'r2', 'performance_flag', 't_value_1y', 'beta_1y'],
  313. [iif(is_id_integer, INT, SYMBOL), MONTH, SYMBOL,
  314. DOUBLE, DOUBLE, STRING, DOUBLE, DOUBLE]);
  315. }
  316. /*
  317. * 建基金经理/公司 bfi连接表 xxx_factor_bfi
  318. *
  319. */
  320. def create_mc_factor_bfi(is_id_integer=false) {
  321. return table(1000:0,
  322. ['entity_id', 'curve_type', 'strategy', 'end_date', 'factor_id',
  323. 'coe', 'r2', 'performance_flag', 't_value_1y', 'beta_1y'],
  324. [SYMBOL, INT, INT, MONTH, SYMBOL,
  325. DOUBLE, DOUBLE, STRING, DOUBLE, DOUBLE]);
  326. }
  327. /*
  328. * 建表 XXXX_indicator_ranking
  329. */
  330. def create_entity_indicator_ranking(is_id_integer=false) {
  331. return table(1000:0,
  332. ['entity_id', 'end_date', 'category_id', 'indicator_id',
  333. 'indicator_1m', 'absrank_1m', 'perrank_1m', 'indicator_3m', 'absrank_3m', 'perrank_3m',
  334. 'indicator_6m', 'absrank_6m', 'perrank_6m', 'indicator_1y', 'absrank_1y', 'perrank_1y',
  335. 'indicator_2y', 'absrank_2y', 'perrank_2y', 'indicator_3y', 'absrank_3y', 'perrank_3y',
  336. 'indicator_5y', 'absrank_5y', 'perrank_5y',
  337. 'indicator_10y', 'absrank_10y', 'perrank_10y', 'indicator_ytd', 'absrank_ytd', 'perrank_ytd'],
  338. [iif(is_id_integer, INT, SYMBOL), STRING, SYMBOL, INT,
  339. DOUBLE, INT, INT, DOUBLE, INT, INT,
  340. DOUBLE, INT, INT, DOUBLE, INT, INT,
  341. DOUBLE, INT, INT, DOUBLE, INT, INT,
  342. DOUBLE, INT, INT,
  343. DOUBLE, INT, INT, DOUBLE, INT, INT]);
  344. }
  345. /*
  346. * 建表 XXXX_indicator_ranking_num, raise_type 没有用
  347. */
  348. def create_entity_indicator_ranking_num() {
  349. return table(1000:0,
  350. ['end_date', 'category_id', 'raise_type', 'indicator_id',
  351. 'avg_1m', 'avg_1m_cnt', 'perrank_percent_5_1m', 'perrank_percent_10_1m', 'perrank_percent_25_1m', 'perrank_percent_50_1m',
  352. 'perrank_percent_75_1m', 'perrank_percent_90_1m', 'perrank_percent_95_1m', 'best_1m', 'worst_1m',
  353. 'avg_3m', 'avg_3m_cnt', 'perrank_percent_5_3m', 'perrank_percent_10_3m', 'perrank_percent_25_3m', 'perrank_percent_50_3m',
  354. 'perrank_percent_75_3m', 'perrank_percent_90_3m', 'perrank_percent_95_3m', 'best_3m', 'worst_3m',
  355. 'avg_6m', 'avg_6m_cnt', 'perrank_percent_5_6m', 'perrank_percent_10_6m', 'perrank_percent_25_6m', 'perrank_percent_50_6m',
  356. 'perrank_percent_75_6m', 'perrank_percent_90_6m', 'perrank_percent_95_6m', 'best_6m', 'worst_6m',
  357. 'avg_1y', 'avg_1y_cnt', 'perrank_percent_5_1y', 'perrank_percent_10_1y', 'perrank_percent_25_1y', 'perrank_percent_50_1y',
  358. 'perrank_percent_75_1y', 'perrank_percent_90_1y', 'perrank_percent_95_1y', 'best_1y', 'worst_1y',
  359. 'avg_2y', 'avg_2y_cnt', 'perrank_percent_5_2y', 'perrank_percent_10_2y', 'perrank_percent_25_2y', 'perrank_percent_50_2y',
  360. 'perrank_percent_75_2y', 'perrank_percent_90_2y', 'perrank_percent_95_2y', 'best_2y', 'worst_2y',
  361. 'avg_3y', 'avg_3y_cnt', 'perrank_percent_5_3y', 'perrank_percent_10_3y', 'perrank_percent_25_3y', 'perrank_percent_50_3y',
  362. 'perrank_percent_75_3y', 'perrank_percent_90_3y', 'perrank_percent_95_3y', 'best_3y', 'worst_3y',
  363. 'avg_5y', 'avg_5y_cnt', 'perrank_percent_5_5y', 'perrank_percent_10_5y', 'perrank_percent_25_5y', 'perrank_percent_50_5y',
  364. 'perrank_percent_75_5y', 'perrank_percent_90_5y', 'perrank_percent_95_5y', 'best_5y', 'worst_5y',
  365. 'avg_10y', 'avg_10y_cnt', 'perrank_percent_5_10y', 'perrank_percent_10_10y', 'perrank_percent_25_10y', 'perrank_percent_50_10y',
  366. 'perrank_percent_75_10y', 'perrank_percent_90_10y', 'perrank_percent_95_10y', 'best_10y', 'worst_10y',
  367. 'avg_ytd', 'avg_ytd_cnt', 'perrank_percent_5_ytd', 'perrank_percent_10_ytd', 'perrank_percent_25_ytd', 'perrank_percent_50_ytd',
  368. 'perrank_percent_75_ytd', 'perrank_percent_90_ytd', 'perrank_percent_95_ytd', 'best_ytd', 'worst_ytd'],
  369. [STRING, SYMBOL, INT, INT,
  370. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  371. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  372. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  373. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  374. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  375. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  376. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  377. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  378. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  379. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  380. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  381. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  382. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  383. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  384. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  385. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  386. DOUBLE, INT, DOUBLE, DOUBLE, DOUBLE, DOUBLE,
  387. DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE ]);
  388. }
  389. /*
  390. * 根据 mysql 表改动某些字段
  391. */
  392. def chg_columns_for_mysql(mutable tb_mysql, id_col_name) {
  393. tb_mysql.rename!('entity_id', id_col_name);
  394. // 将 dolphinDB 的 MONTH 换成 MySQL 的 YYYY-MM 格式
  395. v_end_date = EXEC end_date.temporalFormat('yyyy-MM') FROM tb_mysql;
  396. tb_mysql.replaceColumn!('end_date', v_end_date);
  397. }
  398. /*
  399. * 按照 XXX_performance 表结构准备数据记录
  400. *
  401. *
  402. */
  403. def generate_entity_performance(entity_info, indicators, isToMySQL, mutable entity_performance) {
  404. t = null;
  405. if(isToMySQL) {
  406. if(indicators['PBI-3M'].isVoid() || indicators['PBI-3M'].size() == 0) return;
  407. t = SELECT entity_id AS entity_id, end_date, price_date, nav AS cumulative_nav, ret AS ret_1m, ret AS ret_1m_a, trailing_ret AS ret_3m, trailing_ret_a AS ret_3m_a
  408. FROM indicators['PBI-3M'] AS ind
  409. INNER JOIN entity_info fi ON ind.entity_id = fi.entity_id
  410. WHERE ind.end_date >= fi.price_date.month(); // 过滤掉不必更新的旧记录
  411. UPDATE t
  412. SET ret_6m = trailing_ret, ret_6m_a = trailing_ret_a
  413. FROM ej(t, indicators['PBI-6M'], ['entity_id', 'end_date']);
  414. UPDATE t
  415. SET ret_1y = trailing_ret, ret_1y_a = trailing_ret_a
  416. FROM ej(t, indicators['PBI-1Y'], ['entity_id', 'end_date']);
  417. UPDATE t
  418. SET ret_2y = trailing_ret, ret_2y_a = trailing_ret_a
  419. FROM ej(t, indicators['PBI-2Y'], ['entity_id', 'end_date']);
  420. UPDATE t
  421. SET ret_3y = trailing_ret, ret_3y_a = trailing_ret_a
  422. FROM ej(t, indicators['PBI-3Y'], ['entity_id', 'end_date']);
  423. UPDATE t
  424. SET ret_4y = trailing_ret, ret_4y_a = trailing_ret_a
  425. FROM ej(t, indicators['PBI-4Y'], ['entity_id', 'end_date']);
  426. UPDATE t
  427. SET ret_5y = trailing_ret, ret_5y_a = trailing_ret_a
  428. FROM ej(t, indicators['PBI-5Y'], ['entity_id', 'end_date']);
  429. UPDATE t
  430. SET ret_10y = trailing_ret, ret_10y_a = trailing_ret_a
  431. FROM ej(t, indicators['PBI-10Y'], ['entity_id', 'end_date']);
  432. UPDATE t
  433. SET ret_ytd = trailing_ret, ret_ytd_a = trailing_ret_a
  434. FROM ej(t, indicators['PBI-YTD'], ['entity_id', 'end_date']);
  435. // 取消了 ret_incep_a_all (没意义) 和 ret_incep_a_gips (ret_incep_a 与之相等)
  436. UPDATE t
  437. SET ret_incep = trailing_ret, ret_incep_a = trailing_ret_a
  438. FROM ej(t, indicators['PBI-INCEP'], ['entity_id', 'end_date']);
  439. INSERT INTO entity_performance SELECT * FROM t;
  440. } else {
  441. }
  442. }
  443. /*
  444. * 按照 XXX_risk_stats 表结构准备数据记录
  445. *
  446. *
  447. */
  448. def generate_entity_risk_stats(entity_info, indicators, isToMySQL, mutable entity_risk_stats) {
  449. t = null;
  450. if(indicators['PBI-6M'].isVoid() || indicators['PBI-6M'].size() == 0) return;
  451. if(isToMySQL) {
  452. t = SELECT entity_id, end_date, std_dev_a AS stddev_6m, ds_dev_a AS downsidedev_6m, alpha_a AS alpha_6m, winrate AS winrate_6m, beta AS beta_6m,
  453. skewness AS skewness_6m, kurtosis AS kurtosis_6m, wrst_month AS worstmonth_6m, drawdown AS maxdrawdown_6m // mfdb中的真实字段名是 6m_maxdrawdown
  454. FROM indicators['PBI-6M'] AS ind
  455. INNER JOIN entity_info fi ON ind.entity_id = fi.entity_id
  456. WHERE ind.end_date >= fi.price_date.month(); // 过滤掉不必更新的旧记录
  457. UPDATE t
  458. SET stddev_1y = std_dev_a, downsidedev_1y = ds_dev_a, alpha_1y = alpha_a, winrate_1y = winrate, beta_1y = beta,
  459. skewness_1y = skewness, kurtosis_1y = kurtosis, worstmonth_1y = wrst_month, maxdrawdown_1y = drawdown
  460. FROM ej(t, indicators['PBI-1Y'], ['entity_id', 'end_date']);
  461. UPDATE t
  462. SET stddev_2y = std_dev_a, downsidedev_2y = ds_dev_a, alpha_2y = alpha_a, winrate_2y = winrate, beta_2y = beta,
  463. skewness_2y = skewness, kurtosis_2y = kurtosis, worstmonth_2y = wrst_month, maxdrawdown_2y = drawdown
  464. FROM ej(t, indicators['PBI-2Y'], ['entity_id', 'end_date']);
  465. UPDATE t
  466. SET stddev_3y = std_dev_a, downsidedev_3y = ds_dev_a, alpha_3y = alpha_a, winrate_3y = winrate, beta_3y = beta,
  467. skewness_3y = skewness, kurtosis_3y = kurtosis, worstmonth_3y = wrst_month, maxdrawdown_3y = drawdown
  468. FROM ej(t, indicators['PBI-3Y'], ['entity_id', 'end_date']);
  469. UPDATE t
  470. SET stddev_4y = std_dev_a, downsidedev_4y = ds_dev_a, alpha_4y = alpha_a, winrate_4y = winrate, beta_4y = beta,
  471. skewness_4y = skewness, kurtosis_4y = kurtosis, worstmonth_4y = wrst_month, maxdrawdown_4y = drawdown
  472. FROM ej(t, indicators['PBI-4Y'], ['entity_id', 'end_date']);
  473. UPDATE t
  474. SET stddev_5y = std_dev_a, downsidedev_5y = ds_dev_a, alpha_5y = alpha_a, winrate_5y = winrate, beta_5y = beta,
  475. skewness_5y = skewness, kurtosis_5y = kurtosis, worstmonth_5y = wrst_month, maxdrawdown_5y = drawdown
  476. FROM ej(t, indicators['PBI-5Y'], ['entity_id', 'end_date']);
  477. UPDATE t
  478. SET stddev_10y = std_dev_a, downsidedev_10y = ds_dev_a, alpha_10y = alpha_a, winrate_10y = winrate, beta_10y = beta,
  479. skewness_10y = skewness, kurtosis_10y = kurtosis, worstmonth_10y = wrst_month, maxdrawdown_10y = drawdown
  480. FROM ej(t, indicators['PBI-10Y'], ['entity_id', 'end_date']);
  481. UPDATE t
  482. SET stddev_ytd = std_dev_a, downsidedev_ytd = ds_dev_a, alpha_ytd = alpha_a, winrate_ytd = winrate, beta_ytd = beta,
  483. skewness_ytd = skewness, kurtosis_ytd = kurtosis, worstmonth_ytd = wrst_month, maxdrawdown_ytd = drawdown // mfdb中的真实字段名是 ytd_maxdrawdown
  484. FROM ej(t, indicators['PBI-YTD'], ['entity_id', 'end_date']);
  485. UPDATE t
  486. SET stddev_incep = std_dev_a, downsidedev_incep = ds_dev_a, alpha_incep = alpha_a, winrate_incep = winrate, beta_incep = beta,
  487. skewness_incep = skewness, kurtosis_incep = kurtosis, worstmonth_incep = wrst_month, maxdrawdown_incep = drawdown
  488. FROM ej(t, indicators['PBI-INCEP'], ['entity_id', 'end_date']);
  489. INSERT INTO entity_risk_stats SELECT * FROM t;
  490. } else {
  491. }
  492. }
  493. /*
  494. * 按照 XXX_riskadjret_stats 表结构准备数据记录
  495. *
  496. *
  497. */
  498. def generate_entity_riskadjret_stats(entity_info, indicators, isToMySQL, mutable entity_riskadjret_stats) {
  499. t = null;
  500. if(indicators['PBI-6M'].isVoid() || indicators['PBI-6M'].size() == 0) return;
  501. if(isToMySQL) {
  502. t = SELECT entity_id, end_date,
  503. sharpe_a AS sharperatio_6m, sortino_a AS sortinoratio_6m, treynor AS treynorratio_6m, jensen_a AS jensen_6m,
  504. calmar AS calmarratio_6m, omega AS omegaratio_6m, kappa AS kapparatio_6m
  505. FROM indicators['PBI-6M'] AS ind
  506. INNER JOIN entity_info fi ON ind.entity_id = fi.entity_id
  507. WHERE ind.end_date >= fi.price_date.month(); // 过滤掉不必更新的旧记录
  508. UPDATE t
  509. SET sharperatio_1y = sharpe_a, sortinoratio_1y = sortino_a, treynorratio_1y = treynor, jensen_1y = jensen_a,
  510. calmarratio_1y = calmar, omegaratio_1y = omega, kapparatio_1y = kappa
  511. FROM ej(t, indicators['PBI-1Y'], ['entity_id', 'end_date']);
  512. UPDATE t
  513. SET sharperatio_2y = sharpe_a, sortinoratio_2y = sortino_a, treynorratio_2y = treynor, jensen_2y = jensen_a,
  514. calmarratio_2y = calmar, omegaratio_2y = omega, kapparatio_2y = kappa
  515. FROM ej(t, indicators['PBI-2Y'], ['entity_id', 'end_date']);
  516. UPDATE t
  517. SET sharperatio_3y = sharpe_a, sortinoratio_3y = sortino_a, treynorratio_3y = treynor, jensen_3y = jensen_a,
  518. calmarratio_3y = calmar, omegaratio_3y = omega, kapparatio_3y = kappa
  519. FROM ej(t, indicators['PBI-3Y'], ['entity_id', 'end_date']);
  520. UPDATE t
  521. SET sharperatio_4y = sharpe_a, sortinoratio_4y = sortino_a, treynorratio_4y = treynor, jensen_4y = jensen_a,
  522. calmarratio_4y = calmar, omegaratio_4y = omega, kapparatio_4y = kappa
  523. FROM ej(t, indicators['PBI-4Y'], ['entity_id', 'end_date']);
  524. UPDATE t
  525. SET sharperatio_5y = sharpe_a, sortinoratio_5y = sortino_a, treynorratio_5y = treynor, jensen_5y = jensen_a,
  526. calmarratio_5y = calmar, omegaratio_5y = omega, kapparatio_5y = kappa
  527. FROM ej(t, indicators['PBI-5Y'], ['entity_id', 'end_date']);
  528. UPDATE t
  529. SET sharperatio_10y = sharpe_a, sortinoratio_10y = sortino_a, treynorratio_10y = treynor, jensen_10y = jensen_a,
  530. calmarratio_10y = calmar, omegaratio_10y = omega, kapparatio_10y = kappa
  531. FROM ej(t, indicators['PBI-10Y'], ['entity_id', 'end_date']);
  532. UPDATE t
  533. SET sharperatio_ytd = sharpe_a, sortinoratio_ytd = sortino_a, treynorratio_ytd = treynor, jensen_ytd = jensen_a,
  534. calmarratio_ytd = calmar, omegaratio_ytd = omega, kapparatio_ytd = kappa
  535. FROM ej(t, indicators['PBI-YTD'], ['entity_id', 'end_date']);
  536. UPDATE t
  537. SET sharperatio_incep = sharpe_a, sortinoratio_incep = sortino_a, treynorratio_incep = treynor, jensen_incep = jensen_a,
  538. calmarratio_incep = calmar, omegaratio_incep = omega, kapparatio_incep = kappa
  539. FROM ej(t, indicators['PBI-INCEP'], ['entity_id', 'end_date']);
  540. INSERT INTO entity_riskadjret_stats SELECT * FROM t;
  541. } else {
  542. }
  543. }
  544. /*
  545. * 按照 XXX_indicator 表结构准备数据记录
  546. *
  547. *
  548. */
  549. def generate_entity_indicator(entity_info, indicators, isToMySQL, mutable entity_indicator) {
  550. t = null;
  551. if(indicators['PBI-6M'].isVoid() || indicators['PBI-6M'].size() == 0) return;
  552. if(isToMySQL) {
  553. t = SELECT entity_id, end_date, info_a AS info_ratio_6m, m2_a AS m2_6m, track_error_a AS tracking_error_6m
  554. FROM indicators['PBI-6M'] AS ind
  555. INNER JOIN entity_info fi ON ind.entity_id = fi.entity_id
  556. WHERE ind.end_date >= fi.price_date.month(); // 过滤掉不必更新的旧记录
  557. UPDATE t
  558. SET info_ratio_1y = info_a, m2_1y = m2_a, tracking_error_1y = track_error_a
  559. FROM ej(t, indicators['PBI-1Y'], ['entity_id', 'end_date']);
  560. UPDATE t
  561. SET info_ratio_2y = info_a, m2_2y = m2_a, tracking_error_2y = track_error_a, var_2y = var, cvar_2y = cvar
  562. FROM ej(t, indicators['PBI-2Y'], ['entity_id', 'end_date']);
  563. UPDATE t
  564. SET info_ratio_3y = info_a, m2_3y = m2_a, tracking_error_3y = track_error_a, var_3y = var, cvar_3y = cvar
  565. FROM ej(t, indicators['PBI-3Y'], ['entity_id', 'end_date']);
  566. UPDATE t
  567. SET info_ratio_4y = info_a, m2_4y = m2_a, tracking_error_4y = track_error_a, var_4y = var, cvar_4y = cvar
  568. FROM ej(t, indicators['PBI-4Y'], ['entity_id', 'end_date']);
  569. UPDATE t
  570. SET info_ratio_5y = info_a, m2_5y = m2_a, tracking_error_5y = track_error_a, var_5y = var, cvar_5y = cvar
  571. FROM ej(t, indicators['PBI-5Y'], ['entity_id', 'end_date']);
  572. UPDATE t
  573. SET info_ratio_10y = info_a, m2_10y = m2_a, tracking_error_10y = track_error_a, var_10y = var, cvar_10y = cvar
  574. FROM ej(t, indicators['PBI-10Y'], ['entity_id', 'end_date']);
  575. UPDATE t
  576. SET info_ratio_ytd = info_a, m2_ytd = m2_a, tracking_error_ytd = track_error_a
  577. FROM ej(t, indicators['PBI-YTD'], ['entity_id', 'end_date']);
  578. UPDATE t
  579. SET info_ratio_incep = info_a, m2_incep = m2_a, tracking_error_incep = track_error_a, var_incep = var, cvar_incep = cvar
  580. FROM ej(t, indicators['PBI-INCEP'], ['entity_id', 'end_date']);
  581. INSERT INTO entity_indicator SELECT * FROM t;
  582. } else {
  583. }
  584. }
  585. /*
  586. * 按照 XXX_style_stats 表结构准备数据记录
  587. *
  588. *
  589. */
  590. def generate_entity_style_stats(entity_info, indicators, isToMySQL, mutable entity_style_stats) {
  591. t = null;
  592. if(indicators['PBI-6M'].isVoid() || indicators['PBI-6M'].size() == 0) return;
  593. if(isToMySQL) {
  594. t = SELECT entity_id, end_date, upside_capture_ret AS upsidecapture_ret_6m, downside_capture_ret AS downsidecapture_ret_6m,
  595. upside_capture_ratio AS upsidecapture_ratio_6m, downside_capture_ratio AS downsidecapture_ratio_6m
  596. FROM indicators['PBI-6M'] AS ind
  597. INNER JOIN entity_info fi ON ind.entity_id = fi.entity_id
  598. WHERE ind.end_date >= fi.price_date.month(); // 过滤掉不必更新的旧记录
  599. UPDATE t
  600. SET upsidecapture_ret_1y = upside_capture_ret, downsidecapture_ret_1y = downside_capture_ret,
  601. upsidecapture_ratio_1y = upside_capture_ratio, downsidecapture_ratio_1y = downside_capture_ratio
  602. FROM ej(t, indicators['PBI-1Y'], ['entity_id', 'end_date']);
  603. UPDATE t
  604. SET upsidecapture_ret_2y = upside_capture_ret, downsidecapture_ret_2y = downside_capture_ret,
  605. upsidecapture_ratio_2y = upside_capture_ratio, downsidecapture_ratio_2y = downside_capture_ratio
  606. FROM ej(t, indicators['PBI-2Y'], ['entity_id', 'end_date']);
  607. UPDATE t
  608. SET upsidecapture_ret_3y = upside_capture_ret, downsidecapture_ret_3y = downside_capture_ret,
  609. upsidecapture_ratio_3y = upside_capture_ratio, downsidecapture_ratio_3y = downside_capture_ratio
  610. FROM ej(t, indicators['PBI-3Y'], ['entity_id', 'end_date']);
  611. UPDATE t
  612. SET upsidecapture_ret_4y = upside_capture_ret, downsidecapture_ret_4y = downside_capture_ret,
  613. upsidecapture_ratio_4y = upside_capture_ratio, downsidecapture_ratio_4y = downside_capture_ratio
  614. FROM ej(t, indicators['PBI-4Y'], ['entity_id', 'end_date']);
  615. UPDATE t
  616. SET upsidecapture_ret_5y = upside_capture_ret, downsidecapture_ret_5y = downside_capture_ret,
  617. upsidecapture_ratio_5y = upside_capture_ratio, downsidecapture_ratio_5y = downside_capture_ratio
  618. FROM ej(t, indicators['PBI-5Y'], ['entity_id', 'end_date']);
  619. UPDATE t
  620. SET upsidecapture_ret_10y = upside_capture_ret, downsidecapture_ret_10y = downside_capture_ret,
  621. upsidecapture_ratio_10y = upside_capture_ratio, downsidecapture_ratio_10y = downside_capture_ratio
  622. FROM ej(t, indicators['PBI-10Y'], ['entity_id', 'end_date']);
  623. UPDATE t
  624. SET upsidecapture_ret_ytd = upside_capture_ret, downsidecapture_ret_ytd = downside_capture_ret,
  625. upsidecapture_ratio_ytd = upside_capture_ratio, downsidecapture_ratio_ytd = downside_capture_ratio
  626. FROM ej(t, indicators['PBI-YTD'], ['entity_id', 'end_date']);
  627. UPDATE t
  628. SET upsidecapture_ret_incep = upside_capture_ret, downsidecapture_ret_incep = downside_capture_ret,
  629. upsidecapture_ratio_incep = upside_capture_ratio, downsidecapture_ratio_incep = downside_capture_ratio
  630. FROM ej(t, indicators['PBI-INCEP'], ['entity_id', 'end_date']);
  631. INSERT INTO entity_style_stats SELECT * FROM t;
  632. } else {
  633. }
  634. }
  635. /*
  636. * 按照 XXX_bfi_bm_indicator 表结构准备数据记录
  637. *
  638. * TODO: why we need isToMySQL here?
  639. * 其它的指标恐怕也要按这个改,因为私募可能会有近6月没有数据但近2年之类的周期有数据的情况!
  640. */
  641. def generate_entity_bfi_indicator(entity_info, indicators, isToMySQL, mutable entity_bfi_indicator) {
  642. t = null;
  643. v_cols_from = ['upside_capture_ret', 'downside_capture_ret', 'upside_capture_ratio', 'downside_capture_ratio', 'alpha_a', 'winrate', 'beta', 'info_a', 'track_error_a', 'jensen_a'];
  644. v_cols_to = ['upsidecapture_ret', 'downsidecapture_ret', 'upsidecapture_ratio', 'downsidecapture_ratio', 'alpha', 'winrate', 'beta', 'info_ratio', 'tracking_error', 'jensen'];
  645. v_cols_useless = ['track_error', 'info', 'alpha', 'treynor', 'jensen', 'm2', 'm2_a']; // 标准指标中不被当前表覆盖的数据点
  646. if(isToMySQL) {
  647. t = lj(
  648. lj(
  649. lj(
  650. lj(
  651. lj(
  652. lj(
  653. lj(
  654. lj(
  655. lj(entity_info,
  656. indicators['BFI-6M'] AS t_6m, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  657. indicators['BFI-1Y'] AS t_1y, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  658. indicators['BFI-2Y'] AS t_2y, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  659. indicators['BFI-3Y'] AS t_3y, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  660. indicators['BFI-4Y'] AS t_4y, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  661. indicators['BFI-5Y'] AS t_5y, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  662. indicators['BFI-10Y'] AS t_10y, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  663. indicators['BFI-YTD'] AS t_ytd, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless),
  664. indicators['BFI-INCEP'] AS t_incep, ['entity_id', 'benchmark_id', 'end_date']).dropColumns!(v_cols_useless);
  665. t.rename!(v_cols_from, v_cols_to + '_6m');
  666. t.rename!('t_1y_' + v_cols_from, v_cols_to + '_1y');
  667. t.rename!('t_2y_' + v_cols_from, v_cols_to + '_2y');
  668. t.rename!('t_3y_' + v_cols_from, v_cols_to + '_3y');
  669. t.rename!('t_4y_' + v_cols_from, v_cols_to + '_4y');
  670. t.rename!('t_5y_' + v_cols_from, v_cols_to + '_5y');
  671. t.rename!('t_10y_' + v_cols_from, v_cols_to + '_10y');
  672. t.rename!('t_ytd_' + v_cols_from, v_cols_to + '_ytd');
  673. t.rename!('t_incep_' + v_cols_from, v_cols_to + '_incep');
  674. t.dropColumns!(['inception_date', 'ini_value']).rename!('benchmark_id', 'factor_id');
  675. entity_bfi_indicator.tableInsert(t.reorderColumns!(entity_bfi_indicator.colNames()));
  676. } else {
  677. }
  678. }
  679. /*
  680. * 按照 XXX_performance_weekly 表结构准备数据记录
  681. *
  682. *
  683. */
  684. def generate_entity_performance_weekly(entity_info, ret_w, isToMySQL, mutable entity_performance_weekly) {
  685. t = null;
  686. if(ret_w.isVoid() || ret_w.size() == 0) return;
  687. if(isToMySQL) {
  688. t = SELECT entity_id, year_week, year_week.left(4)$INT AS end_year, year_week.right(2)$INT AS week_of_year, price_date,
  689. cumulative_nav, ret_1w
  690. FROM ret_w r
  691. INNER JOIN entity_info fi ON r.entity_id = fi.entity_id
  692. WHERE r.price_date >= fi.price_date; // 过滤掉不必更新的旧记录
  693. INSERT INTO entity_performance_weekly SELECT * FROM t;
  694. } else {
  695. }
  696. }
  697. /*
  698. * 按照 XXX_latest_performance 表结构准备数据记录
  699. *
  700. *
  701. */
  702. def generate_entity_latest_performance(entity_info, perf_latest, isToMySQL, mutable entity_latest_performance) {
  703. t = null;
  704. if(perf_latest.isVoid() || perf_latest.size() == 0) return;
  705. if(isToMySQL) {
  706. t = SELECT r.*
  707. FROM perf_latest r
  708. INNER JOIN entity_info fi ON r.entity_id = fi.entity_id
  709. WHERE r.price_date >= fi.price_date; // 过滤掉不必更新的旧记录
  710. INSERT INTO entity_latest_performance SELECT * FROM t;
  711. } else {
  712. }
  713. }