fix(margin): correct position sizing formula to prevent insufficient margin errors #435
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
When editing trader configuration, if
system_prompt_templatewas set to an empty string (""), the UI would incorrectly treat it as falsy and overwrite it with 'default', losing the user's selection.Root cause:
JavaScript falsy values that trigger
!operator:undefined✅ Should trigger defaultnull✅ Should trigger default""❌ Should NOT trigger (user explicitly chose empty)false,0,NaN(less relevant here)Solution
Change condition to explicitly check for
undefined:Impact
Testing
Code Changes
📝 Description | 描述
English:
Fixed critical bug where AI was calculating
position_size_usdincorrectly, treating it as margin requirement instead of notional value, causing frequent code=-2019 errors (insufficient margin).中文:
修復關鍵 bug:AI 錯誤計算
position_size_usd,將其視為保證金需求而非名義價值,導致頻繁出現 code=-2019 錯誤(保證金不足)。🎯 Type of Change | 變更類型
🔗 Related Issues | 相關 Issue
📋 Changes Made | 具體變更
English:
Updated AI prompts with correct formula (
prompts/adaptive.txt,nof1.txt,default.txt):position_size_usdis notional value (includes leverage)Available Margin × Leverage = position_size_usdAdded code-level validation (
trader/auto_trader.go):required margin + fees ≤ available balancebefore opening positions中文:
更新 AI 提示詞公式 (
prompts/adaptive.txt,nof1.txt,default.txt):position_size_usd是名義價值(包含杠杆)可用保證金 × 杠杆 = position_size_usd新增代碼級驗證 (
trader/auto_trader.go):🧪 Testing | 測試
Manual Testing | 手動測試
Test Environment | 測試環境
Test Results | 測試結果
✅ Checklist | 檢查清單
Code Quality | 代碼質量
go build)Testing | 測試
Documentation | 文檔
Git
fix:)devbranch | 我已將分支 rebase 到最新的dev分支📋 PR Size Estimate | PR 大小估計
🎯 Review Focus Areas | 審查重點
Please pay special attention to:
請特別注意:
Specific areas:
executeOpenLong/ShortWithRecordBy submitting this PR, I confirm that:
提交此 PR,我確認: