Show options position source (纯期权/永期/期期) on holdings cards.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -87,14 +87,15 @@ OPTIONS_SOURCE_LABELS = {
|
||||
HEDGE_ACTIVE_STATUSES = frozenset({"opening", "active", "partial"})
|
||||
|
||||
|
||||
def _resolve_options_source(conn, inst_id: str) -> tuple[str, str]:
|
||||
"""根据进行中对冲计划腿判定来源;默认纯期权."""
|
||||
def _resolve_options_source(conn, inst_id: str) -> tuple[str, str, int | None]:
|
||||
"""根据进行中对冲计划腿判定来源;默认纯期权. 返回 (source, label, plan_id)."""
|
||||
default = ("option", OPTIONS_SOURCE_LABELS["option"], None)
|
||||
if not inst_id or not _table_exists(conn, "hedge_plans") or not _table_exists(conn, "hedge_plan_legs"):
|
||||
return "option", OPTIONS_SOURCE_LABELS["option"]
|
||||
return default
|
||||
try:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT p.plan_type
|
||||
SELECT p.plan_type, p.id
|
||||
FROM hedge_plans p
|
||||
JOIN hedge_plan_legs l ON l.plan_id = p.id
|
||||
WHERE p.status IN ('opening', 'active', 'partial')
|
||||
@@ -106,13 +107,18 @@ def _resolve_options_source(conn, inst_id: str) -> tuple[str, str]:
|
||||
(inst_id,),
|
||||
).fetchone()
|
||||
except Exception:
|
||||
return "option", OPTIONS_SOURCE_LABELS["option"]
|
||||
return default
|
||||
if not row:
|
||||
return "option", OPTIONS_SOURCE_LABELS["option"]
|
||||
pt = str((_row_dict(row).get("plan_type") if isinstance(row, dict) else row[0]) or "").strip()
|
||||
if pt in OPTIONS_SOURCE_LABELS:
|
||||
return pt, OPTIONS_SOURCE_LABELS[pt]
|
||||
return "option", OPTIONS_SOURCE_LABELS["option"]
|
||||
return default
|
||||
d = _row_dict(row)
|
||||
pt = str(d.get("plan_type") or "").strip()
|
||||
try:
|
||||
plan_id = int(d["id"]) if d.get("id") is not None else None
|
||||
except (TypeError, ValueError):
|
||||
plan_id = None
|
||||
if pt in OPTIONS_SOURCE_LABELS and pt != "option":
|
||||
return pt, OPTIONS_SOURCE_LABELS[pt], plan_id
|
||||
return default
|
||||
|
||||
|
||||
def _format_options_target(p: dict[str, Any]) -> str:
|
||||
@@ -152,9 +158,10 @@ def _format_options_item(p: dict[str, Any], *, conn=None) -> dict[str, Any]:
|
||||
exp_ms = int(float(exp_ms)) if exp_ms not in (None, "") else None
|
||||
except (TypeError, ValueError):
|
||||
exp_ms = None
|
||||
source_key, source_label = (
|
||||
_resolve_options_source(conn, inst) if conn is not None else ("option", OPTIONS_SOURCE_LABELS["option"])
|
||||
)
|
||||
if conn is not None:
|
||||
source_key, source_label, source_plan_id = _resolve_options_source(conn, inst)
|
||||
else:
|
||||
source_key, source_label, source_plan_id = "option", OPTIONS_SOURCE_LABELS["option"], None
|
||||
return {
|
||||
"id": inst,
|
||||
"kind": "options",
|
||||
@@ -166,6 +173,7 @@ def _format_options_item(p: dict[str, Any], *, conn=None) -> dict[str, Any]:
|
||||
"opt_type_label": label,
|
||||
"source": source_key,
|
||||
"source_label": source_label,
|
||||
"source_plan_id": source_plan_id,
|
||||
"pos": pos,
|
||||
"exp_time_ms": exp_ms,
|
||||
"target_monitor": _format_options_target(p),
|
||||
|
||||
Reference in New Issue
Block a user