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,38 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import * as api from '../api/paperTrades';
|
||||
import { useToast } from '../components/ui/Toast';
|
||||
|
||||
export function usePaperTrades(status?: 'open' | 'closed') {
|
||||
return useQuery({
|
||||
queryKey: ['paper-trades', status ?? 'all'],
|
||||
queryFn: () => api.listPaperTrades(status),
|
||||
refetchInterval: 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreatePaperTrade() {
|
||||
const qc = useQueryClient();
|
||||
const { addToast } = useToast();
|
||||
return useMutation({
|
||||
mutationFn: (body: api.CreatePaperTradeBody) => api.createPaperTrade(body),
|
||||
onSuccess: (t) => {
|
||||
qc.invalidateQueries({ queryKey: ['paper-trades'] });
|
||||
addToast('success', `Taken: ${t.shares} ${t.symbol} ${t.direction.toUpperCase()} @ ${t.entry_price}`);
|
||||
},
|
||||
onError: (e: Error) => addToast('error', e.message || 'Failed to take trade'),
|
||||
});
|
||||
}
|
||||
|
||||
export function useClosePaperTrade() {
|
||||
const qc = useQueryClient();
|
||||
const { addToast } = useToast();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, closePrice }: { id: number; closePrice?: number }) =>
|
||||
api.closePaperTrade(id, closePrice),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['paper-trades'] });
|
||||
addToast('success', 'Trade closed.');
|
||||
},
|
||||
onError: (e: Error) => addToast('error', e.message || 'Failed to close trade'),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user