a
This commit is contained in:
parent
d0d40b8f83
commit
bbc11b1bc9
|
|
@ -281,11 +281,19 @@ class MarketScanner:
|
||||||
# 使用标准WebSocket
|
# 使用标准WebSocket
|
||||||
try:
|
try:
|
||||||
if self.client.socket_manager:
|
if self.client.socket_manager:
|
||||||
async with self.client.socket_manager.futures_socket(symbol.lower()) as stream:
|
async with self.client.socket_manager.futures_ticker_socket(symbol.lower()) as stream:
|
||||||
async for msg in stream:
|
async for msg in stream:
|
||||||
if 'data' in msg:
|
try:
|
||||||
price = float(msg['data']['c']) # 最新价格
|
# futures_ticker_socket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...}
|
||||||
|
if 'c' in msg: # 'c' 是当前价格
|
||||||
|
price = float(msg['c'])
|
||||||
await callback(symbol, price)
|
await callback(symbol, price)
|
||||||
|
elif 'data' in msg and 'c' in msg['data']:
|
||||||
|
price = float(msg['data']['c'])
|
||||||
|
await callback(symbol, price)
|
||||||
|
except (KeyError, ValueError, TypeError) as e:
|
||||||
|
logger.debug(f"解析 {symbol} 价格数据失败: {e}")
|
||||||
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"监控 {symbol} 价格失败: {e}")
|
logger.error(f"监控 {symbol} 价格失败: {e}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -872,8 +872,8 @@ class PositionManager:
|
||||||
logger.info(f"{symbol} 持仓已不存在,停止监控")
|
logger.info(f"{symbol} 持仓已不存在,停止监控")
|
||||||
break
|
break
|
||||||
|
|
||||||
# 使用WebSocket订阅价格流
|
# 使用WebSocket订阅价格流(使用futures_ticker_socket订阅单个交易对)
|
||||||
async with self.client.socket_manager.futures_socket(symbol.lower()) as stream:
|
async with self.client.socket_manager.futures_ticker_socket(symbol.lower()) as stream:
|
||||||
logger.debug(f"{symbol} WebSocket连接已建立,开始接收价格更新")
|
logger.debug(f"{symbol} WebSocket连接已建立,开始接收价格更新")
|
||||||
retry_count = 0 # 连接成功,重置重试计数
|
retry_count = 0 # 连接成功,重置重试计数
|
||||||
|
|
||||||
|
|
@ -882,13 +882,18 @@ class PositionManager:
|
||||||
logger.info(f"{symbol} 持仓已不存在,停止监控")
|
logger.info(f"{symbol} 持仓已不存在,停止监控")
|
||||||
break
|
break
|
||||||
|
|
||||||
if 'data' in msg:
|
|
||||||
try:
|
try:
|
||||||
current_price = float(msg['data']['c']) # 最新价格
|
# futures_ticker_socket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...}
|
||||||
|
if 'c' in msg: # 'c' 是当前价格
|
||||||
|
current_price = float(msg['c'])
|
||||||
# 立即检查止损止盈
|
# 立即检查止损止盈
|
||||||
await self._check_single_position(symbol, current_price)
|
await self._check_single_position(symbol, current_price)
|
||||||
|
elif 'data' in msg and 'c' in msg['data']:
|
||||||
|
# 兼容其他可能的数据格式
|
||||||
|
current_price = float(msg['data']['c'])
|
||||||
|
await self._check_single_position(symbol, current_price)
|
||||||
except (KeyError, ValueError, TypeError) as e:
|
except (KeyError, ValueError, TypeError) as e:
|
||||||
logger.debug(f"{symbol} 解析价格数据失败: {e}")
|
logger.debug(f"{symbol} 解析价格数据失败: {e}, 消息: {msg}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user