Skip to content

Minor fixes and feats on the SDK needed for the UI - #246

Open
shresthagrawal wants to merge 7 commits into
mainfrom
ts-sdk-realized-pnl-mm
Open

Minor fixes and feats on the SDK needed for the UI#246
shresthagrawal wants to merge 7 commits into
mainfrom
ts-sdk-realized-pnl-mm

Conversation

@shresthagrawal

@shresthagrawal shresthagrawal commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Five self-contained read-side additions, one per commit so they can be cherry-picked independently. Everything is additive — no changes to
transports, signing, or existing behavior.

  • decode Order.realizedPnl — the orders API reports realized_pnl on PnL-realizing perp fills (reduce/close/flip/liquidation); decode it into
    Order.realizedPnl from REST order history and the WS fill push.
  • expose maintenance margin and liquidation buffer — PositionsSnapshot.maintenanceMargin on enriched (livePositions) snapshots, plus
    maintenanceMargin / liquidationBuffer (= perps equity − maintenance margin) on accountMetrics.
  • estimateLiquidationPrice for hypothetical positions — cross-margin liquidation price for a position as it would exist after a change (size, entry,
    cash delta). Passed an existing position unchanged it reproduces the venue-reported PerpPosition.liquidationPrice; powers a pre-trade "Est.
    liquidation" readout.
  • previewPositionChange — add/reduce/close/flip pro-forma — what an order does to the current position: zone classification, blended entry on
    same-side adds, margin after, realized PnL on the closed slice, fee, and the post-change liquidation price.
  • solvers to size a top-up or order from a target liq price — cashForLiquidation (cash to add/remove so an existing position's liquidation lands on
    a target) and notionalForLiquidation (order notional whose post-fill liquidation lands on a target).

Verification: estimateLiquidationPrice matches the venue-reported liquidationPrice exactly on all 122 open perp positions across 40 staging
leaderboard accounts; realized-slice PnL and both solver round-trips validated against live staging data; build + typecheck clean at every commit
boundary.

@shresthagrawal
shresthagrawal force-pushed the ts-sdk-realized-pnl-mm branch from ad56323 to 00fd32a Compare July 20, 2026 20:15
shresthagrawal and others added 2 commits July 20, 2026 23:18
The orders API reports realized_pnl on PnL-realizing perp fills; decode
it from REST history and the WS fill push.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Surface the maintenance-margin total already computed internally on
enriched snapshots, plus liquidationBuffer in accountMetrics.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@shresthagrawal
shresthagrawal force-pushed the ts-sdk-realized-pnl-mm branch from 00fd32a to 1d5f591 Compare July 20, 2026 20:19
shresthagrawal and others added 3 commits July 20, 2026 23:28
Pre-trade cross-margin liquidation estimate matching the venue's
reported liquidationPrice.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
What an order does to the existing position: zone, blended entry,
margin, realized PnL on the closed slice, post-change liquidation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@shresthagrawal
shresthagrawal force-pushed the ts-sdk-realized-pnl-mm branch from 1d5f591 to 55cdc2d Compare July 21, 2026 05:56
@shresthagrawal
shresthagrawal requested a review from abresas July 21, 2026 09:49
shresthagrawal and others added 2 commits July 21, 2026 15:53
Review feedback: one preview module, not two — the liquidation/pro-forma
functions move into order-preview.ts unchanged, sharing the position
lookup and abs helpers. No behavior change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Against an opposite-side position the liq-vs-notional curve has a reduce
branch and a discontinuity at the exact close; bisecting across it could
return meaningless dust sizes. Solve from the exact-close notional for
flips (from zero for opens/adds), and report unreachable targets as
undefined.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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