fix(margin): correct position sizing formula to prevent insufficient margin errors

## Problem
AI was calculating position_size_usd incorrectly, treating it as margin requirement instead of notional value, causing code=-2019 errors (insufficient margin).
## Solution
### 1. Updated AI prompts with correct formula
- **prompts/adaptive.txt**: Added clear position sizing calculation steps
- **prompts/nof1.txt**: Added English version with example
- **prompts/default.txt**: Added Chinese version with example
**Correct formula:**
1. Available Margin = Available Cash × 0.95 × Allocation % (reserve 5% for fees)
2. Notional Value = Available Margin × Leverage
3. position_size_usd = Notional Value (this is the value for JSON)
**Example:** $500 cash, 5x leverage → position_size_usd = $2,375 (not $500)
### 2. Added code-level validation
- **trader/auto_trader.go**: Added margin checks in executeOpenLong/ShortWithRecord
- Validates required margin + fees ≤ available balance before opening position
- Returns clear error message if insufficient
## Impact
- Prevents code=-2019 errors
- AI now understands the difference between notional value and margin requirement
- Double validation: AI prompt + code check
## Testing
-  Compiles successfully
- ⚠️ Requires live trading environment testing
This commit is contained in:
ZhouYongyou
2025-11-04 18:44:07 +08:00
parent 4f7d21c581
commit 4e6b868531
4 changed files with 84 additions and 19 deletions

View File

@@ -106,6 +106,21 @@
3. 寻找新机会: 有强信号吗?多空机会?
4. 输出决策: 思维链分析 + JSON
# 仓位大小计算
**重要**`position_size_usd` 是**名义价值**(包含杠杆),非保证金需求。
**计算步骤**
1. **可用保证金** = Available Cash × 0.95 × 配置比例预留5%手续费)
2. **名义价值** = 可用保证金 × Leverage
3. **position_size_usd** = 名义价值JSON中填写此值
4. **实际币数** = position_size_usd / Current Price
**示例**:可用资金 $500杠杆 5x配置 100%
- 可用保证金 = $500 × 0.95 = $475
- position_size_usd = $475 × 5 = **$2,375** ← JSON填此值
- 实际占用保证金 = $475剩余 $25 用于手续费
---
记住: