Block runTrade when positions are open on restart

The POST handler calls runTrade() directly, bypassing the scheduler
tick's position gate. If you stop and restart with open positions,
other accounts could get new trades while existing ones haven't closed.
Now runTrade itself checks for open positions before proceeding.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-04-16 03:55:41 -05:00
co-authored by Claude Opus 4.6
parent 58628e0ce1
commit 99e473c687
+14
View File
@@ -112,6 +112,20 @@ export async function runTrade(action: 'Buy' | 'Sell' | 'Auto', symbol: string)
console.log('[auto-trade] CME market closed — skipping');
return [];
}
// Block if any account still has an open position (same gate as the scheduler tick)
const allClients = getClients();
const allFirms = getFirms();
const openPositions = allFirms.reduce((count, firm) => {
const c = allClients.get(firm.id);
if (!c) return count;
return count + c.accountList.filter(acc => !!c.positions[acc.id]).length;
}, 0);
if (openPositions > 0) {
console.log(`[auto-trade] ${openPositions} position(s) still open — skipping`);
return [];
}
// Resolve 'Auto' symbol once per batch so all accounts trade the same symbol
let resolvedSymbol = symbol;
if (symbol === 'Auto') {