diff --git a/packages/longbridge-tools/src/normalizer.test.ts b/packages/longbridge-tools/src/normalizer.test.ts index d34e469c..76f16f53 100644 --- a/packages/longbridge-tools/src/normalizer.test.ts +++ b/packages/longbridge-tools/src/normalizer.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from 'bun:test'; import { existsSync } from 'node:fs'; import { classifyPortfolioFailure, + computeUnrealizedPnL, computeUnrealizedPnLPercent, normalizePortfolioSnapshot, toFiniteNumber, @@ -70,6 +71,26 @@ describe('computeUnrealizedPnLPercent', () => { expect(computeUnrealizedPnLPercent(undefined, 100)).toBeUndefined(); expect(computeUnrealizedPnLPercent(100, undefined)).toBeUndefined(); }); + + it('flips the sign for short positions so it agrees with the signed PnL', () => { + const cost = 100; + const market = 120; + const shortQty = -10; + expect(computeUnrealizedPnLPercent(cost, market, shortQty)).toBeCloseTo(-20); + expect(computeUnrealizedPnLPercent(cost, 80, shortQty)).toBeCloseTo(20); + // 金额与百分比必须同号:空头下价格下跌是盈利 + expect(Math.sign(computeUnrealizedPnL(cost, market, shortQty) ?? 0)) + .toBe(Math.sign(computeUnrealizedPnLPercent(cost, market, shortQty) ?? 0)); + expect(Math.sign(computeUnrealizedPnL(cost, 80, shortQty) ?? 0)) + .toBe(Math.sign(computeUnrealizedPnLPercent(cost, 80, shortQty) ?? 0)); + }); + + it('keeps the long, flat and missing-quantity behaviour unchanged', () => { + expect(computeUnrealizedPnLPercent(100, 120, 10)).toBeCloseTo(20); + expect(computeUnrealizedPnLPercent(100, 120, 0)).toBeCloseTo(20); + expect(computeUnrealizedPnLPercent(100, 120, Number.NaN)).toBeCloseTo(20); + expect(computeUnrealizedPnLPercent(100, 120)).toBeCloseTo(20); + }); }); describe('normalizePortfolioSnapshot fixture matrix (spec §59)', () => { diff --git a/packages/longbridge-tools/src/normalizer.ts b/packages/longbridge-tools/src/normalizer.ts index f1621c49..d39b7164 100644 --- a/packages/longbridge-tools/src/normalizer.ts +++ b/packages/longbridge-tools/src/normalizer.ts @@ -182,15 +182,27 @@ export function computeUnrealizedPnL( return (marketPrice - costPrice) * quantity; } -/** Unrealized P&L percent (already ×100), computed when the vendor omits it. */ +/** + * Unrealized P&L percent (already ×100), computed when the vendor omits it. + * + * 空头(quantity < 0)的盈亏方向与价格涨跌相反:价格从 100 涨到 120 时, + * 空头是亏损。因此传入 quantity 时按数量方向取符号,与 + * `computeUnrealizedPnL` 的金额保持同号。不传 quantity(旧的两参数调用) + * 或数量为 0 时保持原来的纯价格口径。 + */ export function computeUnrealizedPnLPercent( costPrice: number | undefined, - marketPrice: number | undefined + marketPrice: number | undefined, + quantity?: number ): number | undefined { if (costPrice === undefined || marketPrice === undefined || costPrice === 0) { return undefined; } - return ((marketPrice - costPrice) / costPrice) * 100; + const pricePercent = ((marketPrice - costPrice) / costPrice) * 100; + if (quantity === undefined || !Number.isFinite(quantity) || quantity === 0) { + return pricePercent; + } + return quantity < 0 ? -pricePercent : pricePercent; } // ── Normalizers ──────────────────────────────────────────────────────────── @@ -211,7 +223,7 @@ function normalizeHolding(h: RawPortfolioHolding | undefined): Holding { marketValue: toFiniteNumber(h?.market_value), marketValueBase: toFiniteNumber(h?.market_value_usd), unrealizedPnL: computeUnrealizedPnL(costPrice, marketPrice, quantity), - unrealizedPnLPercent: computeUnrealizedPnLPercent(costPrice, marketPrice), + unrealizedPnLPercent: computeUnrealizedPnLPercent(costPrice, marketPrice, quantity), prevClose: toFiniteNumber(h?.prev_close), }; }