Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/broker_executions_validation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ for (const patch of [{ quantity: "0" }, { price: "-1" }, { trade_date: "2026-10-
{ commission: "-0.1234567890123456789" }, { taxes: "1e-9" }, { quantity: "0.123456789" }]) {
assert.throws(() => normalizeExecutions({ ...payload, records: [{ ...record, ...patch }] }, { binding }));
}
// US 23/5: an IBKR overnight fill from 20:00 ET carries the next day's trade date.
{
const zoned = (zone, localTime) => ({ ...payload, records: [{ ...record, local_time: localTime, trade_date: "2026-10-23", time_zone: zone }] });
assert.equal(normalizeExecutions(zoned("America/New_York", "2026-10-22T20:15:00"), { binding }).records[0].time_zone, "America/New_York");
assert.throws(() => normalizeExecutions(zoned("UTC", "2026-10-22T20:15:00"), { binding }), undefined, "explicit non-Eastern zone keeps the same-day rule");
assert.throws(() => normalizeExecutions(zoned("America/Los_Angeles", "2026-10-22T20:15:00"), { binding }));
}
for (const [localTime, tradeDate] of [["2026-10-22T20:00:00", "2026-10-23"], ["2026-10-22T23:59:59", "2026-10-23"],
["2026-09-30T21:15:00", "2026-10-01"], ["2026-10-23T02:30:00", "2026-10-23"]]) {
const overnight = { ...payload, records: [{ ...record, local_time: localTime, trade_date: tradeDate }] };
assert.equal(normalizeExecutions(overnight, { binding }).records[0].local_time, localTime, `${localTime} -> ${tradeDate} accepted`);
}
for (const [localTime, tradeDate] of [["2026-10-22T19:59:59", "2026-10-23"], ["2026-10-21T22:00:00", "2026-10-23"],
["2026-10-23T21:00:00", "2026-10-22"], ["2026-10-22T20:00:00", "2026-10-22T"], ["2026-09-31T20:00:00", "2026-10-02"]]) {
assert.throws(() => normalizeExecutions({ ...payload, records: [{ ...record, local_time: localTime, trade_date: tradeDate }] }, { binding }),
undefined, `${localTime} -> ${tradeDate} rejected`);
}
assert.throws(() => normalizeExecutions({ ...payload, account_selector: "U100002" }, { binding }), /execution_identity_mismatch/);
assert.throws(() => normalizeExecutions({ ...payload, coverage: "complete" }, { binding }));
assert.throws(() => normalizeExecutions({ ...payload, records: [record, record] }, { binding }));
Expand Down
13 changes: 13 additions & 0 deletions tests/console_execution_clock_validation.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// US 23/5: an overnight fill's execution time keeps its local date in the trades UI.
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { executionClock } from "../web/strategy-switch-console/frontend/src/pages/performance/executionClock.ts";

assert.equal(executionClock("2026-10-23T09:31:00", "2026-10-23"), "09:31:00", "same-day fill shows only the clock");
assert.equal(executionClock("2026-10-22T20:30:00", "2026-10-23"), "2026-10-22 20:30:00", "overnight fill keeps its local date");
assert.equal(executionClock("2026-10-23T09:31:00", null), "09:31:00", "no trade date: clock only");

const tabs = readFileSync(new URL("../web/strategy-switch-console/frontend/src/pages/performance/PerformanceTabs.tsx", import.meta.url), "utf8");
assert.equal((tabs.match(/executionClock\(row\.local_time, row\.trade_date\)/g) || []).length, 2, "table and card both use executionClock");
assert.doesNotMatch(tabs, /row\.local_time\.slice\(11\)/, "no view drops the local date");
console.log("console_execution_clock_validation: PASS (overnight fills keep their local execution date)");
19 changes: 19 additions & 0 deletions tests/sa2_orders_fills_validation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,25 @@ assert.ok(ORDER_LIFECYCLE_STATES.includes("ORDER_UNKNOWN"));
assert.equal(adaptedEval.executions[0].fee_usd, null);
assert.equal(adaptedEval.summary.k_pend_usd, null);
assert.equal(PRIVATE_LOCATOR_RE.test(JSON.stringify(adaptedEval)), false);

// US 23/5: an IBKR overnight fill (20:00 ET on the evening before its trade date) is
// projected, and its UTC execution time uses the local calendar date.
{
const baseRow = legacyWithSelector.records[0];
const ctx = { broker: "ibkr", environment: "live", account: "ibkr:live:a1", evidence_ref: "sa2:a12:overnight" };
const withRow = changes => ({ ...legacyWithSelector, records: [{ ...baseRow, ...changes }] });
const overnight = adaptLegacyBrokerExecutionPayload(
withRow({ trade_date: "2026-10-23", local_time: "2026-10-22T20:30:00", time_zone: "America/New_York" }), ctx);
assert.equal(overnight.domains.executions.records[0].executed_at, "2026-10-23T00:30:00.000Z", "executed_at uses the local date");
for (const changes of [
{ trade_date: "2026-10-23", local_time: "2026-10-22T19:59:59", time_zone: "America/New_York" },
{ trade_date: "2026-10-23", local_time: "2026-10-22T20:30:00", time_zone: "America/Los_Angeles" },
{ trade_date: "2026-10-23", local_time: "2026-10-21T21:00:00", time_zone: "America/New_York" },
]) {
assert.throws(() => adaptLegacyBrokerExecutionPayload(withRow(changes), ctx), /invalid_legacy_execution_local_time/,
`${changes.local_time} ${changes.time_zone} -> ${changes.trade_date} rejected`);
}
}
assert.equal(JSON.stringify(adaptedEval).includes("U" + "100001"), false);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Broker executions are private observed facts, not orders or a complete P/L ledger.
import { dateMs } from "../../performance_math.js";

import { localTimeMatchesTradeDate } from "./trade_date_boundary.js";

export const EXECUTIONS_SCHEMA = "broker_executions.v1";
const HASH = /^[a-f0-9]{64}$/;
const DECIMAL = /^-?(?:0|[1-9]\d{0,11})(?:\.\d{1,8})?$/;
Expand Down Expand Up @@ -57,7 +59,7 @@ export function normalizeExecutions(raw, { binding, now = Date.now(), enforceFre
seen.add(row.execution_ref);
if (row.local_time !== null && (typeof row.local_time !== "string"
|| !/^\d{4}-\d{2}-\d{2}T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/.test(row.local_time)
|| row.local_time.slice(0, 10) !== row.trade_date)) throw executionError();
|| !localTimeMatchesTradeDate(row.local_time.slice(0, 10), row.local_time.slice(11), row.trade_date, row.time_zone))) throw executionError();
Comment thread
Pigbibi marked this conversation as resolved.
Resolved
if (row.time_zone !== null) {
if (typeof row.time_zone !== "string" || row.time_zone.length > 64) throw executionError();
try { new Intl.DateTimeFormat("en", { timeZone: row.time_zone }); } catch { throw executionError(); }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// US 23/5 trade-date boundary shared by broker-execution consumers.
//
// IBKR dates fills from 20:00 ET to midnight on the following day, so an overnight
// execution's local wall time falls on the evening before its trade date. A
// local time may therefore differ from its trade date only in that shape: it is
// on the calendar day immediately before the trade date, at or after 20:00, and
// stated in US Eastern time. A null time zone is read as Eastern, the Flex default
// assumed by the producer; any other explicit zone keeps the same-day rule.
// Every other mismatch stays invalid.

export const TRADE_DATE_BOUNDARY = "20:00:00";
const EASTERN = "America/New_York";

function strictDayMs(value) {
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return null;
const ms = Date.parse(`${value}T00:00:00Z`);
return Number.isFinite(ms) && new Date(ms).toISOString().slice(0, 10) === value ? ms : null;
}

/**
* @param {string} localDay YYYY-MM-DD of the local wall time
* @param {string} timeText HH:MM:SS of the local wall time
* @param {string} tradeDate YYYY-MM-DD broker trade date
* @param {string|null|undefined} timeZone record time zone, null when unstated
*/
export function localTimeMatchesTradeDate(localDay, timeText, tradeDate, timeZone = null) {
const localMs = strictDayMs(localDay);
const tradeMs = strictDayMs(tradeDate);
if (localMs === null || tradeMs === null) return false;
if (localMs === tradeMs) return true;
if (timeZone !== null && timeZone !== undefined && timeZone !== EASTERN) return false;
return tradeMs - localMs === 86_400_000 && typeof timeText === "string" && timeText >= TRADE_DATE_BOUNDARY;
}
15 changes: 10 additions & 5 deletions web/strategy-switch-console/application/orders_fills/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* - Provides pure additive adaptLegacyBrokerExecutionPayload(...) preserving broker_executions.v1.
*/

import { localTimeMatchesTradeDate } from "../account_facts/trade_date_boundary.js";
import { isRawAccountIdentifier, isUnsafePrivateLocator } from "../naming_canonical/helpers.js";
import {
absDecimal,
Expand Down Expand Up @@ -557,15 +558,17 @@ function validateLegacyTradeDate(tradeDate) {
return { year, month, day };
}

function parseLegacyLocalTime(localTime, tradeDate) {
function parseLegacyLocalTime(localTime, tradeDate, timeZone = null) {
if (typeof localTime !== "string") {
throw new OrdersFillsError("invalid_legacy_execution_local_time", 400);
}
const match = LEGACY_LOCAL_TIME_RE.exec(localTime);
if (!match) {
throw new OrdersFillsError("invalid_legacy_execution_local_time", 400);
}
if (match[1] !== undefined && match[1] !== tradeDate) {
// A local date may precede the trade date only for a US 23/5 overnight fill.
if (match[1] !== undefined && match[1] !== tradeDate
&& !localTimeMatchesTradeDate(match[1], `${match[2]}:${match[3]}:${match[4]}`, tradeDate, timeZone)) {
throw new OrdersFillsError("invalid_legacy_execution_local_time", 400);
}
const hour = Number(match[2]);
Expand All @@ -575,7 +578,7 @@ function parseLegacyLocalTime(localTime, tradeDate) {
if (hour > 23 || minute > 59 || second > 59 || ms > 999) {
throw new OrdersFillsError("invalid_legacy_execution_local_time", 400);
}
return { hour, minute, second, ms };
return { hour, minute, second, ms, localDate: match[1] !== undefined && match[1] !== tradeDate ? match[1] : null };
}

function buildLegacyZoneFormatter(timeZone) {
Expand Down Expand Up @@ -692,7 +695,7 @@ export function adaptLegacyBrokerExecutionPayload(legacyPayload, context = {}) {
const positiveQty = formatQtyExact(absDecimal(qtyDec));
const dateParts = validateLegacyTradeDate(row.trade_date);
const localTimeParts = row.local_time !== undefined && row.local_time !== null
? parseLegacyLocalTime(row.local_time, row.trade_date)
? parseLegacyLocalTime(row.local_time, row.trade_date, row.time_zone ?? null)
: null;
const zoneFormatter = row.time_zone !== undefined && row.time_zone !== null
? buildLegacyZoneFormatter(row.time_zone)
Expand All @@ -704,7 +707,9 @@ export function adaptLegacyBrokerExecutionPayload(legacyPayload, context = {}) {
? null
: parseStrictUtcString(row.executed_at, "legacy_executed_at");
} else if (localTimeParts !== null && zoneFormatter !== null) {
executedAt = convertLocalTimeWithZoneToUtc(dateParts, localTimeParts, zoneFormatter);
// An overnight fill executes on its local calendar date, not its trade date.
const executionDateParts = localTimeParts.localDate ? validateLegacyTradeDate(localTimeParts.localDate) : dateParts;
executedAt = convertLocalTimeWithZoneToUtc(executionDateParts, localTimeParts, zoneFormatter);
}
return {
broker: identity.broker,
Expand Down
2 changes: 1 addition & 1 deletion web/strategy-switch-console/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "tsc --noEmit && vite build && node scripts/check-budget.mjs",
"sync:v2-assets": "node scripts/sync-v2-asset-map.mjs",
"test:operations": "node ../../../tests/account_config_label_validation.mjs && node --experimental-strip-types ../../../tests/console_v2_operations_validation.mjs && node --experimental-strip-types ../../../tests/console_v2_regressions_validation.mjs && node --experimental-strip-types ../../../tests/console_v2_preferences_accounts_validation.mjs && node ../../../tests/console_runtime_control_result_ui_validation.mjs && node ../../../tests/console_page_load_boundary_validation.mjs && node --test ../../../tests/fe8b_fix03_ui_validation.mjs ../../../tests/console_notification_receipts_ui_validation.mjs && npm run test:performance && node --test ../../../tests/console_execution_evidence_validation.mjs && node ../../../tests/console_strategy_execution_validation.mjs && node --experimental-strip-types ../../../tests/console_binance_wallet_cards_validation.mjs && node ../../../tests/console_tabs_scope_sources_ui_validation.mjs && node ../../../tests/console_shell_unavailable_source_ui_validation.mjs && node ../../../tests/console_cockpit_decision_validation.mjs && node ../../../tests/console_frontend_i18n_validation.mjs && node ../../../tests/console_credential_rotation_ui_validation.mjs && node ../../../tests/console_style_guards_validation.mjs && node ../../../tests/console_tokens_css_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_format_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_mock_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_shell_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_pages_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_p1_acceptance_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_phase8_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_alert_delivery_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_overview_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_benchmarks_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_n1_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_loading_retry_validation.mjs && node --experimental-strip-types --test ../../../tests/console_desk_delivery_validation.mjs && node --experimental-strip-types --test ../../../tests/console_todo_state_validation.mjs ../../../tests/console_load_lifecycle_validation.mjs ../../../tests/promotion_queue_partial_validation.mjs ../../../tests/console_decision_inbox_gaps_validation.mjs",
"test:operations": "node ../../../tests/account_config_label_validation.mjs && node --experimental-strip-types ../../../tests/console_v2_operations_validation.mjs && node --experimental-strip-types ../../../tests/console_v2_regressions_validation.mjs && node --experimental-strip-types ../../../tests/console_v2_preferences_accounts_validation.mjs && node ../../../tests/console_runtime_control_result_ui_validation.mjs && node ../../../tests/console_page_load_boundary_validation.mjs && node --test ../../../tests/fe8b_fix03_ui_validation.mjs ../../../tests/console_notification_receipts_ui_validation.mjs && npm run test:performance && node --test ../../../tests/console_execution_evidence_validation.mjs && node ../../../tests/console_strategy_execution_validation.mjs && node --experimental-strip-types ../../../tests/console_binance_wallet_cards_validation.mjs && node ../../../tests/console_tabs_scope_sources_ui_validation.mjs && node ../../../tests/console_shell_unavailable_source_ui_validation.mjs && node ../../../tests/console_cockpit_decision_validation.mjs && node ../../../tests/console_frontend_i18n_validation.mjs && node ../../../tests/console_credential_rotation_ui_validation.mjs && node ../../../tests/console_style_guards_validation.mjs && node ../../../tests/console_tokens_css_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_format_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_mock_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_shell_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_pages_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_p1_acceptance_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_phase8_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_alert_delivery_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_overview_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_benchmarks_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_n1_validation.mjs && node --experimental-strip-types ../../../tests/console_execution_clock_validation.mjs && node --experimental-strip-types ../../../tests/console_desk_loading_retry_validation.mjs && node --experimental-strip-types --test ../../../tests/console_desk_delivery_validation.mjs && node --experimental-strip-types --test ../../../tests/console_todo_state_validation.mjs ../../../tests/console_load_lifecycle_validation.mjs ../../../tests/promotion_queue_partial_validation.mjs ../../../tests/console_decision_inbox_gaps_validation.mjs",
"check:style-guards": "node scripts/check-style-guards.mjs",
"dev:synthetic": "node scripts/dev-synthetic.mjs",
"test:performance": "node --experimental-strip-types ../../../tests/console_performance_math_validation.mjs && node --experimental-strip-types ../../../tests/console_performance_data_validation.mjs && node --experimental-strip-types ../../../tests/console_calendar_pending_ui_validation.mjs && node ../../../tests/website_currency_ranking_validation.mjs && node ../../../tests/website_summary_request_validation.mjs && node ../../../tests/console_compact_summary_validation.mjs && node ../../../tests/native_daily_gap_ui_validation.mjs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { DataTable, type Column } from "../../components/DataTable";
import { Tabs } from "../../components/Tabs";
import { BarList, type BarListItem } from "../../charts/BarList";
import { renderableStatus } from "./shared";
import { executionClock } from "./executionClock";


export interface PerformanceTabsProps {
scope: string;
Expand Down Expand Up @@ -298,7 +300,7 @@ function TradesTab({ scope, platform, range, epoch }: PerformanceTabsProps) {
cell: row => <>
{/* STK and the execution hash stay as debug attributes only, never as visible columns. */}
<span className="num" data-asset-category={row.asset_category} data-execution-ref={row.execution_ref}>{row.trade_date}</span>
{row.local_time && <small className="trade-time">{row.local_time.slice(11)}{row.time_zone ? ` · ${row.time_zone}` : ""}</small>}
{row.local_time && <small className="trade-time">{executionClock(row.local_time, row.trade_date)}{row.time_zone ? ` · ${row.time_zone}` : ""}</small>}
</>,
sortValue: row => row.trade_date,
},
Expand Down Expand Up @@ -357,7 +359,7 @@ function TradesTab({ scope, platform, range, epoch }: PerformanceTabsProps) {
<span className="trade-card-symbol">{row.symbol}</span>
<span className="trade-side">{sideOf(row)}</span>
{row.local_time && (
<span className="trade-card-time">{row.local_time.slice(11)}{row.time_zone ? ` · ${row.time_zone}` : ""}</span>
<span className="trade-card-time">{executionClock(row.local_time, row.trade_date)}{row.time_zone ? ` · ${row.time_zone}` : ""}</span>
)}
</div>
<div className="trade-card-account">{names.account(row.account_alias, row.platform)}</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Clock text for a broker execution row. A US 23/5 overnight fill is booked on the
* next trade date (IBKR dates 20:00 ET to midnight fills to the following day), so
* when the local date differs from the trade date the local date is kept, so the
* execution time is never shown a day late.
*/
export function executionClock(localTime: string, tradeDate: string | null | undefined): string {
const localDay = localTime.slice(0, 10);
return tradeDate && localDay !== tradeDate ? `${localDay} ${localTime.slice(11)}` : localTime.slice(11);
}
Loading
Loading