Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a7e01b0138 | ||
|
|
acb8879575 |
+21
-1
@@ -3,8 +3,9 @@ import { loadDailyPnL } from '@/lib/db';
|
||||
import { getClients } from '@/lib/clients';
|
||||
import { getCachedCatalog } from '@/lib/firm-catalog';
|
||||
import { getAccountConfig, getInferredSize, matchAccountType } from '@/lib/account-type-resolver';
|
||||
import { historyFresh } from '@/lib/platform-client';
|
||||
import {
|
||||
combineCmeSessions, computeDayPlan, resolveEffectiveConfig, currentTradeDateCT,
|
||||
combineCmeSessions, computeDayPlan, cycleCompletion, resolveEffectiveConfig, currentTradeDateCT,
|
||||
BIG_DAY_THRESHOLD, FIRST_DAY_STOP, LEG_COMPLETION_EPSILON, MIN_DAY_STOP_MULT,
|
||||
} from '@/lib/trading-logic';
|
||||
|
||||
@@ -174,6 +175,24 @@ export async function GET() {
|
||||
}
|
||||
}
|
||||
|
||||
// Today finished the cycle — the reporter's check and guards,
|
||||
// so this pill and the master's purple count agree.
|
||||
let completed = false;
|
||||
const historyLoaded = client.dailyPnL[acc.id] !== undefined
|
||||
&& client.fundTransactions?.[acc.id] !== undefined;
|
||||
if (cfg && effective && historyLoaded && historyFresh(client, acc) && !client.positions[acc.id]) {
|
||||
completed = cycleCompletion({
|
||||
accountId: acc.id,
|
||||
rawTradeDate: tradeDate,
|
||||
cycleDailyPnL: client.dailyPnL[acc.id],
|
||||
rawRealizedPnL: cash.realizedPnL,
|
||||
combineCmeHolidays: cfg.combineCmeHolidays,
|
||||
effective,
|
||||
equityProfit: cash.amount - cfg.accountSize,
|
||||
splitEnabled: cfg.splitEnabled,
|
||||
}) === 'today';
|
||||
}
|
||||
|
||||
return {
|
||||
id: acc.id,
|
||||
name: acc.name,
|
||||
@@ -195,6 +214,7 @@ export async function GET() {
|
||||
autoLiqThreshold,
|
||||
totalProfit,
|
||||
targetHit,
|
||||
completed,
|
||||
stage,
|
||||
// Current stage is a drain stage — dashboards label it "Stage N – Drain".
|
||||
stageDrain: effective?.drain ?? false,
|
||||
|
||||
+13
-10
@@ -10,9 +10,10 @@ type SortDir = 'asc' | 'desc';
|
||||
|
||||
function statusRank(account: AccountState): number {
|
||||
if (isAccountDead(account)) return 0;
|
||||
if (account.completed) return 5; // Completed — finished its cycle today
|
||||
if (!account.active) return 1;
|
||||
if (!account.positionDirection) {
|
||||
if (account.targetHit) return 4; // Target Hit — most accomplished
|
||||
if (account.targetHit) return 4; // Target Hit — next to Completed
|
||||
return 2; // Flat
|
||||
}
|
||||
return 3; // In Trade
|
||||
@@ -384,15 +385,17 @@ function AccountRow({ account, firm, hideDead, privacy, onAssigned }: {
|
||||
<div className="flex items-center justify-between">
|
||||
{dead
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-slate-800 text-white">Dead</span>
|
||||
: !account.active
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-red-100 text-red-700">Hit DLL</span>
|
||||
: account.positionDirection
|
||||
? <span className={`inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold ${account.positionDirection === 'long' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}>{account.positionDirection === 'long' ? 'Long' : 'Short'}</span>
|
||||
: account.personalDll
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-amber-100 text-amber-700" title="Booked the day red via its own stop — make-back rides next session">Personal DLL</span>
|
||||
: account.targetHit
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-green-100 text-green-700">Target Hit</span>
|
||||
: <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-slate-100 text-slate-500">Flat</span>}
|
||||
: account.completed
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-purple-100 text-purple-700" title="Finished its cycle today — target, consistency and min days met">Completed</span>
|
||||
: !account.active
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-red-100 text-red-700">Hit DLL</span>
|
||||
: account.positionDirection
|
||||
? <span className={`inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold ${account.positionDirection === 'long' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}>{account.positionDirection === 'long' ? 'Long' : 'Short'}</span>
|
||||
: account.personalDll
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-amber-100 text-amber-700" title="Booked the day red via its own stop — make-back rides next session">Personal DLL</span>
|
||||
: account.targetHit
|
||||
? <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-green-100 text-green-700">Target Hit</span>
|
||||
: <span className="inline-block px-2.5 py-0.5 rounded-full text-xs font-semibold bg-slate-100 text-slate-500">Flat</span>}
|
||||
<Link
|
||||
href={`/accounts/${account.id}`}
|
||||
className="text-slate-400 hover:text-slate-600 hover:bg-slate-200 p-1 rounded-md transition-colors inline-flex"
|
||||
|
||||
@@ -348,6 +348,31 @@ export function loadAccountMeta(accountId: number, key: string): string | null {
|
||||
return row?.value ?? null;
|
||||
}
|
||||
|
||||
/** Where the cycle the latest fund transaction ended began, saved by the
|
||||
* platform client as it moves last_fund_date forward — and only from a cycle
|
||||
* start this instance saw, so the reporter's catch-up never judges a cycle
|
||||
* whose older rows it lacks. fundDate = that cycle's fund date ('' for the
|
||||
* account's first cycle); includesDay = whether that date's own trades were
|
||||
* the cycle's (a deposit's are; a Tradovate withdrawal's only when it came
|
||||
* before the day's first trade, which the reporter can't work out again). */
|
||||
export function saveEndedCycleStart(accountId: number, start: { fundDate: string; includesDay: boolean }): void {
|
||||
saveAccountMeta(accountId, 'ended_cycle_start', JSON.stringify(start));
|
||||
}
|
||||
|
||||
/** null when this instance never saw the ended cycle start: a new instance
|
||||
* or a wipe (which clears it with the rest of the meta), or TopstepX's
|
||||
* first-check lump. */
|
||||
export function loadEndedCycleStart(accountId: number): { fundDate: string; includesDay: boolean } | null {
|
||||
const raw = loadAccountMeta(accountId, 'ended_cycle_start');
|
||||
if (raw === null) return null;
|
||||
try {
|
||||
const { fundDate, includesDay } = JSON.parse(raw) as { fundDate?: unknown; includesDay?: unknown };
|
||||
return typeof fundDate === 'string' && typeof includesDay === 'boolean' ? { fundDate, includesDay } : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes every cached row for an account — daily P&L, fund transactions and
|
||||
* meta — so the next report fetch rebuilds it from scratch. Rows older than
|
||||
* the report's 28-day window are gone for good; that is the point (stale
|
||||
|
||||
+31
-3
@@ -25,6 +25,22 @@ export interface AccountTypeStats {
|
||||
/** Of the finished accounts, how many booked the day via their own
|
||||
* obfuscation stop (personal DLL) rather than a genuine loss. */
|
||||
personalDll?: number;
|
||||
/** Of targetHit: accounts whose day finished the current cycle — profit
|
||||
* target, consistency and minimum days all met once today's P&L booked
|
||||
* (the final day). Optional: older instances omit it. */
|
||||
completed?: number;
|
||||
/** The accounts behind `completed`, by name, so the master can keep them
|
||||
* after a firm erases a passed account. date = the trade date it
|
||||
* completed on; stage = 1 + withdrawals taken (its meaning depends on the
|
||||
* account type); kind: 'pass' = an eval passing, 'payout' = a funded
|
||||
* cycle ready to withdraw, 'drain' = a drain stage's days booked;
|
||||
* cycle = the date of the fund transaction that started the cycle ('' for
|
||||
* the account's first cycle) — firm + name + cycle identify one
|
||||
* completed cycle. The list also carries caught-up entries: for up to 7
|
||||
* days after a fund transaction starts a new cycle, it includes the cycle
|
||||
* that just ended if that cycle had met the rule. Caught-up entries are
|
||||
* not counted in `completed`. Optional: older instances omit it. */
|
||||
completedAccounts?: { name: string; date: string; stage: number; kind: 'pass' | 'payout' | 'drain'; cycle: string }[];
|
||||
/** Live accounts by trading days completed toward the current target:
|
||||
* {daysTraded: accountCount}. Feeds the master's payout calendar, so
|
||||
* accounts that are dead, DLL-locked, or in the red are left out — the
|
||||
@@ -33,9 +49,10 @@ export interface AccountTypeStats {
|
||||
daysHist?: Record<number, number>;
|
||||
/** Live accounts by the trading day they are on today (1 = first day of
|
||||
* the current cycle, counting traded days before today's trade date),
|
||||
* with how many of them hit today's target. Lets the master show
|
||||
* "Day 1 3/5 · Day 2 1/4". Optional: older instances omit it. */
|
||||
dayStats?: Record<number, { total: number; targetHit: number }>;
|
||||
* with how many of them hit today's target; completed = how many of those
|
||||
* hits finished the cycle. Lets the master show "Day 1 3/5 · Day 2 1/4".
|
||||
* Optional: older instances omit it. */
|
||||
dayStats?: Record<number, { total: number; targetHit: number; completed?: number }>;
|
||||
/** One entry per live account (negative ones included, dead ones not):
|
||||
* its actual progress toward the current target, so the master can
|
||||
* simulate the real required profit instead of assuming the winning
|
||||
@@ -75,6 +92,17 @@ export interface FirmStats {
|
||||
partial?: number;
|
||||
loss?: number;
|
||||
personalDll?: number;
|
||||
/** Of targetHit: accounts that finished their cycle today. Optional. */
|
||||
completed?: number;
|
||||
/** Every account in the firm's broker list right now, dead ones included,
|
||||
* so the master can tell a saved account the firm erased from one still
|
||||
* listed, and spot a newly arrived funded account. size = the account
|
||||
* type's size (0 without one); payoutable = the type pays out — its
|
||||
* payoutable flag or withdrawal stages, the test that reports its
|
||||
* completions as 'payout' — null while the account has no type. Absent
|
||||
* when the firm isn't synced (unknown, not empty). Optional: older
|
||||
* instances omit it. */
|
||||
accounts?: { name: string; size: number; payoutable: boolean | null }[];
|
||||
/** Per account type, summing to the firm's own totals. Optional so a master
|
||||
* on an older build simply ignores it. */
|
||||
accountTypes?: AccountTypeStats[];
|
||||
|
||||
@@ -82,3 +82,20 @@ export interface PlatformClient {
|
||||
disconnect(): void;
|
||||
onFill(orderId: number, callback: (fill: Record<string, unknown>) => void): void;
|
||||
}
|
||||
|
||||
/** lastFetchErrors note for a Tradovate account whose Cash History is
|
||||
* unavailable (passed accounts) and whose rows came from the Fills report —
|
||||
* a fresh fetch, not a failure. */
|
||||
export const FILLS_FALLBACK_NOTE = 'cash history unavailable (using fills fallback)';
|
||||
|
||||
/**
|
||||
* Can this account's daily P&L be trusted for a verdict the master keeps
|
||||
* for good (cycleCompletion 'today')? Not while a fetch run is in flight —
|
||||
* at startup Tradovate serves the DB cache before each account's fetch —
|
||||
* and not when the account's last fetch failed and left cached rows behind.
|
||||
*/
|
||||
export function historyFresh(client: PlatformClient, account: PlatformAccount): boolean {
|
||||
if (!client.fetchDaysComplete) return false;
|
||||
const error = client.lastFetchErrors?.[account.name];
|
||||
return !error || error === FILLS_FALLBACK_NOTE;
|
||||
}
|
||||
|
||||
+108
-7
@@ -11,15 +11,16 @@
|
||||
* POST keeps those working unchanged.
|
||||
*/
|
||||
|
||||
import { getSetting } from './db';
|
||||
import { getSetting, loadEndedCycleStart } from './db';
|
||||
import { pushReport } from './master-link';
|
||||
import { getBuildVersion } from './version';
|
||||
import { getClients } from './clients';
|
||||
import { getTradableFirms } from './firm-catalog';
|
||||
import { getAccountConfig } from './account-type-resolver';
|
||||
import { getSchedulerStatus } from './auto-trade';
|
||||
import { combineCmeSessions, computeDayPlan, currentTradeDateCT, LEG_COMPLETION_EPSILON, projectPerfectPath, resolveEffectiveConfig } from './trading-logic';
|
||||
import type { AccountTypeStats } from './master-protocol';
|
||||
import { combineCmeSessions, computeDayPlan, currentTradeDateCT, cycleCompletion, endedCycleCompletion, LEG_COMPLETION_EPSILON, projectPerfectPath, resolveEffectiveConfig } from './trading-logic';
|
||||
import type { AccountTypeStats, FirmStats } from './master-protocol';
|
||||
import { historyFresh } from './platform-client';
|
||||
|
||||
function collectFirmStats() {
|
||||
// Only firms this machine has credentials for. A firm it cannot log into
|
||||
@@ -27,13 +28,26 @@ function collectFirmStats() {
|
||||
// the master's table with rows for firms it does not run.
|
||||
const firms = getTradableFirms();
|
||||
const clients = getClients();
|
||||
// One raw trade date for the whole report: the combined views below roll
|
||||
// it forward, the completion check needs it as is.
|
||||
const rawToday = currentTradeDateCT();
|
||||
// projectPerfectPath's `loops` test: a type whose completed cycle keeps
|
||||
// going pays out (a funded account), one that stops just passes.
|
||||
const loops = (cfg: { payoutable?: boolean; withdrawalStages: unknown[] }): boolean =>
|
||||
cfg.payoutable === true || cfg.withdrawalStages.length > 0;
|
||||
const completionKind = (cfg: Parameters<typeof loops>[0], drain: boolean): 'pass' | 'payout' | 'drain' =>
|
||||
(drain ? 'drain' : loops(cfg) ? 'payout' : 'pass');
|
||||
|
||||
return firms.map((f) => {
|
||||
const client = clients.get(f.id);
|
||||
if (!client || client.accountList.length === 0) {
|
||||
return {
|
||||
firm: f.name, totalAccounts: 0, accountsTraded: 0, inTrade: 0,
|
||||
targetHit: 0, partial: 0, loss: 0, personalDll: 0, accountTypes: [],
|
||||
targetHit: 0, partial: 0, loss: 0, personalDll: 0, completed: 0,
|
||||
// Empty only once synced — before that, a firm whose accounts
|
||||
// haven't arrived would read as every saved account erased.
|
||||
...(client?.syncComplete ? { accounts: [] } : {}),
|
||||
accountTypes: [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -44,6 +58,11 @@ function collectFirmStats() {
|
||||
let partialCount = 0;
|
||||
let lossCount = 0;
|
||||
let personalDllCount = 0;
|
||||
let completedCount = 0;
|
||||
// The firm's whole broker list, dead accounts included — the master
|
||||
// tells an account the firm erased from one still listed, and spots
|
||||
// a newly arrived funded account.
|
||||
const accounts: NonNullable<FirmStats['accounts']> = [];
|
||||
|
||||
// Per account type, so the master can say what the accounts still to
|
||||
// trade actually are. Keyed by account type id; unassigned accounts
|
||||
@@ -73,11 +92,12 @@ function collectFirmStats() {
|
||||
for (const acc of client.accountList) {
|
||||
const cash = client.accountCashBalances[acc.id] ?? { amount: 0, realizedPnL: 0 };
|
||||
const autoLiqThreshold = client.autoLiqThresholds[acc.id] ?? 0;
|
||||
const cfg = getAccountConfig(acc.id).accountType;
|
||||
accounts.push({ name: acc.name, size: cfg?.accountSize ?? 0, payoutable: cfg ? loops(cfg) : null });
|
||||
// 0 = no floor; negative floors are reconstructed TopstepX MLLs.
|
||||
const isDead = autoLiqThreshold !== 0 && cash.amount <= autoLiqThreshold;
|
||||
if (isDead) continue;
|
||||
|
||||
const cfg = getAccountConfig(acc.id).accountType;
|
||||
const slice = bucket(cfg);
|
||||
|
||||
totalAccounts++;
|
||||
@@ -91,7 +111,7 @@ function collectFirmStats() {
|
||||
// projects from "on the winning path", which they are not.
|
||||
// (Dead accounts never reach here — the auto-liq check above.)
|
||||
const combined = cfg ? combineCmeSessions(
|
||||
cfg.combineCmeHolidays, client.dailyPnL[acc.id] ?? [], currentTradeDateCT(), cash.realizedPnL,
|
||||
cfg.combineCmeHolidays, client.dailyPnL[acc.id] ?? [], rawToday, cash.realizedPnL,
|
||||
) : null;
|
||||
|
||||
// Stage-resolved config, shared by the payout state and the
|
||||
@@ -200,6 +220,41 @@ function collectFirmStats() {
|
||||
});
|
||||
}
|
||||
|
||||
// Only once the account's history has loaded — right after a
|
||||
// restart the missing rows and withdrawals would read as a fresh
|
||||
// stage-1 cycle.
|
||||
const historyLoaded = client.dailyPnL[acc.id] !== undefined
|
||||
&& client.fundTransactions?.[acc.id] !== undefined;
|
||||
|
||||
// Catch-up for the cycle a fund transaction just ended: if it stood
|
||||
// complete, name it, since its final day may never have been
|
||||
// reported (a guard held it back, or this instance was offline).
|
||||
// Named only — it isn't today's outcome, so no counter moves. Ahead
|
||||
// of the in-trade check: the new cycle trading doesn't hide it.
|
||||
if (cfg && effective && historyLoaded && historyFresh(client, acc)) {
|
||||
const ended = endedCycleCompletion({
|
||||
accountId: acc.id,
|
||||
rawTradeDate: rawToday,
|
||||
fullDailyPnL: client.fullDailyPnL[acc.id] ?? [],
|
||||
cycleDailyPnL: client.dailyPnL[acc.id],
|
||||
fundTransactions: fundTxns,
|
||||
lastFundDate: client.lastFundDates?.[acc.id] ?? null,
|
||||
endedCycleStart: loadEndedCycleStart(acc.id),
|
||||
rawRealizedPnL: cash.realizedPnL,
|
||||
equityProfit: cash.amount - cfg.accountSize,
|
||||
cfg,
|
||||
});
|
||||
if (ended) {
|
||||
(slice.completedAccounts ??= []).push({
|
||||
name: acc.name,
|
||||
date: ended.date,
|
||||
stage: ended.stage,
|
||||
kind: completionKind(cfg, ended.drain),
|
||||
cycle: ended.cycle,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (client.positions[acc.id]) {
|
||||
inTrade++;
|
||||
accountsTraded++;
|
||||
@@ -208,6 +263,49 @@ function collectFirmStats() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Did today finish the cycle? Checked ahead of the lockout branch:
|
||||
// firms lock a passed account (Tradovate liquidate-only, TopstepX
|
||||
// canTrade=false), and a pass is no loss. A 'today' only on
|
||||
// freshly fetched rows, since the master keeps it.
|
||||
if (cfg && combined && effective && historyLoaded) {
|
||||
const completion = cycleCompletion({
|
||||
accountId: acc.id,
|
||||
rawTradeDate: rawToday,
|
||||
cycleDailyPnL: client.dailyPnL[acc.id],
|
||||
rawRealizedPnL: cash.realizedPnL,
|
||||
combineCmeHolidays: cfg.combineCmeHolidays,
|
||||
effective,
|
||||
equityProfit: cash.amount - cfg.accountSize,
|
||||
splitEnabled: cfg.splitEnabled,
|
||||
});
|
||||
if (completion === 'today' && historyFresh(client, acc)) {
|
||||
accountsTraded++;
|
||||
slice.accountsTraded++;
|
||||
targetHitCount++;
|
||||
slice.targetHit = (slice.targetHit ?? 0) + 1;
|
||||
completedCount++;
|
||||
slice.completed = (slice.completed ?? 0) + 1;
|
||||
if (dayEntry) {
|
||||
dayEntry.targetHit++;
|
||||
dayEntry.completed = (dayEntry.completed ?? 0) + 1;
|
||||
}
|
||||
(slice.completedAccounts ??= []).push({
|
||||
name: acc.name,
|
||||
date: combined.tradeDate,
|
||||
stage: 1 + fundTxns.filter((f) => f.amount < 0).length,
|
||||
kind: completionKind(cfg, effective.drain),
|
||||
// The fund transaction that started this cycle — the
|
||||
// catch-up names the same cycle by the same key.
|
||||
cycle: client.lastFundDates?.[acc.id] ?? '',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// Passed on an earlier day and locked since: nothing traded
|
||||
// today, so it reads as untraded — like a finished account
|
||||
// that's still active — not as a DLL loss every day after.
|
||||
if (completion === 'before' && !acc.active && combined.realizedPnL === 0) continue;
|
||||
}
|
||||
|
||||
// DLL lockout: Tradovate flips the account to liquidate-only, so
|
||||
// its day is over at a loss even when the booked realizedPnL
|
||||
// hasn't caught up yet (or the account has no matching config).
|
||||
@@ -269,7 +367,10 @@ function collectFirmStats() {
|
||||
return {
|
||||
firm: f.name, totalAccounts, accountsTraded, inTrade,
|
||||
targetHit: targetHitCount, partial: partialCount, loss: lossCount,
|
||||
personalDll: personalDllCount, accountTypes,
|
||||
personalDll: personalDllCount, completed: completedCount,
|
||||
// Unknown until the firm has synced, not empty.
|
||||
...(client.syncComplete ? { accounts } : {}),
|
||||
accountTypes,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
+14
-1
@@ -25,7 +25,7 @@ import * as signalR from '@microsoft/signalr';
|
||||
import { currentTradeDateCT } from './trading-logic';
|
||||
import { getCachedContract } from './contract-resolver';
|
||||
import { signalrWebSocket } from './outbound-proxy';
|
||||
import { saveDailyPnL, loadDailyPnL, loadAccountMeta, saveAccountMeta, loadFundTransactions, saveFundTransactions } from './db';
|
||||
import { saveDailyPnL, loadDailyPnL, loadAccountMeta, saveAccountMeta, loadFundTransactions, saveFundTransactions, saveEndedCycleStart } from './db';
|
||||
import { getAccountConfig } from './account-type-resolver';
|
||||
import type { PlatformClient, PlatformAccount, PlatformContract } from './platform-client';
|
||||
|
||||
@@ -563,9 +563,22 @@ export class TopstepXClient implements PlatformClient {
|
||||
const txns = this.fundTransactions[account.id] ?? [];
|
||||
const funds = txns.reduce((s, f) => s + f.amount, 0);
|
||||
const delta = Math.round((cash.amount - startBal - tradePnL - funds) * 100) / 100;
|
||||
// The first check on this instance (or since a wipe) only has the
|
||||
// fetch window to explain the balance with: a lump it books is unseen
|
||||
// P&L and past payouts, not the end of a cycle this instance watched.
|
||||
const firstCheck = loadAccountMeta(account.id, 'funds_reconciled') === null;
|
||||
if (firstCheck) saveAccountMeta(account.id, 'funds_reconciled', today);
|
||||
if (Math.abs(delta) < 200) return;
|
||||
|
||||
console.log(`[topstepx] ${account.name}: balance ${cash.amount} vs books ${Math.round((tradePnL + funds) * 100) / 100} — recording ${delta < 0 ? 'withdrawal' : 'deposit'} of ${delta}`);
|
||||
// A new cycle starts today: note where the one it ends began, by
|
||||
// applyCycleSlice's rule for that date's own row, for the reporter's
|
||||
// catch-up. A second event today doesn't start another cycle.
|
||||
const prevFundDate = this.lastFundDates[account.id] ?? null;
|
||||
if (!firstCheck && prevFundDate !== today) {
|
||||
const prevAmt = prevFundDate ? (txns.find((f) => f.date === prevFundDate)?.amount ?? null) : null;
|
||||
saveEndedCycleStart(account.id, { fundDate: prevFundDate ?? '', includesDay: !(prevAmt !== null && prevAmt < 0) });
|
||||
}
|
||||
// Same-day events merge into one row (the table keys on account+date),
|
||||
// so add today's delta onto whatever is already booked for today.
|
||||
const existingToday = txns.find((f) => f.date === today)?.amount ?? 0;
|
||||
|
||||
@@ -626,6 +626,198 @@ export function projectPerfectPath(opts: {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Where the current cycle stands against today's P&L: 'today' = today's
|
||||
* trading finished it (the final day), 'before' = it already stood complete
|
||||
* without today, 'none' = profit or days still owed. */
|
||||
export type CycleCompletion = 'none' | 'today' | 'before';
|
||||
|
||||
/** How long a new trade date waits before it can read 'today': the platforms
|
||||
* reset realizedPnL on their own clock (a Tradovate cashBalance event, the
|
||||
* TopstepX 60s sync), not at 5 PM sharp, and until then the last session's
|
||||
* number — already booked under its own date — would count as a second day. */
|
||||
const ROLL_SETTLE_MS = 5 * 60 * 1000;
|
||||
|
||||
/** How long after a fund transaction starts a new cycle the cycle it ended is
|
||||
* still checked for a missed completion. */
|
||||
const ENDED_CYCLE_WINDOW_DAYS = 7;
|
||||
|
||||
/** Nothing left to trade AND the days booked: with no min-day amount the
|
||||
* plan coasts at 0 while days are still owed, so count them too. */
|
||||
function cycleOwesNothing(
|
||||
accountId: number,
|
||||
eff: EffectiveConfig,
|
||||
rows: { date: string; pnl: number }[],
|
||||
date: string,
|
||||
equityProfit: number,
|
||||
realized: number,
|
||||
splitEnabled: boolean,
|
||||
): boolean {
|
||||
const plan = computeDayPlan(
|
||||
accountId, date, eff.profitTarget, eff.consistency, rows, eff.minDayPnL,
|
||||
eff.minTradingDays, equityProfit, realized, splitEnabled, eff.drain,
|
||||
);
|
||||
const qualifying = rows.filter((d) => d.date !== date
|
||||
&& (eff.minDayPnL > 0 ? d.pnl >= eff.minDayPnL : d.pnl !== 0)).length;
|
||||
return plan !== null && plan.dayTarget <= 0 && qualifying >= eff.minTradingDays;
|
||||
}
|
||||
|
||||
/** Is the live realizedPnL too doubtful to count as today's session? A
|
||||
* weekend (it is the last session's stale number, already booked under its
|
||||
* own date), a Monday with a weekend-dated row — the live session filed under
|
||||
* a weekend date, which would count it twice — and the first minutes after
|
||||
* the 5 PM roll. */
|
||||
function sessionDoubtful(rawTradeDate: string, cycleDailyPnL: { date: string; pnl: number }[], now: Date): boolean {
|
||||
const day = new Date(`${rawTradeDate}T00:00:00Z`);
|
||||
const dow = day.getUTCDay();
|
||||
if (dow === 0 || dow === 6) return true;
|
||||
if (dow === 1) {
|
||||
const sunday = fmtUTC(new Date(day.getTime() - 24 * 60 * 60 * 1000));
|
||||
const saturday = fmtUTC(new Date(day.getTime() - 2 * 24 * 60 * 60 * 1000));
|
||||
if (cycleDailyPnL.some((d) => d.date === saturday || d.date === sunday)) return true;
|
||||
}
|
||||
// Settle window: a few minutes ago it was still an earlier trade date.
|
||||
return currentTradeDateCT(new Date(now.getTime() - ROLL_SETTLE_MS)) < rawTradeDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Did today's trading finish the account's current cycle — profit target,
|
||||
* consistency and minimum days all met? projectPerfectPath's "already
|
||||
* complete" probe, run once without today's P&L and once with it, so only
|
||||
* the final day reads 'today'.
|
||||
*
|
||||
* The master keeps a 'today' permanently, so every doubtful case reads
|
||||
* 'none' instead: a weekend (realizedPnL is the last session's stale number,
|
||||
* already booked under its own date), a Monday with a weekend-dated row — the
|
||||
* live session filed under a weekend date, which would count it twice — and
|
||||
* the first minutes after the 5 PM roll. 'before' skips those guards: it only
|
||||
* keeps a locked, untraded pass out of the reporter's losses.
|
||||
*/
|
||||
export function cycleCompletion(opts: {
|
||||
accountId: number;
|
||||
/** currentTradeDateCT() — NOT the combined date, which rolls a weekend
|
||||
* forward to Monday. */
|
||||
rawTradeDate: string;
|
||||
/** Current-cycle daily P&L as the client holds it (client.dailyPnL). */
|
||||
cycleDailyPnL: { date: string; pnl: number }[];
|
||||
rawRealizedPnL: number;
|
||||
combineCmeHolidays: boolean;
|
||||
effective: EffectiveConfig;
|
||||
/** amount − accountSize — today's P&L included. */
|
||||
equityProfit: number;
|
||||
splitEnabled: boolean;
|
||||
/** Defaults to the current time. */
|
||||
now?: Date;
|
||||
}): CycleCompletion {
|
||||
const { accountId, rawTradeDate, effective: eff, equityProfit, splitEnabled } = opts;
|
||||
const view = combineCmeSessions(opts.combineCmeHolidays, opts.cycleDailyPnL, rawTradeDate, opts.rawRealizedPnL);
|
||||
const prior = view.dailyPnL.filter((d) => d.date !== view.tradeDate);
|
||||
|
||||
if (cycleOwesNothing(accountId, eff, prior, view.tradeDate, equityProfit, view.realizedPnL, splitEnabled)) return 'before';
|
||||
if (sessionDoubtful(rawTradeDate, opts.cycleDailyPnL, opts.now ?? new Date())) return 'none';
|
||||
|
||||
const withToday = [...prior, { date: view.tradeDate, pnl: view.realizedPnL }];
|
||||
const probe = nextTradingDate(view.tradeDate);
|
||||
if (view.realizedPnL !== 0 && cycleOwesNothing(accountId, eff, withToday, probe, equityProfit, 0, splitEnabled)) return 'today';
|
||||
return 'none';
|
||||
}
|
||||
|
||||
/**
|
||||
* Catch-up for a missed final day: did the cycle the latest fund transaction
|
||||
* just ended stand complete — profit target, consistency and minimum days all
|
||||
* met? cycleCompletion reads 'today' on the final day only, so a completion
|
||||
* its guards held back (or that the instance was offline for) would vanish
|
||||
* once the payout, erasure or reset starts a new cycle and the days revert
|
||||
* to 0. Checked for a week after that fund transaction.
|
||||
*
|
||||
* The ended cycle is judged on its own rows — from the previous fund date
|
||||
* (its own row too when the client counted it in, as it does for a deposit),
|
||||
* up to the latest one, less any the current cycle holds — against the
|
||||
* equity it ended on: live equity less the current cycle's P&L and the fund
|
||||
* transaction itself. A failed account's reset (a positive adjustment after
|
||||
* a cycle that fell short) correctly reads null: the ended cycle never met
|
||||
* the rule. Null on a doubtful session too — on a weekend the live
|
||||
* realizedPnL is stale and would come off equity twice — and when this
|
||||
* instance never saw the ended cycle start (a new instance or a wipe, whose
|
||||
* fetch window cuts it short; TopstepX's first-check lump, which isn't a
|
||||
* cycle at all).
|
||||
*
|
||||
* `cycle` is the fund date that STARTED the ended cycle ('' for the
|
||||
* account's first), which is what client.lastFundDates held while it was
|
||||
* current — the same key the final-day path reported for this cycle, so the
|
||||
* master dedupes the two.
|
||||
*/
|
||||
export function endedCycleCompletion(opts: {
|
||||
accountId: number;
|
||||
/** currentTradeDateCT(). */
|
||||
rawTradeDate: string;
|
||||
/** Every cycle's daily P&L, raw (client.fullDailyPnL). */
|
||||
fullDailyPnL: { date: string; pnl: number }[];
|
||||
/** Current-cycle daily P&L, raw (client.dailyPnL). */
|
||||
cycleDailyPnL: { date: string; pnl: number }[];
|
||||
fundTransactions: { date: string; amount: number }[];
|
||||
/** client.lastFundDates — the fund transaction that started the current cycle. */
|
||||
lastFundDate: string | null;
|
||||
/** loadEndedCycleStart() — where the client saw the ended cycle begin,
|
||||
* null when it never did. */
|
||||
endedCycleStart: { fundDate: string; includesDay: boolean } | null;
|
||||
rawRealizedPnL: number;
|
||||
/** amount − accountSize — today's P&L included. */
|
||||
equityProfit: number;
|
||||
cfg: {
|
||||
profitTarget: number; consistency: number; minTradingDays: number; minDayPnL: number;
|
||||
targetSameEquity: boolean; withdrawalStages: WithdrawalStage[];
|
||||
combineCmeHolidays: boolean; splitEnabled: boolean;
|
||||
};
|
||||
/** Defaults to the current time. */
|
||||
now?: Date;
|
||||
}): { date: string; stage: number; drain: boolean; cycle: string } | null {
|
||||
const { accountId, rawTradeDate, lastFundDate, endedCycleStart: start, fundTransactions, cfg } = opts;
|
||||
if (lastFundDate === null || start === null) return null;
|
||||
const ageDays = (Date.parse(`${rawTradeDate}T00:00:00Z`) - Date.parse(`${lastFundDate}T00:00:00Z`)) / (24 * 60 * 60 * 1000);
|
||||
if (!(ageDays <= ENDED_CYCLE_WINDOW_DAYS)) return null; // NaN included: an unparseable date
|
||||
if (sessionDoubtful(rawTradeDate, opts.cycleDailyPnL, opts.now ?? new Date())) return null;
|
||||
|
||||
const latest = (a: string, b: string) => (b > a ? b : a);
|
||||
const earlierFunds = fundTransactions.filter((f) => f.date < lastFundDate);
|
||||
const prevFundDate = earlierFunds.length > 0 ? earlierFunds.map((f) => f.date).reduce(latest) : null;
|
||||
// Saved for another transition — two fund dates since the client last
|
||||
// looked — so the start it saw isn't this cycle's.
|
||||
if (start.fundDate !== (prevFundDate ?? '')) return null;
|
||||
const current = new Set(opts.cycleDailyPnL.map((d) => d.date));
|
||||
const endedRaw = opts.fullDailyPnL.filter((d) => d.date <= lastFundDate
|
||||
&& (prevFundDate === null || d.date > prevFundDate || (start.includesDay && d.date === prevFundDate))
|
||||
&& !current.has(d.date));
|
||||
if (!endedRaw.some((d) => d.pnl !== 0)) return null;
|
||||
|
||||
// Equity the ended cycle finished on, worked back from live equity: less
|
||||
// everything the current cycle booked (today's live P&L included) and
|
||||
// less the fund transaction between the two.
|
||||
const cur = combineCmeSessions(cfg.combineCmeHolidays, opts.cycleDailyPnL, rawTradeDate, opts.rawRealizedPnL);
|
||||
const curTotal = cur.dailyPnL.filter((d) => d.date !== cur.tradeDate).reduce((s, d) => s + d.pnl, 0)
|
||||
+ cur.realizedPnL;
|
||||
const fundAmt = fundTransactions.filter((f) => f.date === lastFundDate).reduce((s, f) => s + f.amount, 0);
|
||||
const equityBefore = round2(opts.equityProfit - curTotal - fundAmt);
|
||||
|
||||
// The stage the ended cycle traded on: only the withdrawals before it.
|
||||
const eff = resolveEffectiveConfig(
|
||||
cfg.profitTarget, cfg.consistency, cfg.minTradingDays, cfg.targetSameEquity,
|
||||
cfg.withdrawalStages, 0, earlierFunds, cfg.minDayPnL,
|
||||
);
|
||||
const rows = combineCmeSessions(cfg.combineCmeHolidays, endedRaw, lastFundDate, 0).dailyPnL;
|
||||
// A combining firm's folded day can net to 0 — nothing traded, as the firm counts it.
|
||||
const traded = rows.filter((d) => d.pnl !== 0);
|
||||
if (traded.length === 0) return null;
|
||||
const probe = nextTradingDate(rows.map((d) => d.date).reduce(latest));
|
||||
if (!cycleOwesNothing(accountId, eff, rows, probe, equityBefore, 0, cfg.splitEnabled)) return null;
|
||||
|
||||
return {
|
||||
date: traded.map((d) => d.date).reduce(latest),
|
||||
stage: 1 + earlierFunds.filter((f) => f.amount < 0).length,
|
||||
drain: eff.drain,
|
||||
cycle: prevFundDate ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
/** Dollar-per-point value for common futures products. */
|
||||
export const POINT_VALUES: { [symbol: string]: number } = {
|
||||
NQ: 20, MNQ: 2, ES: 50, MES: 5,
|
||||
|
||||
+16
-3
@@ -5,8 +5,9 @@ import type { AccountItem, AuthLoginResponse, Contract } from './tradovate-helpe
|
||||
import { computeSec, randomUUIDV4 } from './tradovate-helpers';
|
||||
import { POINT_VALUES } from './trading-logic';
|
||||
import { getCachedContract, resolveContracts } from './contract-resolver';
|
||||
import { saveDailyPnL, loadDailyPnL, saveAccountMeta, loadAccountMeta, saveFundTransactions, loadFundTransactions } from './db';
|
||||
import { saveDailyPnL, loadDailyPnL, saveAccountMeta, loadAccountMeta, saveFundTransactions, loadFundTransactions, saveEndedCycleStart } from './db';
|
||||
import { brokerWebSocket } from './outbound-proxy';
|
||||
import { FILLS_FALLBACK_NOTE } from './platform-client';
|
||||
|
||||
/**
|
||||
* Entity types Tradovate sends over the user-sync socket. Keep this as the
|
||||
@@ -266,7 +267,6 @@ export class TradovateClient {
|
||||
console.error('[requestSync] Received null/undefined response — auth may have failed');
|
||||
return;
|
||||
}
|
||||
|
||||
// liquidateOnly flag lives in accountRiskStatuses
|
||||
const riskStatusById: { [accountId: number]: { liquidateOnly?: string } } = (
|
||||
response.accountRiskStatuses || []
|
||||
@@ -594,6 +594,19 @@ export class TradovateClient {
|
||||
// Use the most recent fund transaction as the reset point — persist it so it survives beyond the 28-day window
|
||||
const lastFundDate = [...Object.keys(fundMap)].sort().pop() ?? null;
|
||||
if (lastFundDate) {
|
||||
// A new cycle starts: note where the one it ends began —
|
||||
// with filterActivePnL's call on that date's own row — for
|
||||
// the reporter's catch-up. Only from a stored start: the
|
||||
// first load after a wipe or on a new instance never saw
|
||||
// the cycle before it begin.
|
||||
if (storedFundDate && lastFundDate > storedFundDate) {
|
||||
const prevAmt = this.fundTransactions[account.id]?.find((f) => f.date === storedFundDate)?.amount ?? null;
|
||||
saveEndedCycleStart(account.id, {
|
||||
fundDate: storedFundDate,
|
||||
includesDay: !(prevAmt !== null && prevAmt < 0) || TradovateClient.isWithdrawalBeforeFirstTrade(
|
||||
fundTimestampMap[storedFundDate] ?? storedFundTimestamp, firstTradeTsMap[storedFundDate] ?? null),
|
||||
});
|
||||
}
|
||||
saveAccountMeta(account.id, 'last_fund_date', lastFundDate);
|
||||
this.lastFundDates[account.id] = lastFundDate;
|
||||
if (fundTimestampMap[lastFundDate]) {
|
||||
@@ -689,7 +702,7 @@ export class TradovateClient {
|
||||
this.dailyPnL[account.id] = active;
|
||||
this.daysTraded[account.id] = active.filter((d) => d.pnl !== 0).length;
|
||||
saveDailyPnL(account.id, account.name, fresh); // upsert only fresh entries — preserves older cached rows
|
||||
this.lastFetchErrors[account.name] = 'cash history unavailable (using fills fallback)';
|
||||
this.lastFetchErrors[account.name] = FILLS_FALLBACK_NOTE;
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`[fetchDaysTraded] both reports failed for ${account.name}:`, msg);
|
||||
|
||||
@@ -66,6 +66,10 @@ export interface AccountState {
|
||||
/** True when a stop-enabled account booked its day red via its own stop —
|
||||
* the personal DLL. It stands down until the next session's make-back. */
|
||||
personalDll: boolean;
|
||||
/** True when today's trading finished the current cycle — profit target,
|
||||
* consistency and minimum days all met (cycleCompletion 'today'). Only
|
||||
* the final day; the firm may lock or remove the account afterwards. */
|
||||
completed: boolean;
|
||||
/** Current stage = 1 + number of withdrawals (negative fund transactions). Stage 1 = fresh account. */
|
||||
stage: number;
|
||||
/** True when the current stage is a drain stage (spam-withdraw mode). */
|
||||
|
||||
Reference in New Issue
Block a user