- 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>
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { TradovateClient } from './tradovate-class';
|
|
import { getFirms } from './db';
|
|
|
|
// Use global to persist the client pool across HMR reloads in dev mode
|
|
const g = global as typeof globalThis & {
|
|
__tradovateClients?: Map<number, TradovateClient>;
|
|
__tradovateClientsInitialized?: boolean;
|
|
};
|
|
|
|
function ensureMap(): Map<number, TradovateClient> {
|
|
if (!g.__tradovateClients) {
|
|
g.__tradovateClients = new Map();
|
|
}
|
|
return g.__tradovateClients;
|
|
}
|
|
|
|
export function initClient(id: number, username: string, password: string, firmName: string): TradovateClient {
|
|
const map = ensureMap();
|
|
const client = new TradovateClient(username, password, () => {
|
|
console.log(`[${firmName}] sync complete — ${client.accountList.length} account(s)`);
|
|
});
|
|
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) {
|
|
g.__tradovateClientsInitialized = true;
|
|
try {
|
|
const firms = getFirms();
|
|
for (const firm of firms) {
|
|
initClient(firm.id, firm.username, firm.password, firm.name);
|
|
}
|
|
console.log(`[clients] Initialized ${firms.length} Tradovate client(s)`);
|
|
} catch (err) {
|
|
console.error('[clients] Failed to initialize clients', err);
|
|
}
|
|
}
|
|
return map;
|
|
}
|