import { useData, usePublish, type PanelRef } from '@jaspers-ai/sdk' import { fmt, sign } from './format' import './styles.css' interface Account { mode: string account_number: string | null status: string | null equity: number | null cash: number | null buying_power: number | null day_pl: number | null day_pl_pct: number | null portfolio_value: number | null trading_blocked: boolean pattern_day_trader: boolean daytrade_count: number | null } interface Position { symbol: string side: string | null qty: number | null avg_entry_price: number | null current_price: number | null market_value: number | null unrealized_pl: number | null unrealized_plpc: number | null change_today: number | null } export function AccountPanel({ panel }: { panel: PanelRef }) { const state = useData(`workspaces/${panel.workspaceId}/panels/${panel.id}/state`) as | { live_account?: boolean } | undefined if (state === undefined) return

Loading…

return } function Panel({ panel, live }: { panel: PanelRef; live: boolean }) { const acct = useData('alpaca/account', { live_account: live }) const pos = useData('alpaca/positions', { live_account: live }) const a = ((acct.data ?? []) as Account[])[0] const rows = (pos.data ?? []) as Position[] usePublish(panel, { mode: live ? 'live' : 'paper', equity: a?.equity ?? null, cash: a?.cash ?? null, buying_power: a?.buying_power ?? null, day_pl: a?.day_pl ?? null, positions: rows.length, }) return (

Alpaca account

{live ? 'LIVE' : 'paper'} {a?.account_number && {a.account_number}} {a?.status && {a.status}} {a?.trading_blocked && trading blocked}
{acct.error ? (

{acct.error}

) : !a ? (

Loading…

) : (
)} {pos.error ? (

{pos.error}

) : !pos.data ? null : rows.length === 0 ? (

No open positions.

) : ( {rows.map((p) => ( ))}
Symbol Side Qty Avg entry Price Market value Unrealized P/L P/L % Today %
{p.symbol} {p.side ?? '–'} {fmt(p.qty, 0)} {fmt(p.avg_entry_price)} {fmt(p.current_price)} {fmt(p.market_value)} {fmt(p.unrealized_pl)} {p.unrealized_plpc === null ? '–' : `${fmt(p.unrealized_plpc * 100)}%`} {p.change_today === null ? '–' : `${fmt(p.change_today * 100)}%`}
)}
) } function Card({ label, value, className = '' }: { label: string; value: string; className?: string }) { return (
{label}
{value}
) }