|
@@ -8,7 +8,7 @@ use fundit::dataPuller
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
*/
|
|
-def convert_transaction_to_snapshot(portfolio_ids, end_date) {
|
|
+def convert_transaction_to_snapshot(portfolio_ids, end_day) {
|
|
|
|
|
|
// 取数据库中的持仓交易表
|
|
// 取数据库中的持仓交易表
|
|
tb_transaction = get_portfolio_holding_history(portfolio_ids);
|
|
tb_transaction = get_portfolio_holding_history(portfolio_ids);
|
|
@@ -35,7 +35,7 @@ def convert_transaction_to_snapshot(portfolio_ids, end_date) {
|
|
|
|
|
|
// 补上个虚拟的未来截面,以免buy-n-hold的证券信息损失;用0当NAV也是没办法,DolphinDB不能SELECT出个全NULL的列
|
|
// 补上个虚拟的未来截面,以免buy-n-hold的证券信息损失;用0当NAV也是没办法,DolphinDB不能SELECT出个全NULL的列
|
|
INSERT INTO tb
|
|
INSERT INTO tb
|
|
- SELECT portfolio_id, end_date, fund_id, fund_share.sum(), 0
|
|
+ SELECT portfolio_id, end_day, fund_id, fund_share.sum(), 0
|
|
FROM tb_transaction
|
|
FROM tb_transaction
|
|
GROUP BY portfolio_id, fund_id
|
|
GROUP BY portfolio_id, fund_id
|
|
HAVING fund_share.sum().round(0) > 0;
|
|
HAVING fund_share.sum().round(0) > 0;
|
|
@@ -112,7 +112,7 @@ def convert_transaction_to_snapshot(portfolio_ids, end_date) {
|
|
tb_holdings.addColumn(['ret', 'shares', 'market_value', 'total_mkt_value', 'weight'], [DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
|
|
tb_holdings.addColumn(['ret', 'shares', 'market_value', 'total_mkt_value', 'weight'], [DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE]);
|
|
|
|
|
|
// 计算各持仓证券收益
|
|
// 计算各持仓证券收益
|
|
- UPDATE tb_holdings SET ret = (cumulative_nav.ratios()-1).round(6)
|
|
+ UPDATE tb_holdings SET ret = (cumulative_nav.ratios()-1).round(7)
|
|
CONTEXT BY portfolio_id, sec_id;
|
|
CONTEXT BY portfolio_id, sec_id;
|
|
|
|
|
|
// 把交易日截面的份额数用于组合收益表
|
|
// 把交易日截面的份额数用于组合收益表
|
|
@@ -142,7 +142,7 @@ def convert_transaction_to_snapshot(portfolio_ids, end_date) {
|
|
WHERE total_mkt_value <> 0;
|
|
WHERE total_mkt_value <> 0;
|
|
|
|
|
|
// 组合收益计算: RET = ∑( weight_i * ret_i )
|
|
// 组合收益计算: RET = ∑( weight_i * ret_i )
|
|
- tb_portfolio_ret = SELECT portfolio_id, price_date, (weight * ret).sum().round(6) AS ret
|
|
+ tb_portfolio_ret = SELECT portfolio_id, price_date, (weight * ret).sum().round(7) AS ret
|
|
FROM tb_holdings
|
|
FROM tb_holdings
|
|
GROUP BY portfolio_id, price_date;
|
|
GROUP BY portfolio_id, price_date;
|
|
|
|
|
|
@@ -151,7 +151,7 @@ def convert_transaction_to_snapshot(portfolio_ids, end_date) {
|
|
UPDATE tb_portfolio_ret SET nav = 1 WHERE ret IS NULL;
|
|
UPDATE tb_portfolio_ret SET nav = 1 WHERE ret IS NULL;
|
|
|
|
|
|
// 通过收益反算净值: nav_i = nav_0 * ∏(1 + ret_i)
|
|
// 通过收益反算净值: nav_i = nav_0 * ∏(1 + ret_i)
|
|
- UPDATE tb_portfolio_ret SET nav = (1+ret).cumprod() WHERE nav IS NULL CONTEXT BY portfolio_id;
|
|
+ UPDATE tb_portfolio_ret SET nav = (1+ret).cumprod().round(6) WHERE nav IS NULL CONTEXT BY portfolio_id;
|
|
|
|
|
|
// 删掉没有用的数据
|
|
// 删掉没有用的数据
|
|
DELETE FROM tb_portfolio_ret WHERE price_date >= today();
|
|
DELETE FROM tb_portfolio_ret WHERE price_date >= today();
|