Merge pull request #415 from zhouyongyou/feat/partial-close-core-v2

feat: 部分平倉和動態止盈止損核心實現 / Partial Close & Dynamic TP/SL Core
This commit is contained in:
Icyoung
2025-11-05 15:41:20 +08:00
committed by GitHub
7 changed files with 398 additions and 16 deletions
+11 -5
View File
@@ -409,18 +409,24 @@ func (l *DecisionLogger) AnalyzePerformance(lookbackCycles int) (*PerformanceAna
quantity := openPos["quantity"].(float64)
leverage := openPos["leverage"].(int)
// 对于 partial_close,使用实际平仓数量;否则使用完整仓位数量
actualQuantity := quantity
if action.Action == "partial_close" {
actualQuantity = action.Quantity
}
// 计算实际盈亏(USDT
// 合约交易 PnL 计算:quantity × 价格差
// 合约交易 PnL 计算:actualQuantity × 价格差
// 注意:杠杆不影响绝对盈亏,只影响保证金需求
var pnl float64
if side == "long" {
pnl = quantity * (action.Price - openPrice)
pnl = actualQuantity * (action.Price - openPrice)
} else {
pnl = quantity * (openPrice - action.Price)
pnl = actualQuantity * (openPrice - action.Price)
}
// 计算盈亏百分比(相对保证金)
positionValue := quantity * openPrice
positionValue := actualQuantity * openPrice
marginUsed := positionValue / float64(leverage)
pnlPct := 0.0
if marginUsed > 0 {
@@ -431,7 +437,7 @@ func (l *DecisionLogger) AnalyzePerformance(lookbackCycles int) (*PerformanceAna
outcome := TradeOutcome{
Symbol: symbol,
Side: side,
Quantity: quantity,
Quantity: actualQuantity,
Leverage: leverage,
OpenPrice: openPrice,
ClosePrice: action.Price,