Send WeChat alerts on OKX options open and close.

Cover manual, target, and exchange/expiry sync with idempotent sent flags.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-24 00:48:15 +08:00
parent 54f1857fa2
commit ca499c6104
7 changed files with 475 additions and 2 deletions
+36
View File
@@ -640,11 +640,15 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
conn = cfg["get_db"]()
trade_id = None
target_mon = None
open_underlying = ""
open_opt_type = None
try:
init_options_tables(conn)
meta = q.get("meta") or {}
u = str(meta.get("uly") or inst_id).split("-")[0]
opt_type = meta.get("optType")
open_underlying = u
open_opt_type = opt_type
cur = conn.execute(
"""
INSERT INTO options_trades
@@ -683,9 +687,30 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
finally:
conn.close()
from lib.exchange.okx_options_lib import invalidate_option_positions_cache
from lib.options.options_notify_lib import notify_options_open
invalidate_option_positions_cache()
_sync_options_trades(cfg, force=True)
try:
conn_n = cfg["get_db"]()
try:
notify_options_open(
cfg,
conn_n,
trade_id=trade_id,
inst_id=inst_id,
underlying=open_underlying,
opt_type=open_opt_type,
sheets=sheets,
premium_paid=sizing.get("total_premium"),
open_quote=float(ask) if ask is not None else None,
target_index=target_index,
signal_note=signal_note,
)
finally:
conn_n.close()
except Exception:
pass
return jsonify(
{
"ok": True,
@@ -938,11 +963,21 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
if result.get("fully_closed"):
try:
from lib.options.options_target_lib import cancel_target_monitor
from lib.options.options_notify_lib import notify_options_close
conn2 = cfg["get_db"]()
try:
cancel_target_monitor(conn2, inst_id=inst_id)
conn2.commit()
notify_options_close(
cfg,
conn2,
inst_id=inst_id,
reason="手动平仓",
sheets=result.get("submitted_sheets"),
premium_received=result.get("premium_received"),
close_quote=result.get("locked_bid_px") or result.get("bid"),
)
finally:
conn2.close()
except Exception:
@@ -1253,6 +1288,7 @@ def _start_monitor_thread(app: Flask, cfg: dict[str, Any]) -> None:
conn,
live_inst_ids=live_ids,
fetch_history_fn=lambda inst_id: fetch_option_position_history(ex, inst_id),
notify_cfg=cfg,
)
def _target_close(inst_id: str) -> dict[str, Any]: