Take firms from the master instead of defining them per machine

This box no longer decides what firms exist. It caches the master's catalog
and stores one thing of its own: the Tradovate login for each firm. Creating
firms locally is gone — app/add, POST /api/firms, the account-config routes
and the banned-symbol route with it.

The cache is the point, not an optimisation. The scheduler needs every
account's target and size on each 60s tick, so fetching them per tick would
stop trading with positions open the first time the master rebooted. It is
one JSON blob in settings, replaced only by a complete valid response, and a
response with zero firms is refused outright: a misconfigured FIRMS_DB serves
{"firms":[]} as a healthy 200, and mirroring that would idle the whole fleet
at once.

Accounts bind to an account type by prefix AND size, the size taken from the
opening funding transaction. That does not arrive with the first sync, so an
account sits Unassigned and the resolver tries again next sync rather than
guessing. A type picked by hand is never overwritten by an auto pass. This
replaces the longest-prefix helper that had been copy-pasted into five files,
which could not tell two types with the same prefix apart at all.

firm_credentials and account_assignments deliberately have no foreign key to
the cached catalog. foreign_keys is ON, so a cascade would erase the Tradovate
passwords the moment a firm was removed on the master — routine, not
exceptional. They survive it, and are re-keyed by name if the master's database
is ever rebuilt with fresh ids.

