/* ============================================================ gp-exec.jsx — EXECUTION stage: recorded plan vs. live broker fills Sources (all live, all degrade-or-die honest): - GET /api/positions → live filled side (market value, unrealized P/L) - GET /api/orders/open → pending limit/stop legs not yet filled - GET /api/trades → the engine's RECORDED plan per order (target_weight / drift / notional_usd persisted by centaur/ledger.py — surfaced verbatim, never re-derived). When the broker is not connected (Alpaca creds absent → 500) or the ledger is empty, the rows simply thin out and an honest banner explains why. We never fabricate a target weight, planned size, or drift the data lacks. ============================================================ */ var { useState: eS } = React; const EXEC_STATUS = { FILLED: { kind: "verified", label: "filled" }, PENDING: { kind: "warn", label: "pending" }, HELD: { kind: "accent", label: "held" }, SKIPPED: { kind: "rejected", label: "skipped" }, FAILED: { kind: "flagged", label: "failed" }, UNKNOWN: { kind: "neutral", label: "awaiting" }, }; function BrokerBadge({ isPaper }) { if (isPaper == null) return null; const live = !isPaper; return ( {live ? "live" : "paper"} ); } function ExecRightCell({ children, color = "var(--fg-2)", size = 11 }) { return {children}; } function ExecutionView({ onJump }) { const exec = window.GP.DATA.execution; const meta = window.GP.DATA.execMeta || {}; const decisions = window.GP.DATA.decisions; const [expanded, setExpanded] = eS(null); const totalPlanned = exec.reduce((s, e) => s + (e.plannedUsd || 0), 0); const totalFilled = exec.reduce((s, e) => s + (e.filledUsd || 0), 0); const filledCount = exec.filter((e) => e.status === "FILLED" || e.status === "HELD").length; const pending = exec.filter((e) => e.status === "PENDING").length; const GRID = "20px 1.2fr 1.1fr 0.7fr 0.9fr 0.9fr 0.9fr 1fr auto"; return (
Execution recorded plan vs. live broker fills
{[ ["planned", window.GP.fmt.moneyK(totalPlanned || null), "var(--fg)"], ["deployed", window.GP.fmt.moneyK(totalFilled || null), "var(--cyan)"], ["filled", `${filledCount}/${exec.length}`, "var(--emerald)"], ["pending", pending, pending ? "var(--amber)" : "var(--fg-3)"], ].map(([l, v, c]) => (
{v}
))}
{/* honest link state — explains an empty/thin table instead of faking fills */} {!meta.brokerUp && (
Broker not connected — live positions & open orders unavailable ({meta.reason || "credentials absent on server"}). {meta.ledgerUp ? " Showing the recorded ledger plan only." : " Ledger feed also unavailable."}
)} {exec.length === 0 ? (
{!meta.brokerUp && !meta.ledgerUp ? "NO EXECUTION LINK" : "NO EXECUTION ACTIVITY"} {!meta.brokerUp && !meta.ledgerUp ? "Neither the broker nor the order ledger is reachable. Connect Alpaca credentials on the server and seed the ledger to populate plan-vs-fill." : "No positions, open orders, or ledger orders match today's decisions yet. Rows appear here as the engine submits the committed plan."}
) : (
{["", "Asset", "Strategy", "Side", "Target wt", "Planned", "Filled", "Unreal P/L", "Status"].map((h, i) => ( ))}
{exec.map((e) => { const d = e.decisionId ? decisions.find((x) => x.id === e.decisionId) : null; const r = d ? window.GP.verify.checkDecision(d) : null; const st = EXEC_STATUS[e.status] || EXEC_STATUS.UNKNOWN; const isOpen = expanded === e.symbol; const fillFrac = e.plannedUsd && e.filledUsd != null ? e.filledUsd / e.plannedUsd : null; return (
setExpanded(isOpen ? null : e.symbol)} style={{ display: "grid", gridTemplateColumns: GRID, gap: 12, padding: "10px 8px", alignItems: "center", cursor: "pointer", }}> {isOpen ? "▾" : "▸"}
{e.symbol.split("/")[0]} {r && }
{d ? d.strategy_class : "—"} {e.side || "—"} {e.targetWeight == null ? "—" : (e.targetWeight * 100).toFixed(1) + "%"} {window.GP.fmt.moneyK(e.plannedUsd)} {window.GP.fmt.moneyK(e.filledUsd)} = 0 ? "var(--emerald)" : "var(--rose)"}> {e.unrealizedPl == null ? "—" : window.GP.fmt.money(e.unrealizedPl)}
{st.label}
{isOpen && (
{/* plan adherence — only meaningful when a recorded plan exists */}
{e.plannedUsd == null ? ( No recorded order for {e.symbol} — this row is a live broker {e.filledUsd != null ? " holding" : " state"} with no engine plan to compare against. ) : ( <>
planned
{window.GP.fmt.moneyK(e.plannedUsd)}
filled
0.95 ? "var(--emerald)" : "var(--amber)"} height={8} />
{fillFrac == null ? "—" : (fillFrac * 100).toFixed(0) + "%"}
{e.drift == null ? "—" : (e.drift * 100).toFixed(2) + "%"} {window.GP.fmt.price(e.currentPx)} {e.regime && } {e.orderTs && {window.GP.fmt.time(e.orderTs)}Z}
{e.orderStatus === "FAILED" && (
Order FAILED at the broker — recorded plan was not deployed.
)} )}
{/* open orders + audit cross-link */}
{e.openOrders.length === 0 && no resting orders at the broker} {e.openOrders.map((o, i) => (
{o.side} {o.qty == null ? "—" : o.qty} {window.GP.fmt.price(o.limit_price)} {window.GP.fmt.time(o.submitted_at)}Z
))} {d && ( )}
)}
); })}
)}
); } Object.assign(window, { ExecutionView });