1
This commit is contained in:
parent
9cd39c3655
commit
f6f4ca11ae
|
|
@ -854,13 +854,9 @@ class PositionManager:
|
||||||
position_info = self.active_positions[symbol]
|
position_info = self.active_positions[symbol]
|
||||||
trade_id = position_info.get('tradeId')
|
trade_id = position_info.get('tradeId')
|
||||||
if trade_id:
|
if trade_id:
|
||||||
try:
|
|
||||||
logger.info(f"{symbol} [平仓] 更新数据库状态为已平仓 (ID: {trade_id})...")
|
logger.info(f"{symbol} [平仓] 更新数据库状态为已平仓 (ID: {trade_id})...")
|
||||||
# 获取当前价格作为平仓价格
|
|
||||||
ticker = await self.client.get_ticker_24h(symbol)
|
ticker = await self.client.get_ticker_24h(symbol)
|
||||||
exit_price = float(ticker['price']) if ticker else float(position_info['entryPrice'])
|
exit_price = float(ticker['price']) if ticker else float(position_info['entryPrice'])
|
||||||
|
|
||||||
# 确保所有值都是float类型
|
|
||||||
entry_price = float(position_info['entryPrice'])
|
entry_price = float(position_info['entryPrice'])
|
||||||
quantity = float(position_info['quantity'])
|
quantity = float(position_info['quantity'])
|
||||||
if position_info['side'] == 'BUY':
|
if position_info['side'] == 'BUY':
|
||||||
|
|
@ -869,9 +865,6 @@ class PositionManager:
|
||||||
else:
|
else:
|
||||||
pnl = (entry_price - exit_price) * quantity
|
pnl = (entry_price - exit_price) * quantity
|
||||||
pnl_percent = ((entry_price - exit_price) / entry_price) * 100
|
pnl_percent = ((entry_price - exit_price) / entry_price) * 100
|
||||||
|
|
||||||
# 同步平仓时没有订单号,设为None
|
|
||||||
# 计算持仓持续时间和策略类型
|
|
||||||
entry_time = position_info.get('entryTime')
|
entry_time = position_info.get('entryTime')
|
||||||
duration_minutes = None
|
duration_minutes = None
|
||||||
if entry_time:
|
if entry_time:
|
||||||
|
|
@ -880,15 +873,12 @@ class PositionManager:
|
||||||
entry_dt = datetime.strptime(entry_time, '%Y-%m-%d %H:%M:%S')
|
entry_dt = datetime.strptime(entry_time, '%Y-%m-%d %H:%M:%S')
|
||||||
else:
|
else:
|
||||||
entry_dt = entry_time
|
entry_dt = entry_time
|
||||||
exit_dt = get_beijing_time() # 使用北京时间计算持续时间
|
exit_dt = get_beijing_time()
|
||||||
duration = exit_dt - entry_dt
|
duration = exit_dt - entry_dt
|
||||||
duration_minutes = int(duration.total_seconds() / 60)
|
duration_minutes = int(duration.total_seconds() / 60)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug(f"计算持仓持续时间失败: {e}")
|
logger.debug(f"计算持仓持续时间失败: {e}")
|
||||||
|
|
||||||
strategy_type = position_info.get('strategyType', 'trend_following')
|
strategy_type = position_info.get('strategyType', 'trend_following')
|
||||||
|
|
||||||
# 网络/DB 超时时可重试,避免 TimeoutError 导致状态未更新
|
|
||||||
db_update_retries = 3
|
db_update_retries = 3
|
||||||
for db_attempt in range(db_update_retries):
|
for db_attempt in range(db_update_retries):
|
||||||
try:
|
try:
|
||||||
|
|
@ -898,7 +888,7 @@ class PositionManager:
|
||||||
exit_reason=reason,
|
exit_reason=reason,
|
||||||
pnl=pnl,
|
pnl=pnl,
|
||||||
pnl_percent=pnl_percent,
|
pnl_percent=pnl_percent,
|
||||||
exit_order_id=None, # 同步平仓时没有订单号
|
exit_order_id=None,
|
||||||
strategy_type=strategy_type,
|
strategy_type=strategy_type,
|
||||||
duration_minutes=duration_minutes
|
duration_minutes=duration_minutes
|
||||||
)
|
)
|
||||||
|
|
@ -1157,6 +1147,11 @@ class PositionManager:
|
||||||
logger.error(f" 错误类型: {type(e).__name__}")
|
logger.error(f" 错误类型: {type(e).__name__}")
|
||||||
import traceback
|
import traceback
|
||||||
logger.error(f" 错误详情:\n{traceback.format_exc()}")
|
logger.error(f" 错误详情:\n{traceback.format_exc()}")
|
||||||
|
except Exception as db_error:
|
||||||
|
logger.error(f"{symbol} [平仓] 更新平仓记录到数据库时发生异常: {db_error}")
|
||||||
|
logger.error(f" 错误类型: {type(db_error).__name__}")
|
||||||
|
import traceback
|
||||||
|
logger.error(f" 错误详情:\n{traceback.format_exc()}")
|
||||||
else:
|
else:
|
||||||
logger.warning(f"{symbol} 没有关联的数据库交易ID,无法更新平仓记录")
|
logger.warning(f"{symbol} 没有关联的数据库交易ID,无法更新平仓记录")
|
||||||
elif not DB_AVAILABLE:
|
elif not DB_AVAILABLE:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user