Fix fetchDaysTraded to use reports API with bearer auth and correct daysTraded counting
- Replace fill/ldeps and fill/list approaches with Tradovate reports API - Add bearer auth to getreport polling (root cause of prior 404s) - Use endDate = tomorrow to ensure current-session fills are included - Count all traded days when minDayPnL is 0, otherwise count days >= minDayPnL - Add PATCH /api/debug endpoint for proxying raw Tradovate API calls Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
95e7433941
commit
570a54fe60
@@ -45,3 +45,6 @@ next-env.d.ts
|
|||||||
dev.log
|
dev.log
|
||||||
dev.err
|
dev.err
|
||||||
nul
|
nul
|
||||||
|
scripts/lucid-cookies.json
|
||||||
|
scripts/lucid-config.json
|
||||||
|
scripts/*.png
|
||||||
|
|||||||
@@ -15,12 +15,6 @@ import {
|
|||||||
Dot,
|
Dot,
|
||||||
} from 'recharts';
|
} from 'recharts';
|
||||||
import type { FirmConfig, FirmState, AccountState, AccountConfig } from '@/types';
|
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 {
|
function getAccountConfig(name: string, firm: FirmConfig): AccountConfig | undefined {
|
||||||
return [...firm.accounts]
|
return [...firm.accounts]
|
||||||
@@ -178,20 +172,15 @@ export default function AccountPage() {
|
|||||||
const [account, setAccount] = useState<AccountState | null>(null);
|
const [account, setAccount] = useState<AccountState | null>(null);
|
||||||
const [cfg, setCfg] = useState<AccountConfig | null>(null);
|
const [cfg, setCfg] = useState<AccountConfig | null>(null);
|
||||||
const [firmName, setFirmName] = useState('');
|
const [firmName, setFirmName] = useState('');
|
||||||
const [dailyPnL, setDailyPnL] = useState<DailyPnL[]>([]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function load() {
|
async function load() {
|
||||||
const [stateRes, firmsRes, dailyRes] = await Promise.all([
|
const [stateRes, firmsRes] = await Promise.all([
|
||||||
fetch('/api/state'),
|
fetch('/api/state'),
|
||||||
fetch('/api/firms'),
|
fetch('/api/firms'),
|
||||||
fetch(`/api/accounts/${accountId}/daily-pnl`),
|
|
||||||
]);
|
]);
|
||||||
const states: FirmState[] = await stateRes.json();
|
const states: FirmState[] = await stateRes.json();
|
||||||
const firms: FirmConfig[] = await firmsRes.json();
|
const firms: FirmConfig[] = await firmsRes.json();
|
||||||
const daily: DailyPnL[] = await dailyRes.json();
|
|
||||||
|
|
||||||
setDailyPnL(daily);
|
|
||||||
|
|
||||||
for (const firmState of states) {
|
for (const firmState of states) {
|
||||||
const acc = firmState.accounts.find((a) => a.id === accountId);
|
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 hasLossLimit = cfg != null && cfg.minDayPnL !== -999;
|
||||||
const daysPassed = cfg != null && account.daysTraded >= cfg.minTradingDays;
|
const daysPassed = cfg != null && account.daysTraded >= cfg.minTradingDays;
|
||||||
// Dead when balance hits Tradovate's auto-liquidation floor
|
// Dead when balance hits Tradovate's auto-liquidation floor
|
||||||
@@ -237,15 +229,14 @@ export default function AccountPage() {
|
|||||||
: Math.round(fifoTotal * 100) / 100;
|
: Math.round(fifoTotal * 100) / 100;
|
||||||
const profitPct = cfg?.accountSize ? (totalProfit / cfg.accountSize) * 100 : null;
|
const profitPct = cfg?.accountSize ? (totalProfit / cfg.accountSize) * 100 : null;
|
||||||
const profitPassed = cfg != null && totalProfit >= cfg.profitTarget;
|
const profitPassed = cfg != null && totalProfit >= cfg.profitTarget;
|
||||||
const dailyTarget = cfg && !isDead
|
// dailyTarget is computed server-side in the state API — single source of truth.
|
||||||
? computeDailyTarget(cfg.profitTarget, cfg.consistency, totalProfit, dailyPnL)
|
const dailyTarget = account.dailyTarget ?? null;
|
||||||
: null;
|
|
||||||
const lossPassed = !hasLossLimit || (cfg != null && totalProfit >= cfg.minDayPnL);
|
const lossPassed = !hasLossLimit || (cfg != null && totalProfit >= cfg.minDayPnL);
|
||||||
|
|
||||||
// Consistency target: the total profit level at which the best day no longer
|
// Consistency target: the total profit level at which the best day no longer
|
||||||
// violates the consistency ratio. Only meaningful once a positive day exists.
|
// 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 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
|
? Math.round(maxDayPnL / cfg.consistency * 100) / 100
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { getClients, resetClients } from '@/lib/clients';
|
import { getClients, resetClients } from '@/lib/clients';
|
||||||
import { getFirms } from '@/lib/db';
|
import { getFirms } from '@/lib/db';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
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
|
* DELETE /api/debug — force-reinitialize all Tradovate clients with fresh instances
|
||||||
* Clears the global pool so getClients() recreates everything from DB on next call.
|
* Clears the global pool so getClients() recreates everything from DB on next call.
|
||||||
|
|||||||
+10
-4
@@ -26,11 +26,15 @@ export async function GET() {
|
|||||||
const daysTraded: number = client.daysTraded[acc.id] ?? 0;
|
const daysTraded: number = client.daysTraded[acc.id] ?? 0;
|
||||||
const totalProfit = dailyPnL.reduce((sum, d) => sum + d.pnl, 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 cfg = getAccountConfig(acc.name, f.accounts);
|
||||||
|
const autoLiqThreshold = client.autoLiqThresholds[acc.id] ?? 0;
|
||||||
|
const isDead = autoLiqThreshold > 0 && cash.amount <= autoLiqThreshold;
|
||||||
let targetHit = false;
|
let targetHit = false;
|
||||||
if (cfg) {
|
let dailyTarget: { amount: number; path: 'first_day' | 'normal_day' | 'reduced_day' } | null = null;
|
||||||
const target = computeDailyTarget(cfg.profit_target, cfg.consistency, totalProfit, dailyPnL);
|
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 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
|
// Condition 2: target > 0 → must have made at least the computed daily target
|
||||||
targetHit =
|
targetHit =
|
||||||
@@ -47,9 +51,11 @@ export async function GET() {
|
|||||||
realizedPnL: cash.realizedPnL,
|
realizedPnL: cash.realizedPnL,
|
||||||
daysTraded,
|
daysTraded,
|
||||||
hasPosition: !!client.positions[acc.id],
|
hasPosition: !!client.positions[acc.id],
|
||||||
autoLiqThreshold: client.autoLiqThresholds[acc.id] ?? 0,
|
autoLiqThreshold,
|
||||||
totalProfit,
|
totalProfit,
|
||||||
targetHit,
|
targetHit,
|
||||||
|
dailyTarget,
|
||||||
|
dailyPnL,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return { firm: f.name, connected: true, accounts, perContractFees: client.perContractFees };
|
return { firm: f.name, connected: true, accounts, perContractFees: client.perContractFees };
|
||||||
|
|||||||
@@ -163,6 +163,11 @@ export async function runTrade(action: 'Buy' | 'Sell', symbol: string) {
|
|||||||
const exitAction: 'Buy' | 'Sell' = action === 'Buy' ? 'Sell' : 'Buy';
|
const exitAction: 'Buy' | 'Sell' = action === 'Buy' ? 'Sell' : 'Buy';
|
||||||
const exitFill = await client.sendOrder(acc.id, mnqContract.name, 1, exitAction, 'Market');
|
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)`);
|
console.log(`[auto-trade] ${acc.name} (${item.firmName}) extra-day: ${action} 1xMNQ @ ${fill.price} | exited @ ${exitFill.price} (market)`);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -204,6 +209,15 @@ export async function runTrade(action: 'Buy' | 'Sell', symbol: string) {
|
|||||||
const exitAction: 'Buy' | 'Sell' = action === 'Buy' ? 'Sell' : 'Buy';
|
const exitAction: 'Buy' | 'Sell' = action === 'Buy' ? 'Sell' : 'Buy';
|
||||||
const exitOrder = await client.placeOrderNoWait(acc.id, contract.name, contracts, exitAction, 'Limit', exitPrice);
|
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})`);
|
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 {
|
return {
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ export function computeDailyTarget(
|
|||||||
minDayPnL: number = 0, // -999 or 0 = no minimum per day
|
minDayPnL: number = 0, // -999 or 0 = no minimum per day
|
||||||
minTradingDays: number = 0 // 0 = no minimum trading days
|
minTradingDays: number = 0 // 0 = no minimum trading days
|
||||||
): { amount: number; path: 'first_day' | 'normal_day' | 'reduced_day' } {
|
): { amount: number; path: 'first_day' | 'normal_day' | 'reduced_day' } {
|
||||||
const positiveDays = dailyPnL.filter((d) => d.pnl > 0);
|
const qualifyingDays = minDayPnL === 0 ? dailyPnL : dailyPnL.filter((d) => d.pnl >= minDayPnL);
|
||||||
const daysTraded = positiveDays.length;
|
const daysTraded = qualifyingDays.length;
|
||||||
|
|
||||||
// --- Base target via consistency logic ---
|
// --- Base target via consistency logic ---
|
||||||
let baseAmount: number;
|
let baseAmount: number;
|
||||||
@@ -45,8 +45,13 @@ export function computeDailyTarget(
|
|||||||
if (daysTraded === 0) {
|
if (daysTraded === 0) {
|
||||||
baseAmount = profitTarget * consistency;
|
baseAmount = profitTarget * consistency;
|
||||||
path = 'first_day';
|
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 {
|
} else {
|
||||||
const maxDay = Math.max(...positiveDays.map((d) => d.pnl));
|
const maxDay = Math.max(...qualifyingDays.map((d) => d.pnl));
|
||||||
const realTarget = maxDay / consistency;
|
const realTarget = maxDay / consistency;
|
||||||
const needed = realTarget - totalProfit;
|
const needed = realTarget - totalProfit;
|
||||||
|
|
||||||
|
|||||||
+35
-43
@@ -36,7 +36,7 @@ export class TradovateClient {
|
|||||||
public fetchDaysComplete = false;
|
public fetchDaysComplete = false;
|
||||||
/** Last error per account name from fetchDaysTraded() */
|
/** Last error per account name from fetchDaysTraded() */
|
||||||
public lastFetchErrors: Record<string, string> = {};
|
public lastFetchErrors: Record<string, string> = {};
|
||||||
/** 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<string, string> = {};
|
public lastFetchRaw: Record<string, string> = {};
|
||||||
|
|
||||||
public products: { id: number; name: string }[] = [];
|
public products: { id: number; name: string }[] = [];
|
||||||
@@ -315,9 +315,12 @@ export class TradovateClient {
|
|||||||
if (!this.accessInfo?.accessToken) return;
|
if (!this.accessInfo?.accessToken) return;
|
||||||
this.fetchDaysComplete = false;
|
this.fetchDaysComplete = false;
|
||||||
|
|
||||||
|
const authHeaders = { Authorization: `Bearer ${this.accessInfo.accessToken}` };
|
||||||
|
|
||||||
const now = new Date();
|
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();
|
const start = new Date();
|
||||||
start.setDate(start.getDate() - 28);
|
start.setDate(start.getDate() - 27); // keep total window ≤ 28 days
|
||||||
|
|
||||||
const fmtDate = (d: Date) => {
|
const fmtDate = (d: Date) => {
|
||||||
const m = String(d.getMonth() + 1).padStart(2, '0');
|
const m = String(d.getMonth() + 1).padStart(2, '0');
|
||||||
@@ -325,29 +328,36 @@ export class TradovateClient {
|
|||||||
return `${m}/${day}/${d.getFullYear()}`;
|
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) {
|
for (const account of this.accountList) {
|
||||||
try {
|
try {
|
||||||
const authHeaders = { Authorization: `Bearer ${this.accessInfo.accessToken}` };
|
|
||||||
|
|
||||||
// Step 1 — request the report
|
|
||||||
let reportData = (await axios.post(
|
let reportData = (await axios.post(
|
||||||
'https://rpt-demo.tradovateapi.com/v1/reports/requestreport',
|
'https://rpt-demo.tradovateapi.com/v1/reports/requestreport',
|
||||||
{
|
{
|
||||||
name: 'Fills',
|
name: 'Fills', representationType: 'json', timezone: -300,
|
||||||
params: [
|
params: [
|
||||||
{ name: 'startDate', value: fmtDate(start) },
|
{ name: 'startDate', value: fmtDate(start) },
|
||||||
{ name: 'endDate', value: fmtDate(now) },
|
{ name: 'endDate', value: fmtDate(now) },
|
||||||
{ name: 'startTime', value: '00:00:00' },
|
{ name: 'startTime', value: '00:00:00' },
|
||||||
{ name: 'endTime', value: '00:00:00' },
|
{ name: 'endTime', value: '00:00:00' },
|
||||||
{ name: 'account', value: account.name },
|
{ name: 'account', value: account.name },
|
||||||
],
|
],
|
||||||
representationType: 'json',
|
|
||||||
timezone: 0,
|
|
||||||
},
|
},
|
||||||
{ headers: authHeaders }
|
{ headers: authHeaders }
|
||||||
)).data;
|
)).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;
|
let pollAttempts = 0;
|
||||||
while (reportData?.['p-ticket'] && pollAttempts < 30) {
|
while (reportData?.['p-ticket'] && pollAttempts < 30) {
|
||||||
const pTicket: string = reportData['p-ticket'];
|
const pTicket: string = reportData['p-ticket'];
|
||||||
@@ -361,30 +371,14 @@ export class TradovateClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this.lastFetchRaw) this.lastFetchRaw = {};
|
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
|
// _tradeDate is unquoted in the response (invalid JSON) — fix before parsing
|
||||||
// ("M/D/YY") is a valid quoted string that already reflects CME trade date.
|
const fixed = raw.replace(/"_tradeDate":\s*(\d{4}-\d{2}-\d{2})/g, '"_tradeDate": "$1"');
|
||||||
const rawResponse = reportData?.data ?? '[]';
|
const fills: Fill[] = JSON.parse(fixed);
|
||||||
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);
|
|
||||||
|
|
||||||
// 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));
|
const sorted = [...fills].sort((a, b) => a._timestamp.localeCompare(b._timestamp));
|
||||||
interface Lot { price: number; qty: number; commPerUnit: number }
|
|
||||||
const longBook: Lot[] = [];
|
const longBook: Lot[] = [];
|
||||||
const shortBook: Lot[] = [];
|
const shortBook: Lot[] = [];
|
||||||
const dailyMap: { [date: string]: number } = {};
|
const dailyMap: { [date: string]: number } = {};
|
||||||
@@ -396,13 +390,12 @@ export class TradovateClient {
|
|||||||
const commPerUnit = fill._qty > 0 ? fill.commission / fill._qty : 0;
|
const commPerUnit = fill._qty > 0 ? fill.commission / fill._qty : 0;
|
||||||
|
|
||||||
if (isBuy) {
|
if (isBuy) {
|
||||||
// Close any short lots first (FIFO), then open long
|
|
||||||
while (remaining > 0 && shortBook.length > 0) {
|
while (remaining > 0 && shortBook.length > 0) {
|
||||||
const lot = shortBook[0];
|
const lot = shortBook[0];
|
||||||
const closed = Math.min(lot.qty, remaining);
|
const closed = Math.min(lot.qty, remaining);
|
||||||
const pnl = (lot.price - fill._price) * closed * pointValue
|
const pnl = (lot.price - fill._price) * closed * pointValue
|
||||||
- (commPerUnit * closed) // closing fill commission
|
- (commPerUnit * closed)
|
||||||
- (lot.commPerUnit * closed); // opening fill commission
|
- (lot.commPerUnit * closed);
|
||||||
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
|
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
|
||||||
lot.qty -= closed;
|
lot.qty -= closed;
|
||||||
remaining -= closed;
|
remaining -= closed;
|
||||||
@@ -410,13 +403,12 @@ export class TradovateClient {
|
|||||||
}
|
}
|
||||||
if (remaining > 0) longBook.push({ price: fill._price, qty: remaining, commPerUnit });
|
if (remaining > 0) longBook.push({ price: fill._price, qty: remaining, commPerUnit });
|
||||||
} else {
|
} else {
|
||||||
// Close any long lots first (FIFO), then open short
|
|
||||||
while (remaining > 0 && longBook.length > 0) {
|
while (remaining > 0 && longBook.length > 0) {
|
||||||
const lot = longBook[0];
|
const lot = longBook[0];
|
||||||
const closed = Math.min(lot.qty, remaining);
|
const closed = Math.min(lot.qty, remaining);
|
||||||
const pnl = (fill._price - lot.price) * closed * pointValue
|
const pnl = (fill._price - lot.price) * closed * pointValue
|
||||||
- (commPerUnit * closed) // closing fill commission
|
- (commPerUnit * closed)
|
||||||
- (lot.commPerUnit * closed); // opening fill commission
|
- (lot.commPerUnit * closed);
|
||||||
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
|
dailyMap[fill._tradeDate] = (dailyMap[fill._tradeDate] ?? 0) + pnl;
|
||||||
lot.qty -= closed;
|
lot.qty -= closed;
|
||||||
remaining -= closed;
|
remaining -= closed;
|
||||||
@@ -431,13 +423,13 @@ export class TradovateClient {
|
|||||||
.sort((a, b) => a.date.localeCompare(b.date));
|
.sort((a, b) => a.date.localeCompare(b.date));
|
||||||
|
|
||||||
this.dailyPnL[account.id] = entries;
|
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) {
|
} catch (err) {
|
||||||
const msg = err instanceof Error ? `${err.message}` : String(err);
|
const msg = err instanceof Error ? err.message : String(err);
|
||||||
console.error(`[fetchDaysTraded] ${account.name}:`, msg);
|
console.error(`[fetchDaysTraded] report error for ${account.name}:`, msg);
|
||||||
if (!this.lastFetchErrors) this.lastFetchErrors = {};
|
if (!this.lastFetchErrors) this.lastFetchErrors = {};
|
||||||
this.lastFetchErrors[account.name] = msg;
|
this.lastFetchErrors[account.name] = msg;
|
||||||
|
this.dailyPnL[account.id] ??= [];
|
||||||
this.daysTraded[account.id] ??= 0;
|
this.daysTraded[account.id] ??= 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+45
@@ -11,6 +11,7 @@
|
|||||||
"axios": "^1.13.6",
|
"axios": "^1.13.6",
|
||||||
"better-sqlite3": "^12.6.2",
|
"better-sqlite3": "^12.6.2",
|
||||||
"next": "16.1.6",
|
"next": "16.1.6",
|
||||||
|
"playwright": "^1.58.2",
|
||||||
"react": "19.2.3",
|
"react": "19.2.3",
|
||||||
"react-dom": "19.2.3",
|
"react-dom": "19.2.3",
|
||||||
"recharts": "^3.8.0"
|
"recharts": "^3.8.0"
|
||||||
@@ -4115,6 +4116,20 @@
|
|||||||
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==",
|
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==",
|
||||||
"license": "MIT"
|
"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": {
|
"node_modules/function-bind": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
|
||||||
@@ -5961,6 +5976,36 @@
|
|||||||
"url": "https://github.com/sponsors/jonschlinkert"
|
"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": {
|
"node_modules/possible-typed-array-names": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz",
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
"axios": "^1.13.6",
|
"axios": "^1.13.6",
|
||||||
"better-sqlite3": "^12.6.2",
|
"better-sqlite3": "^12.6.2",
|
||||||
"next": "16.1.6",
|
"next": "16.1.6",
|
||||||
|
"playwright": "^1.58.2",
|
||||||
"react": "19.2.3",
|
"react": "19.2.3",
|
||||||
"react-dom": "19.2.3",
|
"react-dom": "19.2.3",
|
||||||
"recharts": "^3.8.0"
|
"recharts": "^3.8.0"
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ export interface AccountState {
|
|||||||
totalProfit: number;
|
totalProfit: number;
|
||||||
/** True when today's realizedPnL has met or exceeded the computed daily target */
|
/** True when today's realizedPnL has met or exceeded the computed daily target */
|
||||||
targetHit: boolean;
|
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 {
|
export interface FirmState {
|
||||||
|
|||||||
Reference in New Issue
Block a user