import { NextRequest, NextResponse } from 'next/server'; import { runTrade, startScheduler } from '@/lib/auto-trade'; import { POINT_VALUES } from '@/lib/trading-logic'; export async function POST(req: NextRequest) { try { 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 !== 'Auto' && !POINT_VALUES[symbol]) { return NextResponse.json({ error: `Unknown symbol: ${symbol}` }, { status: 400 }); } // Execute trade immediately for all eligible accounts const results = await runTrade(action, symbol); // (Re)start the scheduler with this action + symbol startScheduler(action, symbol, stopAfterAll ?? false); return NextResponse.json(results); } catch (err: any) { console.error('[POST /api/trade]', err); return NextResponse.json({ error: err?.message ?? 'Trade failed' }, { status: 500 }); } }