Fix options add-on premium to sum all open legs for the contract.

Display, close gate, alerts, and full-close accounting no longer use only the latest fill row.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-16 11:11:32 +08:00
parent 4a0b0def1d
commit f0d5b47f50
8 changed files with 162 additions and 72 deletions
+21 -5
View File
@@ -61,18 +61,34 @@ def run_options_profit_alerts(
SELECT id, inst_id, premium_paid, profit_alert_sent
FROM options_trades
WHERE status = 'open'
ORDER BY id ASC
"""
).fetchall()
# 同合约多腿加仓:按合约汇总权利金,整仓只告警一次
by_inst: dict[str, dict[str, Any]] = {}
for row in rows:
if int(row["profit_alert_sent"] or 0):
continue
inst_id = str(row["inst_id"] or "")
if not inst_id:
continue
bucket = by_inst.setdefault(
inst_id,
{"ids": [], "premium": 0.0, "all_sent": True, "has_prem": False},
)
bucket["ids"].append(int(row["id"]))
prem = _safe_float(row["premium_paid"])
if not inst_id or prem is None or prem <= 0:
if prem is not None:
bucket["premium"] += float(prem)
bucket["has_prem"] = True
if not int(row["profit_alert_sent"] or 0):
bucket["all_sent"] = False
for inst_id, bucket in by_inst.items():
if bucket["all_sent"] or not bucket["has_prem"] or bucket["premium"] <= 0:
continue
pos = pos_by_inst.get(inst_id)
if not pos:
continue
prem = float(bucket["premium"])
upl = _safe_float(pos.get("upl"))
upl_ratio = _safe_float(pos.get("upl_ratio_pct"))
if upl_ratio is not None:
@@ -95,8 +111,8 @@ def run_options_profit_alerts(
try:
send_wechat(msg)
conn.execute(
"UPDATE options_trades SET profit_alert_sent = 1 WHERE id = ?",
(int(row["id"]),),
f"UPDATE options_trades SET profit_alert_sent = 1 WHERE id IN ({','.join('?' * len(bucket['ids']))})",
tuple(bucket["ids"]),
)
sent += 1
except Exception: