- lib/instrument-specs.ts (pure, tested): the CME specs — point value, tick size, one-tick spread, Yahoo ticker — replacing POINT_VALUES; the draw's maths; the 12-hour hold cap; the micro for drain and extra-day legs; reading a firm's list (the master's, or the old rules against an older master: this machine's enabled set less bans, the symbol map). - lib/instruments.ts: the one module the trading paths ask. Measures every future's daily move from Yahoo's hourly candles once per trade date, and shows entry cost (a tick of spread plus Tradovate's fee parameters or TopstepX's last measured fill) without ever choosing by it. - Futures Auto draws one root per batch from the batch firm's list: movement per dollar of spread early in the session with a 10% floor each, sliding to the fastest movers by the last entry; nothing expected to hold 12h or more; micros only if nothing else is listed. Every draw logs its odds. - The batch is chosen before its instrument, and a group that can't trade anything yields to the next. Copy trade matches the held contract against the listed roots and won't copy an instrument it can't identify. Manual legs, manual orders and drain/extra-day legs follow the lists. - DXtrade firms rank their own list whatever root was drawn. - Capabilities, the dashboard's trade bar and contract resolves follow the lists; the trade bar and scheduler default to Auto. The master link reconnects when master_dashboard_url changes, and the catalog drops ?since after a switch so two masters' versions can't collide. - Reports carry each futures firm's instrumentStats for the master's Firms page; GET /api/instruments shows them (?simulate=N runs the draw, no orders); the Settings page shows the lists read-only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
1.5 KiB
TypeScript
35 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { runTrade, startScheduler } from '@/lib/auto-trade';
|
|
import { errorMessage } from '@/lib/platform-client';
|
|
import { CME_SPECS } from '@/lib/instrument-specs';
|
|
|
|
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' && !CME_SPECS[symbol]) {
|
|
return NextResponse.json({ error: `Unknown symbol: ${symbol}` }, { status: 400 });
|
|
}
|
|
|
|
// (Re)start the scheduler with this action + symbol first, so a batch
|
|
// that throws or runs long can never leave it unstarted. It can't
|
|
// double-fire against the batch below: the tick waits for flat
|
|
// positions, skips while this batch holds the trade mutex, and never
|
|
// picks an account whose order is still in flight.
|
|
startScheduler(action, symbol, stopAfterAll ?? false);
|
|
|
|
// Execute trade immediately for all eligible accounts
|
|
const results = await runTrade(action, symbol);
|
|
|
|
return NextResponse.json(results);
|
|
} catch (err: unknown) {
|
|
console.error(`[POST /api/trade] ${errorMessage(err)}`);
|
|
return NextResponse.json({ error: errorMessage(err) || 'Trade failed' }, { status: 500 });
|
|
}
|
|
}
|