Fix options add-on premium to sum all open legs for the contract.

Display, close gate, alerts, and full-close accounting no longer use only the latest fill row.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-16 11:11:32 +08:00
parent 4a0b0def1d
commit f0d5b47f50
8 changed files with 162 additions and 72 deletions
+69
View File
@@ -0,0 +1,69 @@
"""期权加仓后权利金汇总."""
from __future__ import annotations
import sqlite3
from lib.options.options_db import init_options_tables, sum_open_premium_paid, sum_open_sheets
def _mem_db() -> sqlite3.Connection:
conn = sqlite3.connect(":memory:")
conn.row_factory = sqlite3.Row
init_options_tables(conn)
return conn
def test_sum_open_premium_after_add():
conn = _mem_db()
inst = "BTC-USD_UM-260717-65500-C"
conn.execute(
"""
INSERT INTO options_trades
(inst_id, underlying, opt_type, sheets, eth_amount, open_quote, premium_paid, status)
VALUES (?, 'BTC-USD_UM', 'C', 1, 0.01, 530, 5.3, 'open')
""",
(inst,),
)
conn.execute(
"""
INSERT INTO options_trades
(inst_id, underlying, opt_type, sheets, eth_amount, open_quote, premium_paid, status)
VALUES (?, 'BTC-USD_UM', 'C', 1, 0.01, 140, 1.4, 'open')
""",
(inst,),
)
conn.commit()
assert sum_open_premium_paid(conn, inst) == 6.7
assert sum_open_sheets(conn, inst) == 2
# 最新一笔单独是 1.4,汇总不能只取最新
latest = conn.execute(
"SELECT premium_paid FROM options_trades WHERE inst_id=? AND status='open' ORDER BY id DESC LIMIT 1",
(inst,),
).fetchone()
assert float(latest["premium_paid"]) == 1.4
conn.close()
def test_sum_open_premium_ignores_closed():
conn = _mem_db()
inst = "ETH-USD_UM-260101-2000-C"
conn.execute(
"""
INSERT INTO options_trades
(inst_id, underlying, opt_type, sheets, eth_amount, premium_paid, status)
VALUES (?, 'ETH-USD_UM', 'C', 1, 0.01, 2.0, 'closed')
""",
(inst,),
)
conn.execute(
"""
INSERT INTO options_trades
(inst_id, underlying, opt_type, sheets, eth_amount, premium_paid, status)
VALUES (?, 'ETH-USD_UM', 'C', 2, 0.02, 3.5, 'open')
""",
(inst,),
)
conn.commit()
assert sum_open_premium_paid(conn, inst) == 3.5
assert sum_open_sheets(conn, inst) == 2
conn.close()