From 6f974dc589fd632fdfe6ab7b76ca54a2f6656e99 Mon Sep 17 00:00:00 2001 From: xxstar-01 <272544687+xxstar-01@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:05:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A9=BA=E5=A4=B4=E6=8C=81=E4=BB=93?= =?UTF-8?q?=E7=9A=84=E6=9C=AA=E5=AE=9E=E7=8E=B0=E7=9B=88=E4=BA=8F=E7=99=BE?= =?UTF-8?q?=E5=88=86=E6=AF=94=E6=8C=89=E6=95=B0=E9=87=8F=E6=96=B9=E5=90=91?= =?UTF-8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit computeUnrealizedPnLPercent 增加可选 quantity 参数,负数时取反,使百分比与 computeUnrealizedPnL 的金额同号;两参数旧调用、零值与非有限数保持原价格口径。 normalizeHolding 传入 quantity,并补空头正反两向与兼容性用例。 Closes #397 --- .../longbridge-tools/src/normalizer.test.ts | 21 +++++++++++++++++++ packages/longbridge-tools/src/normalizer.ts | 20 ++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) 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), }; }