Align hedge plan option leg PnL with OKX exchange history.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Any, Optional
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from lib.exchange.okx_options_lib import normalize_option_exp_ms
|
||||
from lib.exchange.okx_options_lib import normalize_option_exp_ms, resolve_option_close_from_history
|
||||
from lib.hedge_plan.hedge_plan_calc_lib import option_expiry_pnl
|
||||
|
||||
|
||||
@@ -60,3 +61,148 @@ def all_option_legs_expired(legs: list[dict[str, Any]], *, now_ms: Optional[int]
|
||||
if not opts:
|
||||
return False
|
||||
return all(leg_is_expired(x, now_ms=now_ms) for x in opts)
|
||||
|
||||
|
||||
def _parse_opened_ms(raw: Any) -> Optional[int]:
|
||||
if raw is None or raw == "":
|
||||
return None
|
||||
s = str(raw).strip()
|
||||
if not s:
|
||||
return None
|
||||
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M:%f"):
|
||||
try:
|
||||
dt = datetime.strptime(s[:26], fmt).replace(tzinfo=timezone.utc)
|
||||
return int(dt.timestamp() * 1000)
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def resolve_option_leg_realized_pnl(
|
||||
*,
|
||||
ex: Any = None,
|
||||
leg: dict[str, Any],
|
||||
fallback: Optional[float] = None,
|
||||
fetch_history_fn: Optional[Callable[[str], list[dict[str, Any]]]] = None,
|
||||
hist_rows: Optional[list[dict[str, Any]]] = None,
|
||||
) -> tuple[Optional[float], str]:
|
||||
"""
|
||||
期权腿已实现盈亏:优先 OKX positions-history realizedPnl.
|
||||
返回 (pnl, source) source=exchange|fallback|none.
|
||||
"""
|
||||
inst_id = str(leg.get("inst_id") or "").strip()
|
||||
open_ms = _parse_opened_ms(leg.get("opened_at"))
|
||||
rows = hist_rows
|
||||
if rows is None and inst_id:
|
||||
try:
|
||||
if callable(fetch_history_fn):
|
||||
rows = fetch_history_fn(inst_id)
|
||||
elif ex is not None:
|
||||
from lib.exchange.okx_options_lib import fetch_option_position_history
|
||||
|
||||
rows = fetch_option_position_history(ex, inst_id)
|
||||
except Exception:
|
||||
rows = None
|
||||
if rows:
|
||||
info = resolve_option_close_from_history(rows, open_ms=open_ms)
|
||||
pnl = _sf((info or {}).get("realized_pnl")) if info else None
|
||||
if pnl is not None:
|
||||
return round(float(pnl), 4), "exchange"
|
||||
if fallback is not None:
|
||||
return round(float(fallback), 4), "fallback"
|
||||
return None, "none"
|
||||
|
||||
|
||||
def backfill_hedge_option_legs_realized_pnl(
|
||||
conn: Any,
|
||||
hist_rows: list[dict[str, Any]],
|
||||
*,
|
||||
update_plan_fn: Optional[Callable[..., Any]] = None,
|
||||
) -> dict[str, int]:
|
||||
"""用交易所历史覆盖已平期权腿盈亏,并重算已结束计划合计."""
|
||||
from lib.hedge_plan.hedge_plan_db import get_plan, get_plan_legs, update_plan
|
||||
|
||||
by_inst: dict[str, list[dict[str, Any]]] = {}
|
||||
for raw in hist_rows or []:
|
||||
if not isinstance(raw, dict):
|
||||
continue
|
||||
inst = str(raw.get("instId") or "").strip()
|
||||
if inst:
|
||||
by_inst.setdefault(inst, []).append(raw)
|
||||
|
||||
legs = conn.execute(
|
||||
"""
|
||||
SELECT * FROM hedge_plan_legs
|
||||
WHERE status = 'closed'
|
||||
AND inst_id IS NOT NULL AND TRIM(inst_id) != ''
|
||||
AND (leg_role LIKE 'option%' OR opt_type IS NOT NULL)
|
||||
ORDER BY id DESC
|
||||
LIMIT 400
|
||||
"""
|
||||
).fetchall()
|
||||
updated_legs = 0
|
||||
touched_plans: set[int] = set()
|
||||
for row in legs:
|
||||
leg = dict(row)
|
||||
inst = str(leg.get("inst_id") or "").strip()
|
||||
if not inst or inst not in by_inst:
|
||||
continue
|
||||
pnl, src = resolve_option_leg_realized_pnl(
|
||||
leg=leg,
|
||||
hist_rows=by_inst[inst],
|
||||
fallback=None,
|
||||
)
|
||||
if src != "exchange" or pnl is None:
|
||||
continue
|
||||
local = _sf(leg.get("realized_pnl"))
|
||||
if local is not None and abs(local - pnl) < 1e-6:
|
||||
continue
|
||||
conn.execute(
|
||||
"UPDATE hedge_plan_legs SET realized_pnl=? WHERE id=?",
|
||||
(pnl, int(leg["id"])),
|
||||
)
|
||||
updated_legs += 1
|
||||
touched_plans.add(int(leg["plan_id"]))
|
||||
|
||||
updated_plans = 0
|
||||
updater = update_plan_fn or update_plan
|
||||
for pid in touched_plans:
|
||||
plan = get_plan(conn, pid)
|
||||
if not plan or str(plan.get("status") or "") != "closed":
|
||||
continue
|
||||
plan_legs = get_plan_legs(conn, pid)
|
||||
opt_sum = 0.0
|
||||
for lg in plan_legs:
|
||||
role = str(lg.get("leg_role") or "")
|
||||
if not (role.startswith("option") or lg.get("opt_type")):
|
||||
continue
|
||||
if str(lg.get("status") or "") != "closed":
|
||||
continue
|
||||
opt_sum += float(_sf(lg.get("realized_pnl")) or 0.0)
|
||||
perp = float(_sf(plan.get("realized_pnl_perp")) or 0.0)
|
||||
ptype = str(plan.get("plan_type") or "")
|
||||
if ptype == "options_options":
|
||||
total = opt_sum
|
||||
kwargs: dict[str, Any] = {
|
||||
"realized_pnl_options": round(opt_sum, 4),
|
||||
"realized_pnl_total": round(total, 4),
|
||||
}
|
||||
else:
|
||||
total = perp + opt_sum
|
||||
kwargs = {
|
||||
"realized_pnl_perp": round(perp, 4),
|
||||
"realized_pnl_options": round(opt_sum, 4),
|
||||
"realized_pnl_total": round(total, 4),
|
||||
}
|
||||
old_total = _sf(plan.get("realized_pnl_total"))
|
||||
old_opts = _sf(plan.get("realized_pnl_options"))
|
||||
if (
|
||||
old_total is not None
|
||||
and abs(old_total - total) < 1e-6
|
||||
and old_opts is not None
|
||||
and abs(old_opts - opt_sum) < 1e-6
|
||||
):
|
||||
continue
|
||||
updater(conn, pid, **kwargs)
|
||||
updated_plans += 1
|
||||
return {"legs": updated_legs, "plans": updated_plans}
|
||||
|
||||
Reference in New Issue
Block a user