Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/longbridge-tools/src/normalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from 'bun:test';
import { existsSync } from 'node:fs';
import {
classifyPortfolioFailure,
computeUnrealizedPnL,
computeUnrealizedPnLPercent,
normalizePortfolioSnapshot,
toFiniteNumber,
Expand Down Expand Up @@ -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)', () => {
Expand Down
20 changes: 16 additions & 4 deletions packages/longbridge-tools/src/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ────────────────────────────────────────────────────────────
Expand All @@ -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),
};
}
Expand Down