Close residual options when premium recovers above configurable threshold.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -31,6 +31,7 @@ class StrategyEngine:
|
||||
self._lock = asyncio.Lock()
|
||||
self._retry_gate = LiveRetryGate()
|
||||
self._extra_sleep_sec = 0.0
|
||||
self._last_residual_premium_check_ms = 0
|
||||
|
||||
def refresh_executor(self) -> None:
|
||||
"""MODE 变更后刷新执行器。"""
|
||||
@@ -545,6 +546,41 @@ class StrategyEngine:
|
||||
async def _settle_residuals(self) -> None:
|
||||
await asyncio.to_thread(self.matcher.settle_due_residuals)
|
||||
|
||||
async def _maybe_close_residuals_by_premium(self) -> None:
|
||||
"""残留期权:权利金回升达标时周期性尝试中途平仓。"""
|
||||
s = get_settings()
|
||||
interval_sec = int(
|
||||
self.ledger.get_setting_int(
|
||||
"residual_close_check_sec", s.residual_close_check_sec
|
||||
)
|
||||
)
|
||||
interval_sec = max(30, interval_sec)
|
||||
now_ms = int(time.time() * 1000)
|
||||
if now_ms - self._last_residual_premium_check_ms < interval_sec * 1000:
|
||||
return
|
||||
self._last_residual_premium_check_ms = now_ms
|
||||
fn = getattr(self.matcher, "try_close_pending_residuals", None)
|
||||
if not callable(fn):
|
||||
return
|
||||
try:
|
||||
closed = await asyncio.to_thread(fn)
|
||||
except Exception:
|
||||
logger.exception("try_close_pending_residuals failed")
|
||||
return
|
||||
if not closed:
|
||||
return
|
||||
for item in closed:
|
||||
try:
|
||||
from ..notify import wecom
|
||||
|
||||
wecom.notify_close(
|
||||
reason="residual_premium_close",
|
||||
detail="残留期权权利金回收中途平",
|
||||
data=item if isinstance(item, dict) else {},
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("wecom notify residual premium close failed")
|
||||
|
||||
async def _maybe_expiry_close(self) -> bool:
|
||||
"""若持仓已到期则强制全平。返回是否触发到期平仓。"""
|
||||
await self._settle_residuals()
|
||||
@@ -613,6 +649,7 @@ class StrategyEngine:
|
||||
async def _tick_manage_positions(self) -> None:
|
||||
"""有仓时的盯盘:残留结算 / 半仓修复 / 目标平 / 到期平。不新开仓。"""
|
||||
await self._settle_residuals()
|
||||
await self._maybe_close_residuals_by_premium()
|
||||
|
||||
s = get_settings()
|
||||
st = self.db.fetchone("SELECT * FROM strategy_state WHERE id=1")
|
||||
|
||||
Reference in New Issue
Block a user