Skip to content

Balance fixes#981

Merged
carkod merged 4 commits intomasterfrom
balance-fixes
Apr 24, 2026
Merged

Balance fixes#981
carkod merged 4 commits intomasterfrom
balance-fixes

Conversation

@carkod
Copy link
Copy Markdown
Owner

@carkod carkod commented Apr 21, 2026

Closes #982

@chatgpt-codex-connector
Copy link
Copy Markdown

💡 Codex Review

if flip_contracts < current_contracts:
self.active_bot.add_log(
"Insufficient balance to reverse position after hitting stop loss, closing position with stop loss order."
)
self.execute_stop_loss()

P1 Badge Treat equal flip size as insufficient for reversal

_is_reversal_possible() returns current_contracts when there is not enough free balance to fund the extra contract needed to flip direction, but this guard only checks flip_contracts < current_contracts. In that insufficient-balance case (flip_contracts == current_contracts), the code still enters reversal flow and places a non-reducing flip order size that can flatten the old position instead of opening the opposite one, pushing the new bot into the backfill/error path instead of executing stop loss directly.


? (portfolioPnlValue / (latestPortfolioValue - (accountData?.total_deposit ?? 0))) * 100

P2 Badge Avoid subtracting deposits twice in PnL percentage

latestPortfolioValue is already net of deposits (estimated_total_fiat - total_deposit), but the percentage denominator subtracts total_deposit again. This double subtraction distorts the displayed return percentage and can produce divide-by-zero/near-zero behavior for some balances, so the percentage no longer reflects the same net portfolio value used for the PnL amount.


response = self.kucoin_api.get_spot_ledger(
current_page=page,
page_size=self.PAGINATION_PAGE_SIZE,
)

P2 Badge Handle ledger fetch failures when computing total deposit

The new deposit path calls get_spot_ledger(...) in a pagination loop without any error handling, while the previous implementation explicitly caught exchange errors and returned 0.0. A transient KuCoin API failure now bubbles out of get_total_deposit() and can fail /account/balance entirely instead of degrading gracefully, which is a regression in availability for balance reads.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@carkod carkod force-pushed the balance-fixes branch 3 times, most recently from a6c882e to 3eb8041 Compare April 22, 2026 11:31
@carkod carkod force-pushed the balance-fixes branch 2 times, most recently from d184d2a to 77744d0 Compare April 23, 2026 12:29
@carkod carkod merged commit d3ea386 into master Apr 24, 2026
13 checks passed
@carkod carkod deleted the balance-fixes branch April 24, 2026 20:32
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.

Fix estimated balances when withdrawal happens

1 participant