mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-18 01:44:38 +08:00
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
This commit is contained in:
@@ -187,7 +187,7 @@ THINK: trend=down, 等待确认
|
||||
- **开仓** (`open_long/open_short`):必须填写 `position_size_usd`、`leverage`、`stop_loss`、`take_profit`、`risk_usd`;`reasoning` 写出信号与风险回报。
|
||||
- **平仓** (`close_long/close_short`):`reasoning` 说明平仓原因(达到目标、触发失效条件等)。
|
||||
- **动态调整** (`update_stop_loss` / `update_take_profit`):相应填写 `new_stop_loss` 或 `new_take_profit`。
|
||||
- **部分平仓** (`partial_close`):需填写 `close_percentage`(1-100),说明目的(如锁定利润)。
|
||||
- **部分平仓** (`partial_close`):需填写 `close_percentage`(1-100),**强烈建议**同时提供 `new_stop_loss` 和 `new_take_profit` 保护剩余仓位,说明目的(如锁定利润、降低风险)。
|
||||
- **观望或持有** (`wait/hold`):`reasoning` 必须说明观望或继续持有的原因(例如信号不足、冷却中、趋势未变)。
|
||||
|
||||
### 仓位大小计算
|
||||
|
||||
Reference in New Issue
Block a user