This commit is contained in:
薇薇安 2026-01-17 00:34:23 +08:00
parent b45f6121b1
commit bd0dd6c336
2 changed files with 33 additions and 14 deletions

View File

@ -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)} 个有效持仓")

View File

@ -238,9 +238,17 @@ const StatsDashboard = () => {
const takeProfitAmount = margin * takeProfitPercentMargin
//
// = ( - ) × ( - ) ×
// 使
let stopLossPrice = 0
let takeProfitPrice = 0
if (trade.stop_loss_price && trade.take_profit_price) {
// 使
stopLossPrice = parseFloat(trade.stop_loss_price)
takeProfitPrice = parseFloat(trade.take_profit_price)
} else {
//
// = ( - ) × ( - ) ×
if (side === 'BUY') {
// = - ( / )
stopLossPrice = entryPrice - (stopLossAmount / quantity)
@ -252,6 +260,7 @@ const StatsDashboard = () => {
// = - ( / )
takeProfitPrice = entryPrice - (takeProfitAmount / quantity)
}
}
//
const stopLossPercent = stopLossPercentMargin * 100 //