mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-16 09:11:03 +08:00
fix: 自動處理小額倉位,添加三層防護機制
- Layer 1: AI 決策層預防產生小額剩餘(adaptive.txt) - Layer 2: 後端自動修正不合規決策(auto_trader.go) - Layer 3: 交易執行層強制清理小額倉位(binance_futures.go) 修復 Binance MIN_NOTIONAL 限制導致的小額倉位卡住問題。 完全自動化處理,無需手動介入。 實現細節: - adaptive.txt: 添加 partial_close 最小倉位限制說明和計算公式 - auto_trader.go: executePartialCloseWithRecord 添加剩餘價值檢查 - binance_futures.go: 添加 GetMinNotional/CheckMinNotional/forceCloseLong/forceCloseShort 函數 三層防護機制: 1. AI 計算剩餘倉位,< 10 USDT 則改用全部平倉 2. 後端驗證剩餘價值,< 10 USDT 自動修正為全部平倉 3. 交易層檢查訂單金額,< 10 USDT 嘗試強制平倉
This commit is contained in:
@@ -1220,6 +1220,33 @@ func (at *AutoTrader) executePartialCloseWithRecord(decision *decision.Decision,
|
||||
closeQuantity := totalQuantity * (decision.ClosePercentage / 100.0)
|
||||
actionRecord.Quantity = closeQuantity
|
||||
|
||||
// ✅ Layer 2: 最小仓位检查(防止产生小额剩余)
|
||||
markPrice, _ := targetPosition["markPrice"].(float64)
|
||||
currentPositionValue := totalQuantity * markPrice
|
||||
remainingQuantity := totalQuantity - closeQuantity
|
||||
remainingValue := remainingQuantity * markPrice
|
||||
|
||||
const MIN_POSITION_VALUE = 10.0 // 最小持仓价值 10 USDT
|
||||
|
||||
if remainingValue > 0 && remainingValue < MIN_POSITION_VALUE {
|
||||
log.Printf("⚠️ 检测到 partial_close 后剩余仓位 %.2f USDT < %.0f USDT",
|
||||
remainingValue, MIN_POSITION_VALUE)
|
||||
log.Printf(" → 当前仓位价值: %.2f USDT, 平仓 %.1f%%, 剩余: %.2f USDT",
|
||||
currentPositionValue, decision.ClosePercentage, remainingValue)
|
||||
log.Printf(" → 自动修正为全部平仓,避免产生无法平仓的小额剩余")
|
||||
|
||||
// 🔄 自动修正为全部平仓
|
||||
if positionSide == "LONG" {
|
||||
decision.Action = "close_long"
|
||||
log.Printf(" ✓ 已修正为: close_long")
|
||||
return at.executeCloseLongWithRecord(decision, actionRecord)
|
||||
} else {
|
||||
decision.Action = "close_short"
|
||||
log.Printf(" ✓ 已修正为: close_short")
|
||||
return at.executeCloseShortWithRecord(decision, actionRecord)
|
||||
}
|
||||
}
|
||||
|
||||
// 执行平仓
|
||||
var order map[string]interface{}
|
||||
if positionSide == "LONG" {
|
||||
@@ -1237,7 +1264,6 @@ func (at *AutoTrader) executePartialCloseWithRecord(decision *decision.Decision,
|
||||
actionRecord.OrderID = orderID
|
||||
}
|
||||
|
||||
remainingQuantity := totalQuantity - closeQuantity
|
||||
log.Printf(" ✓ 部分平仓成功: 平仓 %.4f (%.1f%%), 剩余 %.4f",
|
||||
closeQuantity, decision.ClosePercentage, remainingQuantity)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user