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>
112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
/**
|
|
* Wire protocol for the control link to the master dashboard.
|
|
*
|
|
* Mirror of the block at the bottom of `autotrader-master/types.ts`, which is
|
|
* the source of truth. The two repos deploy independently, so change both
|
|
* together — and only ever by adding, since an instance and a master can be
|
|
* running different versions of this file at the same time.
|
|
*/
|
|
|
|
export interface AccountTypeStats {
|
|
accountTypeId: string | null;
|
|
label: string;
|
|
accountSize: number;
|
|
totalAccounts: number;
|
|
accountsTraded: number;
|
|
inTrade: number;
|
|
}
|
|
|
|
export interface FirmStats {
|
|
firm: string;
|
|
totalAccounts: number;
|
|
accountsTraded: number;
|
|
inTrade: number;
|
|
/** Per account type, summing to the firm's own totals. Optional so a master
|
|
* on an older build simply ignores it. */
|
|
accountTypes?: AccountTypeStats[];
|
|
}
|
|
|
|
export interface SchedulerState {
|
|
running: boolean;
|
|
action: 'Buy' | 'Sell' | 'Auto';
|
|
symbol: string;
|
|
lastRun: string | null;
|
|
intervalSeconds: number;
|
|
stopAfterAll?: boolean;
|
|
}
|
|
|
|
export interface AutomationSummary {
|
|
key: string;
|
|
label: string;
|
|
firm: string;
|
|
inputs: { id: string; label: string; min: number; max: number; default: number }[];
|
|
}
|
|
|
|
export interface InstanceCapabilities {
|
|
symbols: string[];
|
|
automations: AutomationSummary[];
|
|
runnerOnline: boolean;
|
|
}
|
|
|
|
// ── Firm catalog ─────────────────────────────────────────────────────────────
|
|
// Served by the master's public GET /api/firms/catalog and cached locally by
|
|
// lib/firm-catalog.ts. Credentials are per-machine and absent from all of it.
|
|
|
|
/** One account type as published by the master.
|
|
*
|
|
* The field names match `AccountConfig` in types.ts on purpose — that is what
|
|
* lets the trading code consume a catalog account type wherever it used to
|
|
* read a local account_configs row. */
|
|
export interface CatalogAccountType {
|
|
id: string;
|
|
firmId: string;
|
|
label: string;
|
|
prefix: string;
|
|
accountSize: number;
|
|
profitTarget: number;
|
|
consistency: number;
|
|
minDayPnL: number;
|
|
minTradingDays: number;
|
|
maxLoss: number;
|
|
maxPositionSize: number;
|
|
targetSameEquity: boolean;
|
|
withdrawalStages: { profit: number; consistency: number; minTradingDays: number }[];
|
|
/** ISO. Ties between two matching account types are broken by this. */
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface CatalogFirm {
|
|
id: string;
|
|
name: string;
|
|
bannedSymbols: string[];
|
|
accountTypes: CatalogAccountType[];
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface FirmCatalogPayload {
|
|
version: number;
|
|
updatedAt: string;
|
|
firms: CatalogFirm[];
|
|
}
|
|
|
|
export interface FirmCatalogUnchanged {
|
|
version: number;
|
|
updatedAt: string;
|
|
unchanged: true;
|
|
}
|
|
|
|
export type Command =
|
|
| { kind: 'trade'; action: 'Buy' | 'Sell' | 'Auto'; symbol: string; stopAfterAll?: boolean }
|
|
| { kind: 'stop-trading' }
|
|
| { kind: 'autobuyer-run'; automationId: string; inputs?: Record<string, number> }
|
|
| { kind: 'report-now' };
|
|
|
|
export type MasterMessage =
|
|
| { type: 'welcome' }
|
|
| { type: 'command'; id: string; command: Command };
|
|
|
|
export type InstanceMessage =
|
|
| { type: 'hello'; instance: string; capabilities: InstanceCapabilities }
|
|
| { type: 'report'; firms: FirmStats[]; scheduler: SchedulerState; capabilities?: InstanceCapabilities }
|
|
| { type: 'ack'; id: string; ok: boolean; result?: unknown; error?: string };
|