Skip to content

feat: sim snapshot per-position marks and margin-aware buying power #412

Description

@rustyeddy

Part of #409 (milestone: Account Margin Admission). Step 3 of 8. Depends on #410, #411.

Problem

  • account.Snapshot exposes each position's AvgPrice and a single aggregate UnrealizedPnL, but no per-position current mark. An account-level rule therefore cannot value positions other than the proposal's.
  • The simulator already tracks per-listing marks internally (s.marks in internal/adapters/broker/sim/account.go) but doesn't expose them.
  • The simulator's BuyingPower/MarginAvailable mirror cash and MarginUsed is always zero (a documented M3 placeholder). Cash can go negative with no check.

Scope

  • Expose a per-position current mark (and/or market value) on the snapshot, following ADR-066's valuation decision, and document its staleness ("as of the simulator's last observation of that listing").
  • Add InitialMarginRatio to sim.Config, validated as positive; default per ADR-066.
  • Make simulator snapshots margin-aware using the shared calculation from feat: shared gross-notional / initial-margin calculation #411:
    • MarginUsed = required margin on current gross notional
    • MarginAvailable = equity − MarginUsed
    • BuyingPower = MarginAvailable / ratio
    • This also makes the full-notional sizer (ADR-061), which reads BuyingPower, respect the ratio automatically. Verify that.
  • Include the ratio in the simulator's model descriptor so it reaches the manifest and ConfigDigest.

Acceptance criteria

  • Snapshot tests:
    • marks update on fill and on Advance
    • deterministic ordering
    • margin fields correct for flat / long / short / multi-instrument at ratio 1.0 and 0.5
    • over-limit account after an adverse move reports negative or zero MarginAvailable and no forced action
  • Existing sim tests still pass. Where expected values change (margin fields), explain the reason in the PR.
  • go test -race ./internal/adapters/broker/sim/... passes.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions