From 5209cc522e34622c7d48d20f333ae46b391384e1 Mon Sep 17 00:00:00 2001 From: Celes Renata Date: Thu, 30 Apr 2026 05:49:38 +0000 Subject: [PATCH] feat: enhanced paper trading reset with capital and reserve controls - Add initial capital input (toggle between broker balance or custom amount) - Add reserve/active pool split slider (0-50%, default 20%) - Backend accepts reserve_pct in reset request body - Note in UI that Alpaca balance reset requires Alpaca dashboard - Confirmation dialog shows exact capital and split being applied --- frontend/src/api/tradingHooks.ts | 7 ++- frontend/src/pages/Trading.tsx | 96 ++++++++++++++++++++++++++++++-- services/trading/app.py | 4 +- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/frontend/src/api/tradingHooks.ts b/frontend/src/api/tradingHooks.ts index 2b1e5c6..1856985 100644 --- a/frontend/src/api/tradingHooks.ts +++ b/frontend/src/api/tradingHooks.ts @@ -316,9 +316,12 @@ export function useBacktestLaunch() { export function useResetPaperTrading() { const qc = useQueryClient(); return useMutation({ - mutationFn: (initial_capital: number = 0) => + mutationFn: (params: { initial_capital?: number; reserve_pct?: number } = {}) => apiPost<{ reset: boolean; initial_capital: number; active_pool: number; reserve_pool: number; broker: Record }>( - 'trading', '/api/trading/reset', { initial_capital }, + 'trading', '/api/trading/reset', { + initial_capital: params.initial_capital ?? 0, + reserve_pct: params.reserve_pct ?? undefined, + }, ), onSuccess: () => { qc.invalidateQueries({ queryKey: ['trading-status'] }); diff --git a/frontend/src/pages/Trading.tsx b/frontend/src/pages/Trading.tsx index bf47c6d..0a618aa 100644 --- a/frontend/src/pages/Trading.tsx +++ b/frontend/src/pages/Trading.tsx @@ -116,7 +116,7 @@ export function TradingPage() { {/* Paper Trading Reset */} resetTrading.mutate(0)} + onReset={(params) => resetTrading.mutate(params)} isResetting={resetTrading.isPending} /> @@ -490,26 +490,101 @@ function ApprovalRow({ approval, onReview }: { function ResetCard({ onReset, isResetting }: { - onReset: () => void; + onReset: (params: { initial_capital?: number; reserve_pct?: number }) => void; isResetting: boolean; }) { const [showConfirm, setShowConfirm] = useState(false); + const [capitalInput, setCapitalInput] = useState('100000'); + const [reservePct, setReservePct] = useState(20); + const [useCustomCapital, setUseCustomCapital] = useState(false); + + const capital = parseFloat(capitalInput) || 0; + const reserveAmount = capital * (reservePct / 100); + const activeAmount = capital - reserveAmount; return (

Paper Trading Account

+ + {/* Capital & Reserve Configuration */} +
+
+ + Set initial capital + (otherwise uses broker account balance) +
+ + {useCustomCapital && ( +
+ + setCapitalInput(e.target.value)} + className="w-36 rounded-md border border-surface-700 bg-surface-900 px-2 py-1 text-sm font-mono text-gray-200" + /> +
+ )} + +
+
+ + + Active: {100 - reservePct}% + +
+ setReservePct(Number(e.target.value))} + className="mt-1 w-full accent-brand-600" + /> + {useCustomCapital && capital > 0 && ( +
+ Reserve: ${reserveAmount.toLocaleString(undefined, { maximumFractionDigits: 0 })} + Active: ${activeAmount.toLocaleString(undefined, { maximumFractionDigits: 0 })} +
+ )} +
+
+ + {/* Reset Button */}

Full Reset

Liquidates all broker positions, cancels open orders, wipes local trading history, - and syncs capital from the broker account. + and sets capital from {useCustomCapital ? 'the amount above' : 'the broker account balance'}. +

+

+ Note: To reset the Alpaca paper account balance itself, use the Alpaca dashboard.

@@ -519,11 +594,20 @@ function ResetCard({ onReset, isResetting }: {

This will permanently delete all positions, orders, trading decisions, stop levels, portfolio snapshots, and backtest data. - All broker positions will be liquidated and capital will be set from the broker's account balance. + All broker positions will be liquidated. + {useCustomCapital + ? ` Capital will be set to $${capital.toLocaleString()} (${reservePct}% reserve / ${100 - reservePct}% active).` + : ` Capital will be set from the broker's account balance (${reservePct}% reserve / ${100 - reservePct}% active).`}