Add amplitude filter as first open gate for 永期 hedge.

Reuse OO amplitude settings (default off); Plan shows index/HL when filtering.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-07 21:41:51 +08:00
parent b377956367
commit a88d708ea3
7 changed files with 320 additions and 142 deletions
+59 -24
View File
@@ -352,8 +352,7 @@ class StrategySession:
def align_to_held_position(self) -> OptionPair | None:
"""有活跃仓时:监控对锁定为持仓合约的到期/行权价。"""
if _hedge_mode() == "option_option":
self.refresh_oo_amplitude()
self.refresh_oo_amplitude()
call_id, put_id = _held_option_legs()
held = call_id or _held_option_inst_id()
if not held:
@@ -421,6 +420,8 @@ class StrategySession:
return self.align_to_held_position()
if _hedge_mode() == "option_option":
return self.align_oo_instruments()
# 永期:刷新振幅供 Plan(过滤关也展示;开仓门禁在 pick)
self.refresh_oo_amplitude()
s = self.settings
idx = self.ex.fetch_index(s.index_inst_id)
mark = self.ex.fetch_mark(s.perp_inst_id) or idx
@@ -464,35 +465,67 @@ class StrategySession:
return self._apply_pair(pair, mark=float(mark), idx=idx)
def refresh_oo_amplitude(self) -> dict[str, Any] | None:
"""刷新振幅高低(有仓/无仓都要,否则持仓后 UI 指数/振幅会空)。"""
if _hedge_mode() != "option_option":
return None
"""刷新振幅高低(永期/期期;有仓/无仓都要,否则 UI 指数/振幅会空)。"""
try:
from ..exchange.candles import fetch_amplitude_hl_for_runtime
from .amplitude_gate import evaluate_amplitude_gate
amp_pct, amp_hours, _, _, _, amp_filt = _oo_settings()
amp = fetch_amplitude_hl_for_runtime(amp_hours)
if amp is None:
return self._oo_amp
self._oo_amp = {
"high": float(amp.high),
"low": float(amp.low),
"mid": float(amp.mid),
"range_pct": float(amp.range_pct),
"hours": float(amp_hours),
"max_pct": float(amp_pct),
"filter_enabled": bool(amp_filt),
"ok": (
True
if not amp_filt
else float(amp.range_pct) <= float(amp_pct) + 1e-12
),
}
gate = evaluate_amplitude_gate(
filter_enabled=bool(amp_filt),
amp=amp,
max_pct=float(amp_pct),
hours=float(amp_hours),
)
self._oo_amp = dict(gate["snapshot"])
return self._oo_amp
except Exception:
logger.exception("refresh_oo_amplitude failed")
return self._oo_amp
def _apply_amplitude_first_gate(self) -> bool:
"""
振幅过滤为开仓第一关。返回 True=可继续选约;False=拒开。
无论是否开启过滤都写入 _oo_amp 供 Plan 展示。
"""
from ..exchange.candles import fetch_amplitude_hl_for_runtime
from .amplitude_gate import evaluate_amplitude_gate
amp_pct, amp_hours, _, _, _, amp_filt = _oo_settings()
amp = fetch_amplitude_hl_for_runtime(amp_hours)
gate = evaluate_amplitude_gate(
filter_enabled=bool(amp_filt),
amp=amp,
max_pct=float(amp_pct),
hours=float(amp_hours),
)
self._oo_amp = dict(gate["snapshot"])
if gate["blocked"]:
logger.info("amplitude gate blocked: %s", gate.get("reason"))
return False
return True
def amplitude_gate_fail_reason(self) -> str | None:
"""若最近一次振幅快照显示过滤开启且未过关,返回文案。"""
amp = self._oo_amp
if not amp or not amp.get("filter_enabled"):
return None
if amp.get("ok") is True:
return None
hours = amp.get("hours")
range_pct = amp.get("range_pct")
max_pct = amp.get("max_pct")
if range_pct is None:
return f"振幅未过关:无法获取近 {hours:g}h K 线高低" if hours is not None else "振幅未过关"
try:
return (
f"振幅未过关:{float(hours):g}h 内 "
f"{float(range_pct):.2f}% > {float(max_pct):g}%"
)
except (TypeError, ValueError):
return "振幅未过关"
def align_oo_instruments(self) -> OptionPair | None:
"""期期监控:按振幅高低点选虚值 Call/Put(展示用;振幅超限仍对齐候选)。"""
from .oo_selection import select_oo_pair
@@ -693,6 +726,9 @@ class StrategySession:
min_hours, min_lev, max_atm_off, atm_off_on = _strategy_floats()
fixed_on, fixed_perp = _fixed_direction()
opt_side_hint = _option_side_for_perp(fixed_perp) if fixed_on else None
# 第一关:振幅过滤(默认关;开启则回看窗振幅须 ≤ 最大%)
if not self._apply_amplitude_first_gate():
return None
idx = self.ex.fetch_index(s.index_inst_id)
mark = self.ex.fetch_mark(s.perp_inst_id) or idx
if mark is None or mark <= 0:
@@ -932,9 +968,8 @@ class StrategySession:
async def ensure_atm_async(self, *, force: bool = False) -> OptionPair | None:
if _has_open_position():
# 持仓期间:钉住持仓行权价(禁止漂到新 ATM/虚值);期期仍刷新振幅供 UI
if _hedge_mode() == "option_option":
await asyncio.to_thread(self.refresh_oo_amplitude)
# 持仓期间:钉住持仓行权价(禁止漂到新 ATM/虚值);仍刷新振幅供 UI
await asyncio.to_thread(self.refresh_oo_amplitude)
call_id, put_id = _held_option_legs()
held = call_id or _held_option_inst_id()
if held and (