sample_cal_fof_holding.dos 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. login(`admin, `123456)
  2. loadPlugin("ODBC")
  3. clearCachedModules()
  4. use fundit::fundCalculator
  5. use fundit::dataPuller
  6. end_date = 2023.07.28
  7. // portfolio_ids = "166002,364640,362736"
  8. portfolio_id = "364640"
  9. // size of rolling window
  10. win = 24
  11. // step of moving
  12. step = 24
  13. // get holdings
  14. tb_holdings = get_portfolio_holding_history(portfolio_id)
  15. // calculate current share of each holding
  16. tb_current_holdings = SELECT portfolio_id, end_date AS holding_date, fund_id, fund_share.sum() AS fund_share
  17. FROM tb_holdings
  18. GROUP BY portfolio_id, fund_id
  19. HAVING fund_share.sum() > 0
  20. fund_ids = tb_current_holdings.fund_id.concat("','")$STRING
  21. fund_ids = "'" + fund_ids + "'"
  22. tb_latest_nav = SELECT fund_id, price_date, cumulative_nav FROM get_fund_latest_nav_performance(fund_ids, true)
  23. // calculate portfolio total market value
  24. UPDATE tb_current_holdings a
  25. SET a.market_value = round(a.fund_share * nav.cumulative_nav, 6), nav = nav.cumulative_nav
  26. FROM ej(tb_current_holdings a, tb_latest_nav nav, "fund_id")
  27. // calculate weighting of each holding
  28. tb_current_holdings = SELECT *, market_value.sum() AS total_market_value, round(market_value \ market_value.sum(), 6) AS weighting
  29. FROM tb_current_holdings
  30. CONTEXT BY portfolio_id
  31. // get weekly return of fund holdings
  32. //fund_ids = fund_ids + ",'MF00003PW1','MF00003PW2','IN00000008'"
  33. // fund_ids = fund_ids + "," + fund_pool_188.left(150*13-1)
  34. tb_fund_weekly_ret = SELECT ret
  35. FROM get_fund_weekly_rets(fund_ids, 1990.01.01, end_date, true).rename!(["year_week", "ret_1w"], ["date", "ret"])
  36. PIVOT BY date, fund_id
  37. // get portfolio weekly returns
  38. tb_portfolio_weekly_ret = get_portfolio_weekly_rets(portfolio_id, 1990.01.01, end_date, true).rename!(["year_week", "ret_1w"], ["date", "ret"])
  39. // calculate rolling RBSA weightings
  40. tb = cal_rolling_rbsa(tb_portfolio_weekly_ret, tb_fund_weekly_ret, false, win, step)
  41. // transform the data structure to mySQL friendly
  42. tb_rbsa = table(tb.size()*(tb_fund_weekly_ret.cols()-1):0, "portfolio_id" "asset_type_id" "index_id" "effective_date" "weight", [INT, STRING, STRING, STRING, FLOAT])
  43. for( r in tb)
  44. {
  45. if(r.status <> "solved") continue
  46. w = r.weights.split(" ")$DOUBLE
  47. for(i in 1..(tb_fund_weekly_ret.cols()-1))
  48. {
  49. tb_rbsa.tableInsert(portfolio_id$INT, "TestHolding", tb_fund_weekly_ret.colNames()[i], r.date, w[i-1])
  50. }
  51. }
  52. SELECT * FROM tb_rbsa WHERE index_id in ('MF00003Q1A', 'MF00003T43') and effective_date = '202330' order by weight desc
  53. SELECT * FROM tb_current_holdings order by weighting desc
  54. select * from tb_portfolio_weekly_ret order by date desc
  55. t = (SELECT * FROM tb_fund_weekly_ret WHERE date > '202001').dropColumns!("date")
  56. m = t.matrix().corrMatrix()
  57. m = rename!(m, t.colNames(), t.colNames())
  58. m