- 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>
55 lines
2.4 KiB
TypeScript
55 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getCachedFirm } from '@/lib/firm-catalog';
|
|
import { firmInstruments, isFuturesFirm } from '@/lib/instruments';
|
|
import { getClients } from '@/lib/clients';
|
|
|
|
export async function POST(
|
|
req: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id: firmId } = await params;
|
|
|
|
const body = await req.json() as {
|
|
accountId: number;
|
|
symbol: string; // product name, e.g. "NQ"
|
|
qty: number;
|
|
action: 'Buy' | 'Sell';
|
|
orderType?: 'Market' | 'Limit';
|
|
price?: number;
|
|
};
|
|
|
|
const { accountId, symbol, qty, action, orderType = 'Market', price } = body;
|
|
if (!accountId || !symbol || !qty || !action) {
|
|
return NextResponse.json({ error: 'Missing required fields: accountId, symbol, qty, action' }, { status: 400 });
|
|
}
|
|
|
|
const clients = getClients();
|
|
const client = clients.get(firmId);
|
|
if (!client) return NextResponse.json({ error: 'Client not found for firm' }, { status: 404 });
|
|
|
|
// A futures firm trades only what its instrument list (master Firms
|
|
// page) names.
|
|
const firm = getCachedFirm(firmId);
|
|
if (firm && isFuturesFirm(firm) && !firmInstruments(firm).includes(symbol)) {
|
|
return NextResponse.json({ error: `${symbol} isn't on ${firm.name}'s instrument list` }, { status: 400 });
|
|
}
|
|
|
|
// 1. Find the active contract chosen by the shared resolver
|
|
const contract = await client.findFrontMonthContract(symbol);
|
|
if (!contract) {
|
|
return NextResponse.json({ error: `Could not find an active contract for ${symbol}` }, { status: 404 });
|
|
}
|
|
console.log(`[order] active contract: ${contract.name} (id=${contract.id})`);
|
|
|
|
// 2. Place the order — resolves with the fill event (includes commission/fees)
|
|
const fill = await client.sendOrder(accountId, contract.name, qty, action, orderType, price);
|
|
console.log(`[order] fill received: price=${fill.price} qty=${fill.qty} commission=$${fill.commission} perContract=$${fill.perContractFee}`);
|
|
|
|
return NextResponse.json({ contract: { id: contract.id, name: contract.name }, fill });
|
|
} catch (err: any) {
|
|
console.error('[POST /api/firms/[id]/orders]', err);
|
|
return NextResponse.json({ error: err?.message ?? 'Order failed' }, { status: 500 });
|
|
}
|
|
}
|