修复 OKX 期权翻倍出场与币本位浮盈:中控/看板按指数折 U,避免 BTC 币数量当美金。
翻倍出场按全仓买一判定且对冲表异常不再整段跳过;面板按当前倍数展示需回收公式。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -85,13 +85,13 @@ def build_options_hub_snapshot(cfg: dict[str, Any]) -> dict[str, Any]:
|
||||
conn.close()
|
||||
except Exception:
|
||||
target_monitors = []
|
||||
from lib.options.options_positions_lib import display_pnl_from_option_row
|
||||
from lib.options.options_positions_lib import display_pnl_usdt_from_option_row
|
||||
|
||||
upl_total = 0.0
|
||||
has_upl = False
|
||||
for p in positions:
|
||||
# 与持仓卡展示一致:优先买一净盈亏,残档回退交易所 upl
|
||||
pnl = display_pnl_from_option_row(p)
|
||||
# 合计一律折 U:币本位 × 指数,避免 BTC/ETH 币数量与 USDC 混加
|
||||
pnl = display_pnl_usdt_from_option_row(p)
|
||||
if pnl is None:
|
||||
continue
|
||||
has_upl = True
|
||||
|
||||
@@ -124,14 +124,42 @@ def display_pnl_from_option_row(row: dict[str, Any]) -> float | None:
|
||||
return _safe_float(row.get("upl"))
|
||||
|
||||
|
||||
def display_pnl_usdt_from_option_row(row: dict[str, Any]) -> float | None:
|
||||
"""顶栏/看板/中控「实时盈亏」用:币本位净盈亏按指数折 USDT;USDC 仓原样.
|
||||
|
||||
BTC/ETH 币本位若不折算,会被界面标成 U 而数值仍是币数量(BTC 尤为离谱).
|
||||
"""
|
||||
pnl = display_pnl_from_option_row(row)
|
||||
if pnl is None:
|
||||
return None
|
||||
ccy = str(row.get("premium_ccy") or "USDC").strip().upper() or "USDC"
|
||||
if not ccy or ccy == "USDC":
|
||||
# 兜底:按合约推断币本位,避免 premium_ccy 缺失时把 BTC 盈亏当 U
|
||||
inst = str(row.get("inst_id") or row.get("instId") or "")
|
||||
mode = str(row.get("margin_mode") or "").strip().lower()
|
||||
if mode == "coin" or (
|
||||
"-USD-" in inst.upper() and "_UM" not in inst.upper()
|
||||
):
|
||||
underly = str(row.get("underlying") or (inst.split("-")[0] if inst else "") or "").upper()
|
||||
if underly in ("ETH", "BTC"):
|
||||
ccy = underly
|
||||
if ccy in ("ETH", "BTC"):
|
||||
idx = _safe_float(row.get("idx_px") or row.get("idxPx") or row.get("index_px"))
|
||||
if idx is None or idx <= 0:
|
||||
# 无指数时无法折算,跳过该仓以免把「币数量」当成 U
|
||||
return None
|
||||
return float(pnl) * float(idx)
|
||||
return float(pnl)
|
||||
|
||||
|
||||
def sum_options_net_pnl_usdc(
|
||||
cfg: dict[str, Any],
|
||||
ex: Any,
|
||||
raw_positions: list[dict[str, Any]] | None = None,
|
||||
) -> float | None:
|
||||
"""
|
||||
期权浮盈合计(USDC),与顶栏实时盈亏/中控口径对齐为「净盈亏」:
|
||||
各仓买一可回收 − 权利金之和;残档则回退该仓交易所 upl.
|
||||
期权浮盈合计(USDT/USDC 口径),与顶栏实时盈亏/中控对齐:
|
||||
各仓买一可回收 − 权利金;币本位再 × 指数折成 U;残档回退该仓 upl(同样折算).
|
||||
获取失败返回 None;无持仓返回 0.
|
||||
"""
|
||||
raw = raw_positions
|
||||
@@ -145,12 +173,12 @@ def sum_options_net_pnl_usdc(
|
||||
total = 0.0
|
||||
found = False
|
||||
for p in positions:
|
||||
pnl = display_pnl_from_option_row(p)
|
||||
pnl = display_pnl_usdt_from_option_row(p)
|
||||
if pnl is None:
|
||||
continue
|
||||
found = True
|
||||
total += float(pnl)
|
||||
return round(total, 8) if found else (0.0 if not positions else None)
|
||||
return round(total, 4) if found else (0.0 if not positions else None)
|
||||
|
||||
|
||||
def build_display_option_positions(
|
||||
|
||||
@@ -211,13 +211,34 @@ def _estimate_recycle(
|
||||
pos: dict[str, Any],
|
||||
premium_paid: float | None,
|
||||
) -> float | None:
|
||||
"""翻倍出场用可回收估算.
|
||||
|
||||
用买一价 × 全部持仓张数(不吃深度上限):深度不够时仍应触发分批平仓;
|
||||
残档/无效买一则返回 None(禁止按假买盘自动平).
|
||||
"""
|
||||
from lib.options.options_positions_lib import attach_close_preview
|
||||
from lib.options.options_pricing_lib import total_premium
|
||||
|
||||
row = dict(pos)
|
||||
attach_close_preview(cfg, ex, row, premium_paid=premium_paid)
|
||||
preview = row.get("close_preview") if isinstance(row.get("close_preview"), dict) else {}
|
||||
if preview.get("bid_invalid"):
|
||||
return None
|
||||
levels = preview.get("levels") or []
|
||||
ct_mult = float(row.get("ct_mult") or 0.01)
|
||||
sheets = int(
|
||||
abs(
|
||||
_safe_float(row.get("avail_pos") if row.get("avail_pos") is not None else row.get("pos"))
|
||||
or 0
|
||||
)
|
||||
)
|
||||
if levels and sheets > 0 and ct_mult > 0:
|
||||
try:
|
||||
px = float(levels[0].get("px") or 0)
|
||||
except (TypeError, ValueError, AttributeError):
|
||||
px = 0.0
|
||||
if px > 0:
|
||||
return round(total_premium(px, sheets * ct_mult), 8)
|
||||
return _safe_float(preview.get("total_received"))
|
||||
|
||||
|
||||
@@ -303,7 +324,8 @@ def run_options_profit_exits(
|
||||
init_hedge_plan_tables(conn)
|
||||
hedge_managed = active_hedge_option_inst_ids(conn)
|
||||
except Exception:
|
||||
return 0
|
||||
# 对冲表异常时不得整段跳过翻倍出场
|
||||
hedge_managed = set()
|
||||
|
||||
rules = profit_exit_by_inst(conn)
|
||||
triggered = 0
|
||||
|
||||
Reference in New Issue
Block a user