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: if margin > 0:
pnl_percent = (unrealized_pnl / margin) * 100 pnl_percent = (unrealized_pnl / margin) * 100
# 尝试从数据库获取开仓时间 # 尝试从数据库获取开仓时间、止损止盈价格
entry_time = None entry_time = None
stop_loss_price = None
take_profit_price = None
try: try:
from database.models import Trade from database.models import Trade
db_trades = Trade.get_by_symbol(pos.get('symbol'), status='open') 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: for db_trade in db_trades:
if abs(float(db_trade.get('entry_price', 0)) - entry_price) < 0.01: if abs(float(db_trade.get('entry_price', 0)) - entry_price) < 0.01:
entry_time = db_trade.get('entry_time') 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 break
except Exception as e: except Exception as e:
logger.debug(f"获取开仓时间失败: {e}") logger.debug(f"获取数据库信息失败: {e}")
# 如果没有从数据库获取到止损止盈价格,前端会自己计算
# 注意:数据库可能没有存储止损止盈价格,这是正常的
formatted_positions.append({ formatted_positions.append({
"symbol": pos.get('symbol'), "symbol": pos.get('symbol'),
@ -297,9 +305,11 @@ async def get_realtime_positions():
"entry_value_usdt": entry_value_usdt, # 开仓时的USDT数量 "entry_value_usdt": entry_value_usdt, # 开仓时的USDT数量
"mark_price": mark_price, "mark_price": mark_price,
"pnl": unrealized_pnl, "pnl": unrealized_pnl,
"pnl_percent": pnl_percent, "pnl_percent": pnl_percent, # 基于保证金的盈亏百分比
"leverage": int(pos.get('leverage', 1)), "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)} 个有效持仓") logger.info(f"格式化后 {len(formatted_positions)} 个有效持仓")

View File

@ -238,19 +238,28 @@ const StatsDashboard = () => {
const takeProfitAmount = margin * takeProfitPercentMargin const takeProfitAmount = margin * takeProfitPercentMargin
// //
// = ( - ) × ( - ) × // 使
let stopLossPrice = 0 let stopLossPrice = 0
let takeProfitPrice = 0 let takeProfitPrice = 0
if (side === 'BUY') {
// = - ( / ) if (trade.stop_loss_price && trade.take_profit_price) {
stopLossPrice = entryPrice - (stopLossAmount / quantity) // 使
// = + ( / ) stopLossPrice = parseFloat(trade.stop_loss_price)
takeProfitPrice = entryPrice + (takeProfitAmount / quantity) takeProfitPrice = parseFloat(trade.take_profit_price)
} else { } else {
// = + ( / ) //
stopLossPrice = entryPrice + (stopLossAmount / quantity) // = ( - ) × ( - ) ×
// = - ( / ) if (side === 'BUY') {
takeProfitPrice = entryPrice - (takeProfitAmount / quantity) // = - ( / )
stopLossPrice = entryPrice - (stopLossAmount / quantity)
// = + ( / )
takeProfitPrice = entryPrice + (takeProfitAmount / quantity)
} else {
// = + ( / )
stopLossPrice = entryPrice + (stopLossAmount / quantity)
// = - ( / )
takeProfitPrice = entryPrice - (takeProfitAmount / quantity)
}
} }
// //