returnCalculator.dos 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. module fundit::returnCalculator
  2. use fundit::fundCalculator
  3. use fundit::dataPuller
  4. /*
  5. * 批量计算私募基金收益
  6. *
  7. *
  8. * cal_hedge_fund_returns("'HF000004KN','HF00018WXG','HF000103EU'", true)
  9. *
  10. */
  11. def cal_hedge_fund_returns(fund_ids, isFromMySQL) {
  12. // 用于保证老基金也能取到所有历史净值
  13. very_old_price_date = 1990.01.01
  14. // 基金基本信息,包括初始净值
  15. tb_fund_info = get_fund_info(fund_ids)
  16. // 基金净值
  17. tb_nav = get_hedge_fund_nav_by_price_date(fund_ids, very_old_price_date, isFromMySQL)
  18. // NOTE: mySQL currently uses calendar day, while the codes below takes business day. it might cause a few different numbers calcuated
  19. tb_monthly_nav = SELECT fund_id, businessMonthEnd(price_date).month().last() AS end_date, price_date.last() AS price_date, cumulative_nav.last() AS cumulative_nav
  20. FROM tb_nav
  21. GROUP BY fund_id, businessMonthEnd(price_date)
  22. // 为了把不数据库里不存在的nav记录填空,不得不先做个pivot;然后才能正确计算ratios (ret_1m)
  23. tb_rets_1m = (SELECT cumulative_nav FROM tb_monthly_nav PIVOT BY end_date, fund_id).ffill!().ratios()-1
  24. // 取被pivot掉的fund_Ids
  25. v_col_name = tb_rets_1m.columnNames()[1:]
  26. tb_tmp = tb_rets_1m.unpivot("end_date", v_col_name).rename!("valueType" "value", "fund_id" "ret_1m")
  27. tb_rets = SELECT fund_id, end_date, ret_1m,
  28. (1 + ret_1m).mprod(3) - 1 AS ret_3m, (1 + ret_1m).mprod(6) - 1 AS ret_6m, (1 + ret_1m).mprod(12) - 1 AS ret_1y,
  29. (1 + ret_1m).mprod(24) - 1 AS ret_2y, (1 + ret_1m).mprod(36) - 1 AS ret_3y, (1 + ret_1m).mprod(48) - 1 AS ret_4y,
  30. (1 + ret_1m).mprod(60) - 1 AS ret_5y, (1 + ret_1m).mprod(120) - 1 AS ret_10y
  31. FROM tb_tmp
  32. // WHERE ret_1m IS NOT NULL
  33. CONTEXT BY fund_id
  34. UPDATE tb_rets SET ret_1m_a = (1 + ret_1m).pow(12) - 1, ret_3m_a = (1 + ret_3m).pow(4) - 1, ret_6m_a = (1 + ret_6m).pow(2) - 1, ret_1y_a= ret_1y,
  35. ret_2y_a = (1 + ret_2y).pow(1\2) - 1, ret_3y_a = (1 + ret_3y).pow(1\3) - 1, ret_4y_a = (1 + ret_4y).pow(1\4) - 1,
  36. ret_5y_a = (1 + ret_5y).pow(1\5) - 1, ret_10y_a = (1 + ret_10y).pow(1\10) - 1
  37. // ytd 不会用上面的CONTEXT BY语句实现
  38. tb_ret_ytd = SELECT a.fund_id, a.end_date, a.price_date, a.cumulative_nav, -1 + a.cumulative_nav \ b.cumulative_nav AS ret_ytd, (a.cumulative_nav \ b.cumulative_nav).pow(12\(a.end_date - b.end_date)) - 1 AS ret_ytd_a
  39. FROM tb_monthly_nav a
  40. INNER JOIN tb_monthly_nav b ON a.fund_Id = b.fund_id
  41. AND b.end_date = a.price_date.yearEnd().datetimeAdd(-1y).month()
  42. // since inception 不会用上面的CONTEXT BY语句实现
  43. tb_ret_incep = SELECT a.fund_id, a.end_date, a.price_date, cumulative_nav, -1 + cumulative_nav \ ini_value AS ret_incep
  44. FROM tb_monthly_nav a
  45. INNER JOIN tb_fund_info fi ON a.fund_id = fi.fund_id
  46. UPDATE tb_ret_incep SET ret_incep_a = (1 + ret_incep).pow(12\(end_date - end_date.first())) - 1 CONTEXT BY fund_Id
  47. UPDATE tb_ret_incep SET ret_incep_a_gips = iif( end_date - end_date.first() < 12, ret_incep, ret_incep_a ), ret_incep_a_all = ret_incep_a CONTEXT BY fund_id
  48. // 只选需要更新的记录
  49. tb_fund_performance = SELECT a.fund_id, a.end_date.datetimeFormat("yyyy-MM") AS end_date, c.price_date, c.cumulative_nav,
  50. a.ret_1m, a.ret_1m_a, a.ret_3m, a.ret_3m_a, a.ret_6m, a.ret_6m_a,
  51. a.ret_1y, a.ret_1y_a, a.ret_2y, a.ret_2y_a, a.ret_3y, a.ret_3y_a,
  52. a.ret_4y, a.ret_4y_a, a.ret_5y, a.ret_5y_a, a.ret_10y, a.ret_10y_a,
  53. b.ret_ytd, b.ret_ytd_a, c.ret_incep, c.ret_incep_a, c.ret_incep_a_all, c.ret_incep_a_gips
  54. // , 123 AS creatorid, now() AS createtime, 123 AS updaterid, now() AS updatetime, 1 AS isvalid
  55. FROM tb_rets a
  56. LEFT JOIN tb_ret_ytd b ON a.fund_id = b.fund_id AND a.end_date = b.end_date
  57. LEFT JOIN tb_ret_incep c ON a.fund_id = c.fund_id AND a.end_date = c.end_date
  58. WHERE c.price_date IS NOT NULL
  59. ORDER BY a.fund_id, c.price_date
  60. /*
  61. // 把这些数据写回mySQL数据表
  62. save_table(tb_fund_performance, "mfdb.fund_performance", true)
  63. // 把这些数据写到本地数据表
  64. save_table(tb_fund_performance, "mfdb.fund_performance", false)
  65. */
  66. return tb_fund_performance
  67. }
  68. /*
  69. * 批量计算私募基金周收益
  70. * TODO: 需要用每周最后一个交易日?
  71. *
  72. * cal_hedge_fund_weekly_returns("'HF000004KN','HF00018WXG','HF000103EU'", true)
  73. *
  74. */
  75. def cal_hedge_fund_weekly_returns(fund_ids, isFromMySQL) {
  76. // 用于保证老基金也能取到所有历史净值
  77. very_old_price_date = 1990.01.01
  78. tb_nav = get_hedge_fund_nav_by_price_date(fund_ids, very_old_price_date, isFromMySQL)
  79. UPDATE tb_nav SET year_week = price_date.year()$STRING + (price_date.weekOfYear()$STRING).lpad(2, "0")
  80. tb_weekly_nav = SELECT fund_id, year_week, price_date.last() AS price_date, cumulative_nav.last() AS cumulative_nav
  81. FROM tb_nav
  82. GROUP BY fund_id, year_week
  83. ORDER BY fund_id, year_week
  84. // 这里选最简单的计算方式:不补任何净值空洞,净值前值日期不做任何限制
  85. // TODO: 可以考虑将月收益也改为这种方式
  86. tb_rets_1w = SELECT fund_id, year_week, price_date, cumulative_nav, cumulative_nav.ratios()-1 AS ret_1w
  87. FROM tb_weekly_nav
  88. ORDER BY fund_id, year_week
  89. return tb_rets_1w
  90. }
  91. /*
  92. * 批量计算私募基金区间收益
  93. * TODO: mySQL version 向前取4天,向后不做限制。这里的逻辑是向前取4个交易日
  94. *
  95. * get_trailing_return(tb_last_nav, tb_nav, -7d, "ret_1w")
  96. *
  97. */
  98. def get_trailing_return(table_last_nav, table_nav, duration, return_column_name) {
  99. tb = SELECT a.fund_id, a.price_date.last() AS price_date, a.cumulative_nav.last() \ b.cumulative_nav.last() - 1 AS ret
  100. FROM table_last_nav a
  101. INNER JOIN table_nav b ON a.fund_id = b.fund_id
  102. WHERE b.price_date <= a.price_date.datetimeAdd(duration)
  103. AND b.price_date >= a.price_date.datetimeAdd(duration).datetimeAdd(-4d).businessDay()
  104. GROUP by a.fund_id
  105. ORDER BY fund_id
  106. tb.rename!("ret", return_column_name)
  107. return tb
  108. }
  109. /*
  110. * 批量计算私募基金最新收益
  111. *
  112. *
  113. * cal_hedge_fund_weekly_returns("'HF000004KN','HF00018WXG','HF000103EU'", true)
  114. *
  115. */
  116. def cal_hedge_fund_latest_returns(fund_ids, isFromMySQL) {
  117. // 用于保证老基金也能取到所有历史净值
  118. very_old_price_date = 1990.01.01
  119. tb_nav = get_hedge_fund_nav_by_price_date(fund_ids, very_old_price_date, isFromMySQL)
  120. tb_last_nav = SELECT fund_id, price_date.last() AS price_date, cumulative_nav.last() AS cumulative_nav
  121. FROM tb_nav
  122. GROUP BY fund_id
  123. ORDER BY fund_id
  124. // 近1期收益,对应mySQL fund_latest_nav_performance 中的 net_value_change
  125. // 因为是倒序,所以算出来的 ratios() = n0 / n1, 要把它改换成 n1 / n0 - 1 的收益
  126. tb_last_return = SELECT TOP 2 fund_id, price_date.first() AS price_date, price_date AS pre_preice_date,
  127. cumulative_nav.first() AS cumulative_nav, 1\cumulative_nav.ratios() - 1 AS net_value_change
  128. FROM ( SELECT * FROM tb_nav ORDER BY price_date DESC )
  129. CONTEXT BY fund_id
  130. ORDER BY fund_id
  131. tb_last_return = SELECT * FROM tb_last_return WHERE net_value_change IS NOT NULL
  132. // 近1交易日收益
  133. tb_1d = SELECT a.fund_id, a.price_date, a.cumulative_nav \ b.cumulative_nav - 1 AS ret_1d
  134. FROM tb_last_nav a
  135. INNER JOIN tb_nav b ON a.fund_id = b.fund_id AND b.price_date = a.price_date.datetimeAdd(-1d).businessDay()
  136. ORDER BY fund_id
  137. // 近1周、1/3/6月、1/2/3/4/5/10年收益
  138. tb_1w = get_trailing_return(tb_last_nav, tb_nav, -7d, "ret_1w")
  139. tb_1m = get_trailing_return(tb_last_nav, tb_nav, -1M, "ret_1m")
  140. tb_3m = get_trailing_return(tb_last_nav, tb_nav, -3M, "ret_3m")
  141. tb_6m = get_trailing_return(tb_last_nav, tb_nav, -6M, "ret_6m")
  142. tb_1y = get_trailing_return(tb_last_nav, tb_nav, -1y, "ret_1y")
  143. tb_2y = get_trailing_return(tb_last_nav, tb_nav, -2y, "ret_2y")
  144. tb_3y = get_trailing_return(tb_last_nav, tb_nav, -3y, "ret_3y")
  145. tb_4y = get_trailing_return(tb_last_nav, tb_nav, -4y, "ret_4y")
  146. tb_5y = get_trailing_return(tb_last_nav, tb_nav, -5y, "ret_5y")
  147. tb_10y = get_trailing_return(tb_last_nav, tb_nav, -10y, "ret_10y")
  148. // ytd return
  149. tb_ytd = SELECT a.fund_id, a.price_date.last() AS price_date, a.cumulative_nav.last() \ b.cumulative_nav.last() - 1 AS ret_ytd
  150. FROM tb_last_nav a
  151. INNER JOIN tb_nav b ON a.fund_id = b.fund_id
  152. WHERE b.price_date < a.price_date.yearBegin()
  153. AND b.price_date >= a.price_date.yearBegin().datetimeAdd(-4d)
  154. GROUP by a.fund_id
  155. // since inception return
  156. tb_fund_info = get_fund_info(fund_ids)
  157. tb_incep = SELECT a.fund_id, a.price_date, -1 + cumulative_nav \ ini_value AS ret_incep, fi.inception_date
  158. FROM tb_last_nav a
  159. INNER JOIN tb_fund_info fi ON a.fund_id = fi.fund_id
  160. // annulized since reception return
  161. UPDATE tb_incep SET ret_incep_a = (1 + ret_incep).pow(365.25\(price_date-inception_date)) - 1
  162. UPDATE tb_incep SET ret_incep_a_all = ret_incep_a,
  163. ret_incep_a_gips = iif((price_date-inception_date)<365, ret_incep, ret_incep_a)
  164. // 最大回撤
  165. tb_drawdown_1m = SELECT a.fund_id, max( 1 - b.cumulative_nav \ b.cumulative_nav.cummax() ) AS drawdown_1m
  166. FROM tb_last_return a
  167. INNER JOIN tb_nav b ON a.fund_id = b.fund_id
  168. WHERE b.price_date >= a.price_date.datetimeAdd(-1M)
  169. GROUP BY a.fund_id
  170. tb_drawdown_3m = SELECT a.fund_id, max( 1 - b.cumulative_nav \ b.cumulative_nav.cummax() ) AS drawdown_3m
  171. FROM tb_last_return a
  172. INNER JOIN tb_nav b ON a.fund_id = b.fund_id
  173. WHERE b.price_date >= a.price_date.datetimeAdd(-3M)
  174. GROUP BY a.fund_id
  175. tb_drawdown_incep = SELECT fund_id, max( 1 - cumulative_nav \ cumulative_nav.cummax() ) AS drawdown_incep
  176. FROM tb_nav GROUP BY fund_id
  177. tb_rets = SELECT a.fund_id, a.price_date.datetimeFormat("yyyy-MM") AS end_date, a.price_date, a.pre_preice_date, a.cumulative_nav,
  178. a.net_value_change, d1.ret_1d, w1.ret_1w, m1.ret_1m, m3.ret_3m, m6.ret_6m,
  179. y1.ret_1y, y2.ret_2y, y3.ret_3y, y4.ret_4y, y5.ret_5y, y10.ret_10y,
  180. ytd.ret_ytd, incep.ret_incep, incep.ret_incep_a, incep.ret_incep_a_all, incep.ret_incep_a_gips,
  181. dd_m1.drawdown_1m AS maxdrawdown_1m, dd_m3.drawdown_3m AS maxdrawdown_3m,
  182. dd_incep.drawdown_incep AS maxdrawdown_incep,
  183. iif(dd_incep.drawdown_incep == 0, null,incep.ret_incep_a \ dd_incep.drawdown_incep) AS calmarratio_incep
  184. FROM tb_last_return a
  185. LEFT JOIN tb_1d d1 ON a.fund_id = d1.fund_id
  186. LEFT JOIN tb_1w w1 ON a.fund_id = w1.fund_id
  187. LEFT JOIN tb_1m m1 ON a.fund_id = m1.fund_id
  188. LEFT JOIN tb_3m m3 ON a.fund_id = m3.fund_id
  189. LEFT JOIN tb_6m m6 ON a.fund_id = m6.fund_id
  190. LEFT JOIN tb_1y y1 ON a.fund_id = y1.fund_id
  191. LEFT JOIN tb_2y y2 ON a.fund_id = y2.fund_id
  192. LEFT JOIN tb_3y y3 ON a.fund_id = y3.fund_id
  193. LEFT JOIN tb_4y y4 ON a.fund_id = y4.fund_id
  194. LEFT JOIN tb_5y y5 ON a.fund_id = y5.fund_id
  195. LEFT JOIN tb_10y y10 ON a.fund_id = y10.fund_id
  196. LEFT JOIN tb_ytd ytd ON a.fund_id = ytd.fund_id
  197. LEFT JOIN tb_incep incep ON a.fund_id = incep.fund_id
  198. LEFT JOIN tb_drawdown_1m dd_m1 ON a.fund_id = dd_m1.fund_id
  199. LEFT JOIN tb_drawdown_3m dd_m3 ON a.fund_id = dd_m3.fund_id
  200. LEFT JOIN tb_drawdown_incep dd_incep ON a.fund_id = dd_incep.fund_id
  201. ORDER BY a.fund_id
  202. // 忽略掉非GIPS标准的所有年化收益字段(包括ytd_a)
  203. UPDATE tb_rets SET ret_1y_a = ret_1y, ret_2y_a = (1 + ret_2y).pow(1\2) - 1, ret_3y_a = (1 + ret_3y).pow(1\3) - 1,
  204. ret_4y_a = (1 + ret_4y).pow(1\4) - 1, ret_5y_a = (1 + ret_5y).pow(1\5) - 1, ret_10y_a = (1 + ret_10y).pow(1\10) - 1
  205. return tb_rets
  206. }