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

@@ -243,8 +243,10 @@ func (l *DecisionLogger) GetStatistics() (*Statistics, error) {
switch action.Action {
case "open_long", "open_short":
stats.TotalOpenPositions++
case "close_long", "close_short", "partial_close", "auto_close_long", "auto_close_short":
case "close_long", "close_short", "auto_close_long", "auto_close_short":
stats.TotalClosePositions++
// 🔧 BUG FIXpartial_close 不計入 TotalClosePositions避免重複計數
// case "partial_close": // 不計數,因為只有完全平倉才算一次
// update_stop_loss 和 update_take_profit 不計入統計
}
}
@@ -418,11 +420,15 @@ func (l *DecisionLogger) AnalyzePerformance(lookbackCycles int) (*PerformanceAna
case "open_long", "open_short":
// 更新开仓记录(可能已经在预填充时记录过了)
openPositions[posKey] = map[string]interface{}{
"side": side,
"openPrice": action.Price,
"openTime": action.Timestamp,
"quantity": action.Quantity,
"leverage": action.Leverage,
"side": side,
"openPrice": action.Price,
"openTime": action.Timestamp,
"quantity": action.Quantity,
"leverage": action.Leverage,
"remainingQuantity": action.Quantity, // 🔧 BUG FIX追蹤剩餘數量
"accumulatedPnL": 0.0, // 🔧 BUG FIX累積部分平倉盈虧
"partialCloseCount": 0, // 🔧 BUG FIX部分平倉次數
"partialCloseVolume": 0.0, // 🔧 BUG FIX部分平倉總量
}
case "close_long", "close_short", "partial_close", "auto_close_long", "auto_close_short":
@@ -434,15 +440,22 @@ func (l *DecisionLogger) AnalyzePerformance(lookbackCycles int) (*PerformanceAna
quantity := openPos["quantity"].(float64)
leverage := openPos["leverage"].(int)
// 对于 partial_close使用实际平仓数量否则使用完整仓位数量
actualQuantity := quantity
// 🔧 BUG FIX取得追蹤字段若不存在則初始化
remainingQty, _ := openPos["remainingQuantity"].(float64)
if remainingQty == 0 {
remainingQty = quantity // 兼容舊數據(沒有 remainingQuantity 字段)
}
accumulatedPnL, _ := openPos["accumulatedPnL"].(float64)
partialCloseCount, _ := openPos["partialCloseCount"].(int)
partialCloseVolume, _ := openPos["partialCloseVolume"].(float64)
// 对于 partial_close使用实际平仓数量否则使用剩余仓位数量
actualQuantity := remainingQty
if action.Action == "partial_close" {
actualQuantity = action.Quantity
}
// 计算实际盈亏USDT
// 合约交易 PnL 计算actualQuantity × 价格差
// 注意:杠杆不影响绝对盈亏,只影响保证金需求
// 计算本次平仓的盈亏USDT
var pnl float64
if side == "long" {
pnl = actualQuantity * (action.Price - openPrice)
@@ -450,61 +463,134 @@ func (l *DecisionLogger) AnalyzePerformance(lookbackCycles int) (*PerformanceAna
pnl = actualQuantity * (openPrice - action.Price)
}
// 计算盈亏百分比(相对保证金)
positionValue := actualQuantity * openPrice
marginUsed := positionValue / float64(leverage)
pnlPct := 0.0
if marginUsed > 0 {
pnlPct = (pnl / marginUsed) * 100
}
// 🔧 BUG FIX處理 partial_close 聚合邏輯
if action.Action == "partial_close" {
// 累積盈虧和數量
accumulatedPnL += pnl
remainingQty -= actualQuantity
partialCloseCount++
partialCloseVolume += actualQuantity
// 记录交易结果
outcome := TradeOutcome{
Symbol: symbol,
Side: side,
Quantity: actualQuantity,
Leverage: leverage,
OpenPrice: openPrice,
ClosePrice: action.Price,
PositionValue: positionValue,
MarginUsed: marginUsed,
PnL: pnl,
PnLPct: pnlPct,
Duration: action.Timestamp.Sub(openTime).String(),
OpenTime: openTime,
CloseTime: action.Timestamp,
}
// 更新 openPositions保留持倉記錄但更新追蹤數據
openPos["remainingQuantity"] = remainingQty
openPos["accumulatedPnL"] = accumulatedPnL
openPos["partialCloseCount"] = partialCloseCount
openPos["partialCloseVolume"] = partialCloseVolume
analysis.RecentTrades = append(analysis.RecentTrades, outcome)
analysis.TotalTrades++
// 判斷是否已完全平倉
if remainingQty <= 0.0001 { // 使用小閾值避免浮點誤差
// ✅ 完全平倉:記錄為一筆完整交易
positionValue := quantity * openPrice
marginUsed := positionValue / float64(leverage)
pnlPct := 0.0
if marginUsed > 0 {
pnlPct = (accumulatedPnL / marginUsed) * 100
}
// 分类交易盈利、亏损、持平避免将pnl=0算入亏损
if pnl > 0 {
analysis.WinningTrades++
analysis.AvgWin += pnl
} else if pnl < 0 {
analysis.LosingTrades++
analysis.AvgLoss += pnl
}
// pnl == 0 的交易不计入盈利也不计入亏损,但计入总交易数
outcome := TradeOutcome{
Symbol: symbol,
Side: side,
Quantity: quantity, // 使用原始總量
Leverage: leverage,
OpenPrice: openPrice,
ClosePrice: action.Price, // 最後一次平倉價格
PositionValue: positionValue,
MarginUsed: marginUsed,
PnL: accumulatedPnL, // 🔧 使用累積盈虧
PnLPct: pnlPct,
Duration: action.Timestamp.Sub(openTime).String(),
OpenTime: openTime,
CloseTime: action.Timestamp,
}
// 更新币种统计
if _, exists := analysis.SymbolStats[symbol]; !exists {
analysis.SymbolStats[symbol] = &SymbolPerformance{
Symbol: symbol,
analysis.RecentTrades = append(analysis.RecentTrades, outcome)
analysis.TotalTrades++ // 🔧 只在完全平倉時計數
// 分类交易
if accumulatedPnL > 0 {
analysis.WinningTrades++
analysis.AvgWin += accumulatedPnL
} else if accumulatedPnL < 0 {
analysis.LosingTrades++
analysis.AvgLoss += accumulatedPnL
}
// 更新币种统计
if _, exists := analysis.SymbolStats[symbol]; !exists {
analysis.SymbolStats[symbol] = &SymbolPerformance{
Symbol: symbol,
}
}
stats := analysis.SymbolStats[symbol]
stats.TotalTrades++
stats.TotalPnL += accumulatedPnL
if accumulatedPnL > 0 {
stats.WinningTrades++
} else if accumulatedPnL < 0 {
stats.LosingTrades++
}
// 刪除持倉記錄
delete(openPositions, posKey)
}
}
stats := analysis.SymbolStats[symbol]
stats.TotalTrades++
stats.TotalPnL += pnl
if pnl > 0 {
stats.WinningTrades++
} else if pnl < 0 {
stats.LosingTrades++
}
// ⚠️ 否則不做任何操作(等待後續 partial_close 或 full close
// 移除已平仓记录partial_close 不刪除,因為還有剩餘倉位)
if action.Action != "partial_close" {
} else {
// 🔧 完全平倉close_long/close_short/auto_close
// 如果之前有部分平倉,需要加上累積的 PnL
totalPnL := accumulatedPnL + pnl
positionValue := quantity * openPrice
marginUsed := positionValue / float64(leverage)
pnlPct := 0.0
if marginUsed > 0 {
pnlPct = (totalPnL / marginUsed) * 100
}
outcome := TradeOutcome{
Symbol: symbol,
Side: side,
Quantity: quantity, // 使用原始總量
Leverage: leverage,
OpenPrice: openPrice,
ClosePrice: action.Price,
PositionValue: positionValue,
MarginUsed: marginUsed,
PnL: totalPnL, // 🔧 包含之前部分平倉的 PnL
PnLPct: pnlPct,
Duration: action.Timestamp.Sub(openTime).String(),
OpenTime: openTime,
CloseTime: action.Timestamp,
}
analysis.RecentTrades = append(analysis.RecentTrades, outcome)
analysis.TotalTrades++
// 分类交易
if totalPnL > 0 {
analysis.WinningTrades++
analysis.AvgWin += totalPnL
} else if totalPnL < 0 {
analysis.LosingTrades++
analysis.AvgLoss += totalPnL
}
// 更新币种统计
if _, exists := analysis.SymbolStats[symbol]; !exists {
analysis.SymbolStats[symbol] = &SymbolPerformance{
Symbol: symbol,
}
}
stats := analysis.SymbolStats[symbol]
stats.TotalTrades++
stats.TotalPnL += totalPnL
if totalPnL > 0 {
stats.WinningTrades++
} else if totalPnL < 0 {
stats.LosingTrades++
}
// 刪除持倉記錄
delete(openPositions, posKey)
}
}