|
| 1 | +# 動態止盈止損功能設計方案 |
| 2 | + |
| 3 | +## 🔴 問題描述 |
| 4 | + |
| 5 | +**用戶反饋**: |
| 6 | +> 还有动态止盈止损我建议你给ai decisions里加个adjust tp sl 或者给close加个quantity 不然应该是没有作用 |
| 7 | +
|
| 8 | +**根本原因**: |
| 9 | +- 策略模板(adaptive.txt)提到"追蹤止損"功能: |
| 10 | + - 浮盈達到 0.8% → 止損移到成本價(保證不虧) |
| 11 | + - 浮盈達到 1.2% → 止損移到 +0.5%(鎖定一半利潤) |
| 12 | +- 但 AI 無法執行這些操作,因為 Decision 結構**不支持**: |
| 13 | + - ❌ 調整現有持倉的止盈/止損 |
| 14 | + - ❌ 部分平倉(分批止盈) |
| 15 | + |
| 16 | +## 📊 當前限制 |
| 17 | + |
| 18 | +### Decision 結構(decision/engine.go:72-82) |
| 19 | +```go |
| 20 | +type Decision struct { |
| 21 | + Symbol string `json:"symbol"` |
| 22 | + Action string `json:"action"` // "open_long", "open_short", "close_long", "close_short", "hold", "wait" |
| 23 | + Leverage int `json:"leverage,omitempty"` |
| 24 | + PositionSizeUSD float64 `json:"position_size_usd,omitempty"` |
| 25 | + StopLoss float64 `json:"stop_loss,omitempty"` // ⚠️ 只在開倉時有效 |
| 26 | + TakeProfit float64 `json:"take_profit,omitempty"` // ⚠️ 只在開倉時有效 |
| 27 | + Confidence int `json:"confidence,omitempty"` |
| 28 | + RiskUSD float64 `json:"risk_usd,omitempty"` |
| 29 | + Reasoning string `json:"reasoning"` |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### 當前支持的 Actions |
| 34 | +- `open_long` - 開多倉(有 stop_loss, take_profit) |
| 35 | +- `open_short` - 開空倉(有 stop_loss, take_profit) |
| 36 | +- `close_long` - 全部平多倉 |
| 37 | +- `close_short` - 全部平空倉 |
| 38 | +- `hold` - 持倉不動 |
| 39 | +- `wait` - 觀望 |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## ✅ 解決方案 |
| 44 | + |
| 45 | +### 方案 A:添加新的 Action Types(推薦) |
| 46 | + |
| 47 | +#### 1. `adjust_stop_loss` - 調整止損 |
| 48 | +```json |
| 49 | +{ |
| 50 | + "symbol": "BTCUSDT", |
| 51 | + "action": "adjust_stop_loss", |
| 52 | + "new_stop_loss": 100500.0, |
| 53 | + "reasoning": "浮盈達到 1.5%,將止損移到成本價 (100500) 保證不虧" |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +#### 2. `adjust_take_profit` - 調整止盈 |
| 58 | +```json |
| 59 | +{ |
| 60 | + "symbol": "BTCUSDT", |
| 61 | + "action": "adjust_take_profit", |
| 62 | + "new_take_profit": 102000.0, |
| 63 | + "reasoning": "價格距離 EMA20 僅 0.3%,將止盈提前到 102000 避免回撤" |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +#### 3. `partial_close` - 部分平倉 |
| 68 | +```json |
| 69 | +{ |
| 70 | + "symbol": "BTCUSDT", |
| 71 | + "action": "partial_close", |
| 72 | + "close_percentage": 50, |
| 73 | + "reasoning": "價格到達第一目標 104300,分批平倉 50%,剩餘持倉繼續追蹤" |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### 方案 B:修改現有 close action(次選) |
| 78 | + |
| 79 | +給 `close_long` / `close_short` 添加 `quantity` 參數: |
| 80 | +```json |
| 81 | +{ |
| 82 | + "symbol": "BTCUSDT", |
| 83 | + "action": "close_long", |
| 84 | + "quantity": 0.5, // 0.5 = 50%, 1.0 = 100%(預設) |
| 85 | + "reasoning": "部分止盈" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 🛠️ 實施步驟 |
| 92 | + |
| 93 | +### 步驟 1:修改 Decision 結構 |
| 94 | + |
| 95 | +**文件**: `decision/engine.go` |
| 96 | + |
| 97 | +```go |
| 98 | +type Decision struct { |
| 99 | + Symbol string `json:"symbol"` |
| 100 | + Action string `json:"action"` |
| 101 | + // Actions: "open_long", "open_short", "close_long", "close_short", |
| 102 | + // "adjust_stop_loss", "adjust_take_profit", "partial_close", "hold", "wait" |
| 103 | + |
| 104 | + // 開倉參數 |
| 105 | + Leverage int `json:"leverage,omitempty"` |
| 106 | + PositionSizeUSD float64 `json:"position_size_usd,omitempty"` |
| 107 | + StopLoss float64 `json:"stop_loss,omitempty"` |
| 108 | + TakeProfit float64 `json:"take_profit,omitempty"` |
| 109 | + |
| 110 | + // 調整參數(新增) |
| 111 | + NewStopLoss float64 `json:"new_stop_loss,omitempty"` // 用於 adjust_stop_loss |
| 112 | + NewTakeProfit float64 `json:"new_take_profit,omitempty"` // 用於 adjust_take_profit |
| 113 | + ClosePercentage float64 `json:"close_percentage,omitempty"` // 用於 partial_close (0-100) |
| 114 | + |
| 115 | + // 通用參數 |
| 116 | + Confidence int `json:"confidence,omitempty"` |
| 117 | + RiskUSD float64 `json:"risk_usd,omitempty"` |
| 118 | + Reasoning string `json:"reasoning"` |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### 步驟 2:實現 Action 執行邏輯 |
| 123 | + |
| 124 | +**文件**: `trader/auto_trader.go` 或新建 `trader/position_manager.go` |
| 125 | + |
| 126 | +```go |
| 127 | +// 處理調整止損 |
| 128 | +func (t *AutoTrader) adjustStopLoss(symbol string, newStopLoss float64) error { |
| 129 | + // 1. 獲取當前持倉 |
| 130 | + position := t.getPosition(symbol) |
| 131 | + if position == nil { |
| 132 | + return fmt.Errorf("持倉不存在") |
| 133 | + } |
| 134 | + |
| 135 | + // 2. 調用交易所 API 修改止損單 |
| 136 | + err := t.exchange.ModifyStopLoss(symbol, position.OrderID, newStopLoss) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + // 3. 更新本地持倉記錄 |
| 142 | + position.StopLoss = newStopLoss |
| 143 | + |
| 144 | + log.Printf("✓ %s 止損已調整到 %.2f", symbol, newStopLoss) |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// 處理部分平倉 |
| 149 | +func (t *AutoTrader) partialClose(symbol string, percentage float64) error { |
| 150 | + // 1. 獲取當前持倉 |
| 151 | + position := t.getPosition(symbol) |
| 152 | + if position == nil { |
| 153 | + return fmt.Errorf("持倉不存在") |
| 154 | + } |
| 155 | + |
| 156 | + // 2. 計算平倉數量 |
| 157 | + closeQty := position.Quantity * (percentage / 100.0) |
| 158 | + |
| 159 | + // 3. 執行市價平倉 |
| 160 | + err := t.exchange.ClosePosition(symbol, closeQty) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + // 4. 更新本地持倉記錄 |
| 166 | + position.Quantity -= closeQty |
| 167 | + |
| 168 | + log.Printf("✓ %s 部分平倉 %.1f%% (%.4f)", symbol, percentage, closeQty) |
| 169 | + return nil |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +### 步驟 3:更新模板說明 |
| 174 | + |
| 175 | +**文件**: `prompts/adaptive.txt` |
| 176 | + |
| 177 | +在輸出格式部分添加: |
| 178 | + |
| 179 | +```markdown |
| 180 | +## 可用的 Actions |
| 181 | + |
| 182 | +### 開倉 |
| 183 | +- `open_long` / `open_short` - 開倉(必須指定 leverage, position_size_usd, stop_loss, take_profit) |
| 184 | + |
| 185 | +### 平倉 |
| 186 | +- `close_long` / `close_short` - 全部平倉 |
| 187 | +- `partial_close` - 部分平倉(指定 close_percentage: 0-100) |
| 188 | + |
| 189 | +### 調整持倉 |
| 190 | +- `adjust_stop_loss` - 調整止損(指定 new_stop_loss) |
| 191 | +- `adjust_take_profit` - 調整止盈(指定 new_take_profit) |
| 192 | + |
| 193 | +### 觀望 |
| 194 | +- `hold` - 持倉不動 |
| 195 | +- `wait` - 觀望 |
| 196 | + |
| 197 | +## 追蹤止損範例 |
| 198 | + |
| 199 | +```json |
| 200 | +[ |
| 201 | + { |
| 202 | + "symbol": "BTCUSDT", |
| 203 | + "action": "adjust_stop_loss", |
| 204 | + "new_stop_loss": 100500, |
| 205 | + "confidence": 85, |
| 206 | + "reasoning": "浮盈達到 1.5%(目前價格 101500),將止損移到成本價 100500,保證不虧" |
| 207 | + } |
| 208 | +] |
| 209 | +``` |
| 210 | + |
| 211 | +## 部分止盈範例 |
| 212 | + |
| 213 | +```json |
| 214 | +[ |
| 215 | + { |
| 216 | + "symbol": "BTCUSDT", |
| 217 | + "action": "partial_close", |
| 218 | + "close_percentage": 50, |
| 219 | + "confidence": 80, |
| 220 | + "reasoning": "價格到達第一目標 104300(4h EMA20 前 0.2%),分批止盈 50%,剩餘倉位繼續持有" |
| 221 | + } |
| 222 | +] |
| 223 | +``` |
| 224 | +``` |
| 225 | +
|
| 226 | +### 步驟 4:更新交易邏輯主循環 |
| 227 | +
|
| 228 | +**文件**: `trader/auto_trader.go` - `executeTrades()` 函數 |
| 229 | +
|
| 230 | +```go |
| 231 | +func (t *AutoTrader) executeTrades(decisions []decision.Decision) { |
| 232 | + for _, d := range decisions { |
| 233 | + switch d.Action { |
| 234 | + case "open_long", "open_short": |
| 235 | + t.openPosition(d) |
| 236 | +
|
| 237 | + case "close_long", "close_short": |
| 238 | + t.closePosition(d.Symbol) |
| 239 | +
|
| 240 | + case "adjust_stop_loss": |
| 241 | + t.adjustStopLoss(d.Symbol, d.NewStopLoss) |
| 242 | +
|
| 243 | + case "adjust_take_profit": |
| 244 | + t.adjustTakeProfit(d.Symbol, d.NewTakeProfit) |
| 245 | +
|
| 246 | + case "partial_close": |
| 247 | + t.partialClose(d.Symbol, d.ClosePercentage) |
| 248 | +
|
| 249 | + case "hold", "wait": |
| 250 | + // 不操作 |
| 251 | +
|
| 252 | + default: |
| 253 | + log.Printf("⚠️ 未知的 action: %s", d.Action) |
| 254 | + } |
| 255 | + } |
| 256 | +} |
| 257 | +``` |
| 258 | + |
| 259 | +--- |
| 260 | + |
| 261 | +## 🧪 測試驗證 |
| 262 | + |
| 263 | +### 測試用例 1:追蹤止損 |
| 264 | +1. 開多倉 BTCUSDT @ 100000,止損 99000,止盈 102000 |
| 265 | +2. 價格上漲到 101500(浮盈 1.5%) |
| 266 | +3. AI 決策:`adjust_stop_loss` → 100500(成本價) |
| 267 | +4. 驗證:止損單已更新,即使回撤到 100500 也不會虧損 |
| 268 | + |
| 269 | +### 測試用例 2:部分止盈 |
| 270 | +1. 持倉 BTCUSDT 多單 0.1 BTC |
| 271 | +2. 價格到達第一目標 104300 |
| 272 | +3. AI 決策:`partial_close` 50% |
| 273 | +4. 驗證:平倉 0.05 BTC,剩餘 0.05 BTC 繼續持有 |
| 274 | + |
| 275 | +### 測試用例 3:錯誤處理 |
| 276 | +1. AI 決策:`adjust_stop_loss` 但持倉不存在 |
| 277 | +2. 驗證:記錄錯誤,不影響其他決策 |
| 278 | + |
| 279 | +--- |
| 280 | + |
| 281 | +## 📈 預期效果 |
| 282 | + |
| 283 | +### 優化前(當前狀態) |
| 284 | +- AI 只能在開倉時設定止盈止損 |
| 285 | +- 無法根據行情變化動態調整 |
| 286 | +- "追蹤止損"策略無法執行 ❌ |
| 287 | + |
| 288 | +### 優化後 |
| 289 | +- AI 可以根據浮盈動態移動止損 ✅ |
| 290 | +- 可以分批止盈(第一目標平 50%,第二目標平剩餘)✅ |
| 291 | +- 真正實現"讓利潤奔跑,限制虧損"✅ |
| 292 | +- 提升夏普比率(減少回撤,鎖定利潤)✅ |
| 293 | + |
| 294 | +--- |
| 295 | + |
| 296 | +## 🔗 相關文件 |
| 297 | + |
| 298 | +- `decision/engine.go` - Decision 結構定義 |
| 299 | +- `trader/auto_trader.go` - 交易執行邏輯 |
| 300 | +- `prompts/adaptive.txt` - 策略模板(提到追蹤止損) |
| 301 | +- `prompts/default.txt` - 基礎策略模板 |
| 302 | + |
| 303 | +--- |
| 304 | + |
| 305 | +## ⚠️ 風險提示 |
| 306 | + |
| 307 | +1. **交易所 API 支持**:需要確認 Binance/Hyperliquid 是否支持修改止損單 |
| 308 | +2. **訂單管理**:需要追蹤止損單的 orderID,才能修改 |
| 309 | +3. **錯誤處理**:如果修改失敗,需要回退或重試 |
| 310 | +4. **日誌記錄**:所有調整操作都應該記錄到 decision_logger |
| 311 | + |
| 312 | +--- |
| 313 | + |
| 314 | +**優先級**: 🔴 High - 這是實現追蹤止損策略的必要功能 |
| 315 | + |
| 316 | +**預估工作量**: |
| 317 | +- 修改 Decision 結構: 30 分鐘 |
| 318 | +- 實現執行邏輯: 2-3 小時 |
| 319 | +- 更新模板說明: 30 分鐘 |
| 320 | +- 測試驗證: 1-2 小時 |
| 321 | +- **總計**: 4-6 小時 |
| 322 | + |
| 323 | +--- |
| 324 | + |
| 325 | +**下一步**: 等待用戶確認方案後開始實施 |
0 commit comments