From 396df86367cfa1ab9b9f55c6af0b6c491082f886 Mon Sep 17 00:00:00 2001 From: ZhouYongyou <128128010+zhouyongyou@users.noreply.github.com> Date: Sat, 1 Nov 2025 00:01:38 +0800 Subject: [PATCH] Cancel orphan orders when position is closed Added logic to automatically cancel all orders for a symbol when its position disappears, ensuring orphan stop-loss/take-profit orders are cleaned up. This improves order management and prevents unintended trades after a position is closed. --- trader/auto_trader.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/trader/auto_trader.go b/trader/auto_trader.go index 93889f12..c4744b69 100644 --- a/trader/auto_trader.go +++ b/trader/auto_trader.go @@ -479,9 +479,25 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) { }) } - // 清理已平仓的持仓记录 + // 清理已平仓的持仓记录,并撤销孤儿委托单 for key := range at.positionFirstSeenTime { if !currentPositionKeys[key] { + // 倉位消失了(可能被止損/止盈觸發,或被強平) + // 提取幣種名稱(key 格式:BTCUSDT_long 或 SOLUSDT_short) + parts := strings.Split(key, "_") + if len(parts) == 2 { + symbol := parts[0] + side := parts[1] + log.Printf("⚠️ 檢測到倉位消失: %s %s → 自動撤銷委託單", symbol, side) + + // 撤銷該幣種的所有委託單(清理孤兒止損/止盈單) + if err := at.trader.CancelAllOrders(symbol); err != nil { + log.Printf(" ⚠️ 撤銷 %s 委託單失敗: %v", symbol, err) + } else { + log.Printf(" ✓ 已撤銷 %s 的所有委託單", symbol) + } + } + delete(at.positionFirstSeenTime, key) } }