Files
nofx/decision
ZhouYongyou 1b48c06362 fix(trader): restore TP/SL after partial_close to prevent unprotected positions
## Problem
- User reported: After partial_close, remaining position has NO TP/SL protection
- Root cause: Binance automatically cancels TP/SL orders when position size changes
- Impact: Remaining position exposed to unlimited risk (爆仓风险)

## Why TP/SL disappears
```
Opening:
  Position: 0.130 BTC
  TP order: 0.130 BTC ✓
  SL order: 0.130 BTC ✓

After 50% partial_close:
  Position: 0.065 BTC (remaining)
  TP order: 0.130 BTC  Quantity mismatch → Binance cancels
  SL order: 0.130 BTC  Quantity mismatch → Binance cancels
  → Remaining position has NO protection
```

## Solution

### Code changes (trader/auto_trader.go:1278-1305)

After partial_close execution, restore TP/SL with new quantity:

```go
// If AI provides new TP/SL prices
if decision.NewStopLoss > 0 || decision.NewTakeProfit > 0 {
    // Re-set SL with remaining quantity
    at.trader.SetStopLoss(symbol, side, remainingQuantity, newStopLoss)
    // Re-set TP with remaining quantity
    at.trader.SetTakeProfit(symbol, side, remainingQuantity, newTakeProfit)
} else {
    // WARNING: Remaining position has NO protection
    log.Printf("⚠️⚠️⚠️ 警告: 剩余仓位无止盈止损保护")
}
```

### Prompt updates

**Updated files**:
- prompts/adaptive.txt (line 171)
- prompts/default.txt (line 190)
- prompts/nof1.txt (line 334)

**Key change**: Emphasize AI MUST provide `new_stop_loss` and `new_take_profit` in `partial_close` decisions:

```markdown
- **partial_close**: Fill `close_percentage` (1-100),
  **STRONGLY RECOMMEND** providing `new_stop_loss` AND `new_take_profit`
  to protect remaining position (otherwise NO stop protection)
```

## Benefits

-  Risk control: Remaining position protected after partial_close
-  Clear warning: Logs explicit warning if AI doesn't provide new TP/SL
-  AI guidance: Updated prompts emphasize importance of new TP/SL
-  Backwards compatible: Works even if AI doesn't provide (just warns)

## Testing checklist

- [ ] Partial_close with new_stop_loss & new_take_profit → TP/SL restored
- [ ] Check Binance UI shows TP/SL orders after partial_close
- [ ] Partial_close without new TP/SL → Warning logged
- [ ] Remaining quantity matches TP/SL order quantity

Related: partial-close-tpsl-bug.md
2025-11-05 01:27:09 +08:00
..