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:
co-authored by
Claude Sonnet 4.6
parent
a9b6acd479
commit
dd18f91584
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user