Add auto-trade scheduler with batch locking, commission gross-up, and sync gate

- Auto-trade scheduler fires every 60s; uses Promise.allSettled batch so no new trades fire while any position from the current batch is open
- Commission gross-up: read entryCommission from cash.realizedPnL after fill (fallback 2.5×contracts), grossTarget = target + 2×entryCommission
- Sync gate: TradovateClient.syncComplete flag; scheduler skips tick until every client finishes initial position/balance sync
- Contracts formula changed to Math.ceil so $1500 target = 2 contracts
- Removed all fee caching (perContractFees, recentFills, fillFee handler) from tradovate-class.ts
- Removed firm_fees table, getFirmFees, upsertFirmFee from db.ts
- Deleted instrument-configs API routes; removed Fees UI from firm settings page
- /api/instruments returns full {symbol, enabled}[] objects; dashboard filters to enabled-only for trade selector
- Added auto-trade, debug, orders, settings, and trade API routes
- Instrument selector on dashboard now driven by enabled instruments from DB

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Senofy
2026-03-09 03:31:47 -05:00
co-authored by Claude Sonnet 4.6
parent 532c2e2279
commit b2a1bdd1c3
19 changed files with 1050 additions and 218 deletions
+16 -18
View File
@@ -1,7 +1,5 @@
import { TradovateClient } from './tradovate-class';
import { getFirms, upsertFirmFee } from './db';
const SYMBOLS = ['NQ', 'MNQ', 'ES', 'MES', 'YM', 'MYM', 'RTY', 'M2K', 'GC', 'MGC', 'SI', 'CL', 'MCL', 'NG', 'ZB', 'ZN', 'ZF', '6E', '6J', '6B'];
import { getFirms } from './db';
// Use global to persist the client pool across HMR reloads in dev mode
const g = global as typeof globalThis & {
@@ -18,31 +16,31 @@ function ensureMap(): Map<number, TradovateClient> {
export function initClient(id: number, username: string, password: string, firmName: string): TradovateClient {
const map = ensureMap();
let feesInitialized = false;
const client = new TradovateClient(username, password, async () => {
const client = new TradovateClient(username, password, () => {
console.log(`[${firmName}] sync complete — ${client.accountList.length} account(s)`);
if (!feesInitialized) {
feesInitialized = true;
try {
const fees = await client.fetchInstrumentFees(SYMBOLS);
for (const [symbol, fee] of Object.entries(fees)) {
upsertFirmFee(id, symbol, fee, parseFloat((fee * 2).toFixed(4)));
}
const count = Object.keys(fees).length;
if (count > 0) console.log(`[${firmName}] Auto-fetched fees for ${count} symbol(s)`);
} catch (err) {
console.error(`[${firmName}] Failed to auto-fetch fees`, err);
}
}
});
map.set(id, client);
return client;
}
export function removeClient(id: number): void {
const client = ensureMap().get(id);
client?.disconnect();
ensureMap().delete(id);
}
/** Disconnect all clients and clear the pool so they are recreated on next getClients() call. */
export function resetClients(): void {
const map = g.__tradovateClients;
if (map) {
for (const client of map.values()) {
try { client.disconnect(); } catch { /* ignore */ }
}
}
g.__tradovateClients = undefined;
g.__tradovateClientsInitialized = false;
}
export function getClients(): Map<number, TradovateClient> {
const map = ensureMap();
if (!g.__tradovateClientsInitialized) {