[ENG-1734] feat(comlink): add GET /v4/tradeHistory endpoint with cumulative PnL tracking (backport #3323)#3328
Merged
davidli1997 merged 2 commits intoMar 3, 2026
Conversation
Contributor
Author
|
Cherry-pick of 59cd776 has failed: To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally |
Merged
6 tasks
jusbar23
approved these changes
Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelist
buildClobPairIdToMarket()helper from fills-controller into lib/helpers.ts to avoid duplicationrandomInt()that caused clientId collisions in integration testsGET /v4/tradeHistoryendpoint with two routes:GET /— trade history for a single subaccountGET /parentSubaccountNumber— trade history for a parent subaccount (includes child/isolated subaccounts)lib/trade-history.tscomputes trade history rows on-the-fly from fills:CLOSE+OPENrowsOPEN, EXTEND, PARTIAL_CLOSE, CLOSE, LIQUIDATION_PARTIAL_CLOSE, LIQUIDATION_CLOSEExample: User Trading Activity on BTC-USD
Trade History Cards Produced (most recent first):
How the numbers work:
Row 1: Open long 10 @ $100. entryPrice = $100.
Row 2: Extend by 10 @ $120. entryPrice = weighted avg = (10010 + 12010) / 20 = $110.
Row 3: Partial close 10 @ $130. PnL = (130 - 110) * 10 = $200. Cost basis = 110 * 10 = $1,100. Percent = 200/1100 = 18.18%.
Row 4: Close remaining 10 @ $90. PnL = (90 - 110) * 10 = -$200. Cumulative = 200 + (-200) = $0. Cum cost basis = 1100 + 1100 = $2,200. Percent = 0/2200 = 0%. Lifecycle resets.
Row 5: Open short 5 @ $85. Fresh lifecycle -- netRealizedPnl = 0, netRealizedPnlPercent = null.
Row 6: Close short 5 @ $80. PnL = (85 - 80) * 5 = $25. Cost basis = 85 * 5 = $425. Percent = 25/425 = 5.88%.
Key behaviors:
entryPriceupdates on OPEN (set) and EXTEND (weighted average), stays fixed on partial/full closenetRealizedPnlandnetRealizedPnlPercentare cumulative within a position lifecycle, reset to 0/null when position goes flatnetRealizedPnlPercent= cumPnl / cumCostBasis, where cumCostBasis = sum(entryPrice * closingAmount) across all closesnetRealizedPnlPercentis null on OPEN/EXTEND rows that have no prior closes in the lifecycleTest Plan
Author/Reviewer Checklist
state-breakinglabel.indexer-postgres-breakinglabel.PrepareProposalorProcessProposal, manually add the labelproposal-breaking.feature:[feature-name].backport/[branch-name].refactor,chore,bug.Summary by CodeRabbit
New Features
Documentation
Tests
This is an automatic backport of pull request #3323 done by [Mergify](https://mergify.com).