module fundit::returnCalculator use fundit::operationDataPuller use fundit::performanceDataPuller /* * 通用月收益计算 * * @param entity_info : [COLUMNS] entity_id, price_date, inception_date, ini_value * @param nav
: [COLUMNS] entity_id, price_date, cumulative_nav * */ def cal_monthly_returns_by_nav(entity_info, mutable nav) { tb_rets = null; if(nav.isVoid() || nav.size() == 0 || entity_info.isVoid() || entity_info.size() == 0 ) return null; // 所有月末日期和净值 tb_monthly_nav = SELECT entity_id, price_date.month().last() AS end_date, price_date.last() AS price_date, cumulative_nav.last() AS cumulative_nav FROM nav.sortBy!(['entity_id', 'price_date'], [1, 1]) GROUP BY entity_id, price_date.month(); // 筛掉成立日之前的净值 tb_monthly_nav = SELECT n.entity_id, end_date, price_date, cumulative_nav FROM tb_monthly_nav n INNER JOIN entity_info ei ON n.entity_id = ei.entity_id WHERE n.price_date >= ei.inception_date; // 补回来成立日初始净值 INSERT INTO tb_monthly_nav SELECT entity_id, inception_date.month(), inception_date, ini_value FROM entity_info WHERE inception_date IS NOT NULL; if(tb_monthly_nav.isVoid() || tb_monthly_nav.size() == 0) { return tb_rets; } // 计算月收益 tb_rets = SELECT entity_id, end_date, price_date, cumulative_nav, cumulative_nav.ratios() - 1 AS ret FROM tb_monthly_nav.sortBy!(['entity_id', 'price_date'], [1, 1]) CONTEXT BY entity_id; // the records without return calculated but do have nav are still useful for some calculations (e.g. max drawdown) return ( SELECT * FROM tb_rets WHERE cumulative_nav > 0 ); } /* * 根据最新更新的净值计算收益,并与数据库历史收益合并为完整收益, 数据源是MySQL * * @param entity_type : * @param entity_info
: COLUMN NEED entity_id, inception_date, benchmark_id, ini_value, price_date * @param start_date : 净值(datasource='nav')、收益起始日(datasource='perf')、净值更新的最新日期(datasource='mix') * @param data_source : nav(nav table), perf(xxx_performance table), mix(new updated nav + old performance table ret_1m) * * */ def mix_monthly_returns(entity_type, entity_info) { ret = null; very_old_day = 1990.01.01; end_day = today(); s_json = (SELECT entity_id AS sec_id, price_date FROM entity_info).toStdJson(); // 取基金组合在包括各自的某净值日期的前值及之后的所有净值 tb_nav = get_nav_for_return_calculation(entity_type, 'm', s_json); tb_nav.rename!('sec_id', 'entity_id'); // 计算某净值日期所在月份及之后的所有月收益 ret = cal_monthly_returns_by_nav(entity_info, tb_nav); if(ret.isVoid() || ret.size() == 0) return ret; // 筛掉引入的前值,这些记录用来计算第一期收益后就不再有用 // 不知道为什么 delete ret from ej(ret, tb_entities, 'entity_id') where ret.price_date < tb_entities.price_date 会报错 ret = SELECT ret.* FROM ej(ret, entity_info, 'entity_id') WHERE ret.price_date >= entity_info.price_date; // 取数据库中的所有历史收益 historical_rets = get_monthly_ret(entity_type, entity_info.entity_id, very_old_day, end_day, true); // MIX 将新NAV计算的收益和数据库中的历史收益合并,相同月份时用新计算的收益代替 INSERT INTO ret SELECT entity_id, end_date.temporalParse('yyyy-MM'), price_date, nav, ret FROM historical_rets h WHERE NOT EXISTS ( SELECT * FROM ret WHERE entity_id = historical_rets.entity_id AND end_date = historical_rets.end_date.temporalParse('yyyy-MM') ); // 过滤掉成立日之前的收益 return SELECT ret.* FROM ret INNER JOIN entity_info ei ON ret.entity_id = ei.entity_id WHERE ret.end_date >= ei.inception_date.month() ORDER BY entity_id, end_date, price_date; } /* * 【作废】根据基金净值序列计算月收益序列(适合提供给指标运算) * * Create: 20240907 Joey * TODO: missing pulling data from local * TODO: ONLY support month return now * * @param entity_type : NAV universe, 'HF','MF','PF','EQ'... defined in get_nav_table_description() * @param fund_ids : 基金ID * @param isFromMySQL : 净值来源 1 - 远程MySQL、 0 - 本地 DolphinDB * * Example: cal_fund_monthly_returns('HF', "'HF000004KN','HF000103EU','HF00018WXG'", true); * */ def cal_fund_monthly_returns(entity_type, fund_ids, isFromMySQL){ tb_rets = null; // 暂时只支持公私募和组合 if(!(entity_type IN ['HF', 'MF', 'PF'])) return tb_rets; // 用于保证老基金也能取到所有历史净值 very_old_price_date = 1990.01.01; // 基金基本信息,包括初始净值 tb_fund_info = get_fund_info(fund_ids); // 基金净值 tb_nav = SELECT * FROM get_nav_by_price_date(entity_type, fund_ids, very_old_price_date, isFromMySQL); tb_month_end = table(100:0, ['entity_id', 'price_date'], [STRING, DATE]); // 填充好各基金有效期内所有月份的最后一天 for( f in tb_fund_info ) { INSERT INTO tb_month_end SELECT fund_id AS entity_id, price_date FROM table(f.fund_id.take(1) AS fund_id).cj(table(temporalSeq(f.inception_date, today(), 'M') AS price_date)) ; } UPDATE tb_month_end SET end_date = price_date.month(); tb_monthly_nav = SELECT entity_id, monthEnd(price_date).month().last() AS end_date, price_date.last() AS price_date, cumulative_nav.last() AS cumulative_nav FROM tb_nav GROUP BY entity_id, monthEnd(price_date); // 完整月末日期的净值序列(包括缺失数据为NULL) tb_monthly_nav = SELECT me.entity_id, me.end_date, n.price_date, n.cumulative_nav FROM tb_month_end me LEFT JOIN tb_monthly_nav n ON me.entity_id = n.entity_id AND me.end_date = n.end_date ORDER BY me.entity_id, me.end_date; // 补一下成立日的初始净值 // NOTE: DolphinDB 遇见 EXISTS 语句时,似乎主表的 alias 失效,只好用全名 INSERT INTO tb_monthly_nav SELECT fund_id AS entity_id, inception_date.month(), inception_date, ifNull(ini_value, 1) FROM tb_fund_info fi WHERE NOT EXISTS ( SELECT * FROM tb_monthly_nav n WHERE entity_id = tb_fund_info.fund_id AND n.price_date = tb_fund_info.inception_date); if(tb_monthly_nav.isVoid() || tb_monthly_nav.size() == 0) { return tb_rets; } // 算 ratios 之前先把时间顺序排好 tb_monthly_nav.sortBy!(['entity_id', 'end_date', 'price_date'], [1, 1, 1]); // 计算月收益 tb_rets = SELECT entity_id AS fund_id, end_date, price_date, cumulative_nav, cumulative_nav.ratios() - 1 AS ret FROM tb_monthly_nav CONTEXT BY entity_id; // the records without return calculated but do have nav are still useful for some calculations return ( SELECT * FROM tb_rets WHERE cumulative_nav > 0 ); } /* * 根据基金净值序列计算周收益序列 * * Create: 20240907 Joey * TODO: missing pulling data from local * * @param entity_type : NAV universe, 'HF','MF','PF','EQ'... defined in get_nav_table_description() * @param entity_info
: COLUMN NEED entity_id, price_date, inception_date * * */ def cal_weekly_returns(entity_type, entity_info){ tb_rets_1w = null; if(!(entity_type IN ['HF', 'MF', 'PF', 'MI', 'FI', 'FA'])) return tb_rets_1w; // 将每支证券ID+某个日期转为JSON用于调用sp s_json = (SELECT entity_id AS sec_id, price_date FROM entity_info).toStdJson(); // 取基金组合在包括各自的某净值日期的前值及之后的所有净值 tb_nav = get_nav_for_return_calculation(entity_type, 'w', s_json); tb_nav.rename!('sec_id', 'entity_id'); UPDATE tb_nav // 当12月31日是周四、五、六时,该周为第52周,所以次年前几天有可能是上一年的第52,53周 SET year_week = iif(price_date.weekOfYear() >= 52 && price_date.monthOfYear() == 1, price_date.year()-1, price_date.year())$STRING + (price_date.weekOfYear()$STRING).lpad(2, "0"); tb_weekly_nav = SELECT entity_id, year_week, price_date.last() AS price_date, cumulative_nav.last() AS cumulative_nav FROM tb_nav n INNER JOIN entity_info ei ON n.entity_id = ei.entity_id WHERE n.price_date >= ei.inception_date // 筛掉成立日之前的净值 GROUP BY entity_id, year_week ORDER BY entity_id, year_week; // 这里选最简单的计算方式:不补任何净值空洞,净值前值日期不做任何限制 // TODO: 可以考虑将月收益也改为这种方式 tb_rets_1w = SELECT entity_id, year_week, price_date, cumulative_nav, cumulative_nav.ratios()-1 AS ret_1w FROM tb_weekly_nav ORDER BY entity_id, year_week; return tb_rets_1w; } /* * [RETIRED] 月末 fund_performance 表计算 * * @param fund_ids : 逗号分隔的ID * @param end_date : YYYY-MM * * Example: cal_fund_performance("'HF000004KN','HF00018WXG','HF000103EU'", '2024-06', true); */ def cal_fund_performance(fund_ids, month_end) { // 获取必要的基金月度净值 tb_nav = get_nav_for_hedge_fund_performance(fund_ids, month_end); tb_rets = SELECT fund_id, price_date.month() AS end_date, price_date, cumulative_nav, cumulative_nav \ nav_1m - 1 AS ret_1m, cumulative_nav \ nav_3m - 1 AS ret_3m, cumulative_nav \ nav_6m - 1 AS ret_6m, cumulative_nav \ nav_1y - 1 AS ret_1y, cumulative_nav \ nav_2y - 1 AS ret_2y, cumulative_nav \ nav_3y - 1 AS ret_3y, cumulative_nav \ nav_4y - 1 AS ret_4y, cumulative_nav \ nav_5y - 1 AS ret_5y, cumulative_nav \ nav_10y - 1 AS ret_10y, cumulative_nav \ nav_ytd - 1 AS ret_ytd, cumulative_nav \ nav_incep - 1 AS ret_incep, inception_date FROM tb_nav; // NOTE: this is to keep consistance with MySQL, even it is NOT complied with GIPS standard UPDATE tb_rets SET ret_1m_a = (1 + ret_1m).pow(12\1) - 1, ret_3m_a = (1 + ret_3m).pow(12\3) - 1, ret_6m_a = (1 + ret_6m).pow(12\6) - 1, ret_1y_a= ret_1y, ret_2y_a = (1 + ret_2y).pow(12\24) - 1, ret_3y_a = (1 + ret_3y).pow(12\36) - 1, ret_4y_a = (1 + ret_4y).pow(12\48) - 1, ret_5y_a = (1 + ret_5y).pow(12\60) - 1, ret_10y_a = (1 + ret_10y).pow(12\120) - 1, ret_ytd_a = (1 + ret_ytd).pow(12\int(temporalFormat(end_date, 'MM')))-1, ret_incep_a = (1 + ret_incep).pow(12\(end_date - inception_date.month())) - 1, ret_incep_a_all = (1 + ret_incep).pow(12\(end_date - inception_date.month()))- 1, ret_incep_a_gips = iif(end_date - inception_date.month() < 12, ret_incep, (1 + ret_incep).pow(12\(end_date - inception_date.month()))- 1); return tb_rets; } /* * [RETIRED] 批量计算公募历史基金月度收益(fund_performance) * NOTE: 任何数据频率快于或等于月度的净值数据都可以用此函数一次性计算完整历史记录。双月频、季频甚至更低频率的基金只能按月计算 * * cal_mutual_fund_performance("'HF000004KN','HF00018WXG','HF000103EU'", true) * */ def cal_mutual_fund_performance(fund_ids, isFromMySQL) { // 计算月收益 tb_tmp = cal_fund_monthly_returns('MF', fund_ids, isFromMySQL); tb_rets = SELECT fund_id, end_date, ret_1m, (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, (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, (1 + ret_1m).mprod(60) - 1 AS ret_5y, (1 + ret_1m).mprod(120) - 1 AS ret_10y FROM tb_tmp CONTEXT BY fund_id; // NOTE: this is to keep consistance with MySQL, even it is NOT complied with GIPS standard 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, 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, ret_5y_a = (1 + ret_5y).pow(1\5) - 1, ret_10y_a = (1 + ret_10y).pow(1\10) - 1; // ytd 不会用上面的CONTEXT BY语句实现 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 FROM tb_rets a INNER JOIN tb_rets b ON a.fund_Id = b.fund_id AND b.end_date = a.price_date.yearEnd().datetimeAdd(-1y).month() // since inception 不会用上面的CONTEXT BY语句实现 tb_ret_incep = SELECT a.fund_id, a.end_date, a.price_date, cumulative_nav, -1 + cumulative_nav \ ini_value AS ret_incep FROM tb_rets a INNER JOIN tb_fund_info fi ON a.fund_id = fi.fund_id UPDATE tb_ret_incep SET ret_incep_a = (1 + ret_incep).pow(12\(end_date - end_date.first())) - 1 CONTEXT BY fund_Id 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 // 只选需要更新的记录 tb_fund_performance = SELECT a.fund_id, a.end_date.datetimeFormat("yyyy-MM") AS end_date, c.price_date, c.cumulative_nav, a.ret_1m, a.ret_1m_a, a.ret_3m, a.ret_3m_a, a.ret_6m, a.ret_6m_a, a.ret_1y, a.ret_1y_a, a.ret_2y, a.ret_2y_a, a.ret_3y, a.ret_3y_a, a.ret_4y, a.ret_4y_a, a.ret_5y, a.ret_5y_a, a.ret_10y, a.ret_10y_a, b.ret_ytd, b.ret_ytd_a, c.ret_incep, c.ret_incep_a, c.ret_incep_a_all, c.ret_incep_a_gips // , 123 AS creatorid, now() AS createtime, 123 AS updaterid, now() AS updatetime, 1 AS isvalid FROM tb_rets a LEFT JOIN tb_ret_ytd b ON a.fund_id = b.fund_id AND a.end_date = b.end_date LEFT JOIN tb_ret_incep c ON a.fund_id = c.fund_id AND a.end_date = c.end_date WHERE c.price_date IS NOT NULL ORDER BY a.fund_id, c.price_date return tb_fund_performance } /* * 批量计算区间收益 * TODO: mySQL version 向前取4天,向后不做限制。这里的逻辑是向前取4个交易日, 遇到大长假后可能取不到数据 * 【老程序,可优化】 * * get_trailing_return(tb_last_nav, tb_nav, -7d, "ret_1w") * */ def get_trailing_return(table_last_nav, table_nav, duration, return_column_name) { tb = SELECT a.entity_id, a.price_date.last() AS price_date, a.cumulative_nav.last() \ b.cumulative_nav.last() - 1 AS ret FROM table_last_nav a INNER JOIN table_nav b ON a.entity_id = b.entity_id WHERE b.price_date <= a.price_date.datetimeAdd(duration) AND b.price_date >= a.price_date.datetimeAdd(duration).datetimeAdd(-4d).businessDay() GROUP BY a.entity_id ORDER BY entity_id; tb.rename!("ret", return_column_name); return tb; } /* * 批量计算最新收益 * 【老程序,可优化】 * */ def cal_latest_performance(entity_type, entity_info, isFromMySQL) { // 用于保证老基金也能取到所有历史净值 very_old_price_date = 1990.01.01 tb_nav = get_nav_by_price_date(entity_type, entity_info.entity_id, very_old_price_date, isFromMySQL) tb_last_nav = SELECT entity_id, price_date.last() AS price_date, cumulative_nav.last() AS cumulative_nav FROM tb_nav GROUP BY entity_id ORDER BY entity_id // 近1期收益,对应mySQL fund_latest_nav_performance 中的 net_value_change // 因为是倒序,所以算出来的 ratios() = n0 / n1, 要把它改换成 n1 / n0 - 1 的收益 tb_last_return = SELECT TOP 2 entity_id, price_date.first() AS price_date, price_date AS pre_preice_date, nav.first() AS nav, cumulative_nav.first() AS cumulative_nav, 1\cumulative_nav.ratios() - 1 AS net_value_change FROM ( SELECT * FROM tb_nav ORDER BY price_date DESC ) CONTEXT BY entity_id ORDER BY entity_id tb_last_return = SELECT * FROM tb_last_return WHERE net_value_change IS NOT NULL // 近1交易日收益 tb_1d = SELECT a.entity_id, a.price_date, a.cumulative_nav \ b.cumulative_nav - 1 AS ret_1d FROM tb_last_nav a INNER JOIN tb_nav b ON a.entity_id = b.entity_id AND b.price_date = a.price_date.datetimeAdd(-1d).businessDay() ORDER BY entity_id // 近1周、1/3/6月、1/2/3/4/5/10年收益 tb_1w = get_trailing_return(tb_last_nav, tb_nav, -7d, "ret_1w") tb_1m = get_trailing_return(tb_last_nav, tb_nav, -1M, "ret_1m") tb_3m = get_trailing_return(tb_last_nav, tb_nav, -3M, "ret_3m") tb_6m = get_trailing_return(tb_last_nav, tb_nav, -6M, "ret_6m") tb_1y = get_trailing_return(tb_last_nav, tb_nav, -1y, "ret_1y") tb_2y = get_trailing_return(tb_last_nav, tb_nav, -2y, "ret_2y") tb_3y = get_trailing_return(tb_last_nav, tb_nav, -3y, "ret_3y") tb_4y = get_trailing_return(tb_last_nav, tb_nav, -4y, "ret_4y") tb_5y = get_trailing_return(tb_last_nav, tb_nav, -5y, "ret_5y") tb_10y = get_trailing_return(tb_last_nav, tb_nav, -10y, "ret_10y") // ytd return tb_ytd = SELECT a.entity_id, a.price_date.last() AS price_date, a.cumulative_nav.last() \ b.cumulative_nav.last() - 1 AS ret_ytd FROM tb_last_nav a INNER JOIN tb_nav b ON a.entity_id = b.entity_id WHERE b.price_date < a.price_date.yearBegin() AND b.price_date >= a.price_date.yearBegin().datetimeAdd(-4d) GROUP BY a.entity_id // since inception return tb_incep = SELECT a.entity_id, a.price_date, -1 + cumulative_nav \ ini_value AS ret_incep, fi.inception_date FROM tb_last_nav a INNER JOIN entity_info fi ON a.entity_id = fi.entity_id // annulized since reception return following GIPS rule UPDATE tb_incep SET ret_incep_a = iif((price_date-inception_date)<=365, ret_incep, (1 + ret_incep).pow(365.25\(price_date-inception_date)) - 1) UPDATE tb_incep SET ret_incep_a_all = ret_incep_a, ret_incep_a_gips = ret_incep_a // 最大回撤 tb_drawdown_1m = SELECT a.entity_id, max( 1 - b.cumulative_nav \ b.cumulative_nav.cummax() ) AS drawdown_1m FROM tb_last_return a INNER JOIN tb_nav b ON a.entity_id = b.entity_id WHERE b.price_date >= a.price_date.datetimeAdd(-1M) GROUP BY a.entity_id tb_drawdown_3m = SELECT a.entity_id, max( 1 - b.cumulative_nav \ b.cumulative_nav.cummax() ) AS drawdown_3m FROM tb_last_return a INNER JOIN tb_nav b ON a.entity_id = b.entity_id WHERE b.price_date >= a.price_date.datetimeAdd(-3M) GROUP BY a.entity_id tb_drawdown_1y = SELECT a.entity_id, max( 1 - b.cumulative_nav \ b.cumulative_nav.cummax() ) AS drawdown_1y FROM tb_last_return a INNER JOIN tb_nav b ON a.entity_id = b.entity_id WHERE b.price_date >= a.price_date.datetimeAdd(-1y) GROUP BY a.entity_id tb_drawdown_incep = SELECT entity_id, max( 1 - cumulative_nav \ cumulative_nav.cummax() ) AS drawdown_incep FROM tb_nav GROUP BY entity_id tb_rets = SELECT a.entity_id, a.price_date.datetimeFormat("yyyy-MM") AS end_date, a.price_date, a.pre_preice_date, a.nav, a.cumulative_nav, a.net_value_change, d1.ret_1d, w1.ret_1w, m1.ret_1m, m3.ret_3m, m6.ret_6m, y1.ret_1y, y2.ret_2y, y3.ret_3y, y4.ret_4y, y5.ret_5y, y10.ret_10y, ytd.ret_ytd, incep.ret_incep, incep.ret_incep_a, incep.ret_incep_a_all, incep.ret_incep_a_gips, dd_m1.drawdown_1m AS maxdrawdown_1m, dd_m3.drawdown_3m AS maxdrawdown_3m, dd_y1.drawdown_1y AS maxdrawdown_1y, dd_incep.drawdown_incep AS maxdrawdown_incep, iif(dd_incep.drawdown_incep.round(4) == 0 || incep.ret_incep_a > 1000, NULL, incep.ret_incep_a \ dd_incep.drawdown_incep) AS calmarratio_incep FROM tb_last_return a LEFT JOIN tb_1d d1 ON a.entity_id = d1.entity_id LEFT JOIN tb_1w w1 ON a.entity_id = w1.entity_id LEFT JOIN tb_1m m1 ON a.entity_id = m1.entity_id LEFT JOIN tb_3m m3 ON a.entity_id = m3.entity_id LEFT JOIN tb_6m m6 ON a.entity_id = m6.entity_id LEFT JOIN tb_1y y1 ON a.entity_id = y1.entity_id LEFT JOIN tb_2y y2 ON a.entity_id = y2.entity_id LEFT JOIN tb_3y y3 ON a.entity_id = y3.entity_id LEFT JOIN tb_4y y4 ON a.entity_id = y4.entity_id LEFT JOIN tb_5y y5 ON a.entity_id = y5.entity_id LEFT JOIN tb_10y y10 ON a.entity_id = y10.entity_id LEFT JOIN tb_ytd ytd ON a.entity_id = ytd.entity_id LEFT JOIN tb_incep incep ON a.entity_id = incep.entity_id LEFT JOIN tb_drawdown_1m dd_m1 ON a.entity_id = dd_m1.entity_id LEFT JOIN tb_drawdown_3m dd_m3 ON a.entity_id = dd_m3.entity_id LEFT JOIN tb_drawdown_1y dd_y1 ON a.entity_id = dd_y1.entity_id LEFT JOIN tb_drawdown_incep dd_incep ON a.entity_id = dd_incep.entity_id ORDER BY a.entity_id // 忽略掉非GIPS标准的所有年化收益字段(包括ytd_a) 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, 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 return tb_rets }