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>
26 lines
756 B
TypeScript
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,
|
|
};
|
|
}
|