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>
This commit is contained in:
Senofy
2026-03-08 15:03:21 -05:00
co-authored by Claude Sonnet 4.6
parent a9b6acd479
commit dd18f91584
22 changed files with 2218 additions and 112 deletions
+17 -1
View File
@@ -1,5 +1,7 @@
import { TradovateClient } from './tradovate-class';
import { getFirms } from './db';
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 & {
@@ -16,8 +18,22 @@ 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 () => {
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;
+148 -8
View File
@@ -22,29 +22,59 @@ db.exec(`
min_day_pnl REAL NOT NULL DEFAULT -999,
min_trading_days INTEGER NOT NULL DEFAULT 5
);
CREATE TABLE IF NOT EXISTS firm_fees (
firm_id INTEGER NOT NULL REFERENCES firms(id) ON DELETE CASCADE,
symbol TEXT NOT NULL,
allin_fee REAL NOT NULL DEFAULT 0,
roundtrip_fee REAL NOT NULL DEFAULT 0,
PRIMARY KEY (firm_id, symbol)
);
CREATE TABLE IF NOT EXISTS instruments (
symbol TEXT PRIMARY KEY,
enabled INTEGER NOT NULL DEFAULT 1
);
`);
// Seed default data if empty
// 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
}
// 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
}
// Seed default firms if empty
const firmCount = (db.prepare('SELECT COUNT(*) as count FROM firms').get() as { count: number }).count;
if (firmCount === 0) {
const insertFirm = db.prepare('INSERT INTO firms (name, username, password) VALUES (?, ?, ?)');
const insertAccount = db.prepare(
'INSERT INTO account_configs (firm_id, prefix, profit_target, consistency, min_day_pnl, min_trading_days) VALUES (?, ?, ?, ?, ?, ?)'
'INSERT INTO account_configs (firm_id, prefix, profit_target, consistency, min_day_pnl, min_trading_days, account_size) VALUES (?, ?, ?, ?, ?, ?, ?)'
);
const alpha = insertFirm.run('Alpha', 'brandonsenoli72786', '-Z2kPm7nBg');
insertAccount.run(alpha.lastInsertRowid, 'AFSTDEV', 9000, 0.51, -999, 5);
insertAccount.run(alpha.lastInsertRowid, 'AFSTDQA', 4500, 0.40, -999, 7);
insertAccount.run(alpha.lastInsertRowid, 'AFZEROEV', 3000, 0.50, -999, 5);
insertAccount.run(alpha.lastInsertRowid, 'AFZEROQA', 3000, 0.50, -999, 5);
insertAccount.run(alpha.lastInsertRowid, 'AF', 3000, 0.50, -999, 5);
insertAccount.run(alpha.lastInsertRowid, 'AFSTDEV', 9000, 0.51, -999, 5, 150000);
insertAccount.run(alpha.lastInsertRowid, 'AFSTDQA', 4500, 0.40, -999, 7, 150000);
insertAccount.run(alpha.lastInsertRowid, 'AFZEROEV', 3000, 0.50, -999, 5, 100000);
insertAccount.run(alpha.lastInsertRowid, 'AFZEROQA', 3000, 0.50, -999, 5, 100000);
insertAccount.run(alpha.lastInsertRowid, 'AF', 3000, 0.50, -999, 5, 100000);
const tpt = insertFirm.run('TakeProfitTrader', 'BRANDONLI1', 'W4592F5512U2817tv=');
insertAccount.run(tpt.lastInsertRowid, 'TAKEPROFIT', 9000, 0.50, -999, 5);
insertAccount.run(tpt.lastInsertRowid, 'TAKEPROFIT', 9000, 0.50, -999, 5, 150000);
console.log('[db] Seeded default firms.');
}
// ── Interfaces ─────────────────────────────────────────────────────────────
export interface AccountConfigRow {
id: number;
firm_id: number;
@@ -53,6 +83,8 @@ export interface AccountConfigRow {
consistency: number;
min_day_pnl: number;
min_trading_days: number;
account_size: number;
max_loss: number;
}
export interface FirmRow {
@@ -66,6 +98,15 @@ export interface FirmWithAccounts extends FirmRow {
accounts: AccountConfigRow[];
}
export interface FirmFee {
firmId: number;
symbol: string;
allinFee: number;
roundtripFee: number;
}
// ── 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');
@@ -75,6 +116,13 @@ export function getFirms(): FirmWithAccounts[] {
}));
}
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 };
}
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);
@@ -85,3 +133,95 @@ export function deleteFirm(id: number): boolean {
const result = db.prepare('DELETE FROM firms WHERE id = ?').run(id);
return result.changes > 0;
}
// ── Account Configs ─────────────────────────────────────────────────────────
export function createAccountConfig(firmId: number, data: {
prefix: string;
profitTarget: number;
consistency: number;
minDayPnL: number;
minTradingDays: number;
accountSize: number;
maxLoss: 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) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
);
const result = stmt.run(firmId, data.prefix, data.profitTarget, data.consistency, data.minDayPnL, data.minTradingDays, data.accountSize, data.maxLoss);
return db.prepare('SELECT * FROM account_configs WHERE id = ?').get(result.lastInsertRowid) as AccountConfigRow;
}
export function deleteAccountConfig(id: number): boolean {
const result = db.prepare('DELETE FROM account_configs WHERE id = ?').run(id);
return result.changes > 0;
}
export function updateAccountConfig(id: number, data: {
prefix: string;
profitTarget: number;
consistency: number;
minDayPnL: number;
minTradingDays: number;
accountSize: number;
maxLoss: number;
}): boolean {
const result = db.prepare(`
UPDATE account_configs
SET prefix = ?, profit_target = ?, consistency = ?, min_day_pnl = ?, min_trading_days = ?, account_size = ?, max_loss = ?
WHERE id = ?
`).run(data.prefix, data.profitTarget, data.consistency, data.minDayPnL, data.minTradingDays, data.accountSize, data.maxLoss, id);
return result.changes > 0;
}
// ── Firm Fees ────────────────────────────────────────────────────────────────
export function getFirmFees(firmId: number): FirmFee[] {
return (db.prepare('SELECT firm_id, symbol, allin_fee, roundtrip_fee FROM firm_fees WHERE firm_id = ? ORDER BY symbol').all(firmId) as {
firm_id: number;
symbol: string;
allin_fee: number;
roundtrip_fee: number;
}[]).map((r) => ({
firmId: r.firm_id,
symbol: r.symbol,
allinFee: r.allin_fee,
roundtripFee: r.roundtrip_fee,
}));
}
export function upsertFirmFee(firmId: number, symbol: string, allinFee: number, roundtripFee: number): void {
db.prepare(`
INSERT INTO firm_fees (firm_id, symbol, allin_fee, roundtrip_fee)
VALUES (?, ?, ?, ?)
ON CONFLICT(firm_id, symbol) DO UPDATE SET
allin_fee = excluded.allin_fee,
roundtrip_fee = excluded.roundtrip_fee
`).run(firmId, symbol, allinFee, roundtripFee);
}
// ── Instruments ──────────────────────────────────────────────────────────────
const SYMBOLS = ['NQ','MNQ','ES','MES','YM','MYM','RTY','M2K','GC','MGC','SI','CL','MCL','NG','ZB','ZN','ZF','6E','6J','6B'];
// Seed instruments table if empty
const instrCount = (db.prepare('SELECT COUNT(*) as count FROM instruments').get() as { count: number }).count;
if (instrCount === 0) {
const ins = db.prepare('INSERT INTO instruments (symbol, enabled) VALUES (?, 1)');
for (const s of SYMBOLS) ins.run(s);
}
export interface InstrumentRow {
symbol: string;
enabled: boolean;
}
export function getInstruments(): InstrumentRow[] {
return (db.prepare('SELECT symbol, enabled FROM instruments ORDER BY symbol').all() as { symbol: string; enabled: number }[])
.map((r) => ({ symbol: r.symbol, enabled: r.enabled === 1 }));
}
export function setInstrumentEnabled(symbol: string, enabled: boolean): boolean {
const result = db.prepare('UPDATE instruments SET enabled = ? WHERE symbol = ?').run(enabled ? 1 : 0, symbol);
return result.changes > 0;
}
+231 -80
View File
@@ -27,6 +27,11 @@ export class TradovateClient {
} = {};
public daysTraded: { [accountId: number]: number } = {};
public dailyPnL: { [accountId: number]: { date: string; pnl: number }[] } = {};
/** Balance floor set by the prop firm — account is blown when amount <= this value (0 = not set) */
public autoLiqThresholds: { [accountId: number]: number } = {};
public products: { id: number; name: string }[] = [];
private ws: WebSocket;
private callbackOnSyncRequest: () => Promise<void>;
@@ -52,7 +57,12 @@ export class TradovateClient {
callback: (response: any) => void;
}[] = [];
constructor(name: string, password: string, callbackOnSyncRequest: () => Promise<void>) {
constructor(
name: string,
password: string,
callbackOnSyncRequest: () => Promise<void>
) {
this.name = name;
this.password = password;
this.callbackOnSyncRequest = callbackOnSyncRequest;
@@ -85,9 +95,8 @@ export class TradovateClient {
console.log('Connected to websocket');
this.ws.send('authorize\n2\n\n' + this.accessInfo.accessToken);
this.directEventCallbacks[2] = (response: any) => {
// Once authorize, start syncing every 60 seconds
this.requestAccountUpdates();
setInterval(() => this.requestAccountUpdates(), 60000);
this.requestSync();
setInterval(() => this.requestSync(), 60000);
// Every 2.5 seconds send a heartbeat
setInterval(() => {
@@ -156,79 +165,94 @@ export class TradovateClient {
});
}
private async requestAccountUpdates(): Promise<void> {
private requestSync(): void {
this.directEventCallbacks[3] = (response: any) => {
// Syncing DLL or MLL hit
const riskStatusById: { [id: number]: { liquidateOnly?: string } } = (
response.accountRiskStatuses || []
).reduce((acc: any, item: any) => {
acc[item.id] = item;
return acc;
}, {});
this.accountList = (response.accounts as AccountItem[]).map((account) => {
if (riskStatusById[account.id]?.liquidateOnly) {
return { ...account, active: false };
try {
if (!response) {
console.error('[requestSync] Received null/undefined response — auth may have failed');
return;
}
return account;
});
this.accountCashBalances = response.cashBalances.reduce(
(
acc: {
[accountId: number]: { amount: number; realizedPnL: number };
},
item: { accountId: number; amount: number; realizedPnL: number }
) => {
acc[item.accountId] = {
amount: item.amount,
realizedPnL: item.realizedPnL,
};
// liquidateOnly flag lives in accountRiskStatuses
const riskStatusById: { [accountId: number]: { liquidateOnly?: string } } = (
response.accountRiskStatuses || []
).reduce((acc: any, item: any) => {
const key = item.accountId ?? item.id;
acc[key] = item;
return acc;
},
{} as { [accountId: number]: { amount: number; realizedPnL: number } }
);
this.positions = response.positions
.filter((item) => item.netPos !== 0)
.reduce(
}, {});
// Auto-liquidation balance floor lives in userAccountAutoLiqs.
// item.id IS the account ID. The floor is: trailingMaxDrawdownLimit - trailingMaxDrawdown.
// Tradovate uses 999999999 as a sentinel for "no limit" — skip those.
for (const item of (response.userAccountAutoLiqs ?? [])) {
const accountId: number = item.id;
const limit: number = item.trailingMaxDrawdownLimit ?? 0;
const drawdown: number = item.trailingMaxDrawdown ?? 0;
const isSentinel = limit >= 999999999;
const floor = (!isSentinel && limit > 0 && drawdown > 0) ? limit - drawdown : 0;
this.autoLiqThresholds[accountId] = floor;
if (floor > 0) {
console.log(`[autoLiq] account ${accountId} → floor $${floor} (hwm=$${limit} drawdown=$${drawdown})`);
}
}
this.accountList = ((response.accounts ?? []) as AccountItem[]).map((account) => {
if (riskStatusById[account.id]?.liquidateOnly) {
return { ...account, active: false };
}
return account;
});
this.accountCashBalances = (response.cashBalances ?? []).reduce(
(
acc: {
[accountId: number]: {
contractId: number;
netPos: number;
netPrice: number;
timestamp: Date;
};
},
item: {
accountId: number;
contractId: number;
netPos: number;
netPrice: number;
timestamp: Date;
}
acc: { [accountId: number]: { amount: number; realizedPnL: number } },
item: { accountId: number; amount: number; realizedPnL: number }
) => {
acc[item.accountId] = {
contractId: item.contractId,
netPos: item.netPos,
netPrice: item.netPrice,
timestamp: new Date(item.timestamp),
};
acc[item.accountId] = { amount: item.amount, realizedPnL: item.realizedPnL };
return acc;
},
{} as {
[accountId: number]: {
contractId: number;
netPos: number;
netPrice: number;
timestamp: Date;
};
}
{} as { [accountId: number]: { amount: number; realizedPnL: number } }
);
this.callbackOnSyncRequest();
this.fetchDaysTraded();
this.positions = (response.positions ?? [])
.filter((item: any) => item.netPos !== 0)
.reduce(
(
acc: { [accountId: number]: { contractId: number; netPos: number; netPrice: number; timestamp: Date } },
item: { accountId: number; contractId: number; netPos: number; netPrice: number; timestamp: Date }
) => {
acc[item.accountId] = {
contractId: item.contractId,
netPos: item.netPos,
netPrice: item.netPrice,
timestamp: new Date(item.timestamp),
};
return acc;
},
{} as { [accountId: number]: { contractId: number; netPos: number; netPrice: number; timestamp: Date } }
);
console.log(`[requestSync] ${this.accountList.length} account(s), ${Object.keys(this.accountCashBalances).length} balance(s)`);
this.fetchDaysTraded();
if (this.products.length > 0) {
this.callbackOnSyncRequest();
return;
}
this.directEventCallbacks[30] = (products: any) => {
if (Array.isArray(products) && products.length > 0) {
this.products = products.map((p: any) => ({ id: p.id, name: p.name }));
console.log(`Loaded ${this.products.length} products`);
}
this.callbackOnSyncRequest();
};
this.ws.send('product/list\n30\n\n');
} catch (err) {
console.error('[requestSync] Error processing sync response:', err);
}
};
this.ws.send('user/syncrequest\n3\n\n{"splitResponses":false}');
@@ -237,25 +261,111 @@ export class TradovateClient {
private async fetchDaysTraded(): Promise<void> {
if (!this.accessInfo?.accessToken) return;
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - 28);
const now = new Date();
const start = new Date();
start.setDate(start.getDate() - 28);
const fmtDate = (d: Date) => {
const m = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return `${m}/${day}/${d.getFullYear()}`;
};
for (const account of this.accountList) {
try {
const res = await axios.get(
`https://demo.tradovateapi.com/v1/fill/ldeps?masterid=${account.id}`,
const res = await axios.post(
'https://rpt-demo.tradovateapi.com/v1/reports/requestreport',
{
name: 'Fills',
params: [
{ name: 'startDate', value: fmtDate(start) },
{ name: 'endDate', value: fmtDate(now) },
{ name: 'startTime', value: '00:00:00' },
{ name: 'endTime', value: '00:00:00' },
{ name: 'account', value: account.name },
],
representationType: 'json',
timezone: 0,
},
{ headers: { Authorization: `Bearer ${this.accessInfo.accessToken}` } }
);
const fills: { timestamp: string }[] = res.data ?? [];
const tradingDays = new Set(
fills
.filter((f) => new Date(f.timestamp) >= cutoff)
.map((f) => new Date(f.timestamp).toDateString())
);
this.daysTraded[account.id] = tradingDays.size;
// _tradeDate is unquoted in the response (invalid JSON), but the "Date" field
// ("M/D/YY") is a valid quoted string that already reflects CME trade date.
const raw: string = (res.data?.data ?? '[]')
.replace(/"_tradeDate":\s*(\d{4}-\d{2}-\d{2})/g, '"_tradeDate": "$1"');
type Fill = {
_tradeDate: string;
_timestamp: string;
_action: number; // 0 = Buy, 1 = Sell
_qty: number;
_price: number;
Product: string;
commission: number;
};
const fills: Fill[] = JSON.parse(raw);
const uniqueDays = new Set(fills.map(f => f._tradeDate));
this.daysTraded[account.id] = uniqueDays.size;
// Dollar-per-point map for common futures products
const POINT_VALUES: { [product: string]: number } = {
NQ: 20, MNQ: 2, ES: 50, MES: 5,
YM: 5, MYM: 0.5, RTY: 50, M2K: 10,
GC: 100, MGC: 10, SI: 50, CL: 1000,
MCL: 100, NG: 10000, ZB: 1000, ZN: 1000,
ZF: 1000, '6E': 125000, '6J': 12500000, '6B': 62500,
};
// FIFO P&L computation: match buy/sell fills into round-trips
// Both the opening and closing commissions are deducted on close.
const sorted = [...fills].sort((a, b) => a._timestamp.localeCompare(b._timestamp));
interface Lot { price: number; qty: number; commPerUnit: number }
const longBook: Lot[] = [];
const shortBook: Lot[] = [];
const dailyMap: { [date: string]: number } = {};
for (const fill of sorted) {
const pointValue = POINT_VALUES[fill.Product] ?? 1;
const isBuy = fill._action === 0;
let remaining = fill._qty;
const commPerUnit = fill._qty > 0 ? fill.commission / fill._qty : 0;
if (isBuy) {
// Close any short lots first (FIFO), then open long
while (remaining > 0 && shortBook.length > 0) {
const lot = shortBook[0];
const closed = Math.min(lot.qty, remaining);
const pnl = (lot.price - fill._price) * closed * pointValue
- (commPerUnit * closed) // closing fill commission
- (lot.commPerUnit * closed); // opening fill commission
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
lot.qty -= closed;
remaining -= closed;
if (lot.qty === 0) shortBook.shift();
}
if (remaining > 0) longBook.push({ price: fill._price, qty: remaining, commPerUnit });
} else {
// Close any long lots first (FIFO), then open short
while (remaining > 0 && longBook.length > 0) {
const lot = longBook[0];
const closed = Math.min(lot.qty, remaining);
const pnl = (fill._price - lot.price) * closed * pointValue
- (commPerUnit * closed) // closing fill commission
- (lot.commPerUnit * closed); // opening fill commission
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
lot.qty -= closed;
remaining -= closed;
if (lot.qty === 0) longBook.shift();
}
if (remaining > 0) shortBook.push({ price: fill._price, qty: remaining, commPerUnit });
}
}
this.dailyPnL[account.id] = Object.entries(dailyMap)
.map(([date, pnl]) => ({ date, pnl: Math.round(pnl * 100) / 100 }))
.sort((a, b) => a.date.localeCompare(b.date));
} catch (err) {
console.error(`[fetchDaysTraded] account ${account.id}`, err);
this.daysTraded[account.id] = 0;
console.error(`[fetchDaysTraded] ${account.name}`, err);
this.daysTraded[account.id] ??= 0;
}
}
}
@@ -321,6 +431,47 @@ export class TradovateClient {
return res.data;
}
async fetchInstrumentFees(symbols: string[]): Promise<{ [symbol: string]: number }> {
if (!this.accessInfo?.accessToken || this.products.length === 0) return {};
const productIds = symbols
.map((sym) => this.products.find((p) => p.name === sym)?.id)
.filter((id): id is number => id !== undefined);
const res = await axios.post(
'https://demo.tradovateapi.com/v1/contract/getproductfeeparams',
{ productIds },
{ headers: { Authorization: `Bearer ${this.accessInfo.accessToken}` } }
);
const result: { [symbol: string]: number } = {};
for (const param of (res.data?.params ?? [])) {
const product = this.products.find((p) => p.id === param.productId);
if (product && symbols.includes(product.name)) {
const raw =
(param.clearingFee ?? 0) +
(param.exchangeFee ?? 0) +
(param.nfaFee ?? 0) +
(param.brokerageFee ?? 0) +
(param.ipFee ?? 0) +
(param.commission ?? 0) +
(param.orderRoutingFee ?? 0);
result[product.name] = parseFloat(raw.toFixed(4));
console.log(
`[fees] ${product.name}: clearing=${param.clearingFee ?? 0}` +
` exchange=${param.exchangeFee ?? 0}` +
` nfa=${param.nfaFee ?? 0}` +
` brokerage=${param.brokerageFee ?? 0}` +
` ip=${param.ipFee ?? 0}` +
` commission=${param.commission ?? 0}` +
` routing=${param.orderRoutingFee ?? 0}` +
` → total=${result[product.name]}`
);
}
}
return result;
}
async requestContractsFromSocket(names: string[]): Promise<{
[name: string]: Contract;
}> {