Use daily open as perp-hedge entry and toggle buy-straddle vs perp overlays.

Amp-stats now prices premium from each day's open, and the form switches mutually between straddle and perpetual-options对照.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-28 14:31:17 +08:00
parent d049c5d317
commit 2ce67da8e8
6 changed files with 191 additions and 97 deletions
+48 -21
View File
@@ -39,7 +39,22 @@
return (n * 100).toFixed(1) + "%";
}
function overlayMode() {
return el("amp-overlay-mode")?.value || "straddle";
}
function syncOverlayMode() {
const isPerp = overlayMode() === "perp";
page.querySelectorAll(".amp-overlay-straddle").forEach((n) => n.classList.toggle("hidden", isPerp));
page.querySelectorAll(".amp-overlay-perp").forEach((n) => n.classList.toggle("hidden", !isPerp));
el("amp-overlay-straddle-block")?.classList.toggle("hidden", isPerp);
el("amp-overlay-perp-block")?.classList.toggle("hidden", !isPerp);
const col = el("amp-col-pnl");
if (col) col.textContent = isPerp ? "永期盈亏" : "收益";
}
function readPremium() {
if (overlayMode() !== "straddle") return null;
const raw = (el("amp-straddle-premium")?.value || "").trim();
if (!raw) return null;
const n = Number(raw);
@@ -48,6 +63,7 @@
}
function readTakeProfit() {
if (overlayMode() !== "straddle") return null;
const raw = (el("amp-take-profit")?.value || "").trim();
if (!raw) return null;
const n = Number(raw);
@@ -67,14 +83,13 @@
}
function readPerpHedge() {
const spot = readNum("amp-hedge-spot");
if (overlayMode() !== "perp") return null;
const target = readNum("amp-hedge-target");
const perpLev = readNum("amp-hedge-perp-lev");
const optLev = readNum("amp-hedge-opt-lev");
if (spot == null || target == null || perpLev == null || optLev == null) return null;
if (spot <= 0 || target < 0 || perpLev <= 0 || optLev <= 0) return null;
if (target == null || perpLev == null || optLev == null) return null;
if (target < 0 || perpLev <= 0 || optLev <= 0) return null;
return {
spot,
target_profit_u: target,
perp_leverage: perpLev,
option_leverage: optLev,
@@ -130,6 +145,7 @@
const box = el("amp-summary");
if (!box) return;
const s = summary || {};
syncOverlayMode();
if (!s.sample_count) {
box.innerHTML = '<p class="amp-empty">暂无汇总</p>';
renderStraddle(null);
@@ -147,8 +163,13 @@
`<div><span class="amp-sum-k">涨/跌窗占比</span><span class="amp-sum-v">${esc(s.up_day_ratio)} / ${esc(s.down_day_ratio)}</span></div>` +
`<div><span class="amp-sum-k">价源</span><span class="amp-sum-v">${esc(result && result.price_source)}</span></div>` +
`</div>`;
renderStraddle(s.straddle);
renderPerpHedge(s.perp_hedge);
if (overlayMode() === "perp") {
renderStraddle(null);
renderPerpHedge(s.perp_hedge);
} else {
renderPerpHedge(null);
renderStraddle(s.straddle);
}
}
function renderStraddle(st) {
@@ -189,7 +210,7 @@
if (!box) return;
if (!ph) {
box.innerHTML =
'<p class="amp-empty">填写「永期·现价 / 目标 / 杠杆」后计算;对照所需点数达标天数与组合盈亏(永续多1币+买期权)</p>';
'<p class="amp-empty">填写「目标 / 杠杆」后计算;入场按日开盘;对照所需点数达标与组合盈亏(永续多1币+买期权)</p>';
return;
}
const err =
@@ -198,8 +219,9 @@
: "";
box.innerHTML =
`<div class="amp-sum-grid">` +
`<div><span class="amp-sum-k">入场</span><span class="amp-sum-v">按日开盘 · 推点数中位 ${esc(ph.spot)}</span></div>` +
`<div><span class="amp-sum-k">比例 / 期权仓</span><span class="amp-sum-v">${esc(ph.ratio_label)} · ${esc(ph.opt_coins)} 币</span></div>` +
`<div><span class="amp-sum-k">单币/总权利金</span><span class="amp-sum-v">${esc(ph.prem_per_coin)} / ${esc(ph.premium_total)}</span></div>` +
`<div><span class="amp-sum-k">单币/总权利金(中位)</span><span class="amp-sum-v">${esc(ph.prem_per_coin)} / ${esc(ph.premium_total)}</span></div>` +
`<div><span class="amp-sum-k">A所需点数(永续对)</span><span class="amp-sum-v">${esc(ph.move_a)}</span></div>` +
`<div><span class="amp-sum-k">A达标</span><span class="amp-sum-v">${esc(ph.hit_a_days)} 天 · ${esc(pct(ph.hit_a_ratio))}</span></div>` +
`<div><span class="amp-sum-k">B所需点数(组合)</span><span class="amp-sum-v">${esc(ph.move_b)}</span></div>` +
@@ -225,20 +247,19 @@
const body = el("amp-table-body");
const pager = el("amp-pager");
if (!body) return;
const isPerp = overlayMode() === "perp";
syncOverlayMode();
const rows = (pagePayload && pagePayload.rows) || [];
if (!rows.length) {
body.innerHTML = '<tr><td colspan="12" class="amp-empty">暂无数据</td></tr>';
body.innerHTML = '<tr><td colspan="11" class="amp-empty">暂无数据</td></tr>';
} else {
body.innerHTML = rows
.map((r) => {
const profit =
r.profit == null || r.profit === ""
const pnlVal = isPerp ? r.perp_hedge_pnl : r.profit;
const pnlCell =
pnlVal == null || pnlVal === ""
? "—"
: `<span class="amp-pnl ${pnlClass(r.profit)}">${esc(r.profit)}</span>`;
const hedgePnl =
r.perp_hedge_pnl == null || r.perp_hedge_pnl === ""
? "—"
: `<span class="amp-pnl ${pnlClass(r.perp_hedge_pnl)}">${esc(r.perp_hedge_pnl)}</span>`;
: `<span class="amp-pnl ${pnlClass(pnlVal)}">${esc(pnlVal)}</span>`;
const trClass = r.is_weekend ? ' class="amp-row-weekend"' : "";
return (
`<tr${trClass}>` +
@@ -252,8 +273,7 @@
`<td>${esc(r.down_points)}</td>` +
`<td><strong>${esc(r.amplitude)}</strong></td>` +
`<td>${esc(r.change)}</td>` +
`<td>${profit}</td>` +
`<td>${hedgePnl}</td>` +
`<td>${pnlCell}</td>` +
`</tr>`
);
})
@@ -389,7 +409,6 @@
function appendHedgeQuery(q) {
const h = readPerpHedge();
if (!h) return;
q.set("hedge_spot", String(h.spot));
q.set("hedge_target", String(h.target_profit_u));
q.set("hedge_perp_lev", String(h.perp_leverage));
q.set("hedge_opt_lev", String(h.option_leverage));
@@ -493,13 +512,16 @@
}
const h = lastResult.perp_hedge;
if (h && typeof h === "object") {
if (h.spot != null && el("amp-hedge-spot")) el("amp-hedge-spot").value = String(h.spot);
if (el("amp-overlay-mode")) el("amp-overlay-mode").value = "perp";
if (h.target_profit_u != null && el("amp-hedge-target")) el("amp-hedge-target").value = String(h.target_profit_u);
if (h.perp_leverage != null && el("amp-hedge-perp-lev")) el("amp-hedge-perp-lev").value = String(h.perp_leverage);
if (h.option_leverage != null && el("amp-hedge-opt-lev")) el("amp-hedge-opt-lev").value = String(h.option_leverage);
if (h.ratio_perp != null && el("amp-hedge-ratio-perp")) el("amp-hedge-ratio-perp").value = String(h.ratio_perp);
if (h.ratio_opt != null && el("amp-hedge-ratio-opt")) el("amp-hedge-ratio-opt").value = String(h.ratio_opt);
} else if (lastResult.straddle_premium != null && el("amp-overlay-mode")) {
el("amp-overlay-mode").value = "straddle";
}
syncOverlayMode();
pageNo = 1;
setStatus("已载入历史 " + id);
await reframe(true);
@@ -520,10 +542,13 @@
el("amp-btn-compute")?.addEventListener("click", () => void compute(true));
el("amp-btn-save")?.addEventListener("click", () => void saveHistory());
el("amp-btn-download")?.addEventListener("click", downloadCurrent);
el("amp-overlay-mode")?.addEventListener("change", () => {
syncOverlayMode();
void reframe(true);
});
el("amp-straddle-premium")?.addEventListener("input", scheduleReframe);
el("amp-take-profit")?.addEventListener("input", scheduleReframe);
[
"amp-hedge-spot",
"amp-hedge-target",
"amp-hedge-perp-lev",
"amp-hedge-opt-lev",
@@ -532,6 +557,7 @@
].forEach((id) => el(id)?.addEventListener("input", scheduleReframe));
el("amp-weekend-filter")?.addEventListener("change", () => void reframe(true));
syncCustomDays();
syncOverlayMode();
}
window.hubAmpStatsPage = {
@@ -539,6 +565,7 @@
bind();
setView("stats");
setStatus("");
syncOverlayMode();
renderStraddle(null);
renderPerpHedge(null);
},