1
This commit is contained in:
parent
e0e89624d0
commit
2fe6a1602b
@ -2128,15 +2128,30 @@ class CryptoAgent:
|
|||||||
Returns:
|
Returns:
|
||||||
(positions, account, pending_orders)
|
(positions, account, pending_orders)
|
||||||
"""
|
"""
|
||||||
|
# 1. 余额(独立 try,确保余额始终可用)
|
||||||
try:
|
try:
|
||||||
bg_state = self.bitget.get_account_state()
|
bg_state = self.bitget.get_account_state()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取 Bitget 余额失败: {e}")
|
||||||
|
bg_state = {"account_value": 0, "total_margin_used": 0, "available_balance": 0}
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"[Bitget] 余额: account_value=${bg_state['account_value']:.2f}, "
|
||||||
|
f"available=${bg_state['available_balance']:.2f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 2. 持仓(独立 try)
|
||||||
position_list = []
|
position_list = []
|
||||||
|
try:
|
||||||
for pos in self.bitget.get_open_positions():
|
for pos in self.bitget.get_open_positions():
|
||||||
coin = pos["coin"]
|
coin = pos["coin"]
|
||||||
size = pos["size"]
|
size = pos["size"]
|
||||||
if size != 0:
|
if size != 0:
|
||||||
|
tp_sl = {}
|
||||||
|
try:
|
||||||
tp_sl = self.bitget.get_tp_sl_prices(coin)
|
tp_sl = self.bitget.get_tp_sl_prices(coin)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"获取 {coin} TP/SL 失败(不影响交易): {e}")
|
||||||
position_list.append({
|
position_list.append({
|
||||||
'symbol': f"{coin}USDT",
|
'symbol': f"{coin}USDT",
|
||||||
'side': 'buy' if size > 0 else 'sell',
|
'side': 'buy' if size > 0 else 'sell',
|
||||||
@ -2146,7 +2161,10 @@ class CryptoAgent:
|
|||||||
'stop_loss': tp_sl.get('stop_loss'),
|
'stop_loss': tp_sl.get('stop_loss'),
|
||||||
'take_profit': tp_sl.get('take_profit'),
|
'take_profit': tp_sl.get('take_profit'),
|
||||||
})
|
})
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取 Bitget 持仓失败: {e}")
|
||||||
|
|
||||||
|
# 3. 构建 account 字典
|
||||||
total_position_value = sum(
|
total_position_value = sum(
|
||||||
p['holding'] * p['entry_price'] for p in position_list
|
p['holding'] * p['entry_price'] for p in position_list
|
||||||
)
|
)
|
||||||
@ -2165,8 +2183,10 @@ class CryptoAgent:
|
|||||||
else:
|
else:
|
||||||
account['current_total_leverage'] = 0
|
account['current_total_leverage'] = 0
|
||||||
|
|
||||||
all_orders = self.bitget.get_open_orders()
|
# 4. 挂单(独立 try)
|
||||||
pending_orders = []
|
pending_orders = []
|
||||||
|
try:
|
||||||
|
all_orders = self.bitget.get_open_orders()
|
||||||
for order in all_orders:
|
for order in all_orders:
|
||||||
pending_orders.append({
|
pending_orders.append({
|
||||||
'order_id': order.get('order_id'),
|
'order_id': order.get('order_id'),
|
||||||
@ -2178,13 +2198,11 @@ class CryptoAgent:
|
|||||||
'is_reduce_only': order.get('is_reduce_only', False),
|
'is_reduce_only': order.get('is_reduce_only', False),
|
||||||
'created_at': order.get('created_at'),
|
'created_at': order.get('created_at'),
|
||||||
})
|
})
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取 Bitget 挂单失败: {e}")
|
||||||
|
|
||||||
return position_list, account, pending_orders
|
return position_list, account, pending_orders
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"获取 Bitget 状态失败: {e}")
|
|
||||||
return [], {}, []
|
|
||||||
|
|
||||||
def _calculate_position_size(self, signal: Dict[str, Any],
|
def _calculate_position_size(self, signal: Dict[str, Any],
|
||||||
account: Dict[str, Any],
|
account: Dict[str, Any],
|
||||||
platform_name: str) -> tuple:
|
platform_name: str) -> tuple:
|
||||||
@ -2377,14 +2395,14 @@ class CryptoAgent:
|
|||||||
if remaining_leverage <= 0:
|
if remaining_leverage <= 0:
|
||||||
return False, f"已达最大杠杆 {current_leverage:.1f}x/{max_leverage}x"
|
return False, f"已达最大杠杆 {current_leverage:.1f}x/{max_leverage}x"
|
||||||
|
|
||||||
# 2. 可用余额检查
|
# 2. 可用余额检查(仅警告,不阻止执行——由交易所做最终校验)
|
||||||
available = account.get('available', account.get('available_balance', 0))
|
available = account.get('available', account.get('available_balance', 0))
|
||||||
symbol = signal.get('symbol', '').replace('USDT', '').upper()
|
symbol = signal.get('symbol', '').replace('USDT', '').upper()
|
||||||
rules = self.PLATFORM_RULES.get(platform_name, {})
|
rules = self.PLATFORM_RULES.get(platform_name, {})
|
||||||
min_margin = rules.get('min_margin', {}).get(symbol, 10)
|
min_margin = rules.get('min_margin', {}).get(symbol, 10)
|
||||||
|
|
||||||
if available < min_margin:
|
if available > 0 and available < min_margin:
|
||||||
return False, f"可用余额 ${available:.2f} < 最小保证金 ${min_margin}"
|
logger.warning(f"[{platform_name}] 余额偏低 ${available:.2f} < ${min_margin},仍尝试执行")
|
||||||
|
|
||||||
# 3. 持仓数量限制(每个币种最多3个持仓+挂单)
|
# 3. 持仓数量限制(每个币种最多3个持仓+挂单)
|
||||||
symbol_orders = [o for o in positions + pending_orders if o.get('symbol') == signal.get('symbol')]
|
symbol_orders = [o for o in positions + pending_orders if o.get('symbol') == signal.get('symbol')]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user