mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-17 09:24:36 +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:
@@ -168,7 +168,7 @@
|
||||
| `close_short`| 平掉空仓 | `reasoning` |
|
||||
| `update_stop_loss` | 调整止损 | `new_stop_loss`、`reasoning` |
|
||||
| `update_take_profit` | 调整止盈 | `new_take_profit`、`reasoning` |
|
||||
| `partial_close` | 部分平仓(1-100%) | `close_percentage`、`reasoning` |
|
||||
| `partial_close` | 部分平仓(1-100%) | `close_percentage`、`reasoning`、**强烈建议** `new_stop_loss` + `new_take_profit` 保护剩余仓位 |
|
||||
| `hold` | 维持持仓 | `reasoning` |
|
||||
| `wait` | 观望 | `reasoning` |
|
||||
|
||||
|
||||
@@ -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` 必须说明观望或继续持有的原因(例如信号不足、冷却中、趋势未变)。
|
||||
|
||||
### 仓位大小计算
|
||||
|
||||
@@ -331,7 +331,7 @@ Every decision must follow this structure:
|
||||
### Required field rules
|
||||
- **open_long / open_short**: Fill all numeric fields; `risk_usd` ≤ account_value × 0.03, `confidence` ≥ 75 (use 0-100 scale); `reasoning` explains signal trigger and risk control.
|
||||
- **update_stop_loss / update_take_profit**: Provide `new_stop_loss` or `new_take_profit` with adjustment rationale (e.g., trailing stop, locking profits).
|
||||
- **partial_close**: Fill `close_percentage` (1-100), describe purpose (profit lock / risk reduction) AND remaining position management plan. If adjusting TP/SL simultaneously, include `new_stop_loss` / `new_take_profit`.
|
||||
- **partial_close**: Fill `close_percentage` (1-100), **STRONGLY RECOMMEND** providing `new_stop_loss` AND `new_take_profit` to protect remaining position (otherwise it will have NO stop protection). Describe purpose (profit lock / risk reduction) and remaining position management plan.
|
||||
- **close_long / close_short**: Use current position size; `reasoning` states exit reason (target hit, risk elevated, thesis invalidated).
|
||||
- **hold / wait**: `reasoning` must explain why continuing to hold or waiting (e.g., trend intact, cooling period, insufficient signal).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user