diff --git a/lib/auto-trade.ts b/lib/auto-trade.ts index 8a31288..afe8f8d 100644 --- a/lib/auto-trade.ts +++ b/lib/auto-trade.ts @@ -50,24 +50,49 @@ function mapFirmConfig(firm: FirmWithAccounts): FirmConfig { // ── helpers ─────────────────────────────────────────────────────────────────── -/** Returns true during the 3:00 PM – 4:59 PM Central no-trade window. */ +/** + * Returns true when CME futures markets are closed. + * CME hours: Sunday 5:00 PM – Friday 4:00 PM Central, with a daily halt 4:00–5:00 PM. + * So we block trading when: + * - It's 3:00 PM – 4:59 PM Central (stop early + daily halt) + * - It's Saturday (all day) + * - It's Sunday before 5:00 PM Central + * - It's Friday after 3:00 PM Central + */ function isInNoTradeWindow(): boolean { - const hour = parseInt( - new Intl.DateTimeFormat('en-US', { - hour: 'numeric', - hour12: false, - timeZone: 'America/Chicago', - }).format(new Date()), - 10 - ); - return hour >= 15 && hour < 17; + const now = new Date(); + const centralTime = new Intl.DateTimeFormat('en-US', { + hour: 'numeric', + hour12: false, + timeZone: 'America/Chicago', + }); + const centralDay = new Intl.DateTimeFormat('en-US', { + weekday: 'short', + timeZone: 'America/Chicago', + }); + const hour = parseInt(centralTime.format(now), 10); + const day = centralDay.format(now); // "Sun", "Mon", ... "Sat" + + // Saturday — market closed all day + if (day === 'Sat') return true; + + // Sunday — market opens at 5 PM Central + if (day === 'Sun' && hour < 17) return true; + + // Friday — stop trading after 3 PM Central + if (day === 'Fri' && hour >= 15) return true; + + // Mon–Thu: stop trading after 3 PM Central (market halts at 4 PM, reopens 5 PM) + if (hour >= 15 && hour < 17) return true; + + return false; } // ── core trade logic ────────────────────────────────────────────────────────── export async function runTrade(action: 'Buy' | 'Sell' | 'Random', symbol: string) { if (isInNoTradeWindow()) { - console.log('[auto-trade] no-trade window active (3–5 PM Central) — skipping'); + console.log('[auto-trade] CME market closed — skipping'); return []; } // Resolve 'Random' symbol once per batch so all accounts trade the same symbol