Test_indicatorCalculator.dos 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. login(`admin, `123456)
  2. loadPlugin("ODBC")
  3. clearCachedModules()
  4. use fundit::fundCalculator
  5. use fundit::dataPuller
  6. use fundit::returnCalculator
  7. use fundit::indicatorCalculator
  8. /* init values for test cases */
  9. end_day = 2024.06.28;
  10. fund_ids = "'HF000004KN','HF000103EU','HF00018WXG'";
  11. entity_type = 'HF';
  12. isFromNav = true;
  13. /* codes from cal_fund_indicators */
  14. very_old_date = 1990.01.01;
  15. fund_info = get_fund_info(fund_ids);
  16. fund_info.rename!('fund_id', 'entity_id');
  17. if(isFromNav == true) {
  18. // 从净值开始计算收益
  19. tb_ret = SELECT * FROM cal_fund_monthly_returns(entity_type, fund_ids, true) WHERE price_date <= end_day;
  20. tb_ret.rename!(['fund_id', 'cumulative_nav'], ['entity_id', 'nav']);
  21. } else {
  22. // 从fund_performance表里读月收益
  23. tb_ret = get_monthly_ret('FD', fund_ids, very_old_date, end_day, true);
  24. tb_ret.rename!(['fund_id'], ['entity_id']);
  25. }
  26. // 取基金和基准的对照表
  27. primary_benchmark = SELECT entity_id, iif(benchmark_id.isNull(), 'IN00000008', benchmark_id) AS benchmark_id FROM fund_info;
  28. // 取所有出现的基准月收益
  29. bmk_ret = get_benchmark_return(primary_benchmark, end_day);
  30. risk_free_rate = SELECT fund_id, temporalParse(end_date, 'yyyy-MM') AS end_date, ret FROM get_risk_free_rate(very_old_date, end_day);
  31. /* Tests for standard indicators */
  32. rtn = cal_basic_performance(tb_ret, 'm');
  33. lpm = cal_LPM(tb_ret, risk_free_rate);
  34. lpms = cal_omega_sortino_kappa(tb_ret, risk_free_rate);
  35. alpha_beta = cal_alpha_beta(tb_ret, primary_benchmark, bmk_ret, risk_free_rate);
  36. bmk_tracking = cal_benchmark_tracking(tb_ret, primary_benchmark, bmk_ret);
  37. capture_r = cal_capture_ratio(tb_ret, primary_benchmark, bmk_ret)
  38. sharpe = cal_sharpe(tb_ret, rtn, risk_free_rate);
  39. treynor = cal_treynor(tb_ret, risk_free_rate, alpha_beta);
  40. jensen = cal_jensen(tb_ret, bmk_ret, risk_free_rate, alpha_beta);
  41. m2 = cal_m2(tb_ret, primary_benchmark, bmk_ret, risk_free_rate);
  42. ms = cal_ms_return(tb_ret, risk_free_rate);
  43. /* codes from cal_fund_bfi_indicators */
  44. start_month = 1990.01M;
  45. // 取基金和基准的对照表
  46. bfi_benchmark = SELECT fund_id AS entity_id, end_date.temporalParse('yyyy-MM') AS end_date, factor_id AS benchmark_id
  47. FROM get_fund_bfi_factors(fund_ids, start_month.temporalFormat('yyyy-MM'), end_day.temporalFormat('yyyy-MM'));
  48. bfi_bmk_ret = get_benchmark_return(bfi_benchmark, end_day);
  49. /* Tests for BFI indicators */
  50. t_bfi_bmk = SELECT * FROM bfi_benchmark WHERE entity_id = 'HF000004KN' and end_date = 2024.06M
  51. t0 = SELECT t.entity_id, t.end_date, t.price_date,
  52. t.ret, bmk.ret AS ret_bmk, cumcount(t.entity_id) AS cnt, (t.ret - bmk.ret) AS exc_ret, bm.benchmark_id
  53. FROM tb_ret t
  54. INNER JOIN t_bfi_bmk bm ON t.entity_id = bm.entity_id
  55. INNER JOIN bfi_bmk_ret bmk ON t.end_date = bmk.end_date AND bm.benchmark_id = bmk.benchmark_id
  56. WHERE t.ret > -1
  57. AND bmk.ret > -1
  58. CONTEXT BY t.entity_id, bm.benchmark_id;
  59. t = SELECT entity_id, end_date.cummax() AS end_date, price_date.cummax() AS price_date, price_date.cummin() AS min_date, benchmark_id,
  60. cumcount(iif(exc_ret >= 0, 1, null)) \ cnt AS winrate,
  61. exc_ret.cumstd() AS track_error,
  62. iif(exc_ret.cumstd() == 0, null, exc_ret.cumavg() / exc_ret.cumstd()) AS info
  63. FROM t0 CONTEXT BY entity_id, benchmark_id
  64. ORDER BY entity_id, end_date, benchmark_id;
  65. select * from bfi_benchmark where benchmark_id = 'FA00000VMP' order by end_date desc
  66. bfi_bmk_tracking = cal_benchmark_tracking(tb_ret, bfi_benchmark, bfi_bmk_ret);
  67. bfi_alpha_beta = cal_alpha_beta(tb_ret, bfi_benchmark, bfi_bmk_ret, risk_free_rate);
  68. bfi_indicators = cal_indicators_with_benchmark(tb_ret, bfi_benchmark, bfi_bmk_ret, risk_free_rate);
  69. SELECT * FROM bfi_indicators ORDER BY entity_id, end_date desc, benchmark_id