update
This commit is contained in:
parent
40045b39f7
commit
7817632847
@ -2,6 +2,7 @@
|
||||
加密货币交易智能体 - 主控制器(LLM 驱动版)
|
||||
"""
|
||||
import asyncio
|
||||
import math
|
||||
from typing import Dict, Any, List, Optional
|
||||
from datetime import datetime, timedelta
|
||||
import pandas as pd
|
||||
@ -2073,6 +2074,18 @@ class CryptoAgent:
|
||||
# 根据当前价格计算数量
|
||||
size = max_position_usd / current_price
|
||||
|
||||
# 按 Hyperliquid 要求的精度截断(szDecimals)
|
||||
# ETH=3位, BTC=5位 等,必须截断而非四舍五入,避免超出允许仓位
|
||||
sz_decimals = self.hyperliquid.get_sz_decimals(decision.get('symbol', '').replace('USDT', ''))
|
||||
factor = 10 ** sz_decimals
|
||||
size = math.floor(size * factor) / factor # 截断,不四舍五入
|
||||
|
||||
# 最小下单量检查
|
||||
min_size = 1 / factor
|
||||
if size < min_size:
|
||||
logger.warning(f"⚠️ 计算仓位 {size} 低于最小下单量 {min_size},取消开仓")
|
||||
return 0
|
||||
|
||||
logger.info(f"💰 仓位计算:")
|
||||
logger.info(f" 账户价值: ${current_balance:.2f}")
|
||||
logger.info(f" 可用余额: ${available_balance:.2f}")
|
||||
@ -2080,10 +2093,9 @@ class CryptoAgent:
|
||||
logger.info(f" 当前总杠杆: {current_total_leverage:.2f}x")
|
||||
logger.info(f" 计划杠杆: {leverage}x")
|
||||
logger.info(f" 最大可开仓金额: ${max_position_usd:.2f} (限制: min(配置${max_by_config:.0f}, 可用${max_by_available:.0f}, 杠杆${max_by_total_leverage:.0f}))")
|
||||
logger.info(f" 计算数量: {size:.6f} @ ${current_price:.2f}")
|
||||
logger.info(f" 计算数量: {size} (精度: {sz_decimals}位) @ ${current_price:.2f}")
|
||||
|
||||
# 四舍五入到合理精度
|
||||
return round(size, 6)
|
||||
return size
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"计算仓位大小失败: {e}")
|
||||
|
||||
@ -537,6 +537,20 @@ class HyperliquidTradingService:
|
||||
return pos
|
||||
return None
|
||||
|
||||
def get_sz_decimals(self, symbol: str) -> int:
|
||||
"""
|
||||
获取交易对的数量精度(szDecimals)
|
||||
|
||||
Hyperliquid 要求订单数量必须符合各币种精度,否则报 'Order has invalid size'
|
||||
例如 ETH=3(最小 0.001),BTC=5(最小 0.00001)
|
||||
"""
|
||||
try:
|
||||
asset = self.info.name_to_asset(symbol)
|
||||
return self.info.asset_to_sz_decimals.get(asset, 3)
|
||||
except Exception:
|
||||
logger.warning(f"获取 {symbol} szDecimals 失败,使用默认值 3")
|
||||
return 3
|
||||
|
||||
|
||||
# 单例
|
||||
_hyperliquid_service_instance = None
|
||||
|
||||
Loading…
Reference in New Issue
Block a user