1
This commit is contained in:
parent
c1a9d52ae7
commit
b5590b760f
|
|
@ -82,7 +82,7 @@ async def _ensure_exchange_sltp_for_symbol(symbol: str, account_id: int = 1):
|
||||||
|
|
||||||
# 4) 从数据库 open trade 取止损/止盈价(优先取最近一条)
|
# 4) 从数据库 open trade 取止损/止盈价(优先取最近一条)
|
||||||
from database.models import 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:
|
if not open_trades:
|
||||||
raise HTTPException(status_code=400, detail=f"{symbol} 数据库无 open 交易记录,无法确定止损止盈价")
|
raise HTTPException(status_code=400, detail=f"{symbol} 数据库无 open 交易记录,无法确定止损止盈价")
|
||||||
# 尽量取最新一条 open trade(避免同一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:
|
if not nonzero_positions:
|
||||||
logger.warning(f"⚠ {symbol} 币安账户中没有持仓,可能已被平仓")
|
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:
|
if open_trades:
|
||||||
trade = open_trades[0]
|
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)
|
ticker = await client.get_ticker_24h(symbol)
|
||||||
exit_price = float(ticker['price']) if ticker else 0
|
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:
|
for trade in open_trades:
|
||||||
entry_price = float(trade['entry_price'])
|
entry_price = float(trade['entry_price'])
|
||||||
quantity = float(trade['quantity'])
|
quantity = float(trade['quantity'])
|
||||||
|
|
|
||||||
|
|
@ -422,7 +422,7 @@ async def sync_trades_from_binance(
|
||||||
logger.error(f"获取币安订单失败: {e}")
|
logger.error(f"获取币安订单失败: {e}")
|
||||||
raise HTTPException(status_code=500, detail=f"获取币安订单失败: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"获取币安订单失败: {str(e)}")
|
||||||
|
|
||||||
# 同步订单到数据库
|
# 同步订单到数据库(仅当前账号)
|
||||||
synced_count = 0
|
synced_count = 0
|
||||||
updated_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')},跳过")
|
logger.debug(f"订单 {order_id} 已同步过且 exit_reason={existing_trade.get('exit_reason')},跳过")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 查找数据库中该交易对的open状态记录
|
# 查找数据库中该交易对的open状态记录(仅当前账号)
|
||||||
open_trades = Trade.get_by_symbol(symbol, status='open')
|
open_trades = Trade.get_by_symbol(symbol, status='open', account_id=None)
|
||||||
if existing_trade or open_trades:
|
if existing_trade or open_trades:
|
||||||
# 找到匹配的交易记录(通过symbol匹配,如果有多个则取最近的)
|
# 找到匹配的交易记录(通过symbol匹配,如果有多个则取最近的)
|
||||||
trade = existing_trade or open_trades[0] # 取第一个
|
trade = existing_trade or open_trades[0] # 取第一个
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user