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
+6 -26
View File
@@ -9,7 +9,7 @@ from typing import Any
from flask import Flask, jsonify, redirect, request, url_for
from jinja2 import ChoiceLoader, FileSystemLoader
from lib.options.options_db import init_options_tables
from lib.options.options_db import init_options_tables, sum_open_premium_paid, sum_open_sheets
from lib.options.options_monitor_lib import options_monitor_loop
from lib.options.options_pricing_lib import (
calc_order_size,
@@ -170,19 +170,9 @@ def _open_premium_paid(cfg: dict[str, Any], inst_id: str) -> float | None:
conn = cfg["get_db"]()
try:
init_options_tables(conn)
rec = conn.execute(
"""
SELECT premium_paid FROM options_trades
WHERE inst_id = ? AND status = 'open'
ORDER BY id DESC LIMIT 1
""",
(inst_id,),
).fetchone()
if rec and rec["premium_paid"] is not None:
return round(float(rec["premium_paid"]), 4)
return sum_open_premium_paid(conn, inst_id)
finally:
conn.close()
return None
def _position_avail_sheets(pos: dict[str, Any]) -> int:
@@ -662,18 +652,7 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
rows = []
for p in raw:
inst = str(p.get("instId") or "").strip()
premium_override = None
if inst:
rec = conn.execute(
"""
SELECT premium_paid FROM options_trades
WHERE inst_id = ? AND status = 'open'
ORDER BY id DESC LIMIT 1
""",
(inst,),
).fetchone()
if rec and rec["premium_paid"] is not None:
premium_override = float(rec["premium_paid"])
premium_override = sum_open_premium_paid(conn, inst) if inst else None
row = _enrich_position_row_display(
cfg,
ex,
@@ -732,14 +711,15 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
try:
trade = conn.execute(
"""
SELECT id, sheets, opt_type, underlying FROM options_trades
SELECT id, opt_type, underlying FROM options_trades
WHERE inst_id = ? AND status = 'open'
ORDER BY id DESC LIMIT 1
""",
(inst_id,),
).fetchone()
trade_id = int(trade["id"]) if trade else None
sheets = int(trade["sheets"]) if trade and trade["sheets"] is not None else int(fmt.get("pos") or 0)
sheets_sum = sum_open_sheets(conn, inst_id)
sheets = sheets_sum if sheets_sum is not None else int(fmt.get("pos") or 0)
opt_type = (trade["opt_type"] if trade else None) or fmt.get("opt_type")
underlying = (trade["underlying"] if trade else None) or fmt.get("underlying")
out = upsert_target_monitor(