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
@@ -0,0 +1,81 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { updateAccountConfig, deleteAccountConfig } from '@/lib/db';
|
||||
|
||||
export async function PUT(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id: idStr } = await params;
|
||||
const id = parseInt(idStr, 10);
|
||||
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = await req.json() as {
|
||||
prefix?: string;
|
||||
profitTarget?: number;
|
||||
consistency?: number;
|
||||
minDayPnL?: number;
|
||||
minTradingDays?: number;
|
||||
accountSize?: number;
|
||||
maxLoss?: number;
|
||||
};
|
||||
|
||||
const { prefix, profitTarget, consistency, minDayPnL, minTradingDays, accountSize, maxLoss } = body;
|
||||
|
||||
if (
|
||||
typeof prefix !== 'string' || !prefix.trim() ||
|
||||
typeof profitTarget !== 'number' ||
|
||||
typeof consistency !== 'number' ||
|
||||
typeof minDayPnL !== 'number' ||
|
||||
typeof minTradingDays !== 'number' ||
|
||||
typeof accountSize !== 'number'
|
||||
) {
|
||||
return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = updateAccountConfig(id, {
|
||||
prefix: prefix.trim(),
|
||||
profitTarget,
|
||||
consistency,
|
||||
minDayPnL,
|
||||
minTradingDays,
|
||||
accountSize,
|
||||
maxLoss: maxLoss ?? 0,
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
return NextResponse.json({ error: 'Account config not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('[PUT /api/account-configs/:id]', err);
|
||||
return NextResponse.json({ error: 'Failed to update' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id: idStr } = await params;
|
||||
const id = parseInt(idStr, 10);
|
||||
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = deleteAccountConfig(id);
|
||||
if (!deleted) {
|
||||
return NextResponse.json({ error: 'Account config not found' }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('[DELETE /api/account-configs/:id]', err);
|
||||
return NextResponse.json({ error: 'Failed to delete' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getClients } from '@/lib/clients';
|
||||
|
||||
export async function GET(_req: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
const accountId = Number(id);
|
||||
|
||||
const clients = getClients();
|
||||
for (const client of clients.values()) {
|
||||
const daily = client.dailyPnL?.[accountId];
|
||||
if (daily !== undefined) {
|
||||
return NextResponse.json(daily);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getFirmById, createAccountConfig } from '@/lib/db';
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id: idStr } = await params;
|
||||
const firmId = parseInt(idStr, 10);
|
||||
|
||||
if (isNaN(firmId)) {
|
||||
return NextResponse.json({ error: 'Invalid firm id' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!getFirmById(firmId)) {
|
||||
return NextResponse.json({ error: 'Firm not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await req.json() as {
|
||||
prefix?: string;
|
||||
profitTarget?: number;
|
||||
consistency?: number;
|
||||
minDayPnL?: number;
|
||||
minTradingDays?: number;
|
||||
accountSize?: number;
|
||||
maxLoss?: number;
|
||||
};
|
||||
|
||||
const { prefix, profitTarget, consistency, minDayPnL, minTradingDays, accountSize, maxLoss } = body;
|
||||
|
||||
if (
|
||||
typeof prefix !== 'string' || !prefix.trim() ||
|
||||
typeof profitTarget !== 'number' ||
|
||||
typeof consistency !== 'number' ||
|
||||
typeof minDayPnL !== 'number' ||
|
||||
typeof minTradingDays !== 'number' ||
|
||||
typeof accountSize !== 'number'
|
||||
) {
|
||||
return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const row = createAccountConfig(firmId, {
|
||||
prefix: prefix.trim(),
|
||||
profitTarget,
|
||||
consistency,
|
||||
minDayPnL,
|
||||
minTradingDays,
|
||||
accountSize,
|
||||
maxLoss: maxLoss ?? 0,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
id: row.id,
|
||||
prefix: row.prefix,
|
||||
profitTarget: row.profit_target,
|
||||
consistency: row.consistency,
|
||||
minDayPnL: row.min_day_pnl,
|
||||
minTradingDays: row.min_trading_days,
|
||||
accountSize: row.account_size,
|
||||
maxLoss: row.max_loss,
|
||||
}, { status: 201 });
|
||||
} catch (err) {
|
||||
console.error('[POST /api/firms/:id/accounts]', err);
|
||||
return NextResponse.json({ error: 'Failed to create account type' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { upsertFirmInstrumentConfig } from '@/lib/db';
|
||||
|
||||
export async function PUT(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; instrumentId: string }> }
|
||||
) {
|
||||
const { id: idStr, instrumentId: instrIdStr } = await params;
|
||||
const firmId = parseInt(idStr, 10);
|
||||
const instrumentId = parseInt(instrIdStr, 10);
|
||||
|
||||
if (isNaN(firmId) || isNaN(instrumentId)) {
|
||||
return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = await req.json() as {
|
||||
allinFee?: number;
|
||||
roundtripFee?: number;
|
||||
banned?: boolean;
|
||||
};
|
||||
|
||||
if (
|
||||
typeof body.allinFee !== 'number' ||
|
||||
typeof body.roundtripFee !== 'number' ||
|
||||
typeof body.banned !== 'boolean'
|
||||
) {
|
||||
return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
upsertFirmInstrumentConfig(firmId, instrumentId, {
|
||||
allinFee: body.allinFee,
|
||||
roundtripFee: body.roundtripFee,
|
||||
banned: body.banned,
|
||||
});
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('[PUT /api/firms/:id/instrument-configs/:instrumentId]', err);
|
||||
return NextResponse.json({ error: 'Failed to save config' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getFirmFees } from '@/lib/db';
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id: idStr } = await params;
|
||||
const firmId = parseInt(idStr, 10);
|
||||
|
||||
if (isNaN(firmId)) {
|
||||
return NextResponse.json({ error: 'Invalid firm id' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
return NextResponse.json(getFirmFees(firmId));
|
||||
} catch (err) {
|
||||
console.error('[GET /api/firms/:id/instrument-configs]', err);
|
||||
return NextResponse.json({ error: 'Failed to fetch fees' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,40 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { deleteFirm } from '@/lib/db';
|
||||
import { getFirmById, deleteFirm } from '@/lib/db';
|
||||
import { removeClient } from '@/lib/clients';
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id: idStr } = await params;
|
||||
const id = parseInt(idStr, 10);
|
||||
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
}
|
||||
|
||||
const firm = getFirmById(id);
|
||||
if (!firm) {
|
||||
return NextResponse.json({ error: 'Firm not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
id: firm.id,
|
||||
firm: firm.name,
|
||||
username: firm.username,
|
||||
password: firm.password,
|
||||
accounts: firm.accounts.map((a) => ({
|
||||
id: a.id,
|
||||
prefix: a.prefix,
|
||||
profitTarget: a.profit_target,
|
||||
consistency: a.consistency,
|
||||
minDayPnL: a.min_day_pnl,
|
||||
minTradingDays: a.min_trading_days,
|
||||
accountSize: a.account_size,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
|
||||
@@ -16,6 +16,8 @@ export async function GET() {
|
||||
consistency: a.consistency,
|
||||
minDayPnL: a.min_day_pnl,
|
||||
minTradingDays: a.min_trading_days,
|
||||
accountSize: a.account_size,
|
||||
maxLoss: a.max_loss,
|
||||
})),
|
||||
}));
|
||||
return NextResponse.json(result);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { setInstrumentEnabled } from '@/lib/db';
|
||||
|
||||
export async function PATCH(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ symbol: string }> }
|
||||
) {
|
||||
const { symbol } = await params;
|
||||
const body = await request.json();
|
||||
const ok = setInstrumentEnabled(symbol, !!body.enabled);
|
||||
if (!ok) return NextResponse.json({ error: 'Not found' }, { status: 404 });
|
||||
return NextResponse.json({ symbol, enabled: !!body.enabled });
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getInstruments } from '@/lib/db';
|
||||
|
||||
export function GET() {
|
||||
return NextResponse.json(getInstruments());
|
||||
}
|
||||
@@ -22,6 +22,7 @@ export async function GET() {
|
||||
realizedPnL: cash.realizedPnL,
|
||||
daysTraded: client.daysTraded[acc.id] ?? 0,
|
||||
hasPosition: !!client.positions[acc.id],
|
||||
autoLiqThreshold: client.autoLiqThresholds[acc.id] ?? 0,
|
||||
};
|
||||
});
|
||||
return { firm: f.name, connected: true, accounts };
|
||||
|
||||
Reference in New Issue
Block a user