修复币本位期权历史权利金/盈亏显示0.00;复盘盈亏按指数换算为U
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,7 +3,9 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from lib.exchange.okx_options_lib import format_premium_amount
|
||||
from lib.options.options_db import init_options_tables, sum_open_premium_paid
|
||||
from lib.options.options_margin_mode_lib import margin_mode_from_inst_id, premium_ccy_for_mode
|
||||
|
||||
|
||||
def enrich_position_row_display(
|
||||
@@ -14,8 +16,7 @@ def enrich_position_row_display(
|
||||
meta_cache: dict[str, dict[str, Any] | None] | None = None,
|
||||
premium_override: float | None = None,
|
||||
) -> dict[str, Any]:
|
||||
from lib.exchange.okx_options_lib import format_position_row, format_premium_amount, tick_sz_and_ct_mult
|
||||
from lib.options.options_margin_mode_lib import margin_mode_from_inst_id, premium_ccy_for_mode
|
||||
from lib.exchange.okx_options_lib import format_position_row, tick_sz_and_ct_mult
|
||||
|
||||
inst_id = str(raw_pos.get("instId") or "").strip()
|
||||
tick_sz, ct_mult = tick_sz_and_ct_mult(ex, inst_id, meta_cache)
|
||||
@@ -32,6 +33,59 @@ def enrich_position_row_display(
|
||||
return row
|
||||
|
||||
|
||||
def _safe_float(v: Any) -> float | None:
|
||||
if v is None or v == "":
|
||||
return None
|
||||
try:
|
||||
return float(v)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _load_local_closed_by_inst(conn: Any) -> dict[str, dict[str, Any]]:
|
||||
"""同合约取最新已平本地单,用于补交易所历史权利金/盈亏."""
|
||||
out: dict[str, dict[str, Any]] = {}
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT inst_id, premium_paid, realized_pnl, premium_ccy, margin_mode,
|
||||
open_quote, close_quote, sheets, closed_at
|
||||
FROM options_trades
|
||||
WHERE status = 'closed'
|
||||
ORDER BY id DESC
|
||||
"""
|
||||
).fetchall()
|
||||
except Exception:
|
||||
return out
|
||||
for r in rows:
|
||||
inst = str(r["inst_id"] or "").strip()
|
||||
if not inst or inst in out:
|
||||
continue
|
||||
out[inst] = dict(r)
|
||||
return out
|
||||
|
||||
|
||||
def _overlay_local_closed(row: dict[str, Any], local: dict[str, Any] | None) -> dict[str, Any]:
|
||||
if not local:
|
||||
return row
|
||||
prem = _safe_float(row.get("premium_paid"))
|
||||
pnl = _safe_float(row.get("realized_pnl"))
|
||||
local_prem = _safe_float(local.get("premium_paid"))
|
||||
local_pnl = _safe_float(local.get("realized_pnl"))
|
||||
# 交易所缺数或被两位小数抹成 0 时,用本地币本位落库值
|
||||
if (prem is None or abs(prem) < 1e-10) and local_prem is not None and abs(local_prem) > 0:
|
||||
row["premium_paid"] = local_prem
|
||||
if (pnl is None or abs(pnl) < 1e-10) and local_pnl is not None and abs(local_pnl) > 0:
|
||||
row["realized_pnl"] = local_pnl
|
||||
if not row.get("premium_ccy") and local.get("premium_ccy"):
|
||||
row["premium_ccy"] = local.get("premium_ccy")
|
||||
if not row.get("margin_mode") and local.get("margin_mode"):
|
||||
row["margin_mode"] = local.get("margin_mode")
|
||||
ccy = str(row.get("premium_ccy") or "USDC").strip().upper() or "USDC"
|
||||
row["premium_paid_fmt"] = format_premium_amount(row.get("premium_paid"), ccy=ccy)
|
||||
return row
|
||||
|
||||
|
||||
def load_options_history(ex: Any, cfg: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
"""与期权历史页相同的数据源:交易所全平记录 + 当前持仓,排除本地隐藏项."""
|
||||
from lib.exchange.okx_options_lib import (
|
||||
@@ -55,6 +109,7 @@ def load_options_history(ex: Any, cfg: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
str(r["history_key"])
|
||||
for r in conn.execute("SELECT history_key FROM options_history_hidden").fetchall()
|
||||
}
|
||||
local_closed = _load_local_closed_by_inst(conn)
|
||||
for p in raw_live:
|
||||
inst = str(p.get("instId") or "").strip()
|
||||
premium_override = sum_open_premium_paid(conn, inst) if inst else None
|
||||
@@ -73,15 +128,16 @@ def load_options_history(ex: Any, cfg: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
except (TypeError, ValueError):
|
||||
open_ms = None
|
||||
items.append(format_live_option_history_row(row, open_ms=open_ms))
|
||||
|
||||
hist_raw = fetch_all_option_positions_history(ex, limit=200)
|
||||
for raw in hist_raw:
|
||||
inst_id = str(raw.get("instId") or "").strip()
|
||||
tick_sz, ct_mult = tick_sz_and_ct_mult(ex, inst_id, meta_cache)
|
||||
row = format_option_history_row(raw, tick_sz=tick_sz, ct_mult=ct_mult)
|
||||
items.append(_overlay_local_closed(row, local_closed.get(inst_id)))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
hist_raw = fetch_all_option_positions_history(ex, limit=200)
|
||||
for raw in hist_raw:
|
||||
inst_id = str(raw.get("instId") or "").strip()
|
||||
tick_sz, ct_mult = tick_sz_and_ct_mult(ex, inst_id, meta_cache)
|
||||
items.append(format_option_history_row(raw, tick_sz=tick_sz, ct_mult=ct_mult))
|
||||
|
||||
open_rows = [x for x in items if x.get("status") == "open"]
|
||||
closed = [x for x in items if x.get("status") != "open"]
|
||||
closed.sort(key=lambda x: int(x.get("close_ms") or 0), reverse=True)
|
||||
|
||||
Reference in New Issue
Block a user