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, }; }