fix(position_manager): 优化代码结构和日志记录

在持仓管理模块中,调整了代码缩进和结构,提升了可读性和一致性。同时,增强了日志记录,确保在保存交易记录时提供更清晰的信息。这一改动旨在提升系统的稳定性和可维护性,确保交易策略的有效性与安全性。
This commit is contained in:
薇薇安 2026-02-26 09:49:58 +08:00
parent beafeb2707
commit d80d4559c5

View File

@ -3530,8 +3530,6 @@ class PositionManager:
ticker = await self.client.get_ticker_24h(symbol)
current_price = ticker['price'] if ticker else entry_price
# ---------- 手动开仓补建 SL/TP 顺序:先读交易所 → 决定 SL/TP → 再写 position_info ----------
# 本分支不调用 _ensure_exchange_sltp_orders仅写入 active_positions后续由监控在价格推送时可能调用。
# 若此处用 risk_manager 初始止损写 position_info而交易所已是保本后续同步会覆盖。故先读交易所已达保本则采用。
sl_from_ex, tp_from_ex = await self._get_sltp_from_exchange(symbol, side)
breakeven = self._breakeven_stop_price(entry_price, side, None)
use_exchange_sl = False
@ -3544,14 +3542,11 @@ class PositionManager:
# 计算止损止盈(缺失时用 risk_manager 基于保证金)
leverage = binance_position.get('leverage', 10)
stop_loss_pct_margin = config.TRADING_CONFIG.get('STOP_LOSS_PERCENT', 0.08)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if stop_loss_pct_margin is not None and stop_loss_pct_margin > 1:
stop_loss_pct_margin = stop_loss_pct_margin / 100.0
take_profit_pct_margin = config.TRADING_CONFIG.get('TAKE_PROFIT_PERCENT', 0.15)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if take_profit_pct_margin is not None and take_profit_pct_margin > 1:
take_profit_pct_margin = take_profit_pct_margin / 100.0
# 如果配置中没有设置止盈则使用止损的2倍作为默认
if take_profit_pct_margin is None or take_profit_pct_margin == 0:
take_profit_pct_margin = stop_loss_pct_margin * 2.0
@ -3581,7 +3576,7 @@ class PositionManager:
'side': side,
'quantity': quantity,
'entryPrice': entry_price,
'changePercent': 0, # 手动开仓,无法计算涨跌幅
'changePercent': 0,
'orderId': entry_order_id,
'tradeId': trade_id,
'stopLoss': stop_loss_price,
@ -3589,7 +3584,7 @@ class PositionManager:
'initialStopLoss': initial_stop_loss,
'leverage': leverage,
'entryReason': 'manual_entry',
'entryTime': entry_time_ts if entry_time_ts is not None else get_beijing_time(), # 真实开仓时间(来自币安成交/订单)
'entryTime': entry_time_ts if entry_time_ts is not None else get_beijing_time(),
'atr': None,
'maxProfit': 0.0,
'trailingStopActivated': False,