Commit Graph

6 Commits

Author SHA1 Message Date
ERIC LEUNG
5328166018 fix: 删除多定义的方法 (#528) 2025-11-05 20:11:50 +08:00
Icyoung
c0cb37252b Merge pull request #434 from zhouyongyou/fix/stop-loss-take-profit-separation
fix(trader): separate stop-loss and take-profit order cancellation to prevent accidental deletions
2025-11-05 16:05:00 +08:00
ZhouYongyou
c9d5aed1b6 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
```
2025-11-04 19:05:54 +08:00
ZhouYongyou
9884605c75 修復關鍵缺陷:添加 CancelStopOrders 方法避免多個止損單共存
問題:
- 調整止損/止盈時,直接調用 SetStopLoss/SetTakeProfit 會創建新訂單
- 但舊的止損/止盈單仍然存在,導致多個訂單共存
- 可能造成意外觸發或訂單衝突
解決方案(參考 PR #197):
1. 在 Trader 接口添加 CancelStopOrders 方法
2. 為三個交易所實現:
   - binance_futures.go: 過濾 STOP_MARKET/TAKE_PROFIT_MARKET 類型
   - aster_trader.go: 同樣邏輯
   - hyperliquid_trader.go: 過濾 trigger 訂單(有 triggerPx)
3. 在 executeUpdateStopLossWithRecord 和 executeUpdateTakeProfitWithRecord 中:
   - 先調用 CancelStopOrders 取消舊單
   - 然後設置新止損/止盈
   - 取消失敗不中斷執行(記錄警告)
優勢:
-  避免多個止損單同時存在
-  保留我們的價格驗證邏輯
-  保留執行價格記錄
-  詳細錯誤信息
-  取消失敗時繼續執行(更健壯)
測試建議:
- 開倉後調整止損,檢查舊止損單是否被取消
- 連續調整兩次,確認只有最新止損單存在
致謝:參考 PR #197 的實現思路
2025-11-04 16:40:23 +08:00
icy
142863f554 Add MarginMode configration 2025-10-31 13:14:24 +08:00
nobody
0164ac6cc0 feat: Add Hyperliquid exchange support with unified trader interface
Major changes:
- Add full Hyperliquid trading support (long/short, leverage, SL/TP)
- Create unified Trader interface for multi-exchange support
- Implement automatic precision handling for orders and prices
- Fix balance calculation and unrealized P&L display
- Add comprehensive configuration guide in README
New features:
- Support for both Binance and Hyperliquid exchanges
- Automatic order size precision based on szDecimals
- Price formatting with 5 significant figures
- Non-custodial trading with Ethereum private key
- Seamless exchange switching via configuration
Technical details:
- Add trader/interface.go for unified trader interface
- Add trader/hyperliquid_trader.go for Hyperliquid implementation
- Update manager and auto_trader to support multiple exchanges
- Add go-hyperliquid SDK dependency
- Fix precision errors (float_to_wire, invalid price)
Fixes:
- Correct calculation of wallet balance and unrealized P&L
- Proper handling of AccountValue vs TotalRawUsd
- Frontend display issues for total equity and P&L
Documentation:
- Add Hyperliquid setup guide in README
- Update config.json.example with both exchanges
- Add troubleshooting section for common errors
Tested with live trading on Hyperliquid mainnet.
No breaking changes - backward compatible with existing configs.
2025-10-29 20:00:30 +08:00