Fix option history to full closes only, 2-decimal USDC, restore hide-delete.
Filter OKX positions-history to type 2/3/6, format premium and bid recovery to 2 decimals, and allow locally hiding rows via delete button. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -117,10 +117,32 @@ def format_option_px(px: float, tick_sz: Any) -> str:
|
||||
|
||||
|
||||
def format_usdc_amount(v: float | None) -> str | None:
|
||||
"""USDC 金额展示(与交易所持仓/历史一致,最多 4 位小数)."""
|
||||
"""USDC 金额展示(权利金/回收等,固定 2 位小数)."""
|
||||
if v is None:
|
||||
return None
|
||||
return f"{float(v):.4f}".rstrip("0").rstrip(".") or "0"
|
||||
return f"{float(v):.2f}"
|
||||
|
||||
|
||||
def is_option_full_close_history(raw: dict[str, Any]) -> bool:
|
||||
"""仅保留 OKX 历史仓位中的「全部平仓/强平/ADL 全平」记录,排除部分平仓."""
|
||||
close_type = str(raw.get("type") or "").strip()
|
||||
return close_type in ("2", "3", "6")
|
||||
|
||||
|
||||
def option_history_row_key(
|
||||
*,
|
||||
source: str,
|
||||
inst_id: str = "",
|
||||
pos_id: str | None = None,
|
||||
close_ms: int | None = None,
|
||||
) -> str:
|
||||
inst_id = (inst_id or "").strip()
|
||||
pos_id = (pos_id or "").strip()
|
||||
if source == "live":
|
||||
return f"live:{inst_id}:{pos_id or close_ms or '0'}"
|
||||
if pos_id:
|
||||
return f"ex:{pos_id}"
|
||||
return f"ex:{inst_id}:{close_ms or 0}"
|
||||
|
||||
|
||||
def _ms_to_iso(ms: Any) -> str | None:
|
||||
@@ -815,6 +837,7 @@ def fetch_all_option_positions_history(
|
||||
if after is not None and str(oldest) == after:
|
||||
break
|
||||
after = str(oldest)
|
||||
out = [r for r in out if is_option_full_close_history(r)]
|
||||
out.sort(key=lambda r: int(_safe_float(r.get("uTime")) or 0), reverse=True)
|
||||
return out[:cap]
|
||||
|
||||
@@ -854,9 +877,17 @@ def format_option_history_row(
|
||||
status_label = "强平"
|
||||
else:
|
||||
status_label = "已平"
|
||||
pos_id = str(raw.get("posId") or "").strip() or None
|
||||
close_ms = int(utime) if utime is not None else None
|
||||
return {
|
||||
"source": "exchange",
|
||||
"pos_id": str(raw.get("posId") or "").strip() or None,
|
||||
"history_key": option_history_row_key(
|
||||
source="exchange",
|
||||
inst_id=inst_id,
|
||||
pos_id=pos_id,
|
||||
close_ms=close_ms,
|
||||
),
|
||||
"pos_id": pos_id,
|
||||
"inst_id": inst_id,
|
||||
"underlying": uly,
|
||||
"opt_type": opt_type,
|
||||
@@ -876,7 +907,7 @@ def format_option_history_row(
|
||||
"close_type": close_type,
|
||||
"created_at": _ms_to_iso(ctime),
|
||||
"closed_at": _ms_to_iso(utime),
|
||||
"close_ms": int(utime) if utime is not None else None,
|
||||
"close_ms": close_ms,
|
||||
"tick_sz": tick_sz,
|
||||
"raw": raw,
|
||||
}
|
||||
@@ -889,9 +920,17 @@ def format_live_option_history_row(
|
||||
) -> dict[str, Any]:
|
||||
"""将当前持仓格式化为历史列表中的「持仓中」行."""
|
||||
inst_id = str(row.get("inst_id") or "").strip()
|
||||
pos_id = str((row.get("raw") or {}).get("posId") or "").strip() or None
|
||||
close_ms = open_ms
|
||||
return {
|
||||
"source": "live",
|
||||
"pos_id": str((row.get("raw") or {}).get("posId") or "").strip() or None,
|
||||
"history_key": option_history_row_key(
|
||||
source="live",
|
||||
inst_id=inst_id,
|
||||
pos_id=pos_id,
|
||||
close_ms=close_ms,
|
||||
),
|
||||
"pos_id": pos_id,
|
||||
"inst_id": inst_id,
|
||||
"underlying": str(row.get("underlying") or inst_id.split("-")[0] or ""),
|
||||
"opt_type": row.get("opt_type"),
|
||||
|
||||
Reference in New Issue
Block a user