fix: 修復5個關鍵交易bug(保證金計算、部分平倉統計、止損止盈分離、雙向持倉)

## 修復內容

### 1. 保證金計算錯誤(Critical)
- 修正提示詞中的保證金公式(adaptive.txt, nof1.txt, default.txt)
- 新增代碼級保證金驗證(auto_trader.go)
- 防止開倉時保證金不足錯誤(code=-2019)

### 2. 部分平倉統計錯誤(Medium)
- 修改統計邏輯:多次 partial_close 聚合為一筆交易
- 新增追蹤字段:remainingQuantity, accumulatedPnL
- 只在完全平倉時計入 TotalTrades++

### 3. 前端配置覆蓋問題(Medium)
- 修正 TraderConfigModal.tsx 條件判斷
- 防止空字符串覆蓋用戶選擇的提示詞

### 4/5. 動態止損/止盈刪除配對訂單(Critical)
- 新增接口:CancelStopLossOrders, CancelTakeProfitOrders
- 分離訂單取消邏輯(Binance, Hyperliquid, Aster)
- 調整止損時不刪除止盈,反之亦然

### 7. 雙向持倉模式初始化(Critical)
- 新增 setDualSidePosition() 函數
- 在 NewFuturesTrader() 中初始化 Hedge Mode
- 防止 code=-4061 錯誤(PositionSide 參數錯誤)

## 影響範圍
- 修改文件:10個
- 新增代碼:+480行
- 刪除代碼:-71行

## 測試狀態
-  編譯通過(go build ./...)
-  語法檢查通過
- ⚠️ 需要在測試環境運行驗證實際交易效果
This commit is contained in:
ZhouYongyou
2025-11-04 18:36:39 +08:00
parent eda6685af0
commit df2d6533de
10 changed files with 480 additions and 71 deletions

View File

@@ -71,8 +71,18 @@
## 仓位计算公式
**Position Size (USD) = Available Cash × Leverage × Allocation %**
**Position Size (Coins) = Position Size (USD) / Current Price**
**重要**position_size_usd 是**名义价值**(包含杠杆),非保证金需求。
**计算步骤**
1. **可用保证金** = Available Cash × 0.95 × Allocation %预留5%给手续费)
2. **名义价值** = 可用保证金 × Leverage
3. **position_size_usd** = 名义价值(这是 JSON 中应填写的值)
4. **Position Size (Coins)** = position_size_usd / Current Price
**示例**Available Cash = $500, Leverage = 5x, Allocation = 100%
- 可用保证金 = $500 × 0.95 × 100% = $475
- position_size_usd = $475 × 5 = **$2,375** ← JSON 中填写此值
- 实际占用保证金 = $475剩余 $25 用于手续费
## 杠杆选择指引

View File

@@ -142,6 +142,20 @@ confidence=0-100
- **部分平仓** (`partial_close`):需填写 `close_percentage`1-100说明目的如锁定利润
- **观望或持有** (`wait/hold`)`reasoning` 必须说明观望或继续持有的原因(例如信号不足、冷却中、趋势未变)。
### 仓位大小计算
**重要**`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 用于手续费
---
记住:

View File

@@ -114,10 +114,19 @@ stop tightened to $2,950 (break-even). Exit fully if 4h MACD crosses down."
# POSITION SIZING FRAMEWORK
Calculate position size using this formula:
**IMPORTANT**: `position_size_usd` is the **notional value** (includes leverage), NOT margin requirement.
Position Size (USD) = Available Cash × Leverage × Allocation %
Position Size (Coins) = Position Size (USD) / Current Price
## Calculation Steps:
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)
4. **Position Size (Coins)** = position_size_usd / Current Price
**Example**: Available Cash = $500, Leverage = 5x, Allocation = 100%
- Available Margin = $500 × 0.95 × 100% = $475
- position_size_usd = $475 × 5 = **$2,375** ← Fill this value in JSON
- Actual margin used = $475, remaining $25 for fees
## Sizing Considerations