mirror of
https://github.com/silencesdg/mt5_python_ea_suite.git
synced 2026-08-02 21:57:43 +00:00
fix: 对冲代码审计修复
Bug修复: 1. 信号+回撤同时触发→只做回撤锁仓(更严重), 避免重复对冲 2. 信号解锁收紧: 同向需信号>unhedge_threshold(1.0), 而非>0 3. 新增回撤恢复解锁: 浮亏回到阈值以上自动解锁 逻辑测试: 6/6 通过
This commit is contained in:
+28
-18
@@ -78,19 +78,18 @@ class HedgeManager:
|
|||||||
if any(u["original_ticket"] == ticket for u in self._hedge_units):
|
if any(u["original_ticket"] == ticket for u in self._hedge_units):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 信号对冲
|
hedge_reason = None
|
||||||
if self.signal_hedge_enabled:
|
|
||||||
reason = self._check_signal_hedge(pos, current_signal)
|
|
||||||
if reason:
|
|
||||||
if self._can_hedge():
|
|
||||||
actions.append(("hedge", pos, reason))
|
|
||||||
|
|
||||||
# 回撤锁仓
|
# 回撤锁仓优先(更严重的情况)
|
||||||
if self.drawdown_hedge_enabled:
|
if self.drawdown_hedge_enabled:
|
||||||
reason = self._check_drawdown_hedge(pos, current_price)
|
hedge_reason = self._check_drawdown_hedge(pos, current_price)
|
||||||
if reason:
|
|
||||||
if self._can_hedge():
|
# 信号对冲作为补充
|
||||||
actions.append(("hedge", pos, reason))
|
if not hedge_reason and self.signal_hedge_enabled:
|
||||||
|
hedge_reason = self._check_signal_hedge(pos, current_signal)
|
||||||
|
|
||||||
|
if hedge_reason and self._can_hedge():
|
||||||
|
actions.append(("hedge", pos, hedge_reason))
|
||||||
|
|
||||||
return actions
|
return actions
|
||||||
|
|
||||||
@@ -194,21 +193,32 @@ class HedgeManager:
|
|||||||
|
|
||||||
def _should_unhedge(self, unit, original_pos, current_signal: float, current_price: dict) -> bool:
|
def _should_unhedge(self, unit, original_pos, current_signal: float, current_price: dict) -> bool:
|
||||||
"""判断是否应该解锁对冲"""
|
"""判断是否应该解锁对冲"""
|
||||||
# 条件1:信号回到中性
|
is_drawdown = unit.get("hedge_type") == "drawdown"
|
||||||
|
|
||||||
|
# 条件1:回撤锁仓 → 检查浮亏是否恢复
|
||||||
|
if is_drawdown:
|
||||||
|
price = current_price.get("last", 0)
|
||||||
|
if price > 0:
|
||||||
|
pnl_pct = self._pm._calculate_pnl_pct(original_pos, price)
|
||||||
|
if pnl_pct > self.drawdown_hedge_pct: # 浮亏回到阈值以上
|
||||||
|
unit["unhedge_reason"] = f"回撤恢复(浮亏={pnl_pct:.2%} > {self.drawdown_hedge_pct:.2%})"
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 条件2:信号回到中性 (|signal| < unhedge_threshold)
|
||||||
if abs(current_signal) < self.signal_unhedge_threshold:
|
if abs(current_signal) < self.signal_unhedge_threshold:
|
||||||
unit["unhedge_reason"] = f"信号中性({current_signal:.2f})"
|
unit["unhedge_reason"] = f"信号中性({current_signal:.2f})"
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# 条件2:信号与原始仓位同向
|
# 条件3:信号与原始仓位同向且有足够力度
|
||||||
is_long = original_pos["position_type"] == "long"
|
is_long = original_pos["position_type"] == "long"
|
||||||
if is_long and current_signal > 0:
|
if is_long and current_signal > self.signal_unhedge_threshold:
|
||||||
unit["unhedge_reason"] = f"信号同向做多({current_signal:.2f})"
|
unit["unhedge_reason"] = f"信号同向做多({current_signal:.2f} > {self.signal_unhedge_threshold})"
|
||||||
return True
|
return True
|
||||||
if not is_long and current_signal < 0:
|
if not is_long and current_signal < -self.signal_unhedge_threshold:
|
||||||
unit["unhedge_reason"] = f"信号同向做空({current_signal:.2f})"
|
unit["unhedge_reason"] = f"信号同向做空({current_signal:.2f} < -{self.signal_unhedge_threshold})"
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# 条件3:对冲单自身止盈
|
# 条件4:对冲单自身止盈
|
||||||
price = current_price.get("last", 0)
|
price = current_price.get("last", 0)
|
||||||
if price > 0 and unit.get("entry_price", 0) > 0:
|
if price > 0 and unit.get("entry_price", 0) > 0:
|
||||||
if unit["hedge_direction"] == "sell":
|
if unit["hedge_direction"] == "sell":
|
||||||
|
|||||||
Reference in New Issue
Block a user