权利金折 U 优先用开仓 budget_usdt,避免随币价浮动。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -148,6 +148,27 @@ def sum_open_premium_paid(conn: sqlite3.Connection, inst_id: str) -> float | Non
|
|||||||
return round(float(row["total"] or 0), 8)
|
return round(float(row["total"] or 0), 8)
|
||||||
|
|
||||||
|
|
||||||
|
def sum_open_budget_usdt(conn: sqlite3.Connection, inst_id: str) -> float | None:
|
||||||
|
"""同合约 open 腿开仓预算/兑换占用 USDT 合计(权利金折 U 展示用,不随币价变)."""
|
||||||
|
inst = (inst_id or "").strip()
|
||||||
|
if not inst:
|
||||||
|
return None
|
||||||
|
row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT SUM(budget_usdt) AS total, COUNT(*) AS n
|
||||||
|
FROM options_trades
|
||||||
|
WHERE inst_id = ? AND status = 'open' AND budget_usdt IS NOT NULL
|
||||||
|
""",
|
||||||
|
(inst,),
|
||||||
|
).fetchone()
|
||||||
|
if not row or int(row["n"] or 0) < 1:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return round(float(row["total"] or 0), 4)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def sum_open_sheets(conn: sqlite3.Connection, inst_id: str) -> int | None:
|
def sum_open_sheets(conn: sqlite3.Connection, inst_id: str) -> int | None:
|
||||||
"""同合约所有 open 腿张数合计."""
|
"""同合约所有 open 腿张数合计."""
|
||||||
inst = (inst_id or "").strip()
|
inst = (inst_id or "").strip()
|
||||||
|
|||||||
@@ -140,7 +140,14 @@ def display_pnl_usdt_from_option_row(row: dict[str, Any]) -> float | None:
|
|||||||
|
|
||||||
|
|
||||||
def premium_paid_usdt_from_option_row(row: dict[str, Any]) -> float | None:
|
def premium_paid_usdt_from_option_row(row: dict[str, Any]) -> float | None:
|
||||||
"""开仓权利金折 USDT:币本位 × 指数;USDC 原样."""
|
"""开仓权利金折 USDT.
|
||||||
|
|
||||||
|
优先用开仓时兑换/预算占用的 budget_usdt(不随币价浮动);
|
||||||
|
否则 USDC 原样;币本位无预算记录时才回退 × 当前指数.
|
||||||
|
"""
|
||||||
|
entry = _safe_float(row.get("premium_entry_usdt") or row.get("budget_usdt"))
|
||||||
|
if entry is not None:
|
||||||
|
return round(float(entry), 4)
|
||||||
paid = _safe_float(row.get("premium_paid"))
|
paid = _safe_float(row.get("premium_paid"))
|
||||||
if paid is None:
|
if paid is None:
|
||||||
return None
|
return None
|
||||||
@@ -207,6 +214,8 @@ def build_display_option_positions(
|
|||||||
raw_positions: list[dict[str, Any]],
|
raw_positions: list[dict[str, Any]],
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""与实例 /api/options/positions 相同 enrichment + close_preview."""
|
"""与实例 /api/options/positions 相同 enrichment + close_preview."""
|
||||||
|
from lib.options.options_db import init_options_tables, sum_open_budget_usdt, sum_open_premium_paid
|
||||||
|
|
||||||
meta_cache: dict[str, dict[str, Any] | None] = {}
|
meta_cache: dict[str, dict[str, Any] | None] = {}
|
||||||
rows: list[dict[str, Any]] = []
|
rows: list[dict[str, Any]] = []
|
||||||
conn = cfg["get_db"]()
|
conn = cfg["get_db"]()
|
||||||
@@ -215,6 +224,7 @@ def build_display_option_positions(
|
|||||||
for p in raw_positions:
|
for p in raw_positions:
|
||||||
inst = str(p.get("instId") or "").strip()
|
inst = str(p.get("instId") or "").strip()
|
||||||
premium_override = sum_open_premium_paid(conn, inst) if inst else None
|
premium_override = sum_open_premium_paid(conn, inst) if inst else None
|
||||||
|
budget_usdt = sum_open_budget_usdt(conn, inst) if inst else None
|
||||||
row = enrich_position_row_display(
|
row = enrich_position_row_display(
|
||||||
cfg,
|
cfg,
|
||||||
ex,
|
ex,
|
||||||
@@ -222,6 +232,9 @@ def build_display_option_positions(
|
|||||||
meta_cache=meta_cache,
|
meta_cache=meta_cache,
|
||||||
premium_override=premium_override,
|
premium_override=premium_override,
|
||||||
)
|
)
|
||||||
|
if budget_usdt is not None:
|
||||||
|
row["budget_usdt"] = round(float(budget_usdt), 4)
|
||||||
|
row["premium_entry_usdt"] = round(float(budget_usdt), 4)
|
||||||
attach_close_preview(cfg, ex, row, premium_paid=_safe_float(row.get("premium_paid")))
|
attach_close_preview(cfg, ex, row, premium_paid=_safe_float(row.get("premium_paid")))
|
||||||
rows.append(row)
|
rows.append(row)
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user