mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-18 01:44:38 +08:00
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:
@@ -330,26 +330,25 @@
|
||||
|
||||
## 仓位计算公式
|
||||
|
||||
```
|
||||
仓位大小(USD) = 可用资金 × 风险预算 / 止损距离百分比
|
||||
仓位数量(Coins) = 仓位大小(USD) / 当前价格
|
||||
```
|
||||
**重要**:position_size_usd 是**名义价值**(包含杠杆),非保证金需求。
|
||||
|
||||
**示例**:
|
||||
```
|
||||
账户净值:10,000 USDT
|
||||
风险预算:2%(信心度 90-95)
|
||||
止损距离:2%(50,000 → 49,000)
|
||||
**计算步骤**:
|
||||
1. **可用保证金** = Available Cash × 0.95 × Allocation %(预留5%给手续费)
|
||||
2. **名义价值** = 可用保证金 × Leverage
|
||||
3. **position_size_usd** = 名义价值(这是 JSON 中应填写的值)
|
||||
4. **Position Size (Coins)** = position_size_usd / Current Price
|
||||
|
||||
仓位大小 = 10,000 × 2% / 2% = 10,000 USDT
|
||||
杠杆 5x → 保证金 2,000 USDT
|
||||
```
|
||||
**示例**:Available Cash = $500, Leverage = 5x, Allocation = 100%
|
||||
- 可用保证金 = $500 × 0.95 × 100% = $475
|
||||
- position_size_usd = $475 × 5 = **$2,375** ← JSON 中填写此值
|
||||
- 实际占用保证金 = $475,剩余 $25 用于手续费
|
||||
|
||||
## 杠杆选择指南
|
||||
## 杠杆选择指引
|
||||
|
||||
- 信心度 85-87: 3-5x 杠杆
|
||||
- 信心度 88-92: 5-10x 杠杆
|
||||
- 信心度 93-95: 10-15x 杠杆
|
||||
基于信心度的杠杆配置:
|
||||
- 信心度 <85 → 不开仓
|
||||
- 信心度 85-90 → 杠杆 1-3x,风险预算 1.5%
|
||||
- 信心度 90-95 → 杠杆 3-8x,风险预算 2%
|
||||
- 信心度 >95: 最高 20x 杠杆(谨慎)
|
||||
|
||||
## 风险控制原则
|
||||
|
||||
Reference in New Issue
Block a user