Skip to content

fix(okx): stop double-counting unrealized PnL in account equity - #1539

Open
Tonghaofeng wants to merge 1 commit into
NoFxAiOS:devfrom
Tonghaofeng:fix/okx-equity-double-upl
Open

fix(okx): stop double-counting unrealized PnL in account equity#1539
Tonghaofeng wants to merge 1 commit into
NoFxAiOS:devfrom
Tonghaofeng:fix/okx-equity-double-upl

Conversation

@Tonghaofeng

Copy link
Copy Markdown

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's totalEq as totalWalletBalance. But OKX's totalEq (from /api/v5/account/balance) is the total account equity and already includes unrealized PnL. The generic consumers then apply the fallback formula:

// auto_trader_loop.go / auto_trader_decision.go / auto_trader_orders.go
totalEquity = totalWalletBalance + totalUnrealizedProfit

so the UPL is counted twice: with open positions the displayed equity is off by exactly the current UPL. Position-value caps (position value / equity ratios) are computed against the inflated number as well.

Fix

Match the convention already used by the Hyperliquid and Bitget adapters:

  • expose totalEquity directly (= totalEq), which the consumers prefer over the fallback, and
  • keep totalWalletBalance UPL-free (= totalEq - usdtUPL), so the fallback path converges to the same correct value.

Testing

  • go build ./trader/okx/ passes.
  • Verified on a live OKX unified account with open isolated SWAP positions: before the fix the API-reported equity exceeded OKX's totalEq by exactly the current UPL; after the fix it matches totalEq to the cent and tracks the OKX app in real time as UPL fluctuates.

🤖 Generated with Claude Code

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>
@cla-assistant

cla-assistant Bot commented Aug 24, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@cla-assistant

cla-assistant Bot commented Aug 24, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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