The legacy firms/account_configs/firm_banned_symbols tables are left on disk,
unreferenced. firms is the only table that ever held plaintext credentials and
dropping it would make a rollback unrecoverable.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-08-31 22:17:23 -05:00
co-authored by Claude Opus 5
parent 8b2e78d766
commit 8bdc85080c
27 changed files with 1489 additions and 1311 deletions
+103 -159
View File
@@ -6,167 +6,138 @@ const db = new Database(path.join(process.cwd(), 'autotrader.sqlite'));
db.pragma('foreign_keys = ON');
db.exec(`
CREATE TABLE IF NOT EXISTS firms (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
username TEXT NOT NULL,
password TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS account_configs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
firm_id INTEGER NOT NULL REFERENCES firms(id) ON DELETE CASCADE,
prefix TEXT NOT NULL,
profit_target REAL NOT NULL DEFAULT 3000,
consistency REAL NOT NULL DEFAULT 0.5,
min_day_pnl REAL NOT NULL DEFAULT -999,
min_trading_days INTEGER NOT NULL DEFAULT 5
);
CREATE TABLE IF NOT EXISTS instruments (
symbol TEXT PRIMARY KEY,
enabled INTEGER NOT NULL DEFAULT 1
);
`);
// Migration: add account_size column if it doesn't exist yet
try {
db.exec('ALTER TABLE account_configs ADD COLUMN account_size REAL NOT NULL DEFAULT 50000');
} catch {
// Column already exists
}
// ── Machine-owned firm state ─────────────────────────────────────────────────
//
// Firms and account types themselves come from the master catalog (see
// lib/firm-catalog.ts) and are cached as one JSON blob in `settings`. These two
// tables hold the parts that are genuinely per-machine and must survive the
// catalog changing underneath them.
//
// Both reference firm ids as plain TEXT with NO foreign key on purpose. This
// file sets `foreign_keys = ON`, so an ON DELETE CASCADE here would wipe the
// Tradovate credentials the moment a firm was removed on the master — and
// mirroring master's deletions is routine, not exceptional. They must be
// orphan-tolerant: a firm that disappears and comes back finds its credentials
// and assignments still here.
db.exec(`
CREATE TABLE IF NOT EXISTS firm_credentials (
firm_id TEXT PRIMARY KEY,
firm_name TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL,
updated_at TEXT NOT NULL
);
// Migration: add max_loss column (0 = no max loss limit)
try {
db.exec('ALTER TABLE account_configs ADD COLUMN max_loss REAL NOT NULL DEFAULT 0');
} catch {
// Column already exists
}
CREATE TABLE IF NOT EXISTS account_assignments (
account_id INTEGER PRIMARY KEY,
firm_id TEXT NOT NULL,
account_type_id TEXT,
manual INTEGER NOT NULL DEFAULT 0,
resolved_at TEXT NOT NULL
);
`);
// Migration: add max_position_size column (0 = no limit)
try {
db.exec('ALTER TABLE account_configs ADD COLUMN max_position_size INTEGER NOT NULL DEFAULT 0');
} catch {
// Column already exists
}
// The legacy `firms`, `account_configs` and `firm_banned_symbols` tables are
// dead: firms now come from the master catalog. They are left on disk rather
// than dropped because `firms` is the only table that ever held plaintext
// Tradovate credentials, and a silent DROP would be unrecoverable if this
// change had to be rolled back. Safe to drop by hand once it has stuck.
// Migration: add target_same_equity flag
try {
db.exec('ALTER TABLE account_configs ADD COLUMN target_same_equity INTEGER NOT NULL DEFAULT 1');
} catch {
// Column already exists
}
// ── Firm credentials ─────────────────────────────────────────────────────────
// Migration: add withdrawal_stages JSON array
try {
db.exec("ALTER TABLE account_configs ADD COLUMN withdrawal_stages TEXT NOT NULL DEFAULT '[]'");
} catch {
// Column already exists
}
// ── Interfaces ─────────────────────────────────────────────────────────────
export interface AccountConfigRow {
id: number;
firm_id: number;
prefix: string;
profit_target: number;
consistency: number;
min_day_pnl: number;
min_trading_days: number;
account_size: number;
max_loss: number;
max_position_size: number;
target_same_equity: number; // 0 | 1
withdrawal_stages: string; // JSON { profit: number; consistency: number; minTradingDays: number }[]
}
export interface FirmRow {
id: number;
name: string;
export interface FirmCredentialRow {
firm_id: string;
firm_name: string;
username: string;
password: string;
updated_at: string;
}
export interface FirmWithAccounts extends FirmRow {
accounts: AccountConfigRow[];
export function getCredentials(firmId: string): { username: string; password: string } | undefined {
const row = db.prepare('SELECT username, password FROM firm_credentials WHERE firm_id = ?').get(firmId) as
{ username: string; password: string } | undefined;
return row;
}
// ── Firms ───────────────────────────────────────────────────────────────────
export function getFirms(): FirmWithAccounts[] {
const firms = db.prepare('SELECT * FROM firms ORDER BY id').all() as FirmRow[];
const getAccounts = db.prepare('SELECT * FROM account_configs WHERE firm_id = ? ORDER BY id');
return firms.map((firm) => ({
...firm,
accounts: getAccounts.all(firm.id) as AccountConfigRow[],
}));
export function listCredentials(): FirmCredentialRow[] {
return db.prepare('SELECT * FROM firm_credentials').all() as FirmCredentialRow[];
}
export function getFirmById(id: number): FirmWithAccounts | undefined {
const firm = db.prepare('SELECT * FROM firms WHERE id = ?').get(id) as FirmRow | undefined;
if (!firm) return undefined;
const accounts = db.prepare('SELECT * FROM account_configs WHERE firm_id = ? ORDER BY id').all(id) as AccountConfigRow[];
return { ...firm, accounts };
/** firm_name is stored alongside the id so credentials can be re-keyed by name
* if the master's database is ever rebuilt and the firms come back with new
* ids. See rekeyCredentialsByName in lib/firm-catalog.ts. */
export function setCredentials(firmId: string, firmName: string, username: string, password: string): void {
db.prepare(`
INSERT INTO firm_credentials (firm_id, firm_name, username, password, updated_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(firm_id) DO UPDATE SET
firm_name = excluded.firm_name,
username = excluded.username,
password = excluded.password,
updated_at = excluded.updated_at
`).run(firmId, firmName, username, password, new Date().toISOString());
}
export function createFirm(name: string, username: string, password: string): FirmRow {
const stmt = db.prepare('INSERT INTO firms (name, username, password) VALUES (?, ?, ?)');
const result = stmt.run(name, username, password);
return db.prepare('SELECT * FROM firms WHERE id = ?').get(result.lastInsertRowid) as FirmRow;
export function clearCredentials(firmId: string): boolean {
return db.prepare('DELETE FROM firm_credentials WHERE firm_id = ?').run(firmId).changes > 0;
}
export function deleteFirm(id: number): boolean {
const result = db.prepare('DELETE FROM firms WHERE id = ?').run(id);
return result.changes > 0;
export function rekeyCredentials(fromFirmId: string, toFirmId: string): void {
db.prepare('UPDATE firm_credentials SET firm_id = ? WHERE firm_id = ?').run(toFirmId, fromFirmId);
db.prepare('UPDATE account_assignments SET firm_id = ? WHERE firm_id = ?').run(toFirmId, fromFirmId);
}
// ── Account Configs ─────────────────────────────────────────────────────────
// ── Account type assignments ─────────────────────────────────────────────────
export function createAccountConfig(firmId: number, data: {
prefix: string;
profitTarget: number;
consistency: number;
minDayPnL: number;
minTradingDays: number;
accountSize: number;
maxLoss: number;
maxPositionSize: number;
targetSameEquity?: boolean;
withdrawalStages?: { profit: number; consistency: number; minTradingDays: number }[];
}): AccountConfigRow {
const stmt = db.prepare(
'INSERT INTO account_configs (firm_id, prefix, profit_target, consistency, min_day_pnl, min_trading_days, account_size, max_loss, max_position_size, target_same_equity, withdrawal_stages) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
);
const result = stmt.run(firmId, data.prefix, data.profitTarget, data.consistency, data.minDayPnL, data.minTradingDays, data.accountSize, data.maxLoss, data.maxPositionSize, data.targetSameEquity ? 1 : 0, JSON.stringify(data.withdrawalStages ?? []));
return db.prepare('SELECT * FROM account_configs WHERE id = ?').get(result.lastInsertRowid) as AccountConfigRow;
export interface AssignmentRow {
account_id: number;
firm_id: string;
account_type_id: string | null;
manual: number; // 0 | 1
resolved_at: string;
}
export function deleteAccountConfig(id: number): boolean {
const result = db.prepare('DELETE FROM account_configs WHERE id = ?').run(id);
return result.changes > 0;
export function getAssignment(accountId: number): AssignmentRow | undefined {
return db.prepare('SELECT * FROM account_assignments WHERE account_id = ?').get(accountId) as AssignmentRow | undefined;
}
export function updateAccountConfig(id: number, data: {
prefix: string;
profitTarget: number;
consistency: number;
minDayPnL: number;
minTradingDays: number;
accountSize: number;
maxLoss: number;
maxPositionSize: number;
targetSameEquity?: boolean;
withdrawalStages?: { profit: number; consistency: number; minTradingDays: number }[];
}): boolean {
const result = db.prepare(`
UPDATE account_configs
SET prefix = ?, profit_target = ?, consistency = ?, min_day_pnl = ?, min_trading_days = ?, account_size = ?, max_loss = ?, max_position_size = ?, target_same_equity = ?, withdrawal_stages = ?
WHERE id = ?
`).run(data.prefix, data.profitTarget, data.consistency, data.minDayPnL, data.minTradingDays, data.accountSize, data.maxLoss, data.maxPositionSize, data.targetSameEquity ? 1 : 0, JSON.stringify(data.withdrawalStages ?? []), id);
return result.changes > 0;
export function listAssignments(firmId: string): AssignmentRow[] {
return db.prepare('SELECT * FROM account_assignments WHERE firm_id = ?').all(firmId) as AssignmentRow[];
}
export function setAssignment(
accountId: number,
firmId: string,
accountTypeId: string | null,
manual: boolean,
): void {
db.prepare(`
INSERT INTO account_assignments (account_id, firm_id, account_type_id, manual, resolved_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(account_id) DO UPDATE SET
firm_id = excluded.firm_id,
account_type_id = excluded.account_type_id,
manual = excluded.manual,
resolved_at = excluded.resolved_at
`).run(accountId, firmId, accountTypeId, manual ? 1 : 0, new Date().toISOString());
}
/** Writes an auto-resolved assignment, leaving any manual override alone.
* The `manual = 0` guard is what makes a user's pick stick across syncs. */
export function setAutoAssignment(accountId: number, firmId: string, accountTypeId: string | null): void {
const existing = getAssignment(accountId);
if (existing?.manual === 1) return;
setAssignment(accountId, firmId, accountTypeId, false);
}
export function clearAssignment(accountId: number): boolean {
return db.prepare('DELETE FROM account_assignments WHERE account_id = ?').run(accountId).changes > 0;
}
// ── Settings ─────────────────────────────────────────────────────────────────
@@ -307,33 +278,6 @@ export function loadAccountMeta(accountId: number, key: string): string | null {
return row?.value ?? null;
}
// ── Firm banned symbols ───────────────────────────────────────────────────────
db.exec(`
CREATE TABLE IF NOT EXISTS firm_banned_symbols (
firm_id INTEGER NOT NULL REFERENCES firms(id) ON DELETE CASCADE,
symbol TEXT NOT NULL,
PRIMARY KEY (firm_id, symbol)
);
`);
export function getBannedSymbols(firmId: number): string[] {
return (db.prepare('SELECT symbol FROM firm_banned_symbols WHERE firm_id = ? ORDER BY symbol').all(firmId) as { symbol: string }[])
.map((r) => r.symbol);
}
export function isSymbolBanned(firmId: number, symbol: string): boolean {
return !!db.prepare('SELECT 1 FROM firm_banned_symbols WHERE firm_id = ? AND symbol = ?').get(firmId, symbol);
}
export function setBannedSymbol(firmId: number, symbol: string, banned: boolean): void {
if (banned) {
db.prepare('INSERT OR IGNORE INTO firm_banned_symbols (firm_id, symbol) VALUES (?, ?)').run(firmId, symbol);
} else {
db.prepare('DELETE FROM firm_banned_symbols WHERE firm_id = ? AND symbol = ?').run(firmId, symbol);
}
}
// ── AutoBuyer captures ───────────────────────────────────────────────────────
db.exec(`