diff --git a/.gitignore b/.gitignore index f145c8e..390ae7e 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,6 @@ next-env.d.ts dev.log dev.err nul +scripts/lucid-cookies.json +scripts/lucid-config.json +scripts/*.png diff --git a/app/accounts/[id]/page.tsx b/app/accounts/[id]/page.tsx index 65afdd7..cc9f43a 100644 --- a/app/accounts/[id]/page.tsx +++ b/app/accounts/[id]/page.tsx @@ -15,12 +15,6 @@ import { Dot, } from 'recharts'; import type { FirmConfig, FirmState, AccountState, AccountConfig } from '@/types'; -import { computeDailyTarget } from '@/lib/trading-logic'; - -interface DailyPnL { - date: string; - pnl: number; -} function getAccountConfig(name: string, firm: FirmConfig): AccountConfig | undefined { return [...firm.accounts] @@ -178,20 +172,15 @@ export default function AccountPage() { const [account, setAccount] = useState(null); const [cfg, setCfg] = useState(null); const [firmName, setFirmName] = useState(''); - const [dailyPnL, setDailyPnL] = useState([]); useEffect(() => { async function load() { - const [stateRes, firmsRes, dailyRes] = await Promise.all([ + const [stateRes, firmsRes] = await Promise.all([ fetch('/api/state'), fetch('/api/firms'), - fetch(`/api/accounts/${accountId}/daily-pnl`), ]); const states: FirmState[] = await stateRes.json(); const firms: FirmConfig[] = await firmsRes.json(); - const daily: DailyPnL[] = await dailyRes.json(); - - setDailyPnL(daily); for (const firmState of states) { const acc = firmState.accounts.find((a) => a.id === accountId); @@ -223,6 +212,9 @@ export default function AccountPage() { ); } + // dailyPnL comes from state — same source as dailyTarget, no separate fetch needed. + const dailyPnL = account.dailyPnL; + const hasLossLimit = cfg != null && cfg.minDayPnL !== -999; const daysPassed = cfg != null && account.daysTraded >= cfg.minTradingDays; // Dead when balance hits Tradovate's auto-liquidation floor @@ -237,15 +229,14 @@ export default function AccountPage() { : Math.round(fifoTotal * 100) / 100; const profitPct = cfg?.accountSize ? (totalProfit / cfg.accountSize) * 100 : null; const profitPassed = cfg != null && totalProfit >= cfg.profitTarget; - const dailyTarget = cfg && !isDead - ? computeDailyTarget(cfg.profitTarget, cfg.consistency, totalProfit, dailyPnL) - : null; + // dailyTarget is computed server-side in the state API — single source of truth. + const dailyTarget = account.dailyTarget ?? null; const lossPassed = !hasLossLimit || (cfg != null && totalProfit >= cfg.minDayPnL); // Consistency target: the total profit level at which the best day no longer // violates the consistency ratio. Only meaningful once a positive day exists. const maxDayPnL = dailyPnL.length > 0 ? Math.max(...dailyPnL.filter(d => d.pnl > 0).map(d => d.pnl)) : 0; - const consistencyTarget = cfg && maxDayPnL > 0 + const consistencyTarget = cfg && cfg.consistency > 0 && maxDayPnL > 0 ? Math.round(maxDayPnL / cfg.consistency * 100) / 100 : null; diff --git a/app/api/debug/route.ts b/app/api/debug/route.ts index 0b3565e..070c725 100644 --- a/app/api/debug/route.ts +++ b/app/api/debug/route.ts @@ -1,6 +1,7 @@ import { NextResponse } from 'next/server'; import { getClients, resetClients } from '@/lib/clients'; import { getFirms } from '@/lib/db'; +import axios from 'axios'; export async function GET() { try { @@ -83,6 +84,96 @@ export async function POST() { } } +/** + * PATCH /api/debug — probe Tradovate APIs using a firm's access token. + * + * Generic proxy: { firmId, path?, url?, method?, body? } + * Full report run: { firmId, action: 'report', account, startDate, endDate } + * → does requestreport + getreport polling in one server-side call, returns raw data string + */ +export async function PATCH(req: Request) { + try { + const payload = await req.json(); + const { firmId } = payload; + const client = getClients().get(firmId) as any; + if (!client?.accessInfo?.accessToken) { + return NextResponse.json({ error: 'no token for firmId ' + firmId }, { status: 400 }); + } + const headers = { Authorization: `Bearer ${client.accessInfo.accessToken}` }; + + // Full report cycle + if (payload.action === 'report') { + const { account, startDate, endDate } = payload; + try { + let reportData = (await axios.post( + 'https://rpt-demo.tradovateapi.com/v1/reports/requestreport', + { + name: 'Fills', representationType: 'json', timezone: -300, + params: [ + { name: 'startDate', value: startDate }, + { name: 'endDate', value: endDate }, + { name: 'startTime', value: '00:00:00' }, + { name: 'endTime', value: '00:00:00' }, + ...(account ? [{ name: 'account', value: account }] : []), + ], + }, + { headers } + )).data; + + let attempts = 0; + while (reportData?.['p-ticket'] && attempts < 30) { + const ticket: string = reportData['p-ticket']; + const wait: number = Math.max(1, reportData['p-time'] ?? 1); + await new Promise((r) => setTimeout(r, wait * 1000)); + reportData = (await axios.get( + 'https://rpt-demo.tradovateapi.com/v1/reports/getreport', + { params: { 'p-ticket': ticket }, headers } + )).data; + attempts++; + } + + const raw: string = typeof reportData?.data === 'string' ? reportData.data : JSON.stringify(reportData); + // Parse to count fills and unique tradeDates + let fillCount = 0; + let uniqueDates: string[] = []; + try { + const fixed = raw.replace(/"_tradeDate":\s*(\d{4}-\d{2}-\d{2})/g, '"_tradeDate": "$1"'); + const parsed: any[] = JSON.parse(fixed); + fillCount = parsed.length; + uniqueDates = [...new Set(parsed.map((f) => f._tradeDate as string))].sort(); + } catch { /* not parseable yet */ } + return NextResponse.json({ ok: true, attempts, rawLen: raw.length, fillCount, uniqueDates, preview: raw.slice(0, 400) }); + } catch (err: any) { + return NextResponse.json({ + ok: false, + status: err?.response?.status, + data: err?.response?.data, + message: err?.message, + }); + } + } + + // Generic proxy + const { path, url: rawUrl, method = 'GET', body: reqBody } = payload; + const url = rawUrl ?? `https://demo.tradovateapi.com/v1/${path}`; + try { + const res = method === 'POST' + ? await axios.post(url, reqBody, { headers }) + : await axios.get(url, { headers }); + return NextResponse.json({ ok: true, status: res.status, data: res.data }); + } catch (err: any) { + return NextResponse.json({ + ok: false, + status: err?.response?.status, + data: err?.response?.data, + message: err?.message, + }); + } + } catch (err) { + return NextResponse.json({ error: String(err) }, { status: 500 }); + } +} + /** * DELETE /api/debug — force-reinitialize all Tradovate clients with fresh instances * Clears the global pool so getClients() recreates everything from DB on next call. diff --git a/app/api/state/route.ts b/app/api/state/route.ts index 6624355..bb666ed 100644 --- a/app/api/state/route.ts +++ b/app/api/state/route.ts @@ -26,11 +26,15 @@ export async function GET() { const daysTraded: number = client.daysTraded[acc.id] ?? 0; const totalProfit = dailyPnL.reduce((sum, d) => sum + d.pnl, 0); - // Determine if today's daily target was hit + // Compute daily target and targetHit in one place — the single source of truth. const cfg = getAccountConfig(acc.name, f.accounts); + const autoLiqThreshold = client.autoLiqThresholds[acc.id] ?? 0; + const isDead = autoLiqThreshold > 0 && cash.amount <= autoLiqThreshold; let targetHit = false; - if (cfg) { - const target = computeDailyTarget(cfg.profit_target, cfg.consistency, totalProfit, dailyPnL); + let dailyTarget: { amount: number; path: 'first_day' | 'normal_day' | 'reduced_day' } | null = null; + if (cfg && !isDead) { + const target = computeDailyTarget(cfg.profit_target, cfg.consistency, totalProfit, dailyPnL, cfg.min_day_pnl, cfg.min_trading_days); + dailyTarget = target; // Condition 1: profit target already exceeded (target=0), still need days → any activity counts // Condition 2: target > 0 → must have made at least the computed daily target targetHit = @@ -47,9 +51,11 @@ export async function GET() { realizedPnL: cash.realizedPnL, daysTraded, hasPosition: !!client.positions[acc.id], - autoLiqThreshold: client.autoLiqThresholds[acc.id] ?? 0, + autoLiqThreshold, totalProfit, targetHit, + dailyTarget, + dailyPnL, }; }); return { firm: f.name, connected: true, accounts, perContractFees: client.perContractFees }; diff --git a/lib/auto-trade.ts b/lib/auto-trade.ts index c963958..4855dea 100644 --- a/lib/auto-trade.ts +++ b/lib/auto-trade.ts @@ -163,6 +163,11 @@ export async function runTrade(action: 'Buy' | 'Sell', symbol: string) { const exitAction: 'Buy' | 'Sell' = action === 'Buy' ? 'Sell' : 'Buy'; const exitFill = await client.sendOrder(acc.id, mnqContract.name, 1, exitAction, 'Market'); + // Refresh daily P&L immediately after the extra-day round-trip completes + client.fetchDaysTraded().catch((err) => + console.error('[auto-trade] post-fill fetchDaysTraded error:', err) + ); + console.log(`[auto-trade] ${acc.name} (${item.firmName}) extra-day: ${action} 1xMNQ @ ${fill.price} | exited @ ${exitFill.price} (market)`); return { @@ -204,6 +209,15 @@ export async function runTrade(action: 'Buy' | 'Sell', symbol: string) { const exitAction: 'Buy' | 'Sell' = action === 'Buy' ? 'Sell' : 'Buy'; const exitOrder = await client.placeOrderNoWait(acc.id, contract.name, contracts, exitAction, 'Limit', exitPrice); + // Refresh daily P&L as soon as the exit limit order fills + if (exitOrder.orderId != null) { + client.onFill(exitOrder.orderId, () => { + client.fetchDaysTraded().catch((err) => + console.error('[auto-trade] post-fill fetchDaysTraded error:', err) + ); + }); + } + console.log(`[auto-trade] ${acc.name} (${item.firmName}) ${action} ${contracts}x${symbol} @ ${fill.price} | target $${target.amount} [${target.path}] (+$${totalCommission.toFixed(2)} comm) | exit @ ${exitPrice} (orderId=${exitOrder.orderId})`); return { diff --git a/lib/trading-logic.ts b/lib/trading-logic.ts index 08b070c..7e1f238 100644 --- a/lib/trading-logic.ts +++ b/lib/trading-logic.ts @@ -35,8 +35,8 @@ export function computeDailyTarget( minDayPnL: number = 0, // -999 or 0 = no minimum per day minTradingDays: number = 0 // 0 = no minimum trading days ): { amount: number; path: 'first_day' | 'normal_day' | 'reduced_day' } { - const positiveDays = dailyPnL.filter((d) => d.pnl > 0); - const daysTraded = positiveDays.length; + const qualifyingDays = minDayPnL === 0 ? dailyPnL : dailyPnL.filter((d) => d.pnl >= minDayPnL); + const daysTraded = qualifyingDays.length; // --- Base target via consistency logic --- let baseAmount: number; @@ -45,8 +45,13 @@ export function computeDailyTarget( if (daysTraded === 0) { baseAmount = profitTarget * consistency; path = 'first_day'; + } else if (consistency === 0) { + // 0% consistency means no consistency rule to satisfy — base amount is always $0. + // The min-day reservation block below handles any mandatory-day targeting. + baseAmount = 0; + path = 'reduced_day'; } else { - const maxDay = Math.max(...positiveDays.map((d) => d.pnl)); + const maxDay = Math.max(...qualifyingDays.map((d) => d.pnl)); const realTarget = maxDay / consistency; const needed = realTarget - totalProfit; diff --git a/lib/tradovate-class.ts b/lib/tradovate-class.ts index f45ec29..5453c59 100644 --- a/lib/tradovate-class.ts +++ b/lib/tradovate-class.ts @@ -36,7 +36,7 @@ export class TradovateClient { public fetchDaysComplete = false; /** Last error per account name from fetchDaysTraded() */ public lastFetchErrors: Record = {}; - /** Raw reports API response data per account (first 200 chars) for debugging */ + /** Raw reports API response sample per account (first 100 chars) for debugging */ public lastFetchRaw: Record = {}; public products: { id: number; name: string }[] = []; @@ -315,9 +315,12 @@ export class TradovateClient { if (!this.accessInfo?.accessToken) return; this.fetchDaysComplete = false; + const authHeaders = { Authorization: `Bearer ${this.accessInfo.accessToken}` }; + const now = new Date(); + now.setDate(now.getDate() + 1); // endDate must be tomorrow — report server excludes today's fills when endDate=today const start = new Date(); - start.setDate(start.getDate() - 28); + start.setDate(start.getDate() - 27); // keep total window ≤ 28 days const fmtDate = (d: Date) => { const m = String(d.getMonth() + 1).padStart(2, '0'); @@ -325,29 +328,36 @@ export class TradovateClient { return `${m}/${day}/${d.getFullYear()}`; }; + type Fill = { + _tradeDate: string; + _timestamp: string; + _action: number; // 0 = Buy, 1 = Sell + _qty: number; + _price: number; + Product: string; + commission: number; + }; + + interface Lot { price: number; qty: number; commPerUnit: number } + for (const account of this.accountList) { try { - const authHeaders = { Authorization: `Bearer ${this.accessInfo.accessToken}` }; - - // Step 1 — request the report let reportData = (await axios.post( 'https://rpt-demo.tradovateapi.com/v1/reports/requestreport', { - name: 'Fills', + name: 'Fills', representationType: 'json', timezone: -300, params: [ { name: 'startDate', value: fmtDate(start) }, - { name: 'endDate', value: fmtDate(now) }, + { name: 'endDate', value: fmtDate(now) }, { name: 'startTime', value: '00:00:00' }, - { name: 'endTime', value: '00:00:00' }, - { name: 'account', value: account.name }, + { name: 'endTime', value: '00:00:00' }, + { name: 'account', value: account.name }, ], - representationType: 'json', - timezone: 0, }, { headers: authHeaders } )).data; - // Step 2 — if the report is queued, poll until it's ready + // Poll if queued — getreport also requires the bearer token let pollAttempts = 0; while (reportData?.['p-ticket'] && pollAttempts < 30) { const pTicket: string = reportData['p-ticket']; @@ -361,30 +371,14 @@ export class TradovateClient { } if (!this.lastFetchRaw) this.lastFetchRaw = {}; - this.lastFetchRaw[account.name] = JSON.stringify(reportData).slice(0, 500); + const raw: string = typeof reportData?.data === 'string' ? reportData.data : '[]'; + this.lastFetchRaw[account.name] = raw.slice(0, 100); - // _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 rawResponse = reportData?.data ?? '[]'; - const raw: string = String(rawResponse) - .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); + // _tradeDate is unquoted in the response (invalid JSON) — fix before parsing + const fixed = raw.replace(/"_tradeDate":\s*(\d{4}-\d{2}-\d{2})/g, '"_tradeDate": "$1"'); + const fills: Fill[] = JSON.parse(fixed); - // POINT_VALUES imported from trading-logic.ts - - // 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 } = {}; @@ -396,13 +390,12 @@ export class TradovateClient { 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 + - (commPerUnit * closed) + - (lot.commPerUnit * closed); dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl; lot.qty -= closed; remaining -= closed; @@ -410,13 +403,12 @@ export class TradovateClient { } 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 + - (commPerUnit * closed) + - (lot.commPerUnit * closed); dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl; lot.qty -= closed; remaining -= closed; @@ -431,13 +423,13 @@ export class TradovateClient { .sort((a, b) => a.date.localeCompare(b.date)); this.dailyPnL[account.id] = entries; - // Count only positive-P&L days — consistent with computeDailyTarget's positiveDays - this.daysTraded[account.id] = entries.filter(d => d.pnl > 0).length; + this.daysTraded[account.id] = entries.filter((d) => d.pnl !== 0).length; } catch (err) { - const msg = err instanceof Error ? `${err.message}` : String(err); - console.error(`[fetchDaysTraded] ${account.name}:`, msg); + const msg = err instanceof Error ? err.message : String(err); + console.error(`[fetchDaysTraded] report error for ${account.name}:`, msg); if (!this.lastFetchErrors) this.lastFetchErrors = {}; this.lastFetchErrors[account.name] = msg; + this.dailyPnL[account.id] ??= []; this.daysTraded[account.id] ??= 0; } } diff --git a/package-lock.json b/package-lock.json index dd6473e..3f0d7e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "axios": "^1.13.6", "better-sqlite3": "^12.6.2", "next": "16.1.6", + "playwright": "^1.58.2", "react": "19.2.3", "react-dom": "19.2.3", "recharts": "^3.8.0" @@ -4115,6 +4116,20 @@ "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", "license": "MIT" }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", @@ -5961,6 +5976,36 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/playwright": { + "version": "1.58.2", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.58.2.tgz", + "integrity": "sha512-vA30H8Nvkq/cPBnNw4Q8TWz1EJyqgpuinBcHET0YVJVFldr8JDNiU9LaWAE1KqSkRYazuaBhTpB5ZzShOezQ6A==", + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.58.2" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, + "node_modules/playwright-core": { + "version": "1.58.2", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.58.2.tgz", + "integrity": "sha512-yZkEtftgwS8CsfYo7nm0KE8jsvm6i/PTgVtB8DL726wNf6H2IMsDuxCpJj59KDaxCtSnrWan2AeDqM7JBaultg==", + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/possible-typed-array-names": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz", diff --git a/package.json b/package.json index 1d68216..7d651ec 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "axios": "^1.13.6", "better-sqlite3": "^12.6.2", "next": "16.1.6", + "playwright": "^1.58.2", "react": "19.2.3", "react-dom": "19.2.3", "recharts": "^3.8.0" diff --git a/types.ts b/types.ts index af72ade..3f3f60c 100644 --- a/types.ts +++ b/types.ts @@ -31,6 +31,10 @@ export interface AccountState { totalProfit: number; /** True when today's realizedPnL has met or exceeded the computed daily target */ targetHit: boolean; + /** Next trading day's target, computed server-side. null when account is dead or config is missing. */ + dailyTarget: { amount: number; path: 'first_day' | 'normal_day' | 'reduced_day' } | null; + /** FIFO daily P&L history — used by the equity curve and calendar. */ + dailyPnL: { date: string; pnl: number }[]; } export interface FirmState {