-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(api): respect user-specified initial balance instead of forcing exchange sync (#787, #807, #790) #813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(api): respect user-specified initial balance instead of forcing exchange sync (#787, #807, #790) #813
Conversation
, NoFxAiOS#807, NoFxAiOS#790) **Problem:** 1. NoFxAiOS#787, NoFxAiOS#807: Backend forcibly overrides user input with exchange balance - Users cannot customize initial_balance for testing different scenarios - Located in api/server.go:415 (old code) 2. NoFxAiOS#790: Users cannot modify initial_balance after trader creation - P&L baseline cannot be adjusted after deposit/withdrawal - Located in api/server.go handleUpdateTrader **Root Cause:** - handleCreateTrader always queries exchange and overrides user input - handleUpdateTrader blocks initial_balance modification **Solution:** - Only auto-query exchange when user input <= 0 - Respect user-specified values when provided - Allow initial_balance modification in handleUpdateTrader - Use total equity (wallet + unrealized P&L) instead of available balance **Changes:** - api/server.go handleCreateTrader (lines 531-624): * Add conditional logic: only query exchange if actualBalance <= 0 * Log user intention clearly * Use total equity calculation (fixes NoFxAiOS#807) - api/server.go handleUpdateTrader (lines 741-754): * Allow initial_balance modification * Log changes for audit trail - api/server_initial_balance_test.go: * Add comprehensive test cases for both functions * Document expected behavior **Test Results:** All 5 test cases passed: - User specified balance respected ✅ - Auto-sync from exchange when 0 ✅ - Fallback to default 1000 ✅ - User can modify initial_balance ✅ - P&L recalculates correctly ✅ Based on upstream/dev (commit 3112250) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
🤖 Advisory Check ResultsThese are advisory checks to help improve code quality. They won't block your PR from being merged. 📋 PR InformationTitle Format: ✅ Good - Follows Conventional Commits 🔧 Backend ChecksGo Formatting: Files needing formattingGo Vet: ✅ Good Fix locally: go fmt ./... # Format code
go vet ./... # Check for issues
go test ./... # Run tests⚛️ Frontend ChecksBuild & Type Check: ✅ Success Fix locally: cd web
npm run build # Test build (includes type checking)📖 ResourcesQuestions? Feel free to ask in the comments! 🙏 These checks are advisory and won't block your PR from being merged. This comment is automatically generated from pr-checks-run.yml. |
| log.Printf("⚠️ 获取交易所配置失败,使用用户输入的初始资金: %v", err) | ||
| } | ||
| // ✅ Fix #787, #807: Only query exchange when user doesn't specify initial balance | ||
| // Respect user input, allow custom initial balance for different testing scenarios |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think this change will work if the goroutineautoSyncBalanceIfNeeded keeps running.
| log.Printf("⚠️ 查询交易所余额失败,使用用户输入的初始资金: %v", balanceErr) | ||
| if exchangeCfg == nil { | ||
| log.Printf("⚠️ Exchange %s config not found, using default 1000 USDT", req.ExchangeID) | ||
| actualBalance = 1000.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 1000?
- Format main.go - Format auth/auth.go - Format bootstrap/bootstrap.go - Format config/database.go - Format config/database_test.go - Format crypto/crypto.go - Format trader/auto_trader.go 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This PR combines the fixes from PR NoFxAiOS#813 and PR NoFxAiOS#883 into a unified solution: ✅ From PR NoFxAiOS#813: Respect user-specified initial balance (NoFxAiOS#787, NoFxAiOS#807, NoFxAiOS#790) - Only auto-query from exchange when user input <= 0 - Allows users to specify custom initial balance for different testing scenarios - Resolves user complaints about forced exchange sync ✅ From PR NoFxAiOS#883: Use total equity instead of available balance for accurate P&L - Total equity = wallet balance + unrealized P&L - More accurate than available balance for P&L calculation - Includes comprehensive unit tests Key improvements: 1. **User experience**: Respects user input, queries only when needed 2. **Technical correctness**: Uses total equity for accurate calculation 3. **Best of both**: Combines functionality without trade-offs Changes: - api/server.go: handleCreateTrader(), handleSyncBalance() - api/initial_balance_and_equity_test.go: Comprehensive unit tests (17 test cases) Related: NoFxAiOS#787, NoFxAiOS#807, NoFxAiOS#790, NoFxAiOS#813, NoFxAiOS#883 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This PR combines the fixes from PR NoFxAiOS#813 and PR NoFxAiOS#883 into a unified solution: ✅ From PR NoFxAiOS#813: Respect user-specified initial balance (NoFxAiOS#787, NoFxAiOS#807, NoFxAiOS#790) - Only auto-query from exchange when user input <= 0 - Allows users to specify custom initial balance for different testing scenarios - Resolves user complaints about forced exchange sync ✅ From PR NoFxAiOS#883: Use total equity instead of available balance for accurate P&L - Total equity = wallet balance + unrealized P&L - More accurate than available balance for P&L calculation - Includes comprehensive unit tests Key improvements: 1. **User experience**: Respects user input, queries only when needed 2. **Technical correctness**: Uses total equity for accurate calculation 3. **Best of both**: Combines functionality without trade-offs Changes: - api/server.go: handleCreateTrader(), handleSyncBalance() - api/initial_balance_and_equity_test.go: Comprehensive unit tests (17 test cases) Related: NoFxAiOS#787, NoFxAiOS#807, NoFxAiOS#790, NoFxAiOS#813, NoFxAiOS#883 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This commit includes multiple critical fixes and improvements: **P0 Fixes (Critical):** - ✅ Fix trader deletion logic order (manager/trader_manager.go, api/server.go) - Added RemoveTrader() method to safely remove traders from memory - Fixed order: stop & remove from memory → delete from DB - Prevents "ghost traders" running without DB records - Clears competition cache on deletion **P1 Fixes (Important):** - ✅ Fix two-stage private key input validation (web/src/components/TwoStageKeyModal.tsx) - Normalize "0x" prefix before length validation - Support keys with or without "0x" prefix - ✅ Fix competition page showing deleted traders - RemoveTrader() now clears competition cache - Deleted traders no longer appear in rankings **P2 Improvements (General):** - ✅ Add duplicate trader name check (api/server.go) - Prevents creating traders with identical names - Returns clear error message - ✅ Improve Dashboard empty state messages (web/src/i18n/translations.ts) - More welcoming title: "Let's Get Started!" / "開始使用吧!" - Specific action guidance: connect exchange, choose AI model - Better call-to-action button text - ✅ Improve login error messages (web/src/i18n/translations.ts) - More helpful error hints - Clearer guidance for users **Cherry-picked from upstream:** - ✅ Initial balance and equity calculation fixes (NoFxAiOS#901, 4 commits) - ✅ Dynamic minimum position size for small accounts (NoFxAiOS#902, 1 commit) Related: NoFxAiOS#787, NoFxAiOS#807, NoFxAiOS#790, NoFxAiOS#813, NoFxAiOS#883 Files changed: 4 (backend: 2, frontend: 2) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This commit includes multiple critical fixes and improvements: **P0 Fixes (Critical):** - ✅ Fix trader deletion logic order (manager/trader_manager.go, api/server.go) - Added RemoveTrader() method to safely remove traders from memory - Fixed order: stop & remove from memory → delete from DB - Prevents "ghost traders" running without DB records - Clears competition cache on deletion **P1 Fixes (Important):** - ✅ Fix two-stage private key input validation (web/src/components/TwoStageKeyModal.tsx) - Normalize "0x" prefix before length validation - Support keys with or without "0x" prefix - ✅ Fix competition page showing deleted traders - RemoveTrader() now clears competition cache - Deleted traders no longer appear in rankings **P2 Improvements (General):** - ✅ Add duplicate trader name check (api/server.go) - Prevents creating traders with identical names - Returns clear error message - ✅ Improve Dashboard empty state messages (web/src/i18n/translations.ts) - More welcoming title: "Let's Get Started!" / "開始使用吧!" - Specific action guidance: connect exchange, choose AI model - Better call-to-action button text - ✅ Improve login error messages (web/src/i18n/translations.ts) - More helpful error hints - Clearer guidance for users **Cherry-picked from upstream:** - ✅ Initial balance and equity calculation fixes (NoFxAiOS#901, 4 commits) - ✅ Dynamic minimum position size for small accounts (NoFxAiOS#902, 1 commit) Related: NoFxAiOS#787, NoFxAiOS#807, NoFxAiOS#790, NoFxAiOS#813, NoFxAiOS#883 Files changed: 4 (backend: 2, frontend: 2) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…on logic This PR unifies and improves the initial balance and equity calculation across the application: ✅ Core Changes: 1. **Total Equity Calculation**: Use totalEquity (wallet balance + unrealized P&L) instead of available balance for accurate P&L tracking 2. **Fallback Mechanism**: Use 100 USDT default when exchange queries fail (instead of user input) 3. **Balance Parsing Utilities**: Extract duplicate balance parsing logic into reusable functions (trader/balance_utils.go) 4. **Auto Balance Sync**: Fix auto balance sync to use total equity for correct total P&L calculation 5. **Frontend Relaxation**: Allow minimum initial balance of 10 USDT (down from 100) for testing scenarios ✅ Test Coverage: - api/initial_balance_and_equity_test.go: 17 test cases covering equity calculation edge cases - trader/auto_balance_sync_test.go: 24 test cases covering auto sync scenarios ✅ Files Modified: - api/server.go: Updated handleCreateTrader() and handleSyncBalance() - api/initial_balance_and_equity_test.go: New comprehensive test suite - trader/auto_trader.go: Updated balance sync logic - trader/balance_utils.go: New shared balance parsing utilities - trader/auto_balance_sync_test.go: New comprehensive test suite - web/src/components/TraderConfigModal.tsx: Relaxed frontend validation Related: NoFxAiOS#787, NoFxAiOS#807, NoFxAiOS#790, NoFxAiOS#813, NoFxAiOS#883 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
📝 Description | 描述
English:
This PR fixes a critical UX issue where the backend forcibly overrides user-specified
initial_balancewith the exchange-queried balance, preventing users from customizing their initial balance for testing different trading scenarios.中文:
此 PR 修復了一個嚴重的 UX 問題:後端強制用交易所查詢的餘額覆蓋用戶指定的
initial_balance,導致用戶無法自定義初始餘額來測試不同的交易策略。🎯 Type of Change | 變更類型
🔗 Related Issues | 相關 Issue
Closes #787
Closes #807
Closes #790
🔍 Root Cause Analysis | 根本原因分析
Issue #787 & #807: Backend Forces Exchange Balance Override
Problem Location:
api/server.gohandleCreateTrader functionCurrent Behavior:
Impact:
initial_balancefor testing100, backend always sets it to exchange balance (e.g.,1000)Issue #790: Cannot Modify Initial Balance After Creation
Problem Location:
api/server.gohandleUpdateTrader functionCurrent Behavior:
initial_balancemodification✅ Solution | 解決方案
Core Fix: Respect User Input
New Logic:
Code Changes
1.
api/server.go- handleCreateTrader (lines 531-624)Before:
After:
2.
api/server.go- handleUpdateTrader (lines 741-754)Before:
After:
3.
api/server_initial_balance_test.go(NEW)Added comprehensive test cases:
🔄 Relationship with Other PRs | 與其他 PR 的關係
PR #668: "use total equity instead of available balance"
total equitycalculationPR #802: "修改初始余额计算错误的问题"
initial_balancemodification in handleUpdateTraderMerge Order Recommendation
📊 Test Results | 測試結果
All tests passed locally:
Output:
None - This change is fully backward compatible:
0to auto-sync from exchange (preserves original functionality)📊 Checklist | 檢查清單
🎬 User Journey | 用戶使用流程
Before This Fix:
After This Fix:
📸 Screenshots | 截圖
User reported issue #807 with screenshot showing balance calculation error caused by forced override.
Base Branch:
dev(commit 3112250)Clean Branch: Created from upstream/dev, no z-dev specific code
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]