stock-ai-agent/backend/HYPERLIQUID_REVIEW.md
aaron a22dfe459c feat: Add Hyperliquid trading integration with ClawFi
- Add hyperliquid_trading_service.py with position management and TP/SL
- Implement dual-track trading (paper trading + Hyperliquid)
- Add position size calculation based on available margin
- Support net position mode (Hyperliquid) vs order mode (paper)
- Add risk controls: 10% circuit breaker, 10x max leverage
- Add test script for Hyperliquid SDK validation
2026-03-22 11:42:25 +08:00

101 lines
3.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Hyperliquid 集成代码 Review
## 核心差异总结
### 1. 仓位模式
- **Hyperliquid**: 净持仓模式Position Netting- 同币种订单自动合并
- **模拟盘**: 订单模式Order-based- 每个订单独立
### 2. 止盈止损
- **Hyperliquid**: 独立订单开仓后单独设置reduce_only=True
- **模拟盘**: 订单属性(创建时设置)
### 3. 需要修正的问题
#### 问题 1: `_get_hyperliquid_trading_state()` 需要查询止盈止损订单
```python
def _get_hyperliquid_trading_state(self) -> tuple:
# 需要额外查询挂单,找出 reduce_only 的止盈止损订单
# 并关联到对应的持仓
```
#### 问题 2: `_execute_hyperliquid_trade()` 需要设置止盈止损
```python
async def _execute_hyperliquid_trade(...):
# 1. 开仓
result = self.hyperliquid.place_market_order(...)
# 2. 立即设置止盈止损(新增)
if result.get('success'):
await self._set_hyperliquid_tp_sl(decision)
```
#### 问题 3: 加仓需要重新计算止盈止损
```python
# 加仓时:
# 1. 取消旧的止盈止损订单
# 2. 执行加仓
# 3. 根据新的平均入场价重新设置止盈止损
```
#### 问题 4: 平仓需要先取消止盈止损订单
```python
async def _execute_hyperliquid_close(...):
# 1. 取消该币种的所有止盈止损订单(新增)
# 2. 市价平仓
```
#### 问题 5: 不支持同时多空
```python
# Hyperliquid 同一币种只能有一个方向的净持仓
# 如果决策是反向开仓,会自动平掉现有持仓并反向
# 需要在决策器中考虑这个限制
```
## 修正方案
### 新增方法到 `hyperliquid_trading_service.py`
```python
def get_open_orders(self, symbol: Optional[str] = None) -> List[Dict[str, Any]]:
"""获取挂单(包括止盈止损订单)"""
def get_tp_sl_orders(self, symbol: str) -> Dict[str, Optional[float]]:
"""获取指定币种的止盈止损价格"""
# 返回 {'take_profit': price, 'stop_loss': price}
def set_tp_sl(self, symbol: str, is_long: bool, size: float,
tp_price: Optional[float], sl_price: Optional[float]):
"""设置止盈止损"""
def cancel_tp_sl_orders(self, symbol: str):
"""取消指定币种的所有止盈止损订单"""
```
### 修改 `crypto_agent.py`
```python
async def _execute_hyperliquid_trade(...):
# 1. 检查是否有反向持仓Hyperliquid 会自动平仓)
# 2. 执行开仓
# 3. 设置止盈止损
# 4. 如果是加仓,需要重新计算止盈止损
```
## 决策器需要考虑的差异
1. **加仓决策**: Hyperliquid 会合并仓位,入场价变成加权平均
2. **反向开仓**: Hyperliquid 会自动平掉现有持仓
3. **止盈止损调整**: 加仓后需要重新设置止盈止损
## 建议
1. **先实现基础功能**: 开仓 + 止盈止损 + 平仓
2. **再实现高级功能**: 加仓、减仓、调整止盈止损
3. **测试验证**: 在测试网充分测试后再启用实盘
4. **风控优先**: 确保 10% 熔断和杠杆限制正确工作
## Sources
- [Bybit Copy Trading Settlement Guide](https://www.bybit.nl/en/help-center/article/A-Comprehensive-Guide-to-Copy-Trading-Settlement)
- [Hyperliquid Fees and Margin Guide](https://publish0x.com/toxi-trading-bot-short-review/how-to-trade-perpetuals-on-hyperliquid-fees-margin-liquidati-xrplyqn)