Show options-page price clock and drive header trade stats from pure options.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-16 15:59:26 +08:00
parent 82d891ac92
commit 4ade3be2eb
8 changed files with 84 additions and 9 deletions
+33
View File
@@ -113,9 +113,12 @@ def compute_options_stats(get_db) -> dict[str, Any]:
SELECT created_at FROM options_trades WHERE status = 'open'
"""
).fetchall()
return _stats_from_option_trade_rows(closed_rows, open_rows)
finally:
conn.close()
def _stats_from_option_trade_rows(closed_rows, open_rows) -> dict[str, Any]:
wins: list[float] = []
losses: list[float] = []
win_holds: list[float] = []
@@ -171,3 +174,33 @@ def compute_options_stats(get_db) -> dict[str, Any]:
"open_count": len(open_holds),
"avg_open_hold_sec": _avg_seconds(open_holds),
}
def header_options_stats_for_window(conn, list_window: dict[str, Any], app_tz) -> dict[str, Any]:
"""顶栏:纯期权(options_trades)总交易/胜率/盈亏比,按列表窗过滤."""
from lib.common.history_window_lib import utc_window_to_bj_sql_strings
init_options_tables(conn)
start_bj, end_bj = utc_window_to_bj_sql_strings(
list_window["start_utc"], list_window["end_utc"], app_tz
)
closed_rows = conn.execute(
"""
SELECT realized_pnl, created_at, closed_at
FROM options_trades
WHERE status = 'closed'
AND realized_pnl IS NOT NULL
AND COALESCE(closed_at, created_at) >= ?
AND COALESCE(closed_at, created_at) <= ?
""",
(start_bj, end_bj),
).fetchall()
open_rows = conn.execute(
"SELECT created_at FROM options_trades WHERE status = 'open'"
).fetchall()
stats = _stats_from_option_trade_rows(closed_rows, open_rows)
return {
"total": int(stats.get("total_closed") or 0),
"rate": float(stats.get("win_rate") or 0),
"profit_loss_ratio": stats.get("profit_loss_ratio"),
}