From b48f55552479040e350ddb09e64489c4cdbc07d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Wed, 14 Jan 2026 19:23:17 +0800 Subject: [PATCH] a --- trading_system/position_manager.py | 51 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index b5e5c7f..0487ca6 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -238,10 +238,11 @@ class PositionManager: logger.info(f"{symbol} [平仓] 更新数据库状态为已平仓 (ID: {trade_id})...") # 获取当前价格作为平仓价格 ticker = await self.client.get_ticker_24h(symbol) - exit_price = ticker['price'] if ticker else position_info['entryPrice'] + exit_price = float(ticker['price']) if ticker else float(position_info['entryPrice']) - entry_price = position_info['entryPrice'] - quantity = position_info['quantity'] + # 确保所有值都是float类型 + entry_price = float(position_info['entryPrice']) + quantity = float(position_info['quantity']) if position_info['side'] == 'BUY': pnl = (exit_price - entry_price) * quantity pnl_percent = ((exit_price - entry_price) / entry_price) * 100 @@ -287,13 +288,13 @@ class PositionManager: if order: logger.info(f"{symbol} [平仓] ✓ 平仓订单已提交 (订单ID: {order.get('orderId', 'N/A')})") - # 获取平仓价格 + # 获取平仓价格(确保是float类型) ticker = await self.client.get_ticker_24h(symbol) if not ticker: logger.warning(f"无法获取 {symbol} 价格,使用订单价格") exit_price = float(order.get('avgPrice', 0)) or float(order.get('price', 0)) else: - exit_price = ticker['price'] + exit_price = float(ticker['price']) # 更新数据库记录 if DB_AVAILABLE and Trade and symbol in self.active_positions: @@ -302,18 +303,20 @@ class PositionManager: if trade_id: try: logger.info(f"正在更新 {symbol} 平仓记录到数据库 (ID: {trade_id})...") - # 计算盈亏 - entry_price = position_info['entryPrice'] + # 计算盈亏(确保所有值都是float类型) + entry_price = float(position_info['entryPrice']) + quantity_float = float(quantity) + exit_price_float = float(exit_price) if position_info['side'] == 'BUY': - pnl = (exit_price - entry_price) * quantity - pnl_percent = ((exit_price - entry_price) / entry_price) * 100 + pnl = (exit_price_float - entry_price) * quantity_float + pnl_percent = ((exit_price_float - entry_price) / entry_price) * 100 else: # SELL - pnl = (entry_price - exit_price) * quantity - pnl_percent = ((entry_price - exit_price) / entry_price) * 100 + pnl = (entry_price - exit_price_float) * quantity_float + pnl_percent = ((entry_price - exit_price_float) / entry_price) * 100 Trade.update_exit( trade_id=trade_id, - exit_price=exit_price, + exit_price=exit_price_float, exit_reason=reason, pnl=pnl, pnl_percent=pnl_percent @@ -610,11 +613,11 @@ class PositionManager: # 获取当前价格作为平仓价格 ticker = await self.client.get_ticker_24h(symbol) - exit_price = ticker['price'] if ticker else trade['entry_price'] + exit_price = float(ticker['price']) if ticker else float(trade['entry_price']) - # 计算盈亏 - entry_price = trade['entry_price'] - quantity = trade['quantity'] + # 计算盈亏(确保所有值都是float类型,避免Decimal类型问题) + entry_price = float(trade['entry_price']) + quantity = float(trade['quantity']) if trade['side'] == 'BUY': pnl = (exit_price - entry_price) * quantity pnl_percent = ((exit_price - entry_price) / entry_price) * 100 @@ -876,14 +879,16 @@ class PositionManager: if not position_info: return - entry_price = position_info['entryPrice'] - quantity = position_info['quantity'] + # 确保所有值都是float类型 + entry_price = float(position_info['entryPrice']) + quantity = float(position_info['quantity']) + current_price_float = float(current_price) # 计算当前盈亏 if position_info['side'] == 'BUY': - pnl_percent = ((current_price - entry_price) / entry_price) * 100 + pnl_percent = ((current_price_float - entry_price) / entry_price) * 100 else: # SELL - pnl_percent = ((entry_price - current_price) / entry_price) * 100 + pnl_percent = ((entry_price - current_price_float) / entry_price) * 100 # 更新最大盈利 if pnl_percent > position_info.get('maxProfit', 0): @@ -980,14 +985,14 @@ class PositionManager: if trade_id: try: if position_info['side'] == 'BUY': - pnl = (current_price - entry_price) * quantity + pnl = (current_price_float - entry_price) * quantity else: # SELL - pnl = (entry_price - current_price) * quantity + pnl = (entry_price - current_price_float) * quantity logger.info(f"{symbol} [自动平仓] 更新数据库记录 (ID: {trade_id})...") Trade.update_exit( trade_id=trade_id, - exit_price=current_price, + exit_price=current_price_float, exit_reason=exit_reason, pnl=pnl, pnl_percent=pnl_percent