feat: options UI scroll layout, stats card, funding balance, and cross-account transfer
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -479,8 +479,9 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
return jsonify({"ok": False, "msg": err})
|
||||
data = request.get_json(silent=True) or {}
|
||||
ccy = (data.get("ccy") or "USDT").upper()
|
||||
account = (data.get("account") or "funding").strip()
|
||||
direction = (data.get("direction") or "sub_to_main").strip()
|
||||
from_account = (data.get("from_account") or data.get("account") or "funding").strip()
|
||||
to_account = (data.get("to_account") or data.get("account") or "funding").strip()
|
||||
try:
|
||||
amount = float(data.get("amount"))
|
||||
except (TypeError, ValueError):
|
||||
@@ -492,7 +493,8 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
amount=amount,
|
||||
sub_acct=cfg.get("sub_account_name") or "",
|
||||
main_to_sub=main_to_sub,
|
||||
account=account,
|
||||
from_account=from_account,
|
||||
to_account=to_account,
|
||||
)
|
||||
if result.get("ok"):
|
||||
conn = cfg["get_db"]()
|
||||
@@ -506,9 +508,9 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
(
|
||||
ccy,
|
||||
amount,
|
||||
"main" if main_to_sub else "sub",
|
||||
"sub" if main_to_sub else "main",
|
||||
f"cross:{account}",
|
||||
("main" if main_to_sub else "sub") + ":" + from_account,
|
||||
("sub" if main_to_sub else "main") + ":" + to_account,
|
||||
"cross",
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
@@ -540,6 +542,46 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
conn.close()
|
||||
return jsonify({"ok": True, "history": items})
|
||||
|
||||
@app.route("/api/options/stats")
|
||||
@lr
|
||||
def api_options_stats():
|
||||
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
|
||||
|
||||
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
|
||||
return jsonify(
|
||||
{
|
||||
"ok": True,
|
||||
"total_closed": total_closed,
|
||||
"win_rate": win_rate,
|
||||
"profit_loss_ratio": profit_loss_ratio_from_averages(avg_win, avg_loss),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _start_monitor_thread(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
if app.extensions.get("options_monitor_started"):
|
||||
|
||||
@@ -63,34 +63,53 @@
|
||||
<h2>持仓</h2>
|
||||
<button type="button" class="btn-secondary" id="opt-refresh-positions">刷新</button>
|
||||
</div>
|
||||
<div class="options-pos-tabs" role="tablist">
|
||||
<button type="button" class="btn-secondary opt-pos-tab active" data-tab="live">当前持仓</button>
|
||||
<button type="button" class="btn-secondary opt-pos-tab" data-tab="history">期权历史</button>
|
||||
</div>
|
||||
<div id="opt-pos-live" class="panel-scroll pos-list options-pos-pane">
|
||||
<div class="pos-empty" id="opt-pos-empty">暂无持仓</div>
|
||||
<div id="opt-pos-cards"></div>
|
||||
</div>
|
||||
<div id="opt-pos-history" class="options-pos-pane" hidden>
|
||||
<div class="options-history-table-wrap">
|
||||
<table class="options-strike-table" id="opt-history-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>合约</th>
|
||||
<th>张数</th>
|
||||
<th>权利金</th>
|
||||
<th>状态</th>
|
||||
<th>盈亏</th>
|
||||
<th>时间</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="opt-history-tbody">
|
||||
<tr><td colspan="6" class="muted">加载中…</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="options-pos-stack">
|
||||
<div class="card-nested options-pos-subcard">
|
||||
<h3>当前持仓</h3>
|
||||
<div id="opt-pos-live" class="panel-scroll pos-list options-pos-live-pane">
|
||||
<div class="pos-empty" id="opt-pos-empty">暂无持仓</div>
|
||||
<div id="opt-pos-cards"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-nested options-pos-subcard options-pos-stats-card">
|
||||
<h3>数据统计</h3>
|
||||
<div class="options-stats-grid">
|
||||
<div class="options-stat-item">
|
||||
<span class="k">胜率</span>
|
||||
<span class="v" id="opt-stats-winrate">—</span>
|
||||
</div>
|
||||
<div class="options-stat-item">
|
||||
<span class="k">盈亏比</span>
|
||||
<span class="v" id="opt-stats-plr">—</span>
|
||||
</div>
|
||||
<div class="options-stat-item">
|
||||
<span class="k">已平笔数</span>
|
||||
<span class="v" id="opt-stats-closed">—</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-nested options-pos-subcard options-pos-history-card">
|
||||
<h3>期权历史</h3>
|
||||
<div class="options-history-table-wrap">
|
||||
<table class="options-strike-table" id="opt-history-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>合约</th>
|
||||
<th>张数</th>
|
||||
<th>权利金</th>
|
||||
<th>状态</th>
|
||||
<th>盈亏</th>
|
||||
<th>时间</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="opt-history-tbody">
|
||||
<tr><td colspan="6" class="muted">加载中…</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/options_panel.js?v=5"></script>
|
||||
<script src="/static/options_panel.js?v=6"></script>
|
||||
|
||||
@@ -44,10 +44,15 @@
|
||||
<option value="USDT" selected>USDT</option>
|
||||
<option value="USDC">USDC</option>
|
||||
</select>
|
||||
<select id="opt-set-cross-acct">
|
||||
<select id="opt-set-cross-from-acct">
|
||||
<option value="funding" selected>资金账户</option>
|
||||
<option value="trading">交易账户</option>
|
||||
</select>
|
||||
<span>→</span>
|
||||
<select id="opt-set-cross-to-acct">
|
||||
<option value="trading" selected>交易账户</option>
|
||||
<option value="funding">资金账户</option>
|
||||
</select>
|
||||
<select id="opt-set-cross-dir">
|
||||
<option value="sub_to_main" selected>子账户 → 主账户</option>
|
||||
<option value="main_to_sub">主账户 → 子账户</option>
|
||||
@@ -58,4 +63,4 @@
|
||||
<div id="opt-set-cross-msg" class="muted"></div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/options_settings.js?v=1"></script>
|
||||
<script src="/static/options_settings.js?v=2"></script>
|
||||
|
||||
Reference in New Issue
Block a user