From bd0dd6c336fb377442dc524cf8333c8de0c27bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Sat, 17 Jan 2026 00:34:23 +0800 Subject: [PATCH] a --- backend/api/routes/account.py | 18 +++++++++++--- frontend/src/components/StatsDashboard.jsx | 29 ++++++++++++++-------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/backend/api/routes/account.py b/backend/api/routes/account.py index 218cd2c..302fd1c 100644 --- a/backend/api/routes/account.py +++ b/backend/api/routes/account.py @@ -275,8 +275,10 @@ async def get_realtime_positions(): if margin > 0: pnl_percent = (unrealized_pnl / margin) * 100 - # 尝试从数据库获取开仓时间 + # 尝试从数据库获取开仓时间、止损止盈价格 entry_time = None + stop_loss_price = None + take_profit_price = None try: from database.models import Trade db_trades = Trade.get_by_symbol(pos.get('symbol'), status='open') @@ -285,9 +287,15 @@ async def get_realtime_positions(): for db_trade in db_trades: if abs(float(db_trade.get('entry_price', 0)) - entry_price) < 0.01: entry_time = db_trade.get('entry_time') + # 尝试从数据库获取止损止盈价格(如果存储了) + stop_loss_price = db_trade.get('stop_loss_price') + take_profit_price = db_trade.get('take_profit_price') break except Exception as e: - logger.debug(f"获取开仓时间失败: {e}") + logger.debug(f"获取数据库信息失败: {e}") + + # 如果没有从数据库获取到止损止盈价格,前端会自己计算 + # 注意:数据库可能没有存储止损止盈价格,这是正常的 formatted_positions.append({ "symbol": pos.get('symbol'), @@ -297,9 +305,11 @@ async def get_realtime_positions(): "entry_value_usdt": entry_value_usdt, # 开仓时的USDT数量 "mark_price": mark_price, "pnl": unrealized_pnl, - "pnl_percent": pnl_percent, + "pnl_percent": pnl_percent, # 基于保证金的盈亏百分比 "leverage": int(pos.get('leverage', 1)), - "entry_time": entry_time # 开仓时间 + "entry_time": entry_time, # 开仓时间 + "stop_loss_price": stop_loss_price, # 止损价格(如果可用) + "take_profit_price": take_profit_price # 止盈价格(如果可用) }) logger.info(f"格式化后 {len(formatted_positions)} 个有效持仓") diff --git a/frontend/src/components/StatsDashboard.jsx b/frontend/src/components/StatsDashboard.jsx index 9a40f87..af826b4 100644 --- a/frontend/src/components/StatsDashboard.jsx +++ b/frontend/src/components/StatsDashboard.jsx @@ -238,19 +238,28 @@ const StatsDashboard = () => { const takeProfitAmount = margin * takeProfitPercentMargin // 计算止损价和止盈价(基于保证金金额) - // 止损金额 = (开仓价 - 止损价) × 数量 或 (止损价 - 开仓价) × 数量 + // 优先使用后端返回的止损止盈价格,如果没有则自己计算 let stopLossPrice = 0 let takeProfitPrice = 0 - if (side === 'BUY') { - // 做多:止损价 = 开仓价 - (止损金额 / 数量) - stopLossPrice = entryPrice - (stopLossAmount / quantity) - // 做多:止盈价 = 开仓价 + (止盈金额 / 数量) - takeProfitPrice = entryPrice + (takeProfitAmount / quantity) + + if (trade.stop_loss_price && trade.take_profit_price) { + // 使用后端返回的止损止盈价格(如果可用) + stopLossPrice = parseFloat(trade.stop_loss_price) + takeProfitPrice = parseFloat(trade.take_profit_price) } else { - // 做空:止损价 = 开仓价 + (止损金额 / 数量) - stopLossPrice = entryPrice + (stopLossAmount / quantity) - // 做空:止盈价 = 开仓价 - (止盈金额 / 数量) - takeProfitPrice = entryPrice - (takeProfitAmount / quantity) + // 自己计算止损止盈价格 + // 止损金额 = (开仓价 - 止损价) × 数量 或 (止损价 - 开仓价) × 数量 + if (side === 'BUY') { + // 做多:止损价 = 开仓价 - (止损金额 / 数量) + stopLossPrice = entryPrice - (stopLossAmount / quantity) + // 做多:止盈价 = 开仓价 + (止盈金额 / 数量) + takeProfitPrice = entryPrice + (takeProfitAmount / quantity) + } else { + // 做空:止损价 = 开仓价 + (止损金额 / 数量) + stopLossPrice = entryPrice + (stopLossAmount / quantity) + // 做空:止盈价 = 开仓价 - (止盈金额 / 数量) + takeProfitPrice = entryPrice - (takeProfitAmount / quantity) + } } // 计算止损比例和止盈比例(相对于保证金,用于显示)