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>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
@@ -179,6 +179,21 @@ export interface SentimentProviderConfig {
|
||||
custom_base_url_providers: string[];
|
||||
}
|
||||
|
||||
export interface PaperTrade {
|
||||
id: number;
|
||||
symbol: string;
|
||||
direction: 'long' | 'short';
|
||||
entry_price: number;
|
||||
shares: number;
|
||||
stop_loss: number;
|
||||
target: number;
|
||||
status: 'open' | 'closed';
|
||||
opened_at: string;
|
||||
close_price: number | null;
|
||||
closed_at: string | null;
|
||||
current_price: number | null;
|
||||
}
|
||||
|
||||
export interface BacktestBucket {
|
||||
total: number;
|
||||
wins: number;
|
||||
|
||||
Reference in New Issue
Block a user