fix(ledger): 修复历史净值双重汇率折算 bug 与汇率时间查询边界

This commit is contained in:
kennethcheng 2026-05-02 17:22:32 +08:00
parent b76a6ef577
commit 7cdee75bb9
4 changed files with 18 additions and 9 deletions

View File

@ -1,5 +1,13 @@
# Omniledger 架构与开发记忆 (Memory) # Omniledger 架构与开发记忆 (Memory)
## 修复时光机引擎1. 将汇率查询条件延展至 23:59:59 以解决跨日边界导致的数据穿透失败2. 修复对已结算法币成本进行双重汇率乘法的严重财务逻辑 Bug (Task 74)
- **汇率时间边界修复**:在 `src/actions/snapshots.ts``app/api/debug/snapshot/route.ts``buildDailyRatesMap` 函数中,将 `getClosestRateForDate` 内部的时间比较边界从 `targetDateStr + 'T00:00:00Z'` 延展至 `targetDateStr + 'T23:59:59.999'`。修复根因:初始 SQL 查询使用 `lte(fetchTime, 23:59:59)` 拉取全天数据,但内层循环用次日 00:00:00 做 `<=` 截断,导致跨日时区边界下汇率数据穿透失败,美元汇率回退到 7.22 兜底值而非数据库真实的 6.82。
- **双重汇率乘法修复**:在 `src/utils/finance.ts``calculateAssetMetrics` 返回值中新增 `accumulatedCost` 字段(`totalInvested - totalRealized`代表持仓净成本Base Currency
- **架构红线**`accumulatedCost` / `totalCost` 在底层数据库已是法币 (CNY) 本位(交易录入时已乘以 `exchangeRate`**严禁再与 `snapshotFxRate` 相乘**。修复 `reconstructPortfolioHistory()` 中的 `posCostCny` 计算:从 `(metrics.marketValue - metrics.accumulatedPnl) * snapshotFxRate` 改为直接取 `metrics.accumulatedCost`,彻底消除 USD→CNY 再×FXRate 的双重折算暴击。
- 同步修复 `app/api/debug/snapshot/route.ts` X光机接口`calcCostCny` 从 `holding.totalCost * fxNum` 改为 `holding.totalCost`,确保调试接口与时光机引擎逻辑完全一致。
- **验收**META 的 `calculatedCostCny` 从虚高的 61 万量级回落到 4255 左右真实水平,美股 `snapshotFxRate` 成功抓取数据库 6.82 而非 7.22 兜底值。
## 重构时光机底层引擎,引入基于 lte 的历史价格/汇率向后穿透查询,解决数据断层导致的 0 价格黑洞与汇率串用 Bug (Task 72) ## 重构时光机底层引擎,引入基于 lte 的历史价格/汇率向后穿透查询,解决数据断层导致的 0 价格黑洞与汇率串用 Bug (Task 72)
- 在 `src/actions/snapshots.ts``reconstructPortfolioHistory()` 中,将汇率获取从"一次性全量加载"重构为"按天循环顶部动态构建":每天 `targetDate` 循环开始时调用 `buildDailyRatesMap(dateStr)`,查询 `exchange_rates_history``fetch_time <= targetDate` 的所有记录,按 `(fromCurrency, toCurrency)` 分组构建当日汇率字典O(1) 内存访问。 - 在 `src/actions/snapshots.ts``reconstructPortfolioHistory()` 中,将汇率获取从"一次性全量加载"重构为"按天循环顶部动态构建":每天 `targetDate` 循环开始时调用 `buildDailyRatesMap(dateStr)`,查询 `exchange_rates_history``fetch_time <= targetDate` 的所有记录,按 `(fromCurrency, toCurrency)` 分组构建当日汇率字典O(1) 内存访问。
- **汇率兜底安全值**USD → 7.22HKD → 0.92CNY → 1确保新系统建的老账单查不到历史汇率时不会崩溃。 - **汇率兜底安全值**USD → 7.22HKD → 0.92CNY → 1确保新系统建的老账单查不到历史汇率时不会崩溃。

View File

@ -48,10 +48,10 @@ async function buildDailyRatesMap(targetDateStr: string): Promise<Record<string,
function getClosestRateForDate(currencyPair: string): string | null { function getClosestRateForDate(currencyPair: string): string | null {
const records = ratesCache.get(currencyPair); const records = ratesCache.get(currencyPair);
if (!records || records.length === 0) return null; 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; let closest: RateRecord | null = null;
for (const rec of records) { for (const rec of records) {
if (rec.fetchTime <= targetDt) { if (rec.fetchTime <= endOfDay) {
closest = rec; closest = rec;
} else { } else {
break; break;
@ -222,7 +222,7 @@ export async function GET(req: Request) {
const fxNum = snapshotFxRate; const fxNum = snapshotFxRate;
const calcMarketValueCny = holding.quantity.times(priceNum).times(fxNum); const calcMarketValueCny = holding.quantity.times(priceNum).times(fxNum);
const calcCostCny = holding.totalCost.times(fxNum); const calcCostCny = holding.totalCost;
totalMarketValue = totalMarketValue.plus(calcMarketValueCny); totalMarketValue = totalMarketValue.plus(calcMarketValueCny);
totalCost = totalCost.plus(calcCostCny); totalCost = totalCost.plus(calcCostCny);

View File

@ -242,10 +242,10 @@ async function buildDailyRatesMap(targetDateStr: string): Promise<Record<string,
function getClosestRateForDate(currencyPair: string): string | null { function getClosestRateForDate(currencyPair: string): string | null {
const records = ratesCache.get(currencyPair); const records = ratesCache.get(currencyPair);
if (!records || records.length === 0) return null; 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; let closest: RateRecord | null = null;
for (const rec of records) { for (const rec of records) {
if (rec.fetchTime <= targetDt) { if (rec.fetchTime <= endOfDay) {
closest = rec; closest = rec;
} else { } else {
break; break;
@ -395,9 +395,7 @@ export async function reconstructPortfolioHistory() {
const metrics = calculateAssetMetrics(assetTxs, priceStrForMetrics); const metrics = calculateAssetMetrics(assetTxs, priceStrForMetrics);
const posValueCny = new Big(metrics.marketValue).times(snapshotFxRate); const posValueCny = new Big(metrics.marketValue).times(snapshotFxRate);
const posCostCny = new Big(metrics.marketValue) const posCostCny = new Big(metrics.accumulatedCost || '0');
.minus(metrics.accumulatedPnl)
.times(snapshotFxRate);
totalValueCny = totalValueCny.plus(posValueCny); totalValueCny = totalValueCny.plus(posValueCny);
totalCostCny = totalCostCny.plus(posCostCny); totalCostCny = totalCostCny.plus(posCostCny);

View File

@ -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 dilutedCost = holdings.gt(0) ? totalInvested.minus(totalRealized).div(holdings) : new Big(0);
const accumulatedCost = totalInvested.minus(totalRealized);
return { return {
holdings: holdings.toString(), holdings: holdings.toString(),
averageCost: averageCost.toString(), averageCost: averageCost.toString(),
@ -63,6 +65,7 @@ export function calculateAssetMetrics(transactions: TxRecord[], currentPrice: st
floatingPnl: floatingPnl.toString(), floatingPnl: floatingPnl.toString(),
accumulatedPnl: accumulatedPnl.toString(), accumulatedPnl: accumulatedPnl.toString(),
marketValue: currentMarketValue.toString(), marketValue: currentMarketValue.toString(),
totalInvested: totalInvested.toString() totalInvested: totalInvested.toString(),
accumulatedCost: accumulatedCost.toString()
}; };
} }