From b5590b760f9658ec28a3dc6eb77e02331300f7c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Mon, 16 Feb 2026 10:36:03 +0800 Subject: [PATCH] 1 --- backend/api/routes/account.py | 10 +++++----- backend/api/routes/trades.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/backend/api/routes/account.py b/backend/api/routes/account.py index c9aa8bf..9f4bef0 100644 --- a/backend/api/routes/account.py +++ b/backend/api/routes/account.py @@ -82,7 +82,7 @@ async def _ensure_exchange_sltp_for_symbol(symbol: str, account_id: int = 1): # 4) 从数据库 open trade 取止损/止盈价(优先取最近一条) from database.models import Trade - open_trades = Trade.get_by_symbol(symbol, status='open') or [] + open_trades = Trade.get_by_symbol(symbol, status='open', account_id=account_id) or [] if not open_trades: raise HTTPException(status_code=400, detail=f"{symbol} 数据库无 open 交易记录,无法确定止损止盈价") # 尽量取最新一条 open trade(避免同一symbol多条 open 时取到旧记录) @@ -911,8 +911,8 @@ async def close_position(symbol: str, account_id: int = Depends(get_account_id)) if not nonzero_positions: logger.warning(f"⚠ {symbol} 币安账户中没有持仓,可能已被平仓") - # 检查数据库中是否有未平仓的记录,如果有则更新 - open_trades = Trade.get_by_symbol(symbol, status='open') + # 检查数据库中是否有未平仓的记录,如果有则更新(仅当前账号) + open_trades = Trade.get_by_symbol(symbol, status='open', account_id=account_id) if open_trades: trade = open_trades[0] # 获取当前价格作为平仓价格 @@ -1268,8 +1268,8 @@ async def close_all_positions(account_id: int = Depends(get_account_id)): ticker = await client.get_ticker_24h(symbol) exit_price = float(ticker['price']) if ticker else 0 - # 更新数据库记录 - open_trades = Trade.get_by_symbol(symbol, status='open') + # 更新数据库记录(仅当前账号) + open_trades = Trade.get_by_symbol(symbol, status='open', account_id=account_id) for trade in open_trades: entry_price = float(trade['entry_price']) quantity = float(trade['quantity']) diff --git a/backend/api/routes/trades.py b/backend/api/routes/trades.py index 1a6b1e8..229df11 100644 --- a/backend/api/routes/trades.py +++ b/backend/api/routes/trades.py @@ -422,7 +422,7 @@ async def sync_trades_from_binance( logger.error(f"获取币安订单失败: {e}") raise HTTPException(status_code=500, detail=f"获取币安订单失败: {str(e)}") - # 同步订单到数据库 + # 同步订单到数据库(仅当前账号) synced_count = 0 updated_count = 0 @@ -458,8 +458,8 @@ async def sync_trades_from_binance( logger.debug(f"订单 {order_id} 已同步过且 exit_reason={existing_trade.get('exit_reason')},跳过") continue - # 查找数据库中该交易对的open状态记录 - open_trades = Trade.get_by_symbol(symbol, status='open') + # 查找数据库中该交易对的open状态记录(仅当前账号) + open_trades = Trade.get_by_symbol(symbol, status='open', account_id=None) if existing_trade or open_trades: # 找到匹配的交易记录(通过symbol匹配,如果有多个则取最近的) trade = existing_trade or open_trades[0] # 取第一个