fix(ledger): 修复历史净值双重汇率折算 bug 与汇率时间查询边界
This commit is contained in:
@@ -242,10 +242,10 @@ async function buildDailyRatesMap(targetDateStr: string): Promise<Record<string,
|
||||
function getClosestRateForDate(currencyPair: string): string | null {
|
||||
const records = ratesCache.get(currencyPair);
|
||||
if (!records || records.length === 0) return null;
|
||||
const targetDt = new Date(targetDateStr + 'T00:00:00Z');
|
||||
const endOfDay = new Date(targetDateStr + 'T23:59:59.999');
|
||||
let closest: RateRecord | null = null;
|
||||
for (const rec of records) {
|
||||
if (rec.fetchTime <= targetDt) {
|
||||
if (rec.fetchTime <= endOfDay) {
|
||||
closest = rec;
|
||||
} else {
|
||||
break;
|
||||
@@ -395,9 +395,7 @@ export async function reconstructPortfolioHistory() {
|
||||
const metrics = calculateAssetMetrics(assetTxs, priceStrForMetrics);
|
||||
|
||||
const posValueCny = new Big(metrics.marketValue).times(snapshotFxRate);
|
||||
const posCostCny = new Big(metrics.marketValue)
|
||||
.minus(metrics.accumulatedPnl)
|
||||
.times(snapshotFxRate);
|
||||
const posCostCny = new Big(metrics.accumulatedCost || '0');
|
||||
|
||||
totalValueCny = totalValueCny.plus(posValueCny);
|
||||
totalCostCny = totalCostCny.plus(posCostCny);
|
||||
|
||||
@@ -56,6 +56,8 @@ export function calculateAssetMetrics(transactions: TxRecord[], currentPrice: st
|
||||
|
||||
const dilutedCost = holdings.gt(0) ? totalInvested.minus(totalRealized).div(holdings) : new Big(0);
|
||||
|
||||
const accumulatedCost = totalInvested.minus(totalRealized);
|
||||
|
||||
return {
|
||||
holdings: holdings.toString(),
|
||||
averageCost: averageCost.toString(),
|
||||
@@ -63,6 +65,7 @@ export function calculateAssetMetrics(transactions: TxRecord[], currentPrice: st
|
||||
floatingPnl: floatingPnl.toString(),
|
||||
accumulatedPnl: accumulatedPnl.toString(),
|
||||
marketValue: currentMarketValue.toString(),
|
||||
totalInvested: totalInvested.toString()
|
||||
totalInvested: totalInvested.toString(),
|
||||
accumulatedCost: accumulatedCost.toString()
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user