fix多笔买/卖回用VWAP计入真盈亏,卖完再结算并标记待卖回。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 10:38:46 +08:00
parent 42a7ee9961
commit 53a624633d
11 changed files with 451 additions and 28 deletions
+10 -5
View File
@@ -91,8 +91,7 @@ def repair(conn: sqlite3.Connection, *, apply: bool) -> list[dict[str, Any]]:
elif coin_pnl is not None and open_idx is not None and open_idx > 0:
stats_u = round(coin_pnl * open_idx, 4)
# 口径 A:权利金 USDT 波动式(忽略现货多买/账户差分)
# 真盈亏必须同时有开仓价与卖回价;缺一则标记缺价未核(不用开仓价冒充卖回价)
# 口径 A:权利金 USDT 波动式;开/卖回价优先桥多笔 VWAP
true_a, src = resolve_true_pnl_usdt_from_trade(
premium_paid=paid,
premium_received=recv,
@@ -102,6 +101,8 @@ def repair(conn: sqlite3.Connection, *, apply: bool) -> list[dict[str, Any]]:
open_fee=r["open_fee"],
close_fee=r["close_fee"],
premium_ccy=ccy,
conn=conn,
bridge_id=r["bridge_id"],
)
action = "ok"
@@ -109,7 +110,12 @@ def repair(conn: sqlite3.Connection, *, apply: bool) -> list[dict[str, Any]]:
new_pnl_settled = 1 if settled else 0
status_label = "已平"
if true_a is not None and src != "none":
if src == "pending_sell":
action = "mark_pending_sell"
new_pnl_settled = 0
new_settled_u = None
status_label = "待卖回"
elif true_a is not None and src != "none":
if settled_u is None or abs(float(settled_u) - float(true_a)) > 0.05:
action = "rewrite_A_premium_move"
new_settled_u = round(float(true_a), 4)
@@ -119,7 +125,6 @@ def repair(conn: sqlite3.Connection, *, apply: bool) -> list[dict[str, Any]]:
new_settled_u = round(float(true_a), 4)
new_pnl_settled = 1
else:
# 缺开仓价/卖回价/币盈亏等:无法按新公式更新 → 标记「缺价未核」
action = "mark_uncomputable_A"
new_pnl_settled = 0
new_settled_u = None
@@ -158,7 +163,7 @@ def repair(conn: sqlite3.Connection, *, apply: bool) -> list[dict[str, Any]]:
"UPDATE options_trades SET initial_premium_usdt=? WHERE id=?",
(new_initial, tid),
)
if action == "mark_uncomputable_A":
if action in ("mark_uncomputable_A", "mark_pending_sell"):
conn.execute(
"""
UPDATE options_trades