|
| 1 | +package trader |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "nofx/decision" |
| 6 | + "nofx/logger" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// MockTrader 用於測試 partial close 邏輯 |
| 11 | +type MockTrader struct { |
| 12 | + positions []map[string]interface{} |
| 13 | + closePartialCalled bool |
| 14 | + closeLongCalled bool |
| 15 | + closeShortCalled bool |
| 16 | + stopLossCalled bool |
| 17 | + takeProfitCalled bool |
| 18 | + lastStopLoss float64 |
| 19 | + lastTakeProfit float64 |
| 20 | +} |
| 21 | + |
| 22 | +func (m *MockTrader) GetPositions() ([]map[string]interface{}, error) { |
| 23 | + return m.positions, nil |
| 24 | +} |
| 25 | + |
| 26 | +func (m *MockTrader) ClosePartialLong(symbol string, quantity float64) (map[string]interface{}, error) { |
| 27 | + m.closePartialCalled = true |
| 28 | + return map[string]interface{}{"orderId": "12345"}, nil |
| 29 | +} |
| 30 | + |
| 31 | +func (m *MockTrader) ClosePartialShort(symbol string, quantity float64) (map[string]interface{}, error) { |
| 32 | + m.closePartialCalled = true |
| 33 | + return map[string]interface{}{"orderId": "12345"}, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (m *MockTrader) CloseLong(symbol string, quantity float64) (map[string]interface{}, error) { |
| 37 | + m.closeLongCalled = true |
| 38 | + return map[string]interface{}{"orderId": "12346"}, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (m *MockTrader) CloseShort(symbol string, quantity float64) (map[string]interface{}, error) { |
| 42 | + m.closeShortCalled = true |
| 43 | + return map[string]interface{}{"orderId": "12346"}, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (m *MockTrader) SetStopLoss(symbol, side string, quantity, price float64) error { |
| 47 | + m.stopLossCalled = true |
| 48 | + m.lastStopLoss = price |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (m *MockTrader) SetTakeProfit(symbol, side string, quantity, price float64) error { |
| 53 | + m.takeProfitCalled = true |
| 54 | + m.lastTakeProfit = price |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// TestPartialCloseMinPositionCheck 測試最小倉位檢查邏輯 |
| 59 | +func TestPartialCloseMinPositionCheck(t *testing.T) { |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + totalQuantity float64 |
| 63 | + markPrice float64 |
| 64 | + closePercentage float64 |
| 65 | + expectFullClose bool // 是否應該觸發全平邏輯 |
| 66 | + expectRemainValue float64 |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "正常部分平倉_剩餘價值充足", |
| 70 | + totalQuantity: 1.0, |
| 71 | + markPrice: 100.0, |
| 72 | + closePercentage: 50.0, |
| 73 | + expectFullClose: false, |
| 74 | + expectRemainValue: 50.0, // 剩餘 0.5 * 100 = 50 USDT |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "部分平倉_剩餘價值小於10USDT_應該全平", |
| 78 | + totalQuantity: 0.2, |
| 79 | + markPrice: 100.0, |
| 80 | + closePercentage: 95.0, // 平倉 95%,剩餘 1 USDT (0.2 * 5% * 100) |
| 81 | + expectFullClose: true, |
| 82 | + expectRemainValue: 1.0, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "部分平倉_剩餘價值剛好10USDT_應該全平", |
| 86 | + totalQuantity: 1.0, |
| 87 | + markPrice: 100.0, |
| 88 | + closePercentage: 90.0, // 剩餘 10 USDT (1.0 * 10% * 100),邊界測試 (<=) |
| 89 | + expectFullClose: true, |
| 90 | + expectRemainValue: 10.0, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "部分平倉_剩餘價值11USDT_不應全平", |
| 94 | + totalQuantity: 1.1, |
| 95 | + markPrice: 100.0, |
| 96 | + closePercentage: 90.0, // 剩餘 11 USDT (1.1 * 10% * 100) |
| 97 | + expectFullClose: false, |
| 98 | + expectRemainValue: 11.0, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "大倉位部分平倉_剩餘價值遠大於10USDT", |
| 102 | + totalQuantity: 10.0, |
| 103 | + markPrice: 1000.0, |
| 104 | + closePercentage: 80.0, |
| 105 | + expectFullClose: false, |
| 106 | + expectRemainValue: 2000.0, // 剩餘 2 * 1000 = 2000 USDT |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + for _, tt := range tests { |
| 111 | + t.Run(tt.name, func(t *testing.T) { |
| 112 | + // 計算剩餘價值 |
| 113 | + closeQuantity := tt.totalQuantity * (tt.closePercentage / 100.0) |
| 114 | + remainingQuantity := tt.totalQuantity - closeQuantity |
| 115 | + remainingValue := remainingQuantity * tt.markPrice |
| 116 | + |
| 117 | + // 驗證計算(使用浮點數比較允許微小誤差) |
| 118 | + const epsilon = 0.001 |
| 119 | + if remainingValue-tt.expectRemainValue > epsilon || tt.expectRemainValue-remainingValue > epsilon { |
| 120 | + t.Errorf("計算錯誤: 剩餘價值 = %.2f, 期望 = %.2f", |
| 121 | + remainingValue, tt.expectRemainValue) |
| 122 | + } |
| 123 | + |
| 124 | + // 驗證最小倉位檢查邏輯 |
| 125 | + const MIN_POSITION_VALUE = 10.0 |
| 126 | + shouldFullClose := remainingValue > 0 && remainingValue <= MIN_POSITION_VALUE |
| 127 | + |
| 128 | + if shouldFullClose != tt.expectFullClose { |
| 129 | + t.Errorf("最小倉位檢查失敗: shouldFullClose = %v, 期望 = %v (剩餘價值 = %.2f USDT)", |
| 130 | + shouldFullClose, tt.expectFullClose, remainingValue) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// TestPartialCloseWithStopLossTakeProfitRecovery 測試止盈止損恢復邏輯 |
| 137 | +func TestPartialCloseWithStopLossTakeProfitRecovery(t *testing.T) { |
| 138 | + tests := []struct { |
| 139 | + name string |
| 140 | + newStopLoss float64 |
| 141 | + newTakeProfit float64 |
| 142 | + expectStopLoss bool |
| 143 | + expectTakeProfit bool |
| 144 | + }{ |
| 145 | + { |
| 146 | + name: "有新止損和止盈_應該恢復兩者", |
| 147 | + newStopLoss: 95.0, |
| 148 | + newTakeProfit: 110.0, |
| 149 | + expectStopLoss: true, |
| 150 | + expectTakeProfit: true, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "只有新止損_僅恢復止損", |
| 154 | + newStopLoss: 95.0, |
| 155 | + newTakeProfit: 0, |
| 156 | + expectStopLoss: true, |
| 157 | + expectTakeProfit: false, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "只有新止盈_僅恢復止盈", |
| 161 | + newStopLoss: 0, |
| 162 | + newTakeProfit: 110.0, |
| 163 | + expectStopLoss: false, |
| 164 | + expectTakeProfit: true, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "沒有新止損止盈_不恢復", |
| 168 | + newStopLoss: 0, |
| 169 | + newTakeProfit: 0, |
| 170 | + expectStopLoss: false, |
| 171 | + expectTakeProfit: false, |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + for _, tt := range tests { |
| 176 | + t.Run(tt.name, func(t *testing.T) { |
| 177 | + // 模擬止盈止損恢復邏輯 |
| 178 | + stopLossRecovered := tt.newStopLoss > 0 |
| 179 | + takeProfitRecovered := tt.newTakeProfit > 0 |
| 180 | + |
| 181 | + if stopLossRecovered != tt.expectStopLoss { |
| 182 | + t.Errorf("止損恢復邏輯錯誤: recovered = %v, 期望 = %v", |
| 183 | + stopLossRecovered, tt.expectStopLoss) |
| 184 | + } |
| 185 | + |
| 186 | + if takeProfitRecovered != tt.expectTakeProfit { |
| 187 | + t.Errorf("止盈恢復邏輯錯誤: recovered = %v, 期望 = %v", |
| 188 | + takeProfitRecovered, tt.expectTakeProfit) |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// TestPartialCloseEdgeCases 測試邊界情況 |
| 195 | +func TestPartialCloseEdgeCases(t *testing.T) { |
| 196 | + tests := []struct { |
| 197 | + name string |
| 198 | + closePercentage float64 |
| 199 | + totalQuantity float64 |
| 200 | + markPrice float64 |
| 201 | + expectError bool |
| 202 | + errorContains string |
| 203 | + }{ |
| 204 | + { |
| 205 | + name: "平倉百分比為0_應該報錯", |
| 206 | + closePercentage: 0, |
| 207 | + totalQuantity: 1.0, |
| 208 | + markPrice: 100.0, |
| 209 | + expectError: true, |
| 210 | + errorContains: "0-100", |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "平倉百分比超過100_應該報錯", |
| 214 | + closePercentage: 101.0, |
| 215 | + totalQuantity: 1.0, |
| 216 | + markPrice: 100.0, |
| 217 | + expectError: true, |
| 218 | + errorContains: "0-100", |
| 219 | + }, |
| 220 | + { |
| 221 | + name: "平倉百分比為負數_應該報錯", |
| 222 | + closePercentage: -10.0, |
| 223 | + totalQuantity: 1.0, |
| 224 | + markPrice: 100.0, |
| 225 | + expectError: true, |
| 226 | + errorContains: "0-100", |
| 227 | + }, |
| 228 | + { |
| 229 | + name: "正常範圍_不應報錯", |
| 230 | + closePercentage: 50.0, |
| 231 | + totalQuantity: 1.0, |
| 232 | + markPrice: 100.0, |
| 233 | + expectError: false, |
| 234 | + }, |
| 235 | + } |
| 236 | + |
| 237 | + for _, tt := range tests { |
| 238 | + t.Run(tt.name, func(t *testing.T) { |
| 239 | + // 模擬百分比驗證邏輯 |
| 240 | + var err error |
| 241 | + if tt.closePercentage <= 0 || tt.closePercentage > 100 { |
| 242 | + err = fmt.Errorf("平仓百分比必须在 0-100 之间,当前: %.1f", tt.closePercentage) |
| 243 | + } |
| 244 | + |
| 245 | + if tt.expectError { |
| 246 | + if err == nil { |
| 247 | + t.Errorf("期望報錯但沒有報錯") |
| 248 | + } |
| 249 | + } else { |
| 250 | + if err != nil { |
| 251 | + t.Errorf("不應報錯但報錯了: %v", err) |
| 252 | + } |
| 253 | + } |
| 254 | + }) |
| 255 | + } |
| 256 | +} |
| 257 | + |
| 258 | +// TestPartialCloseIntegration 整合測試(使用 mock trader) |
| 259 | +func TestPartialCloseIntegration(t *testing.T) { |
| 260 | + tests := []struct { |
| 261 | + name string |
| 262 | + symbol string |
| 263 | + side string |
| 264 | + totalQuantity float64 |
| 265 | + markPrice float64 |
| 266 | + closePercentage float64 |
| 267 | + newStopLoss float64 |
| 268 | + newTakeProfit float64 |
| 269 | + expectFullClose bool |
| 270 | + expectStopLossCall bool |
| 271 | + expectTakeProfitCall bool |
| 272 | + }{ |
| 273 | + { |
| 274 | + name: "LONG倉_正常部分平倉_有止盈止損", |
| 275 | + symbol: "BTCUSDT", |
| 276 | + side: "LONG", |
| 277 | + totalQuantity: 1.0, |
| 278 | + markPrice: 50000.0, |
| 279 | + closePercentage: 50.0, |
| 280 | + newStopLoss: 48000.0, |
| 281 | + newTakeProfit: 52000.0, |
| 282 | + expectFullClose: false, |
| 283 | + expectStopLossCall: true, |
| 284 | + expectTakeProfitCall: true, |
| 285 | + }, |
| 286 | + { |
| 287 | + name: "SHORT倉_剩餘價值過小_應自動全平", |
| 288 | + symbol: "ETHUSDT", |
| 289 | + side: "SHORT", |
| 290 | + totalQuantity: 0.02, |
| 291 | + markPrice: 3000.0, // 總價值 60 USDT |
| 292 | + closePercentage: 95.0, // 剩餘 3 USDT < 10 USDT |
| 293 | + newStopLoss: 3100.0, |
| 294 | + newTakeProfit: 2900.0, |
| 295 | + expectFullClose: true, |
| 296 | + expectStopLossCall: false, // 全平不需要恢復止盈止損 |
| 297 | + expectTakeProfitCall: false, |
| 298 | + }, |
| 299 | + } |
| 300 | + |
| 301 | + for _, tt := range tests { |
| 302 | + t.Run(tt.name, func(t *testing.T) { |
| 303 | + // 創建 mock trader |
| 304 | + mockTrader := &MockTrader{ |
| 305 | + positions: []map[string]interface{}{ |
| 306 | + { |
| 307 | + "symbol": tt.symbol, |
| 308 | + "side": tt.side, |
| 309 | + "quantity": tt.totalQuantity, |
| 310 | + "markPrice": tt.markPrice, |
| 311 | + }, |
| 312 | + }, |
| 313 | + } |
| 314 | + |
| 315 | + // 創建決策 |
| 316 | + dec := &decision.Decision{ |
| 317 | + Symbol: tt.symbol, |
| 318 | + Action: "partial_close", |
| 319 | + ClosePercentage: tt.closePercentage, |
| 320 | + NewStopLoss: tt.newStopLoss, |
| 321 | + NewTakeProfit: tt.newTakeProfit, |
| 322 | + } |
| 323 | + |
| 324 | + // 創建 actionRecord |
| 325 | + actionRecord := &logger.DecisionAction{} |
| 326 | + |
| 327 | + // 計算剩餘價值 |
| 328 | + closeQuantity := tt.totalQuantity * (tt.closePercentage / 100.0) |
| 329 | + remainingQuantity := tt.totalQuantity - closeQuantity |
| 330 | + remainingValue := remainingQuantity * tt.markPrice |
| 331 | + |
| 332 | + // 驗證最小倉位檢查 |
| 333 | + const MIN_POSITION_VALUE = 10.0 |
| 334 | + shouldFullClose := remainingValue > 0 && remainingValue <= MIN_POSITION_VALUE |
| 335 | + |
| 336 | + if shouldFullClose != tt.expectFullClose { |
| 337 | + t.Errorf("最小倉位檢查不符: shouldFullClose = %v, 期望 = %v (剩餘 %.2f USDT)", |
| 338 | + shouldFullClose, tt.expectFullClose, remainingValue) |
| 339 | + } |
| 340 | + |
| 341 | + // 模擬執行邏輯 |
| 342 | + if shouldFullClose { |
| 343 | + // 應該轉為全平 |
| 344 | + if tt.side == "LONG" { |
| 345 | + mockTrader.CloseLong(tt.symbol, tt.totalQuantity) |
| 346 | + } else { |
| 347 | + mockTrader.CloseShort(tt.symbol, tt.totalQuantity) |
| 348 | + } |
| 349 | + } else { |
| 350 | + // 正常部分平倉 |
| 351 | + if tt.side == "LONG" { |
| 352 | + mockTrader.ClosePartialLong(tt.symbol, closeQuantity) |
| 353 | + } else { |
| 354 | + mockTrader.ClosePartialShort(tt.symbol, closeQuantity) |
| 355 | + } |
| 356 | + |
| 357 | + // 恢復止盈止損 |
| 358 | + if dec.NewStopLoss > 0 { |
| 359 | + mockTrader.SetStopLoss(tt.symbol, tt.side, remainingQuantity, dec.NewStopLoss) |
| 360 | + } |
| 361 | + if dec.NewTakeProfit > 0 { |
| 362 | + mockTrader.SetTakeProfit(tt.symbol, tt.side, remainingQuantity, dec.NewTakeProfit) |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + // 驗證調用 |
| 367 | + if tt.expectFullClose { |
| 368 | + if !mockTrader.closeLongCalled && !mockTrader.closeShortCalled { |
| 369 | + t.Error("期望調用全平但沒有調用") |
| 370 | + } |
| 371 | + if mockTrader.closePartialCalled { |
| 372 | + t.Error("不應該調用部分平倉") |
| 373 | + } |
| 374 | + } else { |
| 375 | + if !mockTrader.closePartialCalled { |
| 376 | + t.Error("期望調用部分平倉但沒有調用") |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + if mockTrader.stopLossCalled != tt.expectStopLossCall { |
| 381 | + t.Errorf("止損調用不符: called = %v, 期望 = %v", |
| 382 | + mockTrader.stopLossCalled, tt.expectStopLossCall) |
| 383 | + } |
| 384 | + |
| 385 | + if mockTrader.takeProfitCalled != tt.expectTakeProfitCall { |
| 386 | + t.Errorf("止盈調用不符: called = %v, 期望 = %v", |
| 387 | + mockTrader.takeProfitCalled, tt.expectTakeProfitCall) |
| 388 | + } |
| 389 | + |
| 390 | + _ = actionRecord // 避免未使用警告 |
| 391 | + }) |
| 392 | + } |
| 393 | +} |
0 commit comments