fix(ledger): 修復平均成本顯示 Bug,並優化分紅獨立統計邏輯

This commit is contained in:
2026-04-28 18:51:40 +08:00
parent a04c573cd3
commit 03e8e98260
2 changed files with 15 additions and 9 deletions
+9 -8
View File
@@ -25,7 +25,7 @@ interface Position {
dilutedCost: string;
holdingDays: number;
exchange: string;
totalDividendCny: string;
accumulatedDividendsCny: string;
}
interface RawRate {
@@ -158,6 +158,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
totalBuyQuantity: Big;
// 已实现盈亏
realizedPnlCny: Big;
accumulatedDividendsCny: Big;
// 首次买入日期
firstBuyDate: Date | null;
}>();
@@ -180,7 +181,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
totalBuyCostNative: new Big('0'),
totalBuyQuantity: new Big('0'),
realizedPnlCny: new Big('0'),
totalDividendCny: new Big('0'),
accumulatedDividendsCny: new Big('0'),
firstBuyDate: null,
});
}
@@ -200,6 +201,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
}
const costCny = costPerUnit.times(new Big(appliedRate || '1'));
holding.totalBuyCostCny = holding.totalBuyCostCny.plus(costCny);
holding.totalBuyQuantity = holding.totalBuyQuantity.plus(new Big(tx.quantity));
// 记录首次买入日期
if (!holding.firstBuyDate && tx.executedAt) {
@@ -232,8 +234,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
} else if (tx.txType === 'DIVIDEND') {
const dividendAmountNative = new Big(tx.quantity).times(new Big(tx.price));
const dividendCny = dividendAmountNative.times(new Big(tx.exchangeRate || '1'));
holding.realizedPnlCny = holding.realizedPnlCny.plus(dividendCny);
holding.totalDividendCny = holding.totalDividendCny.plus(dividendCny);
holding.accumulatedDividendsCny = holding.accumulatedDividendsCny.plus(dividendCny);
}
if (tx.assetLatestPrice) {
@@ -256,8 +257,8 @@ export async function getPortfolioPositions(): Promise<Position[]> {
// 未实现盈亏 = 当前市值 - 总买入成本
const unrealizedPnlCny = cnyValue.minus(holding.totalBuyCostCny);
// 总盈亏 = 未实现盈亏 + 已实现盈亏
const totalPnlCny = unrealizedPnlCny.plus(holding.realizedPnlCny);
// 总盈亏 = 当前市值 - 总买入成本 + 已实现盈亏 + 累计分红
const totalPnlCny = unrealizedPnlCny.plus(holding.realizedPnlCny).plus(holding.accumulatedDividendsCny);
const currentNativeValue = new Big(holding.latestPrice).times(holding.quantity);
const pnlNative = currentNativeValue.minus(holding.totalBuyCostNative);
@@ -271,7 +272,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
// 摊薄成本 = (总买入成本 - 已实现盈亏 - 总分红) / 当前持仓数量
let dilutedCost = new Big('0');
if (holding.quantity.gt(0)) {
dilutedCost = holding.totalBuyCostCny.minus(holding.realizedPnlCny).minus(holding.totalDividendCny).div(holding.quantity);
dilutedCost = holding.totalBuyCostCny.minus(holding.realizedPnlCny).minus(holding.accumulatedDividendsCny).div(holding.quantity);
}
// 持仓天数
@@ -300,7 +301,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
dilutedCost: dilutedCost.toString(),
holdingDays,
exchange: holding.exchange,
totalDividendCny: holding.totalDividendCny.toString(),
accumulatedDividendsCny: holding.accumulatedDividendsCny.toString(),
});
}