Skip to content

Prevent position closes from crossing through flat - #768

Draft
luokerenx4 wants to merge 1 commit into
devfrom
codex/guard-position-overclose
Draft

Prevent position closes from crossing through flat#768
luokerenx4 wants to merge 1 commit into
devfrom
codex/guard-position-overclose

Conversation

@luokerenx4

Copy link
Copy Markdown
Contributor

Summary

  • validate close quantities in the manual order-entry dialog against the displayed position size
  • reject non-positive and invalid explicit close quantities while staging
  • re-read the latest broker positions immediately before dispatch and reject stale or oversized partial closes before any broker mutation

Evidence

On the Demo Portfolio account, AAPL showed an open quantity of 120. The close dialog accepted 200 with a commit message and enabled Close Position; the Demo backend only rejected it later because Demo mutations are read-only. Alpaca and IBKR adapters build an explicit reverse market order for the supplied quantity, so an oversized close can cross through flat into a new opposite exposure.

Verification

  • pnpm vitest run ui/src/components/uta/OrderEntryDialog.spec.tsx services/uta/src/domain/trading/UnifiedTradingAccount.spec.ts (116 passed)
  • npx tsc --noEmit
  • cd ui && npx tsc -b
  • pnpm test (3397 passed, 9 skipped)
  • pnpm test:e2e (30 passed, 3 skipped)
  • real Demo route: AAPL 120 → quantity 200 shows the current-position limit and keeps Close Position disabled; quantity 60 clears the error and enables the button

No live-paper order was sent: this is a broker-independent pre-dispatch safety boundary, and the MockBroker lifecycle verifies that rejected operations never reach broker.closePosition.

@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
openalice-demo Ready Ready Preview, Comment Jul 28, 2026 10:55pm

Request Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant