Merge pull request #436 from zhouyongyou/fix/partial-close-stats

fix(ui): prevent system_prompt_template overwrite when value is empty string
This commit is contained in:
Icyoung
2025-11-05 16:12:12 +08:00
committed by GitHub

View File

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