feat(options): use premium profit RR instead of target index

单独期权与中控改为盈亏比×权利金触发买一平仓,默认2;不达标等到期。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-11 10:34:14 +08:00
parent 3b56e15fb1
commit 5c3969674a
13 changed files with 494 additions and 194 deletions
+108 -89
View File
@@ -321,7 +321,7 @@
[
"opt-sheets-amount",
"opt-eth-amount",
"opt-target-idx",
"opt-profit-rr",
].forEach(function (id) {
harden(document.getElementById(id));
});
@@ -894,11 +894,10 @@
}
function updateOrderEstimates() {
const levEl = document.getElementById("opt-order-leverage");
const valueEl = document.getElementById("opt-est-value");
const profitEl = document.getElementById("opt-est-profit");
const targetLevEl = document.getElementById("opt-est-leverage");
const targetEl = document.getElementById("opt-target-idx");
const levEl = document.getElementById("opt-order-leverage");
const rrEl = document.getElementById("opt-profit-rr");
const q = state.orderQuote;
if (!q || !q.ok || !q.can_open) {
if (levEl) levEl.textContent = "—";
@@ -907,7 +906,6 @@
profitEl.textContent = "—";
profitEl.className = "v";
}
if (targetLevEl) targetLevEl.textContent = "—";
return;
}
const sz = q.sizing || {};
@@ -916,30 +914,19 @@
const lev = calcContractLeverage(q.index_px, ethAmount, premium);
if (levEl) levEl.textContent = fmtLeverage(lev);
if (valueEl && profitEl && targetEl) {
const targetRaw = targetEl.value;
if (targetRaw === "" || targetRaw == null) {
if (valueEl && profitEl && rrEl) {
const rrRaw = rrEl.value;
const rr = rrRaw === "" || rrRaw == null ? NaN : Number(rrRaw);
if (!Number.isFinite(rr) || rr <= 0 || !(Number(premium) > 0)) {
valueEl.textContent = "—";
profitEl.textContent = "—";
profitEl.className = "v";
if (targetLevEl) targetLevEl.textContent = "—";
} else {
const value = estimateExpiryValue(q.opt_type, q.strike, Number(targetRaw), ethAmount);
const profit = estimateExpiryProfit(q.opt_type, q.strike, Number(targetRaw), ethAmount, premium);
if (value == null || Number.isNaN(value)) {
valueEl.textContent = "—";
} else {
valueEl.textContent = fmtUsdc(value) + " USDC";
}
if (profit == null || Number.isNaN(profit)) {
profitEl.textContent = "—";
profitEl.className = "v";
} else {
profitEl.textContent = fmtUsdcSigned(profit);
profitEl.className = "v " + pnlCls(profit);
}
const targetLev = calcContractLeverage(Number(targetRaw), ethAmount, premium);
if (targetLevEl) targetLevEl.textContent = fmtLeverage(targetLev);
const targetProfit = Number(premium) * rr;
const needRecycle = Number(premium) + targetProfit;
valueEl.textContent = fmtUsdc(needRecycle) + " USDC";
profitEl.textContent = fmtUsdcSigned(targetProfit);
profitEl.className = "v " + pnlCls(targetProfit);
}
}
}
@@ -1329,15 +1316,13 @@
} else if (mode === "sheets") {
body.sheets = parseInt(document.getElementById("opt-sheets-amount").value, 10);
}
const tgtRaw = (document.getElementById("opt-target-idx").value || "").trim();
if (tgtRaw !== "") {
const tgt = parseFloat(tgtRaw);
if (!Number.isFinite(tgt) || tgt <= 0) {
alert("目标位无效");
return false;
}
body.target_index = tgt;
const rrRaw = (document.getElementById("opt-profit-rr").value || "").trim();
const rr = rrRaw === "" ? 2 : parseFloat(rrRaw);
if (!Number.isFinite(rr) || rr <= 0) {
alert("盈亏比无效");
return false;
}
body.profit_rr = rr;
const d = await apiJson("/api/options/open", {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -1428,15 +1413,17 @@
return null;
}
function formatTargetEstimateHtml(optType, strike, targetIdx, ethAmount, premiumPaid) {
const value = estimateExpiryValue(optType, strike, targetIdx, ethAmount);
const profit = estimateExpiryProfit(optType, strike, targetIdx, ethAmount, premiumPaid);
if (value == null && profit == null) return "";
function formatRrEstimateHtml(rr, premiumPaid) {
const r = Number(rr);
const prem = Number(premiumPaid);
if (!Number.isFinite(r) || r <= 0 || !Number.isFinite(prem) || prem <= 0) return "";
const profit = Math.round(prem * r * 100) / 100;
const need = Math.round((prem + profit) * 100) / 100;
let html = '<span class="opt-target-est">';
html += '<span class="opt-target-est-item"><span class="k">价值</span><span class="v">' +
(value == null ? "—" : fmtUsdc(value) + " USDC") + "</span></span>";
html += '<span class="opt-target-est-item"><span class="k">预估盈利</span><span class="v ' + pnlCls(profit) + '">' +
(profit == null ? "—" : fmtUsdcSigned(profit)) + "</span></span>";
html += '<span class="opt-target-est-item"><span class="k">目标盈利</span><span class="v ' + pnlCls(profit) + '">' +
fmtUsdcSigned(profit) + "</span></span>";
html += '<span class="opt-target-est-item"><span class="k">需回收</span><span class="v">' +
fmtUsdc(need) + " USDC</span></span>";
html += "</span>";
return html;
}
@@ -1444,47 +1431,73 @@
function renderTargetDelegateRow(p) {
const inst = p.inst_id || "";
const hedgeTarget = p.hedge_plan_target || null;
if (hedgeTarget && Number(hedgeTarget.target_index) > 0) {
const side = (p.opt_type || hedgeTarget.opt_type || "").toUpperCase() === "P" ? "Put ≤" : "Call ≥";
if (hedgeTarget && hedgeTarget.managed_by === "hedge_plan") {
const rr = hedgeTarget.oo_profit_rr != null ? Number(hedgeTarget.oo_profit_rr) : null;
const armedTxt =
rr != null && Number.isFinite(rr) && rr > 0
? "盈亏比 ×" + fmt(rr, 2)
: hedgeTarget.target_index != null
? "目标 " + fmt(hedgeTarget.target_index, 1)
: "托管中";
return (
'<div class="opt-target-row opt-target-row--managed">' +
'<span class="opt-target-row-label">对冲计划</span>' +
'<span class="opt-target-armed">计划 #' +
hedgeTarget.plan_id +
" · " +
side +
" " +
fmt(hedgeTarget.target_index, 1) +
armedTxt +
"</span>" +
'<span class="muted opt-target-row-hint">进行中 · 由对冲计划监控,到位后仅平盈利腿</span>' +
'<span class="muted opt-target-row-hint">进行中 · 由对冲计划监控</span>' +
"</div>"
);
}
const tgt = p.target_index != null && p.target_index !== "" ? Number(p.target_index) : null;
const armed = tgt != null && Number.isFinite(tgt) && tgt > 0;
const ethAmt = posEthAmount(p);
const rrArmed =
p.profit_rr != null && p.profit_rr !== ""
? Number(p.profit_rr)
: p.target_monitor && p.target_monitor.profit_rr != null
? Number(p.target_monitor.profit_rr)
: null;
const armed = rrArmed != null && Number.isFinite(rrArmed) && rrArmed > 0;
const prem = p.premium_paid;
const draft =
state.targetDraftByInst[inst] != null
? String(state.targetDraftByInst[inst])
: armed
? String(rrArmed)
: "2";
const estHtml = armed
? formatTargetEstimateHtml(p.opt_type, p.strike, tgt, ethAmt, prem)
? formatRrEstimateHtml(rrArmed, prem)
: '<span class="opt-target-est opt-target-est--idle"></span>';
return (
'<div class="opt-target-row" data-inst="' + inst + '"' +
' data-opt-type="' + (p.opt_type || "") + '"' +
' data-strike="' + (p.strike != null ? p.strike : "") + '"' +
' data-eth="' + (ethAmt != null ? ethAmt : "") + '"' +
' data-prem="' + (prem != null ? prem : "") + '"' +
' data-armed-target="' + (armed ? tgt : "") + '">' +
'<div class="opt-target-row" data-inst="' +
inst +
'"' +
' data-prem="' +
(prem != null ? prem : "") +
'"' +
' data-armed-rr="' +
(armed ? rrArmed : "") +
'">' +
'<span class="opt-target-row-label">委托</span>' +
'<input type="number" class="opt-pos-target-input" data-inst="' + inst + '" step="0.1" min="0" placeholder="监控目标指数" value="' +
(state.targetDraftByInst[inst] != null ? String(state.targetDraftByInst[inst]) : "") + '">' +
'<button type="button" class="btn-secondary opt-target-set-btn" data-inst="' + inst + '">设定</button>' +
'<button type="button" class="btn-secondary opt-target-cancel-btn" data-inst="' + inst + '"' + (armed ? "" : " disabled") + ">取消</button>" +
(armed
? '<span class="opt-target-armed">目标 ' + fmt(tgt, 1) + "</span>"
: "") +
'<input type="number" class="opt-pos-target-input" data-inst="' +
inst +
'" step="0.1" min="0.1" placeholder="盈亏比" value="' +
draft +
'">' +
'<button type="button" class="btn-secondary opt-target-set-btn" data-inst="' +
inst +
'">设定</button>' +
'<button type="button" class="btn-secondary opt-target-cancel-btn" data-inst="' +
inst +
'"' +
(armed ? "" : " disabled") +
">取消</button>" +
(armed ? '<span class="opt-target-armed">盈亏比 ×' + fmt(rrArmed, 2) + "</span>" : "") +
estHtml +
'<span class="muted opt-target-row-hint">' +
(armed ? "监控中 · 到位按买一限价平" : "输入后设定 · 到位按买一限价平 · 到期即止损") +
(armed
? "监控中 · 买一浮盈达盈亏比后全平"
: "默认2 · 买一浮盈达盈亏比×权利金后全平 · 不达标等到期") +
"</span>" +
"</div>"
);
@@ -1496,20 +1509,14 @@
if (!est) return;
const inp = row.querySelector(".opt-pos-target-input");
const typed = inp ? String(inp.value || "").trim() : "";
const armed = row.getAttribute("data-armed-target") || "";
const targetRaw = typed !== "" ? typed : armed;
if (targetRaw === "") {
const armed = row.getAttribute("data-armed-rr") || "";
const rrRaw = typed !== "" ? typed : armed;
if (rrRaw === "") {
est.className = "opt-target-est opt-target-est--idle";
est.innerHTML = "";
return;
}
const html = formatTargetEstimateHtml(
row.getAttribute("data-opt-type"),
row.getAttribute("data-strike"),
targetRaw,
row.getAttribute("data-eth"),
row.getAttribute("data-prem")
);
const html = formatRrEstimateHtml(rrRaw, row.getAttribute("data-prem"));
if (!html) {
est.className = "opt-target-est opt-target-est--idle";
est.innerHTML = "";
@@ -1641,9 +1648,9 @@
const row = card ? card.querySelector(".opt-target-row") : null;
const inp = card ? card.querySelector(".opt-pos-target-input") : null;
const raw = inp ? String(inp.value || "").trim() : "";
const tgt = parseFloat(raw);
if (!Number.isFinite(tgt) || tgt <= 0) {
alert("请输入有效目标指数价");
const rr = raw === "" ? 2 : parseFloat(raw);
if (!Number.isFinite(rr) || rr <= 0) {
alert("请输入有效盈亏比(相对权利金,默认2)");
return;
}
if (btn) btn.disabled = true;
@@ -1651,17 +1658,16 @@
const d = await apiJson("/api/options/target", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ inst_id: inst, target_index: tgt }),
body: JSON.stringify({ inst_id: inst, profit_rr: rr }),
});
if (!d.ok) {
alert(d.msg || "设定失败");
return;
}
delete state.targetDraftByInst[inst];
if (inp) inp.value = "";
if (inp) inp.value = String(rr);
if (row) {
row.setAttribute("data-armed-target", String(tgt));
updatePosTargetEstimate(row);
row.setAttribute("data-armed-rr", String(rr));
}
await refreshAllPositions();
} finally {
@@ -1701,12 +1707,22 @@
}
box.hidden = false;
host.innerHTML = rows.map(function (t) {
const side = (t.opt_type || "").toUpperCase() === "P" ? "Put ≤" : "Call ≥";
const managed = t.managed_by === "hedge_plan";
let rule;
if (t.profit_rr != null && Number(t.profit_rr) > 0) {
rule = "盈亏比 ×" + fmt(t.profit_rr, 2);
} else if (t.oo_profit_rr != null && Number(t.oo_profit_rr) > 0) {
rule = "盈亏比 ×" + fmt(t.oo_profit_rr, 2);
} else if (t.target_index != null && Number(t.target_index) > 0) {
const side = (t.opt_type || "").toUpperCase() === "P" ? "Put ≤" : "Call ≥";
rule = side + " " + fmt(t.target_index, 1);
} else {
rule = "委托中";
}
return (
'<div class="opt-target-mon-item' + (managed ? " opt-target-mon-item--managed" : "") + '">' +
'<code class="opt-target-mon-inst" title="' + (t.inst_id || "") + '">' + (t.inst_id || "") + "</code>" +
'<span class="opt-target-mon-rule">' + side + " " + fmt(t.target_index, 1) + "</span>" +
'<span class="opt-target-mon-rule">' + rule + "</span>" +
(managed
? '<span class="opt-target-mon-managed">对冲计划 #' + (t.plan_id || "") + " · 进行中</span>"
: '<button type="button" class="btn-secondary opt-target-mon-cancel" data-inst="' + (t.inst_id || "") + '">取消</button>') +
@@ -1887,20 +1903,23 @@
paintPositions(list);
const fromPos = list.reduce(function (targets, p) {
if (!p) return targets;
if (p.target_index != null) {
if (p.profit_rr != null || p.target_index != null) {
targets.push({
id: p.target_monitor_id,
inst_id: p.inst_id,
opt_type: p.opt_type,
target_index: p.target_index,
profit_rr: p.profit_rr,
});
}
const hedgeTarget = p.hedge_plan_target;
if (hedgeTarget && hedgeTarget.target_index != null) {
if (hedgeTarget) {
targets.push({
inst_id: p.inst_id,
opt_type: p.opt_type || hedgeTarget.opt_type,
target_index: hedgeTarget.target_index,
oo_profit_rr: hedgeTarget.oo_profit_rr,
profit_rr: hedgeTarget.oo_profit_rr,
plan_id: hedgeTarget.plan_id,
managed_by: hedgeTarget.managed_by,
});
@@ -2286,17 +2305,17 @@
}
bindOrderDialogChrome();
["opt-sheets-amount", "opt-eth-amount", "opt-target-idx"].forEach(function (id) {
["opt-sheets-amount", "opt-eth-amount", "opt-profit-rr"].forEach(function (id) {
const el = document.getElementById(id);
if (!el) return;
el.addEventListener("change", function () {
if (id === "opt-target-idx") {
if (id === "opt-profit-rr") {
updateEstimatedProfit();
return;
}
if (state.selectedInst) selectContract(state.selectedInst, null, true);
});
if (id === "opt-target-idx") {
if (id === "opt-profit-rr") {
el.addEventListener("input", updateEstimatedProfit);
}
});
+23 -19
View File
@@ -219,38 +219,42 @@
const hint = closeGateHint(closePreview);
return hint ? '<div class="muted opt-bid-invalid-hint">' + hint + "</div>" : "";
})() +
(p.target_index != null
(p.profit_rr != null || p.target_index != null
? (function () {
const eth = p.eth_amount != null ? Number(p.eth_amount)
: (Number(p.pos) > 0 ? Number(p.pos) * Number(p.ct_mult || 0.01) : null);
const strike = Number(p.strike);
const tgt = Number(p.target_index);
const hedgeTarget = p.hedge_plan_target || null;
const managed = hedgeTarget && hedgeTarget.managed_by === "hedge_plan";
const rr =
managed && hedgeTarget.oo_profit_rr != null
? Number(hedgeTarget.oo_profit_rr)
: p.profit_rr != null
? Number(p.profit_rr)
: null;
const prem = Number(p.premium_paid);
let profit = null;
let value = null;
if (Number.isFinite(tgt) && Number.isFinite(strike) && eth > 0) {
const o = String(p.opt_type || "").toUpperCase();
const intrinsic = o === "C" ? Math.max(0, tgt - strike) : o === "P" ? Math.max(0, strike - tgt) : null;
if (intrinsic != null) {
value = Math.round(intrinsic * eth * 100) / 100;
if (!hidePnl && Number.isFinite(prem)) profit = Math.round((value - prem) * 100) / 100;
}
let need = null;
if (rr != null && Number.isFinite(rr) && rr > 0 && Number.isFinite(prem) && prem > 0) {
profit = Math.round(prem * rr * 100) / 100;
need = Math.round((prem + profit) * 100) / 100;
}
const profitTxt = profit == null ? "—" : ((profit > 0 ? "+" : "") + fmtUsdc(profit) + " USDC");
const profitCls = profit > 0 ? " pnl-pos" : profit < 0 ? " pnl-neg" : "";
const hedgeTarget = p.hedge_plan_target || null;
const managed = hedgeTarget && hedgeTarget.managed_by === "hedge_plan";
const profitSpan = hidePnl
? ""
: '<span class="pos-value' + profitCls + '">预估盈利 ' + profitTxt + "</span>";
: '<span class="pos-value' + profitCls + '">目标盈利 ' + profitTxt + "</span>";
const ruleTxt =
rr != null && Number.isFinite(rr) && rr > 0
? "盈亏比 ×" + fmt(rr, 2)
: p.target_index != null
? "目标 " + fmt(p.target_index, 1)
: "委托中";
return (
'<div class="opt-target-row opt-target-row--ro' + (managed ? " opt-target-row--managed" : "") + '">' +
'<span class="opt-target-row-label">' + (managed ? "对冲计划 #" + hedgeTarget.plan_id : "委托") + "</span>" +
'<span class="pos-value">目标 ' + fmt(p.target_index, 1) + "</span>" +
'<span class="pos-value">价值 ' + (value == null ? "—" : fmtUsdc(value) + " USDC") + "</span>" +
'<span class="pos-value">' + ruleTxt + "</span>" +
(need != null ? '<span class="pos-value">需回收 ' + fmtUsdc(need) + " USDC</span>" : "") +
profitSpan +
'<span class="muted opt-target-row-hint">' +
(managed ? "进行中 · 由对冲计划监控,到位后仅平盈利腿" : "监控中 · 到位按买一限价平") +
(managed ? "进行中 · 由对冲计划监控" : "监控中 · 买一浮盈达盈亏比后全平") +
"</span></div>"
);
})()