fix(okx): stop double-counting unrealized PnL in account equity - #1539
Open
Tonghaofeng wants to merge 1 commit into
Open
fix(okx): stop double-counting unrealized PnL in account equity#1539Tonghaofeng wants to merge 1 commit into
Tonghaofeng wants to merge 1 commit into
Conversation
OKX totalEq already includes UPL; passing it as totalWalletBalance made the generic fallback (equity = wallet + unrealized) add UPL twice, inflating the displayed equity (and the equity fed to AI sizing) by the current UPL. Expose totalEquity directly and keep totalWalletBalance UPL-free, matching the hyperliquid/bitget adapter conventions. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Tonghaofeng
requested review from
SkywalkerJi,
hzb1115 and
tangmengqiu
as code owners
August 24, 2026 13:18
|
|
1 similar comment
|
|
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.
Problem
For OKX accounts, the equity shown in the dashboard (and fed to the AI for position sizing) is inflated by the current unrealized PnL — it only matches the OKX app when the account is flat.
OKXTrader.GetBalance()returns OKX'stotalEqastotalWalletBalance. But OKX'stotalEq(from/api/v5/account/balance) is the total account equity and already includes unrealized PnL. The generic consumers then apply the fallback formula:so the UPL is counted twice: with open positions the displayed equity is off by exactly the current UPL. Position-value caps (
position value / equityratios) are computed against the inflated number as well.Fix
Match the convention already used by the Hyperliquid and Bitget adapters:
totalEquitydirectly (= totalEq), which the consumers prefer over the fallback, andtotalWalletBalanceUPL-free (= totalEq - usdtUPL), so the fallback path converges to the same correct value.Testing
go build ./trader/okx/passes.totalEqby exactly the current UPL; after the fix it matchestotalEqto the cent and tracks the OKX app in real time as UPL fluctuates.🤖 Generated with Claude Code