Files
signal-platform/frontend/src/lib/paperTrade.ts
T
dennisthiessenandClaude Opus 4.8 a69557f5d8
Deploy / lint (push) Successful in 5s
Deploy / test (push) Successful in 35s
Deploy / deploy (push) Successful in 24s
add paper trading: mark a setup as taken, track open P&L, sell
New paper_trades table (migration 007) + service/router. "Mark as taken" on each
setup card (shares prefilled from position sizing, entry from current price, both
editable) records a simulated trade. Overview gains an Open Trades table that
marks each position to the latest close — P&L in $, %, and R-multiples — with a
total unrealized P&L footer and a Sell button to close at the current price.
Closed trades are retained for future realized-P&L reporting.

Deploy: alembic upgrade (new paper_trades table).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 06:33:56 +02:00

26 lines
756 B
TypeScript

import type { PaperTrade } from './types';
export interface TradePnl {
/** Reference price: live close for open trades, exit price for closed. */
ref: number;
perShare: number;
pnl: number;
pct: number;
/** Profit in R-multiples (relative to the per-share risk), null if no risk. */
r: number | null;
}
export function tradePnl(t: PaperTrade): TradePnl | null {
const ref = t.current_price;
if (ref == null || !t.entry_price) return null;
const perShare = t.direction === 'long' ? ref - t.entry_price : t.entry_price - ref;
const risk = Math.abs(t.entry_price - t.stop_loss);
return {
ref,
perShare,
pnl: perShare * t.shares,
pct: (perShare / t.entry_price) * 100,
r: risk > 0 ? perShare / risk : null,
};
}