mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-09 22:10:57 +08:00
fix(trader): separate stop-loss and take-profit order cancellation to prevent accidental deletions
## Problem When adjusting stop-loss or take-profit levels, `CancelStopOrders()` deleted BOTH stop-loss AND take-profit orders simultaneously, causing: - **Adjusting stop-loss** → Take-profit order deleted → Position has no exit plan ❌ - **Adjusting take-profit** → Stop-loss order deleted → Position unprotected ❌ **Root cause:** ```go CancelStopOrders(symbol) { // Cancelled ALL orders with type STOP_MARKET or TAKE_PROFIT_MARKET // No distinction between stop-loss and take-profit } ``` ## Solution ### 1. Added new interface methods (trader/interface.go) ```go CancelStopLossOrders(symbol string) error // Only cancel stop-loss orders CancelTakeProfitOrders(symbol string) error // Only cancel take-profit orders CancelStopOrders(symbol string) error // Deprecated (cancels both) ``` ### 2. Implemented for all 3 exchanges **Binance (trader/binance_futures.go)**: - `CancelStopLossOrders`: Filters `OrderTypeStopMarket | OrderTypeStop` - `CancelTakeProfitOrders`: Filters `OrderTypeTakeProfitMarket | OrderTypeTakeProfit` - Full order type differentiation ✅ **Hyperliquid (trader/hyperliquid_trader.go)**: - ⚠️ Limitation: SDK's OpenOrder struct doesn't expose trigger field - Both methods call `CancelStopOrders` (cancels all pending orders) - Trade-off: Safe but less precise **Aster (trader/aster_trader.go)**: - `CancelStopLossOrders`: Filters `STOP_MARKET | STOP` - `CancelTakeProfitOrders`: Filters `TAKE_PROFIT_MARKET | TAKE_PROFIT` - Full order type differentiation ✅ ### 3. Usage in auto_trader.go When `update_stop_loss` or `update_take_profit` actions are implemented, they will use: ```go // update_stop_loss: at.trader.CancelStopLossOrders(symbol) // Only cancel SL, keep TP at.trader.SetStopLoss(...) // update_take_profit: at.trader.CancelTakeProfitOrders(symbol) // Only cancel TP, keep SL at.trader.SetTakeProfit(...) ``` ## Impact - ✅ Adjusting stop-loss no longer deletes take-profit - ✅ Adjusting take-profit no longer deletes stop-loss - ✅ Backward compatible: `CancelStopOrders` still exists (deprecated) - ⚠️ Hyperliquid limitation: still cancels all orders (SDK constraint) ## Testing - ✅ Compiles successfully across all 3 exchanges - ⚠️ Requires live testing: - [ ] Binance: Adjust SL → verify TP remains - [ ] Binance: Adjust TP → verify SL remains - [ ] Hyperliquid: Verify behavior with limitation - [ ] Aster: Verify order filtering works correctly ## Code Changes ``` trader/interface.go: +9 lines (new interface methods) trader/binance_futures.go: +133 lines (3 new functions) trader/hyperliquid_trader.go: +56 lines (3 new functions) trader/aster_trader.go: +157 lines (3 new functions) Total: +355 lines ```
This commit is contained in:
@@ -36,6 +36,16 @@ type Trader interface {
|
||||
// SetTakeProfit 设置止盈单
|
||||
SetTakeProfit(symbol string, positionSide string, quantity, takeProfitPrice float64) error
|
||||
|
||||
// CancelStopOrders 取消该币种的止盈/止损单(已废弃:会同时删除止损和止盈)
|
||||
// 请使用 CancelStopLossOrders 或 CancelTakeProfitOrders
|
||||
CancelStopOrders(symbol string) error
|
||||
|
||||
// CancelStopLossOrders 仅取消止损单(修复 BUG:调整止损时不删除止盈)
|
||||
CancelStopLossOrders(symbol string) error
|
||||
|
||||
// CancelTakeProfitOrders 仅取消止盈单(修复 BUG:调整止盈时不删除止损)
|
||||
CancelTakeProfitOrders(symbol string) error
|
||||
|
||||
// CancelAllOrders 取消该币种的所有挂单
|
||||
CancelAllOrders(symbol string) error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user