Files
autofirmer-expanded/lib/clients.ts
T
SenofyandClaude Sonnet 4.6 dd18f91584 Add full Next.js autotrader app with SQLite persistence and live Tradovate data
- SQLite DB (better-sqlite3) with firms, account_configs, firm_fees, instruments tables
- REST API routes: firms CRUD, account configs CRUD, state, accounts, instruments
- Live Tradovate WebSocket client: login, sync, positions, auto-liq thresholds
- Dashboard (app/page.tsx): per-firm account list with balance, day P&L, days traded,
  target progress, and Dead/Inactive/Flat status based on Tradovate auto-liq floors
- Account detail page: objectives progress, daily P&L chart, consistency tracking
- Per-firm settings page: account configs and instrument fee management
- Dead detection uses trailingMaxDrawdownLimit - trailingMaxDrawdown from
  userAccountAutoLiqs; filters Tradovate sentinel value (999999999 = no limit)
- FIFO P&L engine with commission accounting for daily P&L history
- Removed manual maxLoss fallback in favour of live Tradovate auto-liq data

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-08 15:03:21 -05:00

62 lines
2.3 KiB
TypeScript

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'];
// 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();
let feesInitialized = false;
const client = new TradovateClient(username, password, async () => {
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 {
ensureMap().delete(id);
}
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;
}