This commit is contained in:
薇薇安 2026-02-16 10:36:03 +08:00
parent c1a9d52ae7
commit b5590b760f
2 changed files with 8 additions and 8 deletions

View File

@ -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'])

View File

@ -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] # 取第一个