fix: OKX options buy/close orders with tick alignment and reduceOnly
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2390,4 +2390,15 @@ html[data-theme="light"] .settings-export-link {
|
||||
font-size: 0.85rem;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
#opt-order-msg.opt-error,
|
||||
.opt-error {
|
||||
color: #ff6b6b;
|
||||
}
|
||||
.opt-row-actions {
|
||||
white-space: nowrap;
|
||||
}
|
||||
.opt-row-actions .btn-primary,
|
||||
.opt-row-actions .btn-secondary {
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,13 @@
|
||||
return r.json();
|
||||
}
|
||||
|
||||
function scrollToOrderPanel() {
|
||||
const panel = document.getElementById("opt-order-panel");
|
||||
if (panel && panel.style.display !== "none") {
|
||||
panel.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshBalances() {
|
||||
const d = await apiJson("/api/options/balances");
|
||||
if (!d.ok) return;
|
||||
@@ -81,7 +88,10 @@
|
||||
"<td><code>" + c.inst_id + "</code></td>" +
|
||||
"<td>" + fmt(c.ask, 4) + "</td>" +
|
||||
"<td>" + fmt(c.bid, 4) + "</td>" +
|
||||
'<td><button type="button" class="btn-secondary opt-pick-btn" data-inst="' + c.inst_id + '">选择</button></td>';
|
||||
'<td class="opt-row-actions">' +
|
||||
'<button type="button" class="btn-secondary opt-pick-btn" data-inst="' + c.inst_id + '">选择</button> ' +
|
||||
'<button type="button" class="btn-primary opt-buy-btn" data-inst="' + c.inst_id + '">买入</button>' +
|
||||
"</td>";
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
tbody.querySelectorAll(".opt-pick-btn").forEach(function (btn) {
|
||||
@@ -89,6 +99,12 @@
|
||||
selectContract(btn.getAttribute("data-inst"));
|
||||
});
|
||||
});
|
||||
tbody.querySelectorAll(".opt-buy-btn").forEach(function (btn) {
|
||||
btn.addEventListener("click", async function () {
|
||||
await selectContract(btn.getAttribute("data-inst"));
|
||||
await openPosition();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function selectContract(instId) {
|
||||
@@ -108,7 +124,19 @@
|
||||
document.getElementById("opt-order-sheets").textContent = sz.sheets != null ? sz.sheets : "—";
|
||||
document.getElementById("opt-order-eth").textContent = sz.eth_amount != null ? sz.eth_amount : "—";
|
||||
document.getElementById("opt-order-premium").textContent = sz.total_premium != null ? fmt(sz.total_premium, 4) + " USDC" : "—";
|
||||
document.getElementById("opt-order-msg").textContent = sz.ok === false ? (sz.msg || "") : "";
|
||||
const msgEl = document.getElementById("opt-order-msg");
|
||||
if (!d.ok) {
|
||||
msgEl.textContent = d.msg || "报价失败";
|
||||
msgEl.classList.add("opt-error");
|
||||
} else if (sz.ok === false) {
|
||||
msgEl.textContent = sz.msg || "";
|
||||
msgEl.classList.add("opt-error");
|
||||
} else {
|
||||
msgEl.textContent = "";
|
||||
msgEl.classList.remove("opt-error");
|
||||
}
|
||||
scrollToOrderPanel();
|
||||
return d;
|
||||
}
|
||||
|
||||
async function loadChain() {
|
||||
@@ -123,25 +151,69 @@
|
||||
}
|
||||
|
||||
async function openPosition() {
|
||||
if (!state.selectedInst) return;
|
||||
const mode = document.querySelector('input[name="opt-size-mode"]:checked').value;
|
||||
const body = {
|
||||
inst_id: state.selectedInst,
|
||||
mode: mode,
|
||||
signal_note: document.getElementById("opt-signal-note").value || "",
|
||||
};
|
||||
if (mode === "eth_amount") {
|
||||
body.eth_amount = parseFloat(document.getElementById("opt-eth-amount").value);
|
||||
if (!state.selectedInst) {
|
||||
alert("请先选择合约");
|
||||
return;
|
||||
}
|
||||
const d = await apiJson("/api/options/open", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
document.getElementById("opt-order-msg").textContent = d.ok ? "下单已提交" : (d.msg || "失败");
|
||||
if (d.ok) {
|
||||
refreshBalances();
|
||||
const btn = document.getElementById("opt-open-btn");
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const mode = document.querySelector('input[name="opt-size-mode"]:checked').value;
|
||||
const body = {
|
||||
inst_id: state.selectedInst,
|
||||
mode: mode,
|
||||
signal_note: document.getElementById("opt-signal-note").value || "",
|
||||
};
|
||||
if (mode === "eth_amount") {
|
||||
body.eth_amount = parseFloat(document.getElementById("opt-eth-amount").value);
|
||||
}
|
||||
const d = await apiJson("/api/options/open", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const msgEl = document.getElementById("opt-order-msg");
|
||||
msgEl.textContent = d.ok ? "下单已提交,可在 OKX 委托中查看" : (d.msg || "失败");
|
||||
msgEl.classList.toggle("opt-error", !d.ok);
|
||||
if (d.ok) {
|
||||
refreshBalances();
|
||||
refreshPositions();
|
||||
} else {
|
||||
alert(d.msg || "下单失败");
|
||||
}
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function closePosition(inst, btn) {
|
||||
const q = await apiJson("/api/options/quote?inst_id=" + encodeURIComponent(inst) + "&mode=budget_full");
|
||||
if (!q.ok) {
|
||||
alert(q.msg || "获取买一价失败");
|
||||
return;
|
||||
}
|
||||
const bid = q.bid;
|
||||
if (bid == null || bid <= 0) {
|
||||
alert("暂无买一价,请稍后在 OKX App 平仓或等盘口恢复");
|
||||
return;
|
||||
}
|
||||
if (!confirm("限价卖出 @ 买一 " + fmt(bid, 4) + "(每 1 ETH/BTC)?\n合约:" + inst)) return;
|
||||
if (btn) btn.disabled = true;
|
||||
try {
|
||||
const r = await apiJson("/api/options/close", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ inst_id: inst }),
|
||||
});
|
||||
if (r.ok) {
|
||||
alert("平仓单已提交" + (r.bid != null ? " @ " + fmt(r.bid, 4) : ""));
|
||||
} else {
|
||||
alert(r.msg || "平仓失败");
|
||||
}
|
||||
refreshPositions();
|
||||
refreshBalances();
|
||||
} finally {
|
||||
if (btn) btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,17 +240,8 @@
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
tbody.querySelectorAll(".opt-close-btn").forEach(function (btn) {
|
||||
btn.addEventListener("click", async function () {
|
||||
const inst = btn.getAttribute("data-inst");
|
||||
if (!confirm("确认限价卖出 @ 买一?")) return;
|
||||
const r = await apiJson("/api/options/close", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ inst_id: inst }),
|
||||
});
|
||||
alert(r.ok ? "平仓单已提交" : (r.msg || "失败"));
|
||||
refreshPositions();
|
||||
refreshBalances();
|
||||
btn.addEventListener("click", function () {
|
||||
closePosition(btn.getAttribute("data-inst"), btn);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user