Add options stats holding time metrics and lightweight charts.

Show average hold duration for wins and losses, open positions, and CSS ring/bar visualizations in the stats tab.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-10 11:32:45 +08:00
parent 9f255f13dd
commit d1ab2d5172
7 changed files with 455 additions and 88 deletions
+2 -36
View File
@@ -666,43 +666,9 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
ex, err = _require_options_ex(cfg)
if ex is None:
return jsonify({"ok": False, "msg": err})
from lib.instance.instance_embed_context_lib import profit_loss_ratio_from_averages
from lib.options.options_stats_lib import compute_options_stats
conn = cfg["get_db"]()
try:
init_options_tables(conn)
rows = conn.execute(
"""
SELECT realized_pnl FROM options_trades
WHERE status = 'closed' AND realized_pnl IS NOT NULL
"""
).fetchall()
finally:
conn.close()
wins: list[float] = []
losses: list[float] = []
for row in rows:
pnl = float(row["realized_pnl"])
if pnl > 0:
wins.append(pnl)
elif pnl < 0:
losses.append(pnl)
total_closed = len(wins) + len(losses)
win_rate = round(len(wins) / total_closed * 100, 2) if total_closed else 0
avg_win = sum(wins) / len(wins) if wins else None
avg_loss = sum(losses) / len(losses) if losses else None
total_profit = round(sum(wins), 4) if wins else 0.0
total_loss = round(abs(sum(losses)), 4) if losses else 0.0
return jsonify(
{
"ok": True,
"total_closed": total_closed,
"win_rate": win_rate,
"profit_loss_ratio": profit_loss_ratio_from_averages(avg_win, avg_loss),
"total_profit": total_profit,
"total_loss": total_loss,
}
)
return jsonify({"ok": True, **compute_options_stats(cfg["get_db"])})
@app.route("/api/options/history/<int:trade_id>", methods=["DELETE"])
@lr