/**
* 中控策略对比:同风险额 R 下 合约 / 单期权 / 期期7:3
*/
(function () {
const page = document.getElementById("page-compare");
if (!page) return;
let inited = false;
let calcTimer = null;
function $(id) {
return document.getElementById(id);
}
function esc(s) {
return String(s == null ? "" : s)
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """);
}
function num(id) {
const el = $(id);
if (!el) return null;
const n = Number(el.value);
return Number.isFinite(n) ? n : null;
}
function text(id) {
const el = $(id);
return el ? String(el.value || "").trim() : "";
}
function fmtU(v) {
if (v == null || !Number.isFinite(Number(v))) return "—";
const n = Number(v);
const abs = Math.abs(n).toFixed(2);
if (Math.abs(n) < 1e-9) return "0.00U";
return (n > 0 ? "+" : "-") + abs + "U";
}
function pnlClass(v) {
const n = Number(v);
if (!Number.isFinite(n) || Math.abs(n) < 1e-9) return "";
return n > 0 ? "cmp-pnl-pos" : "cmp-pnl-neg";
}
function setStatus(msg, isErr) {
const el = $("cmp-status");
if (!el) return;
el.textContent = msg || "";
el.className = "toolbar-meta" + (isErr ? " err" : "");
}
function syncDirectionDefaults() {
const dir = text("cmp-direction") || "long";
const isLong = dir === "long";
const optType = $("cmp-opt-type");
const mainType = $("cmp-hedge-main-type");
const sideType = $("cmp-hedge-side-type");
if (optType && !optType.dataset.touched) optType.value = isLong ? "C" : "P";
if (mainType && !mainType.dataset.touched) mainType.value = isLong ? "C" : "P";
if (sideType && !sideType.dataset.touched) sideType.value = isLong ? "P" : "C";
}
function collectPayload() {
const tp = num("cmp-tp");
return {
base: text("cmp-base") || "ETH",
direction: text("cmp-direction") || "long",
entry: num("cmp-entry"),
sl: num("cmp-sl"),
tp: tp,
risk_u: num("cmp-risk"),
tp_opt: num("cmp-tp-opt") != null ? num("cmp-tp-opt") : tp,
tp_hedge: num("cmp-tp-hedge") != null ? num("cmp-tp-hedge") : tp,
option: {
opt_type: text("cmp-opt-type") || "C",
strike: num("cmp-opt-strike"),
ask: num("cmp-opt-ask"),
},
hedge: {
main: {
opt_type: text("cmp-hedge-main-type") || "C",
strike: num("cmp-hedge-main-strike"),
ask: num("cmp-hedge-main-ask"),
},
side: {
opt_type: text("cmp-hedge-side-type") || "P",
strike: num("cmp-hedge-side-strike"),
ask: num("cmp-hedge-side-ask"),
},
},
};
}
function renderSummaryCards(data) {
const box = $("cmp-summary");
if (!box) return;
const perp = data.perp || {};
const opt = data.option || {};
const hedge = data.hedge || {};
const cards = [];
cards.push(` ${esc(opt.msg || "输入不完整")} ${esc(hedge.msg || "输入不完整")}单独合约
单独期权 · ${esc(opt.opt_type)} ${esc(opt.strike)}
单独期权
期期对冲 7:3
期期对冲
| 路径 | 单独合约 | 单独期权 | 期期对冲 |
|---|---|---|---|
| A 干净止盈 盈利能力主对比 |
${cell(perp.path_a_tp)} | ${opt ? cell(opt.path_a_tp) : dash} | ${hedge ? cell(hedge.path_a_tp) : dash} |
| B 打止损 合约实现亏损;期权另注最坏 |
${cell(perp.path_b_sl)} | ${ opt ? cell(opt.path_b_sl, "最坏到期亏满权利金 " + fmtU(opt.path_b_worst)) : dash } | ${ hedge ? cell(hedge.path_b_sl, "最坏双腿归零 " + fmtU(hedge.path_b_worst)) : dash } |
| C 先止损再去止盈 合约踏空对照 |
${cell( perp.path_c_realized, "踏空未拿到 " + fmtU(perp.path_c_missed) )} | ${opt ? cell(opt.path_c_hold_to_tp, opt.path_c_note || "") : dash} | ${hedge ? cell(hedge.path_c_hold_to_tp, hedge.path_c_note || "") : dash} |
${esc(rec.reason || "")}
${(data.notes || []).map(esc).join(" · ")}