fix(ledger): 修復分紅業務邏輯與成本算法,支持攤薄成本為負數的極端場景
This commit is contained in:
@@ -25,6 +25,7 @@ interface Position {
|
||||
dilutedCost: string;
|
||||
holdingDays: number;
|
||||
exchange: string;
|
||||
totalDividendCny: string;
|
||||
}
|
||||
|
||||
interface RawRate {
|
||||
@@ -179,6 +180,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
|
||||
totalBuyCostNative: new Big('0'),
|
||||
totalBuyQuantity: new Big('0'),
|
||||
realizedPnlCny: new Big('0'),
|
||||
totalDividendCny: new Big('0'),
|
||||
firstBuyDate: null,
|
||||
});
|
||||
}
|
||||
@@ -228,7 +230,10 @@ export async function getPortfolioPositions(): Promise<Position[]> {
|
||||
} else if (tx.txType === 'AIRDROP') {
|
||||
holding.quantity = holding.quantity.plus(new Big(tx.quantity));
|
||||
} else if (tx.txType === 'DIVIDEND') {
|
||||
holding.quantity = holding.quantity.plus(new Big(tx.quantity));
|
||||
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);
|
||||
}
|
||||
|
||||
if (tx.assetLatestPrice) {
|
||||
@@ -263,10 +268,10 @@ export async function getPortfolioPositions(): Promise<Position[]> {
|
||||
avgCost = holding.totalBuyCostCny.div(holding.totalBuyQuantity);
|
||||
}
|
||||
|
||||
// 摊薄成本 = (总买入成本 - 已实现盈亏) / 当前持仓数量
|
||||
// 摊薄成本 = (总买入成本 - 已实现盈亏 - 总分红) / 当前持仓数量
|
||||
let dilutedCost = new Big('0');
|
||||
if (holding.quantity.gt(0)) {
|
||||
dilutedCost = holding.totalBuyCostCny.minus(holding.realizedPnlCny).div(holding.quantity);
|
||||
dilutedCost = holding.totalBuyCostCny.minus(holding.realizedPnlCny).minus(holding.totalDividendCny).div(holding.quantity);
|
||||
}
|
||||
|
||||
// 持仓天数
|
||||
@@ -295,6 +300,7 @@ export async function getPortfolioPositions(): Promise<Position[]> {
|
||||
dilutedCost: dilutedCost.toString(),
|
||||
holdingDays,
|
||||
exchange: holding.exchange,
|
||||
totalDividendCny: holding.totalDividendCny.toString(),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user