From 01070af564f2c8feddb993200ebe3c9bfff7460e Mon Sep 17 00:00:00 2001 From: on101094 <333183542+on101094@users.noreply.github.com> Date: Sun, 4 Oct 2026 04:51:42 +0300 Subject: [PATCH] Fix Trades list Net ROI: apply the contract multiplier The Net ROI column divided net P&L by avgEntry x quantity, ignoring the contract multiplier, so futures/options/forex/CFD returns were too large by the multiplier (x2 for MNQ, x100 for options). A zero notional showed 0.00% and a negative entry price showed 0%. - packages/core: netReturnOnNotional(), net P&L / (|avg entry| x qty x multiplier), null when a multiplier-priced trade has no multiplier or the notional is zero. Unit tests for futures, options, missing multiplier, equities/crypto, negative prices and zero notional. - /api/trades sends each row's contractMultiplier. - The Trades list and the trade page both use the helper, so the two figures always agree. Fixes #34 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + apps/web/src/app/api/trades/route.ts | 1 + apps/web/src/app/trades/[key]/page.tsx | 13 +----- apps/web/src/app/trades/page.tsx | 11 +++-- packages/core/src/analysis.ts | 23 ++++++++++ packages/core/tests/net-return.test.ts | 61 ++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 packages/core/tests/net-return.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 89250b0c..a343aa91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ All notable changes to this project are documented here. The format follows [Kee ### Fixed +- The Trades list "Net ROI" column now applies the contract multiplier, like the trade page's "Net / entry notional". Futures, options, forex and CFD trades previously showed returns too large by the multiplier (×2 for MNQ, ×100 for options); a trade whose notional is unknown now shows "–" instead of a wrong or 0% value. - Password protection now verifies the session signature on every API route. Previously, when `JOURNAL_PASSWORD` was set, any request carrying a cookie of the right name was accepted, so a forged cookie could read the journal. ## [0.1.0] - 2026-09-03 diff --git a/apps/web/src/app/api/trades/route.ts b/apps/web/src/app/api/trades/route.ts index f2e24d90..45d50d87 100644 --- a/apps/web/src/app/api/trades/route.ts +++ b/apps/web/src/app/api/trades/route.ts @@ -18,6 +18,7 @@ export const GET = handler(async (request: Request) => { const { notes, exitsJson, executionIdsJson, tagsJson, mistakesJson, ...summary } = row; return { ...(listView ? summary : row), + contractMultiplier: trades[index]!.contractMultiplier ?? null, tags: trades[index]!.annotations?.tags ?? [], mistakes: trades[index]!.annotations?.mistakes ?? [], reviewed: row.reviewedAt !== null, diff --git a/apps/web/src/app/trades/[key]/page.tsx b/apps/web/src/app/trades/[key]/page.tsx index 66def4af..be323a7f 100644 --- a/apps/web/src/app/trades/[key]/page.tsx +++ b/apps/web/src/app/trades/[key]/page.tsx @@ -38,6 +38,7 @@ import { ReviewExport } from "@/components/review-export"; import { RuleChecklist } from "@/components/rule-checklist"; import { useAutosave } from "@/lib/use-autosave"; import { postJson, useApi } from "@/lib/use-api"; +import { netReturnOnNotional } from "@luxalgo/journal-core"; import { fmtDuration, fmtMoney, fmtNumber, fmtPercent } from "@/lib/utils"; import { tradeKeyFromSegment } from "@/lib/trade-links"; import { formatTimestamp } from "@/lib/timezone"; @@ -191,17 +192,7 @@ function TradeView({ tradeKey }: { tradeKey: string }) { 0 && - (trade.contractMultiplier !== null || - !["futures", "option", "forex", "cfd"].includes(trade.assetClass ?? "")) - ? trade.netPnl / - (Math.abs(trade.avgEntry) * - trade.quantity * - (trade.contractMultiplier ?? 1)) - : null, - 2, - )} + value={fmtPercent(netReturnOnNotional(trade), 2)} /> - row.avgEntry * row.quantity > 0 ? row.netPnl / (row.avgEntry * row.quantity) : 0, + accessorFn: (row) => netReturnOnNotional(row) ?? undefined, header: "Net ROI", - cell: ({ getValue }) => {fmtPercent(getValue(), 2)}, + cell: ({ getValue }) => ( + {fmtPercent(getValue() ?? null, 2)} + ), }, { id: "fees", diff --git a/packages/core/src/analysis.ts b/packages/core/src/analysis.ts index 7c54fb75..c3f0340d 100644 --- a/packages/core/src/analysis.ts +++ b/packages/core/src/analysis.ts @@ -44,6 +44,29 @@ export const readFilters = (params: { get(key: string): string | null }): Analys ); /** Planned risk uses the weighted entry and total entry quantity, not peak concurrent exposure. */ +/** Asset classes whose price is per unit of a contract, so notional needs the contract multiplier. */ +const MULTIPLIER_ASSETS = ["futures", "option", "forex", "cfd"]; + +export interface NotionalReturnInput { + netPnl: number; + avgEntry: number; + quantity: number; + contractMultiplier?: number | null; + assetClass?: string | null; +} + +/** + * Net P&L as a fraction of entry notional (|avg entry| × quantity × contract multiplier). + * Null when the notional is unknown: a futures/option/forex/CFD trade without a configured + * multiplier, or a zero notional. The trade list and the trade page both use this, so they always agree. + */ +export const netReturnOnNotional = (trade: NotionalReturnInput): number | null => { + const multiplier = trade.contractMultiplier ?? null; + if (multiplier === null && MULTIPLIER_ASSETS.includes(trade.assetClass ?? "")) return null; + const notional = Math.abs(trade.avgEntry) * trade.quantity * (multiplier ?? 1); + return notional > 0 && Number.isFinite(notional) ? trade.netPnl / notional : null; +}; + export function tradeRisk(trade: AnnotatedTrade): number | null { const stop = trade.annotations?.stopLoss; const multiplier = diff --git a/packages/core/tests/net-return.test.ts b/packages/core/tests/net-return.test.ts new file mode 100644 index 00000000..362f061f --- /dev/null +++ b/packages/core/tests/net-return.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, it } from "vitest"; +import { netReturnOnNotional } from "../src/analysis"; + +describe("net return on entry notional", () => { + it("applies the futures contract multiplier", () => { + // 4 MNQ at 31,157.25 ($2/point): notional $249,258; net $283 + const r = netReturnOnNotional({ + netPnl: 283, + avgEntry: 31157.25, + quantity: 4, + contractMultiplier: 2, + assetClass: "futures", + }); + expect(r).toBeCloseTo(283 / 249258, 10); + }); + + it("applies the option contract size", () => { + // 6 spreads sold at a 2.01 credit (100 shares each): notional $1,206; net $296.40 + const r = netReturnOnNotional({ + netPnl: 296.4, + avgEntry: 2.01, + quantity: 6, + contractMultiplier: 100, + assetClass: "option", + }); + expect(r).toBeCloseTo(296.4 / 1206, 10); + }); + + it("is unknown, not a wrong number, when a multiplier-priced trade has no multiplier", () => { + for (const assetClass of ["futures", "option", "forex", "cfd"]) { + expect( + netReturnOnNotional({ netPnl: 100, avgEntry: 50, quantity: 1, assetClass }), + ).toBeNull(); + } + }); + + it("treats equities and crypto as one unit per share or coin", () => { + expect( + netReturnOnNotional({ netPnl: 50, avgEntry: 100, quantity: 10, assetClass: "equity" }), + ).toBeCloseTo(0.05, 10); + expect(netReturnOnNotional({ netPnl: 50, avgEntry: 100, quantity: 10 })).toBeCloseTo(0.05, 10); + }); + + it("uses the absolute entry price (negative-priced spreads and calendars)", () => { + expect( + netReturnOnNotional({ + netPnl: 25, + avgEntry: -0.5, + quantity: 1, + contractMultiplier: 100, + assetClass: "option", + }), + ).toBeCloseTo(0.5, 10); + }); + + it("is unknown for a zero notional instead of showing 0%", () => { + expect( + netReturnOnNotional({ netPnl: 10, avgEntry: 0, quantity: 3, assetClass: "equity" }), + ).toBeNull(); + }); +});