Fix consistency bug, rename Random to Auto, add stop-after-all, direction pills, copy-trade
Bug fixes: - Fix computeDailyTarget when consistency is 0% or 100%: treat as no constraint, letting min-day reservation or full remaining profit drive the target - Rename 'Random' to 'Auto' across entire codebase (types, API, UI, scheduler) Features: - Add "Stop after all eligible" checkbox: auto-stops scheduler when all configured accounts are dead, inactive, already traded, or challenge complete - Show position direction in status pill: "Long" (green) / "Short" (red) instead of generic "In Trade" (blue) - Add "Copy to Max" button: copies current trade direction to remaining eligible accounts up to max_concurrent_accounts limit Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
8d9a9a5ea9
commit
df793bfd70
@@ -0,0 +1,12 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { copyTrade } from '@/lib/auto-trade';
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
const results = await copyTrade();
|
||||
return NextResponse.json(results);
|
||||
} catch (err: any) {
|
||||
console.error('[POST /api/copy-trade]', err);
|
||||
return NextResponse.json({ error: err?.message ?? 'Copy trade failed' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,9 @@ export async function GET() {
|
||||
amount: cash.amount,
|
||||
realizedPnL: cash.realizedPnL,
|
||||
daysTraded,
|
||||
hasPosition: !!client.positions[acc.id],
|
||||
positionDirection: client.positions[acc.id]
|
||||
? (client.positions[acc.id].netPos > 0 ? 'long' as const : 'short' as const)
|
||||
: null,
|
||||
autoLiqThreshold,
|
||||
totalProfit,
|
||||
targetHit,
|
||||
|
||||
@@ -4,14 +4,14 @@ import { POINT_VALUES } from '@/lib/trading-logic';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json() as { action: 'Buy' | 'Sell' | 'Random'; symbol: string };
|
||||
const { action, symbol } = body;
|
||||
const body = await req.json() as { action: 'Buy' | 'Sell' | 'Auto'; symbol: string; stopAfterAll?: boolean };
|
||||
const { action, symbol, stopAfterAll } = body;
|
||||
|
||||
if (!action || !symbol) {
|
||||
return NextResponse.json({ error: 'Missing required fields: action, symbol' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (symbol !== 'Random' && !POINT_VALUES[symbol]) {
|
||||
if (symbol !== 'Auto' && !POINT_VALUES[symbol]) {
|
||||
return NextResponse.json({ error: `Unknown symbol: ${symbol}` }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export async function POST(req: NextRequest) {
|
||||
const results = await runTrade(action, symbol);
|
||||
|
||||
// (Re)start the scheduler with this action + symbol
|
||||
startScheduler(action, symbol);
|
||||
startScheduler(action, symbol, stopAfterAll ?? false);
|
||||
|
||||
return NextResponse.json(results);
|
||||
} catch (err: any) {
|
||||
|
||||
+67
-23
@@ -11,7 +11,7 @@ type SortDir = 'asc' | 'desc';
|
||||
function statusRank(account: AccountState): number {
|
||||
if (isAccountDead(account)) return 0;
|
||||
if (!account.active) return 1;
|
||||
if (!account.hasPosition) {
|
||||
if (!account.positionDirection) {
|
||||
if (account.targetHit) return 4; // Target Hit — most accomplished
|
||||
return 2; // Flat
|
||||
}
|
||||
@@ -147,8 +147,8 @@ function AccountRow({ account, firm, hideDead, privacy }: { account: AccountStat
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-slate-800 text-white">Dead</span>
|
||||
: !account.active
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-red-100 text-red-700">Hit DLL</span>
|
||||
: account.hasPosition
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-blue-100 text-blue-700">In Trade</span>
|
||||
: account.positionDirection
|
||||
? <span className={`inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold ${account.positionDirection === 'long' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}>{account.positionDirection === 'long' ? 'Long' : 'Short'}</span>
|
||||
: account.targetHit
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-green-100 text-green-700">Target Hit</span>
|
||||
: <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-slate-100 text-slate-500">Flat</span>}
|
||||
@@ -258,10 +258,11 @@ function FirmRows({ state, firm, deleteMode, selected, onToggle, hideDead, priva
|
||||
|
||||
interface SchedulerStatus {
|
||||
running: boolean;
|
||||
action: 'Buy' | 'Sell' | 'Random';
|
||||
action: 'Buy' | 'Sell' | 'Auto';
|
||||
symbol: string;
|
||||
lastRun: string | null;
|
||||
intervalSeconds: number;
|
||||
stopAfterAll: boolean;
|
||||
}
|
||||
|
||||
export default function Home() {
|
||||
@@ -276,12 +277,13 @@ export default function Home() {
|
||||
const [sortDir, setSortDir] = useState<SortDir>('asc');
|
||||
|
||||
// Trade controls
|
||||
const [scheduler, setScheduler] = useState<SchedulerStatus>({ running: false, action: 'Buy', symbol: 'NQ', lastRun: null, intervalSeconds: 60 });
|
||||
const [scheduler, setScheduler] = useState<SchedulerStatus>({ running: false, action: 'Buy', symbol: 'NQ', lastRun: null, intervalSeconds: 60, stopAfterAll: false });
|
||||
const [enabledSymbols, setEnabledSymbols] = useState<string[]>([]);
|
||||
const [tradeSymbol, setTradeSymbol] = useState('');
|
||||
const [tradeAction, setTradeAction] = useState<'Buy' | 'Sell' | 'Random'>('Buy');
|
||||
const [tradeAction, setTradeAction] = useState<'Buy' | 'Sell' | 'Auto'>('Buy');
|
||||
const [tickInterval, setTickInterval] = useState('60');
|
||||
const [tradeLoading, setTradeLoading] = useState(false);
|
||||
const [stopAfterAll, setStopAfterAll] = useState(false);
|
||||
|
||||
const handleSort = (col: SortKey) => {
|
||||
if (sortKey === col) {
|
||||
@@ -355,7 +357,7 @@ export default function Home() {
|
||||
await fetch('/api/trade', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ action: tradeAction, symbol: tradeSymbol }),
|
||||
body: JSON.stringify({ action: tradeAction, symbol: tradeSymbol, stopAfterAll }),
|
||||
});
|
||||
await fetchScheduler();
|
||||
} finally {
|
||||
@@ -368,6 +370,17 @@ export default function Home() {
|
||||
setScheduler((s) => ({ ...s, running: false, lastRun: null }));
|
||||
};
|
||||
|
||||
const hasAnyPosition = firms.some(f => f.accounts.some(a => a.positionDirection !== null));
|
||||
const [copyLoading, setCopyLoading] = useState(false);
|
||||
const handleCopy = async () => {
|
||||
setCopyLoading(true);
|
||||
try {
|
||||
await fetch('/api/copy-trade', { method: 'POST' });
|
||||
} finally {
|
||||
setCopyLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Count dead accounts across all firms for the toggle button label
|
||||
const deadCount = firms.reduce((total, firmState) => {
|
||||
const firmCfg = config.find((c) => c.firm === firmState.firm);
|
||||
@@ -476,12 +489,23 @@ export default function Home() {
|
||||
· last run {new Date(scheduler.lastRun).toLocaleTimeString()}
|
||||
</span>
|
||||
)}
|
||||
<button
|
||||
onClick={handleStop}
|
||||
className="ml-auto px-4 py-1.5 bg-red-100 hover:bg-red-200 text-red-700 text-sm font-semibold rounded-lg transition-colors"
|
||||
>
|
||||
■ Stop
|
||||
</button>
|
||||
<div className="ml-auto flex items-center gap-2">
|
||||
{hasAnyPosition && (
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
disabled={copyLoading}
|
||||
className="px-4 py-1.5 bg-amber-100 hover:bg-amber-200 disabled:opacity-50 text-amber-700 text-sm font-semibold rounded-lg transition-colors"
|
||||
>
|
||||
{copyLoading ? 'Copying…' : 'Copy to Max'}
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={handleStop}
|
||||
className="px-4 py-1.5 bg-red-100 hover:bg-red-200 text-red-700 text-sm font-semibold rounded-lg transition-colors"
|
||||
>
|
||||
■ Stop
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
@@ -494,7 +518,7 @@ export default function Home() {
|
||||
className="rounded-lg border border-slate-200 bg-slate-50 px-2.5 py-1.5 text-sm font-mono text-slate-800 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
{enabledSymbols.map((s) => <option key={s} value={s}>{s}</option>)}
|
||||
<option value="Random">Auto</option>
|
||||
<option value="Auto">Auto</option>
|
||||
</select>
|
||||
<div className="flex rounded-lg border border-slate-200 overflow-hidden text-sm font-semibold">
|
||||
<button
|
||||
@@ -510,8 +534,8 @@ export default function Home() {
|
||||
Sell
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTradeAction('Random')}
|
||||
className={`px-3 py-1.5 transition-colors border-l border-slate-200 ${tradeAction === 'Random' ? 'bg-purple-500 text-white' : 'text-slate-500 hover:bg-slate-50'}`}
|
||||
onClick={() => setTradeAction('Auto')}
|
||||
className={`px-3 py-1.5 transition-colors border-l border-slate-200 ${tradeAction === 'Auto' ? 'bg-purple-500 text-white' : 'text-slate-500 hover:bg-slate-50'}`}
|
||||
>
|
||||
Auto
|
||||
</button>
|
||||
@@ -538,13 +562,33 @@ export default function Home() {
|
||||
<span className="text-xs text-slate-400">s</span>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleStart}
|
||||
disabled={tradeLoading}
|
||||
className="ml-auto px-4 py-1.5 bg-blue-500 hover:bg-blue-600 disabled:opacity-50 text-white text-sm font-semibold rounded-lg transition-colors"
|
||||
>
|
||||
{tradeLoading ? 'Starting…' : '▶ Start'}
|
||||
</button>
|
||||
<label className="flex items-center gap-1.5 ml-2 cursor-pointer select-none">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={stopAfterAll}
|
||||
onChange={(e) => setStopAfterAll(e.target.checked)}
|
||||
className="rounded border-slate-300 text-blue-500 focus:ring-blue-500"
|
||||
/>
|
||||
<span className="text-xs text-slate-400 whitespace-nowrap">Stop after all eligible</span>
|
||||
</label>
|
||||
<div className="ml-auto flex items-center gap-2">
|
||||
{hasAnyPosition && (
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
disabled={copyLoading}
|
||||
className="px-4 py-1.5 bg-amber-100 hover:bg-amber-200 disabled:opacity-50 text-amber-700 text-sm font-semibold rounded-lg transition-colors"
|
||||
>
|
||||
{copyLoading ? 'Copying…' : 'Copy to Max'}
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={handleStart}
|
||||
disabled={tradeLoading}
|
||||
className="px-4 py-1.5 bg-blue-500 hover:bg-blue-600 disabled:opacity-50 text-white text-sm font-semibold rounded-lg transition-colors"
|
||||
>
|
||||
{tradeLoading ? 'Starting…' : '▶ Start'}
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user