This commit is contained in:
薇薇安 2026-01-22 23:40:53 +08:00
parent 3154dd5518
commit ae953d119e

View File

@ -2574,88 +2574,88 @@ class PositionManager:
# 3) 交易所级别止盈单已提供保护 # 3) 交易所级别止盈单已提供保护
# 4) 及时止盈可以保护利润,避免价格回落 # 4) 及时止盈可以保护利润,避免价格回落
should_close = False should_close = False
take_profit_1 = position_info.get('takeProfit1') # 第一目标盈亏比1:1 take_profit_1 = position_info.get('takeProfit1') # 第一目标盈亏比1:1
take_profit_2 = position_info.get('takeProfit2', position_info.get('takeProfit')) # 第二目标1.5:1 take_profit_2 = position_info.get('takeProfit2', position_info.get('takeProfit')) # 第二目标1.5:1
partial_profit_taken = position_info.get('partialProfitTaken', False) partial_profit_taken = position_info.get('partialProfitTaken', False)
remaining_quantity = position_info.get('remainingQuantity', quantity) remaining_quantity = position_info.get('remainingQuantity', quantity)
# 第一目标盈亏比1:1了结50%仓位
if not partial_profit_taken and take_profit_1 is not None:
# 计算第一目标对应的保证金百分比
if position_info['side'] == 'BUY':
take_profit_1_amount = (take_profit_1 - entry_price) * quantity
else: # SELL
take_profit_1_amount = (entry_price - take_profit_1) * quantity
take_profit_1_pct_margin = (take_profit_1_amount / margin * 100) if margin > 0 else 0
# 第一目标盈亏比1:1了结50%仓位 # 直接比较当前盈亏百分比与第一目标(基于保证金)
if not partial_profit_taken and take_profit_1 is not None: if pnl_percent_margin >= take_profit_1_pct_margin:
# 计算第一目标对应的保证金百分比 logger.info(
if position_info['side'] == 'BUY': f"{symbol} [实时监控] 触发第一目标止盈盈亏比1:1基于保证金: "
take_profit_1_amount = (take_profit_1 - entry_price) * quantity f"当前盈亏={pnl_percent_margin:.2f}% of margin >= 目标={take_profit_1_pct_margin:.2f}% of margin | "
else: # SELL f"当前价={current_price_float:.4f}, 目标价={take_profit_1:.4f}"
take_profit_1_amount = (entry_price - take_profit_1) * quantity )
take_profit_1_pct_margin = (take_profit_1_amount / margin * 100) if margin > 0 else 0 # 部分平仓50%
partial_quantity = quantity * 0.5
# 直接比较当前盈亏百分比与第一目标(基于保证金) try:
if pnl_percent_margin >= take_profit_1_pct_margin: close_side = 'SELL' if position_info['side'] == 'BUY' else 'BUY'
logger.info( close_position_side = 'LONG' if position_info['side'] == 'BUY' else 'SHORT'
f"{symbol} [实时监控] 触发第一目标止盈盈亏比1:1基于保证金: " live_amt = await self._get_live_position_amt(symbol, position_side=close_position_side)
f"当前盈亏={pnl_percent_margin:.2f}% of margin >= 目标={take_profit_1_pct_margin:.2f}% of margin | " if live_amt is None or abs(live_amt) <= 0:
f"当前价={current_price_float:.4f}, 目标价={take_profit_1:.4f}" logger.warning(f"{symbol} [实时监控] 部分止盈实时持仓已为0跳过部分平仓")
) else:
# 部分平仓50% partial_quantity = min(partial_quantity, abs(live_amt))
partial_quantity = quantity * 0.5 partial_quantity = await self._adjust_close_quantity(symbol, partial_quantity)
try: if partial_quantity > 0:
close_side = 'SELL' if position_info['side'] == 'BUY' else 'BUY' partial_order = await self.client.place_order(
close_position_side = 'LONG' if position_info['side'] == 'BUY' else 'SHORT' symbol=symbol,
live_amt = await self._get_live_position_amt(symbol, position_side=close_position_side) side=close_side,
if live_amt is None or abs(live_amt) <= 0: quantity=partial_quantity,
logger.warning(f"{symbol} [实时监控] 部分止盈实时持仓已为0跳过部分平仓") order_type='MARKET',
else: reduce_only=True,
partial_quantity = min(partial_quantity, abs(live_amt)) position_side=close_position_side,
partial_quantity = await self._adjust_close_quantity(symbol, partial_quantity) )
if partial_quantity > 0: if partial_order:
partial_order = await self.client.place_order( position_info['partialProfitTaken'] = True
symbol=symbol, position_info['remainingQuantity'] = remaining_quantity - partial_quantity
side=close_side, logger.info(
quantity=partial_quantity, f"{symbol} [实时监控] 部分止盈成功: 平仓{partial_quantity:.4f},剩余{position_info['remainingQuantity']:.4f}"
order_type='MARKET',
reduce_only=True,
position_side=close_position_side,
) )
if partial_order: # 分步止盈后的"保本"处理:将剩余仓位止损移至成本价(保本)
position_info['partialProfitTaken'] = True position_info['stopLoss'] = entry_price
position_info['remainingQuantity'] = remaining_quantity - partial_quantity logger.info(
logger.info( f"{symbol} [实时监控] 部分止盈后:剩余仓位止损移至成本价 {entry_price:.4f}(保本),"
f"{symbol} [实时监控] 部分止盈成功: 平仓{partial_quantity:.4f},剩余{position_info['remainingQuantity']:.4f}" f"剩余50%仓位追求1.5:1止盈目标"
) )
# 分步止盈后的"保本"处理:将剩余仓位止损移至成本价(保本) except Exception as e:
position_info['stopLoss'] = entry_price logger.error(f"{symbol} [实时监控] 部分止盈失败: {e}")
logger.info(
f"{symbol} [实时监控] 部分止盈后:剩余仓位止损移至成本价 {entry_price:.4f}(保本)," # 第二目标1.5:1止盈平掉剩余仓位
f"剩余50%仓位追求1.5:1止盈目标" if partial_profit_taken and take_profit_2 is not None and not should_close:
) # 计算第二目标对应的保证金百分比(基于剩余仓位)
except Exception as e: if position_info['side'] == 'BUY':
logger.error(f"{symbol} [实时监控] 部分止盈失败: {e}") take_profit_2_amount = (take_profit_2 - entry_price) * remaining_quantity
else: # SELL
take_profit_2_amount = (entry_price - take_profit_2) * remaining_quantity
remaining_margin = (entry_price * remaining_quantity) / leverage if leverage > 0 else (entry_price * remaining_quantity)
take_profit_2_pct_margin = (take_profit_2_amount / remaining_margin * 100) if remaining_margin > 0 else 0
# 计算剩余仓位的当前盈亏
if position_info['side'] == 'BUY':
remaining_pnl_amount = (current_price_float - entry_price) * remaining_quantity
else:
remaining_pnl_amount = (entry_price - current_price_float) * remaining_quantity
remaining_pnl_pct_margin = (remaining_pnl_amount / remaining_margin * 100) if remaining_margin > 0 else 0
# 第二目标1.5:1止盈平掉剩余仓位 # 直接比较剩余仓位盈亏百分比与第二目标(基于保证金)
if partial_profit_taken and take_profit_2 is not None and not should_close: if remaining_pnl_pct_margin >= take_profit_2_pct_margin:
# 计算第二目标对应的保证金百分比(基于剩余仓位) should_close = True
if position_info['side'] == 'BUY': exit_reason = 'take_profit'
take_profit_2_amount = (take_profit_2 - entry_price) * remaining_quantity logger.info(
else: # SELL f"{symbol} [实时监控] 触发第二目标止盈1.5:1基于保证金: "
take_profit_2_amount = (entry_price - take_profit_2) * remaining_quantity f"剩余仓位盈亏={remaining_pnl_pct_margin:.2f}% of margin >= 目标={take_profit_2_pct_margin:.2f}% of margin | "
remaining_margin = (entry_price * remaining_quantity) / leverage if leverage > 0 else (entry_price * remaining_quantity) f"当前价={current_price_float:.4f}, 目标价={take_profit_2:.4f}, "
take_profit_2_pct_margin = (take_profit_2_amount / remaining_margin * 100) if remaining_margin > 0 else 0 f"剩余数量={remaining_quantity:.4f}"
# 计算剩余仓位的当前盈亏 )
if position_info['side'] == 'BUY':
remaining_pnl_amount = (current_price_float - entry_price) * remaining_quantity
else:
remaining_pnl_amount = (entry_price - current_price_float) * remaining_quantity
remaining_pnl_pct_margin = (remaining_pnl_amount / remaining_margin * 100) if remaining_margin > 0 else 0
# 直接比较剩余仓位盈亏百分比与第二目标(基于保证金)
if remaining_pnl_pct_margin >= take_profit_2_pct_margin:
should_close = True
exit_reason = 'take_profit'
logger.info(
f"{symbol} [实时监控] 触发第二目标止盈1.5:1基于保证金: "
f"剩余仓位盈亏={remaining_pnl_pct_margin:.2f}% of margin >= 目标={take_profit_2_pct_margin:.2f}% of margin | "
f"当前价={current_price_float:.4f}, 目标价={take_profit_2:.4f}, "
f"剩余数量={remaining_quantity:.4f}"
)
# 检查止盈(基于保证金收益比)- 用于未启用分步止盈的情况 # 检查止盈(基于保证金收益比)- 用于未启用分步止盈的情况
if not should_close: if not should_close: