diff --git a/backend/app/crypto_agent/executor/hyperliquid_executor.py b/backend/app/crypto_agent/executor/hyperliquid_executor.py index 9b14f7c..9275c9e 100644 --- a/backend/app/crypto_agent/executor/hyperliquid_executor.py +++ b/backend/app/crypto_agent/executor/hyperliquid_executor.py @@ -22,7 +22,6 @@ class HyperliquidExecutor(BaseExecutor): try: symbol = decision.get('symbol', '').replace('USDT', '') action = decision.get('signal_action', decision.get('action')) # buy/sell - margin = decision.get('margin', decision.get('quantity', 0)) entry_price = decision.get('entry_price', current_price) stop_loss = decision.get('stop_loss') take_profit = decision.get('take_profit') @@ -31,13 +30,20 @@ class HyperliquidExecutor(BaseExecutor): order_type, order_reason = self.decide_order_type(decision, current_price) logger.info(f" 订单类型: {order_reason}") - # 调整保证金(预留手续费) + # 获取账户状态 account_state = self.hyperliquid.get_account_state() available = account_state.get('available_balance', 0) - adjusted_margin = self.calculate_effective_margin(available, margin) + account_value = account_state.get('account_value', 0) + + # 仓位价值 = 1x 账户价值,所需保证金 = 账户价值 / 杠杆 + leverage = min(decision.get('leverage', 10), 10) + target_position_value = account_value # 1倍账户价值 + margin_needed = target_position_value / leverage if leverage > 0 else 0 + + # 预留手续费并限制在可用余额内 + adjusted_margin = self.calculate_effective_margin(available, margin_needed) # 计算仓位大小 - leverage = min(decision.get('leverage', 10), 10) position_size = self._calculate_position_size(symbol, adjusted_margin, entry_price, leverage) if position_size <= 0: @@ -78,7 +84,27 @@ class HyperliquidExecutor(BaseExecutor): logger.info(f" ✅ 开仓成功: {symbol} {position_size} @ ${order_type}") - # 发送飞书通知 + # 成交后设置止盈止损(Hyperliquid 不支持下单时设置 TP/SL) + # 必须在飞书通知之前设置,避免通知异常导致止盈止损跳过 + if stop_loss or take_profit: + try: + tp_sl_result = self.hyperliquid.set_tp_sl( + symbol=symbol, + is_long=is_buy, + size=position_size, + tp_price=take_profit, + sl_price=stop_loss + ) + if not tp_sl_result.get('success'): + logger.warning(f" ⚠️ 止盈止损设置失败: {tp_sl_result.get('error', tp_sl_result.get('message'))}") + result['tp_sl_warning'] = tp_sl_result.get('error', tp_sl_result.get('message')) + else: + logger.info(f" ✅ 止盈止损已设置: TP={take_profit}, SL={stop_loss}") + except Exception as tp_sl_err: + logger.error(f" ⚠️ 止盈止损设置异常: {tp_sl_err}") + result['tp_sl_warning'] = str(tp_sl_err) + + # 发送飞书通知(在止盈止损之后,通知失败不影响交易结果) await self.send_execution_notification( operation='OPEN', symbol=symbol, @@ -94,22 +120,6 @@ class HyperliquidExecutor(BaseExecutor): } ) - # 成交后设置止盈止损(Hyperliquid 不支持下单时设置 TP/SL) - # 市价单已成交直接设置;限价单(resting)也先设置,等成交后生效 - if stop_loss or take_profit: - tp_sl_result = self.hyperliquid.set_tp_sl( - symbol=symbol, - is_long=is_buy, - size=position_size, - tp_price=take_profit, - sl_price=stop_loss - ) - if not tp_sl_result.get('success'): - logger.warning(f" ⚠️ 止盈止损设置失败: {tp_sl_result.get('error', tp_sl_result.get('message'))}") - result['tp_sl_warning'] = tp_sl_result.get('error', tp_sl_result.get('message')) - else: - logger.info(f" ✅ 止盈止损已设置: TP={take_profit}, SL={stop_loss}") - return result except Exception as e: