This commit is contained in:
薇薇安 2026-01-17 09:50:11 +08:00
parent 6d2498b717
commit b2411fe745
2 changed files with 58 additions and 4 deletions

View File

@ -274,9 +274,47 @@
.stop-loss-info, .stop-loss-info,
.take-profit-info { .take-profit-info {
font-size: 0.85rem; font-size: 0.85rem;
display: flex;
flex-direction: column;
gap: 0.25rem;
padding: 0.5rem;
margin-bottom: 0.5rem;
border-radius: 4px;
}
.stop-loss-info {
background-color: #fff5f5;
border-left: 3px solid #dc3545;
}
.take-profit-info {
background-color: #f0fff4;
border-left: 3px solid #28a745;
}
.stop-header,
.take-header {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.5rem; gap: 0.5rem;
font-weight: 600;
}
.stop-details,
.take-details {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
font-size: 0.8rem;
color: #666;
}
.stop-explanation,
.take-explanation {
font-size: 0.75rem;
color: #888;
font-style: italic;
margin-top: 0.25rem;
} }
.stop-loss-info .negative, .stop-loss-info .negative,

View File

@ -1466,14 +1466,30 @@ class PositionManager:
stop_loss_pct_margin = (stop_loss_amount / margin * 100) if margin > 0 else 0 stop_loss_pct_margin = (stop_loss_amount / margin * 100) if margin > 0 else 0
# 每5%亏损记录一次诊断日志(帮助排查问题)
if pnl_percent_margin <= -5.0:
should_log = (int(abs(pnl_percent_margin)) % 5 == 0) or (pnl_percent_margin <= -10.0 and pnl_percent_margin > -10.5)
if should_log:
trigger_condition = pnl_percent_margin <= -stop_loss_pct_margin
logger.warning(
f"{symbol} [实时监控] 诊断: 亏损{pnl_percent_margin:.2f}% of margin | "
f"当前价: {current_price_float:.4f} | "
f"入场价: {entry_price:.4f} | "
f"止损价: {stop_loss:.4f} (目标: -{stop_loss_pct_margin:.2f}% of margin) | "
f"方向: {position_info['side']} | "
f"是否触发: {trigger_condition} | "
f"监控状态: {'运行中' if symbol in self._monitor_tasks else '未启动'}"
)
# 直接比较当前盈亏百分比与止损目标(基于保证金) # 直接比较当前盈亏百分比与止损目标(基于保证金)
if pnl_percent_margin <= -stop_loss_pct_margin: if pnl_percent_margin <= -stop_loss_pct_margin:
should_close = True should_close = True
exit_reason = 'trailing_stop' if position_info.get('trailingStopActivated') else 'stop_loss' exit_reason = 'trailing_stop' if position_info.get('trailingStopActivated') else 'stop_loss'
logger.warning( logger.warning(
f"{symbol} [实时监控] 触发止损(基于保证金): " f"{symbol} [实时监控] ⚠⚠⚠ 触发止损(基于保证金): "
f"当前盈亏={pnl_percent_margin:.2f}% of margin <= 止损目标=-{stop_loss_pct_margin:.2f}% of margin | " f"当前盈亏={pnl_percent_margin:.2f}% of margin <= 止损目标=-{stop_loss_pct_margin:.2f}% of margin | "
f"当前价={current_price_float:.4f}, 止损价={stop_loss:.4f}" f"当前价={current_price_float:.4f}, 止损价={stop_loss:.4f} | "
f"保证金={margin:.4f} USDT, 亏损金额={pnl_amount:.4f} USDT"
) )
# 检查止盈(基于保证金收益比) # 检查止盈(基于保证金收益比)
@ -1541,9 +1557,9 @@ class PositionManager:
exit_price=current_price_float, exit_price=current_price_float,
exit_reason=exit_reason, exit_reason=exit_reason,
pnl=pnl, pnl=pnl,
pnl_percent=pnl_percent pnl_percent=pnl_percent_margin # 修复:使用 pnl_percent_margin 而不是 pnl_percent
) )
logger.info(f"{symbol} [自动平仓] ✓ 数据库记录已更新 (盈亏: {pnl:.2f} USDT)") logger.info(f"{symbol} [自动平仓] ✓ 数据库记录已更新 (盈亏: {pnl:.2f} USDT, {pnl_percent_margin:.2f}% of margin)")
except Exception as e: except Exception as e:
logger.error(f"{symbol} [自动平仓] ❌ 更新数据库记录失败: {e}") logger.error(f"{symbol} [自动平仓] ❌ 更新数据库记录失败: {e}")
import traceback import traceback