cfc703ae5b
Embed capture-phase form handler and allowManualOrderSubmit both submitted /add_order; skip custom forms and use a single fetch reload path. Co-authored-by: Cursor <cursoragent@cursor.com>
1356 lines
62 KiB
HTML
1356 lines
62 KiB
HTML
<script>
|
||
const JOURNAL_ENTRY_REASON_OPTIONS = {{ entry_reason_options | tojson }};
|
||
const JOURNAL_ENTRY_REASON_OTHER = {{ entry_reason_other_value | tojson }};
|
||
|
||
function reloadInstancePage(){
|
||
if(document.body && document.body.getAttribute("data-embed-shell") === "1" && window.InstanceEmbed){
|
||
window.InstanceEmbed.reloadCurrentTab();
|
||
return;
|
||
}
|
||
window.location.href = `${window.location.pathname}?_ts=${Date.now()}`;
|
||
}
|
||
|
||
function syncJournalEntryReasonOtherUi(){
|
||
const form = document.getElementById("journal-form");
|
||
if(!form) return;
|
||
const sel = form.querySelector('[name="entry_reason"]');
|
||
const box = form.querySelector('[name="entry_reason_custom"]');
|
||
if(!sel || !box) return;
|
||
if(sel.value === JOURNAL_ENTRY_REASON_OTHER){
|
||
box.style.display = "";
|
||
box.required = true;
|
||
} else {
|
||
box.style.display = "none";
|
||
box.required = false;
|
||
box.value = "";
|
||
}
|
||
}
|
||
|
||
function validateJournalEntryReason(){
|
||
const form = document.getElementById("journal-form");
|
||
if(!form) return true;
|
||
const sel = form.querySelector('[name="entry_reason"]');
|
||
const box = form.querySelector('[name="entry_reason_custom"]');
|
||
if(!sel || !sel.value){
|
||
alert("请选择开仓类型");
|
||
return false;
|
||
}
|
||
if(sel.value === JOURNAL_ENTRY_REASON_OTHER){
|
||
const t = (box && box.value || "").trim();
|
||
if(!t){
|
||
alert("选择「其他」时请填写自定义开仓类型说明");
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
function showImage(src){document.getElementById("bigImg").src=src;document.getElementById("imgModal").style.display="flex";}
|
||
function closeModal(){document.getElementById("imgModal").style.display="none";}
|
||
function setDetailModalFullscreen(on){
|
||
const modal = document.getElementById("detailModal");
|
||
if(modal){ modal.classList.toggle("fullscreen", !!on); }
|
||
}
|
||
function forceCloseDetailModal(){
|
||
const modal = document.getElementById("detailModal");
|
||
if(modal){ modal.style.display = "none"; modal.classList.remove("fullscreen"); }
|
||
}
|
||
function closeDetailModal(e){if(e.target && e.target.id==="detailModal"){forceCloseDetailModal();}}
|
||
function expandDetailToFullscreen(){ setDetailModalFullscreen(true); }
|
||
function toggleReviewCardFullscreen(){
|
||
const card = document.getElementById("review-card");
|
||
if(!card) return;
|
||
const on = !card.classList.contains("is-fullscreen");
|
||
card.classList.toggle("is-fullscreen", on);
|
||
document.body.classList.toggle("review-card-fullscreen-open", on);
|
||
const btn = document.getElementById("review-card-fs-btn");
|
||
if(btn){ btn.textContent = on ? "退出全屏" : "全屏"; }
|
||
}
|
||
document.addEventListener("keydown", function(e){
|
||
if(e.key !== "Escape") return;
|
||
const card = document.getElementById("review-card");
|
||
if(card && card.classList.contains("is-fullscreen")){ toggleReviewCardFullscreen(); }
|
||
});
|
||
function setAiReviewMarkdown(el, rawText){
|
||
if(!el) return;
|
||
if(window.AiReviewRender && AiReviewRender.setElementMarkdown){
|
||
AiReviewRender.setElementMarkdown(el, rawText || "");
|
||
} else {
|
||
el.classList.remove("ai-result-md");
|
||
el.innerText = rawText || "";
|
||
}
|
||
}
|
||
function setDetailBodyPlain(text){
|
||
const body = document.getElementById("detailBody");
|
||
if(!body) return;
|
||
body.classList.remove("md-review");
|
||
body.innerText = text || "";
|
||
}
|
||
function setDetailBodyMarkdown(text){
|
||
if(window.InstanceUI && InstanceUI.clearDetailActions) InstanceUI.clearDetailActions();
|
||
const body = document.getElementById("detailBody");
|
||
if(!body) return;
|
||
body.classList.remove("trade-record-detail-wrap", "journal-detail-meta");
|
||
if(window.AiReviewRender && AiReviewRender.setElementMarkdown){
|
||
body.classList.add("md-review");
|
||
AiReviewRender.setElementMarkdown(body, text || "");
|
||
} else {
|
||
setDetailBodyPlain(text);
|
||
}
|
||
}
|
||
function openAiInlineResultFullscreen(title, elementId){
|
||
const el = document.getElementById(elementId || "daily_result");
|
||
const text = (window.AiReviewRender && AiReviewRender.getElementMarkdown)
|
||
? String(AiReviewRender.getElementMarkdown(el) || "").trim()
|
||
: String((el && el.innerText) || "").trim();
|
||
if(!text){ alert("暂无内容"); return; }
|
||
document.getElementById("detailTitle").innerText = title || "AI复盘";
|
||
setDetailBodyMarkdown(text);
|
||
const imgEl = document.getElementById("detailImage");
|
||
imgEl.src = "";
|
||
imgEl.style.display = "none";
|
||
setDetailModalFullscreen(true);
|
||
document.getElementById("detailModal").style.display = "flex";
|
||
}
|
||
|
||
const journalCache = {};
|
||
const reviewCache = {};
|
||
|
||
function formatJournalExitOneLine(o){
|
||
const t = (o.early_exit_trigger || "").trim();
|
||
const n = (o.early_exit_note || "").trim();
|
||
if(t === "手动平仓") return n || (o.exit_reason || "").trim() || "无";
|
||
if(t) return t;
|
||
return (o.exit_reason || "").trim() || (o.early_exit_reason || "").trim() || "无";
|
||
}
|
||
|
||
function openJournalDetail(id){
|
||
InstanceUI.openJournalDetailModal(id, journalCache, formatJournalExitOneLine);
|
||
}
|
||
|
||
function openReviewDetail(id, fullscreen){
|
||
const r = reviewCache[id];
|
||
if(!r){ return; }
|
||
document.getElementById("detailTitle").innerText = `${r.review_type === "daily" ? "日复盘" : "周复盘"}|${r.target_date || "-"}`;
|
||
setDetailBodyMarkdown(r.content || "");
|
||
const imgEl = document.getElementById("detailImage");
|
||
imgEl.src = "";
|
||
imgEl.style.display = "none";
|
||
setDetailModalFullscreen(!!fullscreen);
|
||
document.getElementById("detailModal").style.display = "flex";
|
||
}
|
||
|
||
function deleteJournal(id){
|
||
if(!confirm("确定删除该交易复盘记录?")) return;
|
||
fetch(`/delete_journal/${id}`,{method:"POST"}).then(()=>loadJournals());
|
||
}
|
||
|
||
function deleteReview(id){
|
||
if(!confirm("确定删除该AI复盘?")) return;
|
||
fetch(`/delete_review/${id}`,{method:"POST"}).then(()=>loadReviews());
|
||
}
|
||
|
||
function deleteTradeRecord(id){
|
||
if(!confirm("确定删除这条交易记录?")) return;
|
||
fetch(`/delete_trade_record/${id}`,{method:"POST"})
|
||
.then(r=>r.json())
|
||
.then(data=>{
|
||
if(data && data.ok){
|
||
const row = document.getElementById(`trade-row-${id}`);
|
||
if(row){ row.remove(); return; }
|
||
}
|
||
reloadInstancePage();
|
||
})
|
||
.catch(()=>{ reloadInstancePage(); });
|
||
}
|
||
|
||
function normalizeBeijingDatetimeString(v){
|
||
const raw = String(v || "").trim().replace("T"," ");
|
||
const m = raw.match(/^(\d{4}-\d{2}-\d{2})[ ](\d{2}:\d{2})(:\d{2})?/);
|
||
if(!m) return "";
|
||
const sec = m[3] ? m[3].slice(1,3) : "00";
|
||
return `${m[1]} ${m[2]}:${sec}`;
|
||
}
|
||
|
||
function toggleReviewMode(){
|
||
const on = !!(document.getElementById("review-mode-toggle") || {}).checked;
|
||
document.querySelectorAll(".review-edit-btn").forEach(btn=>{
|
||
btn.disabled = !on;
|
||
});
|
||
}
|
||
|
||
function editTradeRecordReview(t){
|
||
if(!t) return;
|
||
const opened = prompt("开仓时间(YYYY-MM-DD HH:MM:SS)", normalizeBeijingDatetimeString(t.opened_at || ""));
|
||
if(opened === null) return;
|
||
const closed = prompt("平仓时间(YYYY-MM-DD HH:MM:SS)", normalizeBeijingDatetimeString(t.closed_at || ""));
|
||
if(closed === null) return;
|
||
const stopLoss = prompt("止损价格(核对后用于统计)", formatPriceForInput(t.stop_loss));
|
||
if(stopLoss === null) return;
|
||
const takeProfit = prompt("止盈价格(核对后用于统计)", formatPriceForInput(t.take_profit));
|
||
if(takeProfit === null) return;
|
||
const pnl = prompt("最终盈亏(可手工核对后填写)", String(t.pnl_amount ?? ""));
|
||
if(pnl === null) return;
|
||
const result = prompt("结果(止盈/止损/保本止盈/移动止盈/手动平仓/时间平仓)", String(t.result || ""));
|
||
if(result === null) return;
|
||
const note = prompt("备注(可空)", String(t.miss_reason || "")) ?? "";
|
||
const entryHint = "开仓类型:五种固定整句、或自定义说明(2000字内;与复盘表单一致;留空=本次不改该项)";
|
||
const entryIn = prompt(entryHint, String(t.effective_entry_reason || ""));
|
||
if(entryIn === null) return;
|
||
const payload = {
|
||
id: t.id,
|
||
reviewed_opened_at: normalizeBeijingDatetimeString(opened),
|
||
reviewed_closed_at: normalizeBeijingDatetimeString(closed),
|
||
reviewed_stop_loss: stopLoss,
|
||
reviewed_take_profit: takeProfit,
|
||
reviewed_pnl_amount: pnl,
|
||
reviewed_result: String(result || "").trim(),
|
||
reviewed_miss_reason: String(note || "").trim()
|
||
};
|
||
const entryTrim = String(entryIn || "").trim();
|
||
if(entryTrim) payload.reviewed_entry_reason = entryTrim;
|
||
fetch("/api/trade_record_review_update",{
|
||
method:"POST",
|
||
headers:{"Content-Type":"application/json"},
|
||
body: JSON.stringify(payload)
|
||
})
|
||
.then(r=>r.json().then(d=>({status:r.status, data:d})))
|
||
.then(({status,data})=>{
|
||
if(status >= 400 || !data.ok){
|
||
alert((data && data.msg) || "核对保存失败");
|
||
return;
|
||
}
|
||
alert(`核对已保存:持仓分钟=${data.hold_minutes} 实际RR=${data.actual_rr ?? "-"}`);
|
||
reloadInstancePage();
|
||
})
|
||
.catch(()=>alert("核对保存请求失败"));
|
||
}
|
||
|
||
|
||
function deleteKeyMonitor(id){
|
||
if(!confirm("删除该关键位?将写入下方历史并刷新页面。")) return;
|
||
fetch(`/delete_key_monitor/${id}`,{method:"POST"})
|
||
.then(r=>r.json())
|
||
.then(data=>{
|
||
reloadInstancePage();
|
||
})
|
||
.catch(()=>{ reloadInstancePage(); });
|
||
}
|
||
|
||
function deleteKeyHistory(id){
|
||
if(!confirm("确定删除这条关键位历史?")) return;
|
||
fetch(`/delete_key_history/${id}`,{method:"POST"})
|
||
.then(r=>r.json())
|
||
.then(()=>{
|
||
reloadInstancePage();
|
||
})
|
||
.catch(()=>{ reloadInstancePage(); });
|
||
}
|
||
|
||
function listWindowQueryString(){
|
||
const presetEl = document.getElementById("win-preset-select");
|
||
const preset = (presetEl && presetEl.value) || new URLSearchParams(window.location.search).get("win_preset") || "utc_today";
|
||
const q = new URLSearchParams(window.location.search);
|
||
q.set("win_preset", preset);
|
||
if(preset === "custom"){
|
||
const fromEl = document.getElementById("win-from-utc");
|
||
const toEl = document.getElementById("win-to-utc");
|
||
if(fromEl && fromEl.value) q.set("from_utc", fromEl.value.replace("T", " ") + ":00");
|
||
else q.delete("from_utc");
|
||
if(toEl && toEl.value) q.set("to_utc", toEl.value.replace("T", " ") + ":00");
|
||
else q.delete("to_utc");
|
||
} else {
|
||
q.delete("from_utc");
|
||
q.delete("to_utc");
|
||
}
|
||
return q.toString();
|
||
}
|
||
|
||
function toggleListWindowCustom(){
|
||
const preset = document.getElementById("win-preset-select");
|
||
const box = document.getElementById("win-custom-range");
|
||
if(!preset || !box) return;
|
||
box.style.display = preset.value === "custom" ? "" : "none";
|
||
}
|
||
|
||
function applyListWindow(){
|
||
const qs = listWindowQueryString();
|
||
const path = window.location.pathname || "/trade";
|
||
window.location.href = qs ? (path + "?" + qs) : path;
|
||
}
|
||
|
||
function attachListWindowToExports(){
|
||
const qs = listWindowQueryString();
|
||
if(!qs) return;
|
||
document.querySelectorAll('.export-bar a[href^="/export/trade_records"], .export-bar a[href^="/export/key_monitor_history"]').forEach(a=>{
|
||
const base = a.getAttribute("href").split("?")[0];
|
||
a.setAttribute("href", base + "?" + qs);
|
||
});
|
||
}
|
||
|
||
function loadJournals(){
|
||
const qs = listWindowQueryString();
|
||
fetch("/api/journals" + (qs ? "?" + qs : "")).then(r=>r.json()).then(data=>{
|
||
Object.keys(journalCache).forEach(k=>delete journalCache[k]);
|
||
data.forEach(o=>{ journalCache[o.id] = o; });
|
||
const box = document.getElementById("journal-list");
|
||
if(box){
|
||
const html = InstanceUI.renderJournalListHtml(data);
|
||
box.innerHTML = html || "<div class='journal-empty-msg'>暂无数据</div>";
|
||
}
|
||
});
|
||
}
|
||
|
||
function loadReviews(){
|
||
const qs = listWindowQueryString();
|
||
fetch("/api/reviews" + (qs ? "?" + qs : "")).then(r=>r.json()).then(data=>{
|
||
Object.keys(reviewCache).forEach(k=>delete reviewCache[k]);
|
||
let html="";
|
||
data.forEach(r=>{
|
||
reviewCache[r.id] = r;
|
||
const preview = (r.content || "").replace(/\s+/g, " ").trim();
|
||
const shortText = preview.length > 90 ? `${preview.slice(0, 90)}...` : preview;
|
||
html += `<div class="entry">
|
||
<div><strong>${r.review_type === "daily" ? "日复盘" : "周复盘"}</strong> | ${r.target_date}</div>
|
||
<div style="font-size:12px;color:#9aa">${r.created_at || ""}</div>
|
||
<div style="margin-top:4px;color:#c9d2ff">${shortText || "(空)"}</div>
|
||
<div style="display:flex;gap:8px;flex-wrap:wrap;margin-top:6px">
|
||
<button type="button" class="btn-del" style="border:none;cursor:pointer;background:#1f3a5a;color:#8fc8ff" onclick="openReviewDetail('${r.id}', false)">查看</button>
|
||
<button type="button" class="btn-del" style="border:none;cursor:pointer;background:#1f3a5a;color:#8fc8ff" onclick="openReviewDetail('${r.id}', true)">全屏</button>
|
||
<a class="btn-del" style="text-decoration:none;background:#1f3a5a;color:#8fc8ff" href="/export/review_md/${r.id}">导出MD</a>
|
||
<button type="button" class="btn-del" onclick="deleteReview('${r.id}')">删除</button>
|
||
</div>
|
||
</div>`;
|
||
});
|
||
const box = document.getElementById("review-list");
|
||
if(box){ box.innerHTML = html || "<div class='entry'>暂无数据</div>"; }
|
||
});
|
||
}
|
||
|
||
function genDaily(){
|
||
if(window.AiReviewRender && AiReviewRender.isGenerating && AiReviewRender.isGenerating()) return;
|
||
const d = document.getElementById("day_date").value;
|
||
if(!d){alert("请选择日期");return;}
|
||
if(window.AiReviewRender && AiReviewRender.setGenerating){
|
||
AiReviewRender.setGenerating({
|
||
wrapId:"daily_result_wrap",
|
||
elId:"daily_result",
|
||
btnId:"gen-daily-btn",
|
||
message:"生成日复盘中,请稍候…(AI 分析可能需要 1~3 分钟)",
|
||
btnLabel:"日复盘生成中…"
|
||
});
|
||
}
|
||
const ac = new AbortController();
|
||
const timer = setTimeout(()=>ac.abort(), 360000);
|
||
fetch("/ai_daily_review",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:`date=${encodeURIComponent(d)}`,signal:ac.signal})
|
||
.then(r=>{ if(!r.ok) throw new Error("HTTP "+r.status); return r.json(); })
|
||
.then(data=>{
|
||
if(!data || data.result == null) throw new Error("返回数据为空");
|
||
const el=document.getElementById("daily_result");
|
||
const wrap=document.getElementById("daily_result_wrap");
|
||
setAiReviewMarkdown(el, data.result);
|
||
if(wrap){ wrap.style.display="block"; }
|
||
else if(el){ el.style.display="block"; }
|
||
loadReviews();
|
||
})
|
||
.catch(err=>{
|
||
const el=document.getElementById("daily_result");
|
||
if(el && el.classList.contains("is-loading")){
|
||
el.classList.remove("is-loading","ai-result-md");
|
||
el.innerText = err.name === "AbortError"
|
||
? "生成超时(>6分钟),请检查 OPENAI_MODEL 是否与网关已启用模型一致,或增大 AI_REVIEW_TIMEOUT_SECONDS。"
|
||
: "生成失败,请重试。";
|
||
}
|
||
alert("生成日复盘失败:"+(err.message||err));
|
||
})
|
||
.finally(()=>{
|
||
clearTimeout(timer);
|
||
if(window.AiReviewRender && AiReviewRender.clearGenerating) AiReviewRender.clearGenerating("gen-daily-btn");
|
||
});
|
||
}
|
||
|
||
function genWeekly(){
|
||
if(window.AiReviewRender && AiReviewRender.isGenerating && AiReviewRender.isGenerating()) return;
|
||
const s=document.getElementById("week_start").value;
|
||
const e=document.getElementById("week_end").value;
|
||
if(!s || !e){alert("请选择起止日期");return;}
|
||
if(window.AiReviewRender && AiReviewRender.setGenerating){
|
||
AiReviewRender.setGenerating({
|
||
wrapId:"weekly_result_wrap",
|
||
elId:"weekly_result",
|
||
btnId:"gen-weekly-btn",
|
||
message:"生成周复盘中,请稍候…(AI 分析可能需要 1~3 分钟)",
|
||
btnLabel:"周复盘生成中…"
|
||
});
|
||
}
|
||
const ac = new AbortController();
|
||
const timer = setTimeout(()=>ac.abort(), 360000);
|
||
fetch("/ai_weekly_review",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:`start_date=${encodeURIComponent(s)}&end_date=${encodeURIComponent(e)}`,signal:ac.signal})
|
||
.then(r=>{ if(!r.ok) throw new Error("HTTP "+r.status); return r.json(); })
|
||
.then(data=>{
|
||
if(!data || data.result == null) throw new Error("返回数据为空");
|
||
const el=document.getElementById("weekly_result");
|
||
const wrap=document.getElementById("weekly_result_wrap");
|
||
setAiReviewMarkdown(el, data.result);
|
||
if(wrap){ wrap.style.display="block"; }
|
||
else if(el){ el.style.display="block"; }
|
||
loadReviews();
|
||
})
|
||
.catch(err=>{
|
||
const el=document.getElementById("weekly_result");
|
||
if(el && el.classList.contains("is-loading")){
|
||
el.classList.remove("is-loading","ai-result-md");
|
||
el.innerText = err.name === "AbortError"
|
||
? "生成超时(>6分钟),请检查 OPENAI_MODEL 是否与网关已启用模型一致,或增大 AI_REVIEW_TIMEOUT_SECONDS。"
|
||
: "生成失败,请重试。";
|
||
}
|
||
alert("生成周复盘失败:"+(err.message||err));
|
||
})
|
||
.finally(()=>{
|
||
clearTimeout(timer);
|
||
if(window.AiReviewRender && AiReviewRender.clearGenerating) AiReviewRender.clearGenerating("gen-weekly-btn");
|
||
});
|
||
}
|
||
|
||
function exportDailyBundleMd(){
|
||
const d = document.getElementById("day_date").value;
|
||
if(!d){ alert("请先选择日期"); return; }
|
||
const url = `/export/reviews_md_bundle?review_type=daily&target_date=${encodeURIComponent(d)}`;
|
||
window.location.href = url;
|
||
}
|
||
|
||
function exportWeeklyBundleMd(){
|
||
const s = document.getElementById("week_start").value;
|
||
const e = document.getElementById("week_end").value;
|
||
if(!s || !e){ alert("请先选择周起止日期"); return; }
|
||
const target = `${s}~${e}`;
|
||
const url = `/export/reviews_md_bundle?review_type=weekly&target_date=${encodeURIComponent(target)}`;
|
||
window.location.href = url;
|
||
}
|
||
|
||
function setJournalField(name, value){
|
||
const form = document.getElementById("journal-form");
|
||
const el = form ? form.querySelector(`[name="${name}"]`) : null;
|
||
if(!el) return;
|
||
if(typeof value === "undefined" || value === null) return;
|
||
el.value = String(value);
|
||
}
|
||
|
||
const EARLY_EXIT_TRIGGERS = new Set(["止盈","保本止盈","移动止盈","时间平仓","手动平仓","止损","其他"]);
|
||
const KEY_ENTRY_REASON_BY_SIGNAL = {
|
||
"箱体突破": "关键位箱体突破",
|
||
"收敛突破": "关键位收敛突破",
|
||
"斐波回调0.618": "关键位斐波0.618",
|
||
"斐波回调0.786": "关键位斐波0.786",
|
||
"假突破": "关键位假突破"
|
||
};
|
||
|
||
function splitLegacyEarlyExitReason(raw){
|
||
const s = String(raw || "").trim();
|
||
if(!s) return { trigger: "", note: "" };
|
||
const sep = s.indexOf("|");
|
||
if(sep > -1){
|
||
const a = s.slice(0, sep).trim();
|
||
const b = s.slice(sep + 1).trim();
|
||
if(EARLY_EXIT_TRIGGERS.has(a)){
|
||
return { trigger: a, note: b };
|
||
}
|
||
}
|
||
if(EARLY_EXIT_TRIGGERS.has(s)){
|
||
return { trigger: s, note: "" };
|
||
}
|
||
return { trigger: "", note: s };
|
||
}
|
||
|
||
function normalizeDatetimeLocal(v){
|
||
const raw = String(v || "").trim();
|
||
if(!raw) return "";
|
||
const m = raw.match(/^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2})/);
|
||
if(m) return `${m[1]}T${m[2]}`;
|
||
return raw;
|
||
}
|
||
|
||
function toDatetimeLocalFromBeijing(v){
|
||
const raw = String(v || "").trim();
|
||
if(!raw) return "";
|
||
const m = raw.match(/^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2})/);
|
||
if(m) return `${m[1]}T${m[2]}`;
|
||
return "";
|
||
}
|
||
|
||
function coinFromSymbol(symbol){
|
||
const s = String(symbol || "").trim().toUpperCase();
|
||
if(!s) return "";
|
||
if(s.includes("/")) return s.split("/")[0];
|
||
if(s.includes("-")) return s.split("-")[0];
|
||
if(s.endsWith("USDT")) return s.slice(0, -4);
|
||
return s;
|
||
}
|
||
|
||
/** 输入框/备注用价格:去掉浮点尾数,按量级保留有效小数(与后端 price_fmt 兜底一致) */
|
||
function formatPriceForInput(val){
|
||
if(val === null || val === undefined || val === "") return "";
|
||
const v = Number(val);
|
||
if(!Number.isFinite(v)) return String(val);
|
||
const av = Math.abs(v);
|
||
let d;
|
||
if(av >= 10000) d = 2;
|
||
else if(av >= 100) d = 3;
|
||
else if(av >= 1) d = 4;
|
||
else if(av >= 0.01) d = 6;
|
||
else if(av >= 0.0001) d = 8;
|
||
else d = 10;
|
||
let text = v.toFixed(d);
|
||
if(text.includes(".")) text = text.replace(/\.?0+$/, "");
|
||
return text;
|
||
}
|
||
|
||
function calcExpectedRrFromTrade(t){
|
||
const entry = Number(t.trigger_price);
|
||
const sl = Number(t.stop_loss);
|
||
const tp = Number(t.take_profit);
|
||
if(!Number.isFinite(entry) || !Number.isFinite(sl) || !Number.isFinite(tp)) return "";
|
||
if(entry <= 0 || sl <= 0 || tp <= 0) return "";
|
||
const direction = (t.direction || "long").toLowerCase();
|
||
let risk = 0;
|
||
let reward = 0;
|
||
if(direction === "short"){
|
||
risk = sl - entry;
|
||
reward = entry - tp;
|
||
} else {
|
||
risk = entry - sl;
|
||
reward = tp - entry;
|
||
}
|
||
if(risk <= 0 || reward <= 0) return "";
|
||
return (reward / risk).toFixed(2);
|
||
}
|
||
|
||
function fillJournalFromTrade(t){
|
||
if(!t){ return; }
|
||
setJournalField("open_datetime", toDatetimeLocalFromBeijing(t.opened_at));
|
||
setJournalField("close_datetime", toDatetimeLocalFromBeijing(t.closed_at));
|
||
setJournalField("coin", coinFromSymbol(t.symbol));
|
||
setJournalField("tf", "5m");
|
||
setJournalField("pnl", (t.pnl_amount === null || typeof t.pnl_amount === "undefined") ? "" : String(t.pnl_amount));
|
||
const rr = calcExpectedRrFromTrade(t);
|
||
setJournalField("expect_rr", rr);
|
||
let realRr = rr;
|
||
const riskAmount = Number(t.risk_amount);
|
||
const pnlAmount = Number(t.pnl_amount);
|
||
if(Number.isFinite(riskAmount) && riskAmount > 0 && Number.isFinite(pnlAmount)){
|
||
realRr = (pnlAmount / riskAmount).toFixed(2);
|
||
}
|
||
setJournalField("real_rr", realRr);
|
||
const riskHint = document.getElementById("risk-amount-hint");
|
||
if(riskHint){ riskHint.value = (Number.isFinite(riskAmount) && riskAmount > 0) ? String(riskAmount) : ""; }
|
||
const entryPx = formatPriceForInput(t.trigger_price);
|
||
const slPx = formatPriceForInput(t.stop_loss);
|
||
const tpPx = formatPriceForInput(t.take_profit);
|
||
const entryHint = document.getElementById("entry-price-hint");
|
||
if(entryHint){ entryHint.value = entryPx; }
|
||
const stopHint = document.getElementById("stop-loss-hint");
|
||
if(stopHint){ stopHint.value = slPx; }
|
||
const dirHint = document.getElementById("direction-hint");
|
||
if(dirHint){ dirHint.value = t.direction || "long"; }
|
||
setJournalField("early_exit_trigger", "");
|
||
setJournalField("early_exit_note", "");
|
||
const kst = String(t.key_signal_type || "").trim();
|
||
const mt = String(t.monitor_type || "").trim();
|
||
if(mt === "趋势回调" && JOURNAL_ENTRY_REASON_OPTIONS.includes("趋势回调")){
|
||
setJournalField("entry_reason", "趋势回调");
|
||
} else if(mt === "顺势加仓" && JOURNAL_ENTRY_REASON_OPTIONS.includes("顺势加仓")){
|
||
setJournalField("entry_reason", "顺势加仓");
|
||
} else {
|
||
const erFromKey = KEY_ENTRY_REASON_BY_SIGNAL[kst] || "";
|
||
if(erFromKey && JOURNAL_ENTRY_REASON_OPTIONS.includes(erFromKey)){
|
||
setJournalField("entry_reason", erFromKey);
|
||
} else {
|
||
setJournalField("entry_reason", "");
|
||
}
|
||
}
|
||
setJournalField("entry_reason_custom", "");
|
||
syncJournalEntryReasonOtherUi();
|
||
const er = String(t.result || "").trim();
|
||
const exitTrigMap = { 止盈: "止盈", 保本止盈: "保本止盈", 移动止盈: "移动止盈", 时间平仓: "时间平仓", 手动平仓: "手动平仓", 止损: "止损" };
|
||
if(exitTrigMap[er]) setJournalField("early_exit_trigger", exitTrigMap[er]);
|
||
const note = `来自交易记录自动填充:${t.symbol || "-"} ${t.direction || "-"} | 入场:${entryPx || "-"} 止损:${slPx || "-"} 止盈:${tpPx || "-"} | 类型:${t.monitor_type || "-"}`;
|
||
setJournalField("note", note);
|
||
const form = document.getElementById("journal-form");
|
||
if(form && typeof form.scrollIntoView === "function"){
|
||
form.scrollIntoView({behavior:"smooth", block:"start"});
|
||
}
|
||
recomputeJournalRealRr();
|
||
if(typeof syncEarlyExitNoteRequired === "function") syncEarlyExitNoteRequired();
|
||
alert("已填入下方复盘表单,请手动补充主观原因。");
|
||
}
|
||
|
||
function prefillJournalByImage(){
|
||
const fileInput = document.getElementById("journal-screenshot");
|
||
if(!fileInput || !fileInput.files || !fileInput.files.length){
|
||
alert("请先选择截图");
|
||
return;
|
||
}
|
||
const fd = new FormData();
|
||
fd.append("screenshot", fileInput.files[0]);
|
||
fetch("/api/journal_prefill", { method: "POST", body: fd })
|
||
.then(r=>r.json())
|
||
.then(res=>{
|
||
if(!res.ok){
|
||
alert(res.msg || "AI识别失败");
|
||
return;
|
||
}
|
||
const d = res.data || {};
|
||
setJournalField("open_datetime", normalizeDatetimeLocal(d.open_datetime));
|
||
setJournalField("close_datetime", normalizeDatetimeLocal(d.close_datetime));
|
||
setJournalField("coin", d.coin || "");
|
||
setJournalField("tf", d.tf || "");
|
||
setJournalField("pnl", d.pnl || "");
|
||
setJournalField("expect_rr", d.expect_rr || "");
|
||
setJournalField("real_rr", d.real_rr || "");
|
||
let entryReason = String(d.entry_reason || "").trim();
|
||
let customEr = "";
|
||
if(JOURNAL_ENTRY_REASON_OPTIONS && JOURNAL_ENTRY_REASON_OPTIONS.includes(entryReason)){
|
||
// keep
|
||
} else if(entryReason){
|
||
customEr = entryReason;
|
||
entryReason = JOURNAL_ENTRY_REASON_OTHER;
|
||
} else {
|
||
entryReason = "";
|
||
}
|
||
setJournalField("entry_reason", entryReason);
|
||
setJournalField("entry_reason_custom", customEr);
|
||
syncJournalEntryReasonOtherUi();
|
||
const trig = (d.early_exit_trigger || "").trim();
|
||
let noteEx = (d.early_exit_note || "").trim();
|
||
const legacy = (d.early_exit_reason || "").trim();
|
||
if(!noteEx && legacy && !trig){
|
||
const sp = splitLegacyEarlyExitReason(legacy);
|
||
setJournalField("early_exit_trigger", sp.trigger);
|
||
setJournalField("early_exit_note", sp.note);
|
||
} else {
|
||
setJournalField("early_exit_trigger", trig);
|
||
setJournalField("early_exit_note", noteEx);
|
||
}
|
||
setJournalField("note", d.note || "");
|
||
if(typeof syncEarlyExitNoteRequired === "function") syncEarlyExitNoteRequired();
|
||
recomputeJournalRealRr();
|
||
alert("已完成预填,请手动检查并补充原因");
|
||
})
|
||
.catch(()=>alert("AI识别请求失败"));
|
||
}
|
||
|
||
function recomputeJournalRealRr(){
|
||
const form = document.getElementById("journal-form");
|
||
if(!form) return;
|
||
const pnlEl = form.querySelector('[name="pnl"]');
|
||
const rrEl = form.querySelector('[name="real_rr"]');
|
||
const riskHint = document.getElementById("risk-amount-hint");
|
||
if(!pnlEl || !rrEl || !riskHint) return;
|
||
const pnl = Number(String(pnlEl.value || "").trim());
|
||
const risk = Number(String(riskHint.value || "").trim());
|
||
if(Number.isFinite(pnl) && Number.isFinite(risk) && risk > 0){
|
||
rrEl.value = (pnl / risk).toFixed(4);
|
||
}
|
||
}
|
||
|
||
|
||
function switchStatsSegment(){
|
||
const sel = document.getElementById("stats-segment-select");
|
||
if(!sel) return;
|
||
const key = sel.value;
|
||
document.querySelectorAll(".stats-segment-panel").forEach(p=>{
|
||
p.style.display = p.getAttribute("data-stats-segment") === key ? "block" : "none";
|
||
});
|
||
const q = new URLSearchParams(window.location.search);
|
||
q.set("stats_segment", key);
|
||
const qs = q.toString();
|
||
history.replaceState(null, "", qs ? (window.location.pathname + "?" + qs) : window.location.pathname);
|
||
}
|
||
|
||
function initStatsSegmentFromUrl(){
|
||
const sel = document.getElementById("stats-segment-select");
|
||
if(!sel) return;
|
||
const key = new URLSearchParams(window.location.search).get("stats_segment");
|
||
if(key && sel.querySelector('option[value="' + key.replace(/"/g, "") + '"]')){
|
||
sel.value = key;
|
||
}
|
||
switchStatsSegment();
|
||
}
|
||
|
||
function toggleStatsCard(){
|
||
const card = document.getElementById("stats-card");
|
||
const btn = document.getElementById("stats-toggle-btn");
|
||
if(!card || !btn) return;
|
||
const collapsed = card.classList.toggle("collapsed");
|
||
btn.innerText = collapsed ? "展开" : "折叠";
|
||
}
|
||
|
||
function bindListWindowDateAutoCustom(){
|
||
const preset = document.getElementById("win-preset-select");
|
||
const fromEl = document.getElementById("win-from-utc");
|
||
const toEl = document.getElementById("win-to-utc");
|
||
function toCustom(){
|
||
if(preset) preset.value = "custom";
|
||
toggleListWindowCustom();
|
||
}
|
||
if(fromEl) fromEl.addEventListener("change", toCustom);
|
||
if(toEl) toEl.addEventListener("change", toCustom);
|
||
}
|
||
|
||
attachListWindowToExports();
|
||
toggleListWindowCustom();
|
||
bindListWindowDateAutoCustom();
|
||
initStatsSegmentFromUrl();
|
||
if(document.getElementById("journal-list")) loadJournals();
|
||
if(document.getElementById("review-list")) loadReviews();
|
||
const reviewToggle = document.getElementById("review-mode-toggle");
|
||
if(reviewToggle){
|
||
reviewToggle.addEventListener("change", toggleReviewMode);
|
||
toggleReviewMode();
|
||
}
|
||
const journalForm = document.getElementById("journal-form");
|
||
if(journalForm){
|
||
const pnlInput = journalForm.querySelector('[name="pnl"]');
|
||
if(pnlInput){
|
||
pnlInput.addEventListener("input", recomputeJournalRealRr);
|
||
pnlInput.addEventListener("change", recomputeJournalRealRr);
|
||
}
|
||
const earlyTrig = journalForm.querySelector('[name="early_exit_trigger"]');
|
||
const earlyNote = journalForm.querySelector('[name="early_exit_note"]');
|
||
function syncEarlyExitNoteRequired(){
|
||
if(!earlyTrig || !earlyNote) return;
|
||
if(earlyTrig.value === "手动平仓"){
|
||
earlyNote.setAttribute("required", "required");
|
||
earlyNote.placeholder = "手工平仓须说明原因(必填)";
|
||
} else {
|
||
earlyNote.removeAttribute("required");
|
||
earlyNote.placeholder = "离场补充(仅手工平仓必填)";
|
||
}
|
||
}
|
||
window.syncEarlyExitNoteRequired = syncEarlyExitNoteRequired;
|
||
if(earlyTrig){
|
||
earlyTrig.addEventListener("change", syncEarlyExitNoteRequired);
|
||
syncEarlyExitNoteRequired();
|
||
}
|
||
}
|
||
|
||
if(window.TimeCloseUI){
|
||
TimeCloseUI.bindTimeCloseForm("order-time-close-cb", "order-time-close-hours", "order-time-close-wrap");
|
||
}
|
||
// 复盘/AI列表:初次进入页面后再异步刷新一次,避免浏览器 bfcache/重定向后仍显示旧缓存
|
||
setTimeout(() => {
|
||
if(document.getElementById("journal-list")) loadJournals();
|
||
if(document.getElementById("review-list")) loadReviews();
|
||
}, 300);
|
||
|
||
|
||
const MANUAL_MIN_PLANNED_RR = {{ manual_min_planned_rr }};
|
||
const MANUAL_FIXED_RR_DEFAULT = 1.5;
|
||
const FIXED_RR_LS_KEY = "manualFixedRr";
|
||
function loadFixedRrPref(){
|
||
try{
|
||
const raw = localStorage.getItem(FIXED_RR_LS_KEY);
|
||
const el = document.getElementById("order-fixed-rr");
|
||
if(!el || raw == null || raw === "") return;
|
||
const v = Number(raw);
|
||
if(Number.isFinite(v) && v > 0) el.value = raw;
|
||
}catch(_){}
|
||
}
|
||
function saveFixedRrPref(){
|
||
try{
|
||
const el = document.getElementById("order-fixed-rr");
|
||
if(el && el.value) localStorage.setItem(FIXED_RR_LS_KEY, el.value);
|
||
}catch(_){}
|
||
}
|
||
function calcTpFromFixedRr(direction, entry, sl, rr){
|
||
const e = Number(entry), s = Number(sl), r = Number(rr);
|
||
if(!Number.isFinite(e) || !Number.isFinite(s) || !Number.isFinite(r) || r <= 0) return null;
|
||
if(direction === "short"){
|
||
if(s <= e) return null;
|
||
return e - (s - e) * r;
|
||
}
|
||
if(s >= e) return null;
|
||
return e + (e - s) * r;
|
||
}
|
||
function refreshOrderTpPreview(entryPx){
|
||
const mode = (document.getElementById("sltp-mode")||{}).value || "fixed_rr";
|
||
const preview = document.getElementById("order-tp-preview");
|
||
if(!preview) return;
|
||
if(mode !== "fixed_rr"){
|
||
preview.style.display = "none";
|
||
return;
|
||
}
|
||
preview.style.display = "";
|
||
const direction = (document.getElementById("order-direction")||{}).value || "long";
|
||
const sl = Number((document.getElementById("order-sl")||{}).value);
|
||
const rr = Number((document.getElementById("order-fixed-rr")||{}).value) || MANUAL_FIXED_RR_DEFAULT;
|
||
const entry = entryPx != null && Number.isFinite(Number(entryPx)) ? Number(entryPx) : sl;
|
||
const tp = calcTpFromFixedRr(direction, entry, sl, rr);
|
||
preview.textContent = tp == null ? "预估止盈:—" : ("预估止盈:" + formatPriceForInput(tp));
|
||
if(window.ManualOrderRrPreview) ManualOrderRrPreview.schedule();
|
||
}
|
||
function calcClientRr(direction, entry, sl, tp){
|
||
const e = Number(entry), s = Number(sl), t = Number(tp);
|
||
if(!Number.isFinite(e) || !Number.isFinite(s) || !Number.isFinite(t)) return null;
|
||
if(direction === 'short'){
|
||
if(s <= e || t >= e) return null;
|
||
return (e - t) / (s - e);
|
||
}
|
||
if(s >= e || t <= e) return null;
|
||
return (t - e) / (e - s);
|
||
}
|
||
function calcClientRrFromPct(slPct, tpPct){
|
||
const sl = Number(slPct), tp = Number(tpPct);
|
||
if(!Number.isFinite(sl) || !Number.isFinite(tp) || sl <= 0 || tp <= 0) return null;
|
||
return tp / sl;
|
||
}
|
||
function rejectManualOrderRr(rr){
|
||
if(rr !== null && rr >= MANUAL_MIN_PLANNED_RR) return false;
|
||
alert(`计划盈亏比 ${rr === null ? '无效' : rr.toFixed(2)}:1 低于最低要求 ${MANUAL_MIN_PLANNED_RR}:1,已阻止人工下单。`);
|
||
return true;
|
||
}
|
||
|
||
let tpslEntrustMonitorId = null;
|
||
function formatExTpslLine(role, slot){
|
||
const label = role === 'sl' ? '止损' : '止盈';
|
||
if(!slot || !slot.order_id) return label + ':未挂单';
|
||
const px = slot.trigger_display || slot.trigger_price || '-';
|
||
const amt = slot.amount != null && !Number.isNaN(Number(slot.amount)) ? ` 数量 ${Number(slot.amount)}` : '';
|
||
return `${label}:触发 ${px}${amt}`;
|
||
}
|
||
function paintExchangeTpslRow(orderId, tpsl){
|
||
const data = tpsl || {};
|
||
const slText = document.getElementById(`ex-sl-text-${orderId}`);
|
||
const tpText = document.getElementById(`ex-tp-text-${orderId}`);
|
||
const slBtn = document.getElementById(`ex-sl-cancel-${orderId}`);
|
||
const tpBtn = document.getElementById(`ex-tp-cancel-${orderId}`);
|
||
if(slText) slText.innerText = formatExTpslLine('sl', data.sl);
|
||
if(tpText) tpText.innerText = formatExTpslLine('tp', data.tp);
|
||
if(slBtn) slBtn.disabled = !(data.sl && data.sl.order_id);
|
||
if(tpBtn) tpBtn.disabled = !(data.tp && data.tp.order_id);
|
||
}
|
||
function toggleTpslModalMode(){
|
||
const mode = (document.getElementById('tpsl-modal-mode')||{}).value || 'price';
|
||
const pct = mode === 'pct';
|
||
['tpsl-modal-sl','tpsl-modal-tp'].forEach(id=>{ const el=document.getElementById(id); if(el) el.style.display=pct?'none':''; });
|
||
['tpsl-modal-sl-pct','tpsl-modal-tp-pct'].forEach(id=>{ const el=document.getElementById(id); if(el) el.style.display=pct?'':'none'; });
|
||
}
|
||
function openTpslEntrustModal(orderId){
|
||
const card = document.getElementById(`order-row-${orderId}`);
|
||
if(!card) return;
|
||
tpslEntrustMonitorId = orderId;
|
||
const slEl = document.getElementById('tpsl-modal-sl');
|
||
const tpEl = document.getElementById('tpsl-modal-tp');
|
||
if(slEl) slEl.value = formatPriceForInput(card.getAttribute('data-plan-sl') || '');
|
||
if(tpEl) tpEl.value = formatPriceForInput(card.getAttribute('data-plan-tp') || '');
|
||
const modeEl = document.getElementById('tpsl-modal-mode');
|
||
if(modeEl) modeEl.value = 'price';
|
||
toggleTpslModalMode();
|
||
const title = document.getElementById('tpsl-modal-title');
|
||
if(title) title.innerText = `挂止盈止损 · ${card.getAttribute('data-symbol')||''}`;
|
||
const modal = document.getElementById('tpsl-modal');
|
||
if(modal) modal.classList.add('open');
|
||
}
|
||
function closeTpslEntrustModal(){
|
||
tpslEntrustMonitorId = null;
|
||
const modal = document.getElementById('tpsl-modal');
|
||
if(modal) modal.classList.remove('open');
|
||
}
|
||
function submitTpslEntrust(){
|
||
const orderId = tpslEntrustMonitorId;
|
||
if(!orderId) return;
|
||
const mode = (document.getElementById('tpsl-modal-mode')||{}).value || 'price';
|
||
const body = { sltp_mode: mode };
|
||
if(mode === 'pct'){
|
||
body.sl_pct = Number((document.getElementById('tpsl-modal-sl-pct')||{}).value);
|
||
body.tp_pct = Number((document.getElementById('tpsl-modal-tp-pct')||{}).value);
|
||
}else{
|
||
body.sl = (document.getElementById('tpsl-modal-sl')||{}).value;
|
||
body.tp = (document.getElementById('tpsl-modal-tp')||{}).value;
|
||
}
|
||
fetch(`/api/order/${orderId}/place_tpsl`, { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body) })
|
||
.then(r=>r.json()).then(data=>{
|
||
if(!data.ok){ alert(data.msg || '委托失败'); return; }
|
||
alert(data.msg || '已提交');
|
||
closeTpslEntrustModal();
|
||
if(data.exchange_tpsl) paintExchangeTpslRow(orderId, data.exchange_tpsl);
|
||
refreshPriceSnapshotConditional();
|
||
}).catch(()=>alert('委托请求失败'));
|
||
}
|
||
function cancelExchangeTpsl(orderId, role){
|
||
const label = role === 'sl' ? '止损' : '止盈';
|
||
if(!confirm(`确认撤销交易所${label}委托?(不会平仓)`)) return;
|
||
fetch(`/api/order/${orderId}/cancel_tpsl`, { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({ role }) })
|
||
.then(r=>r.json()).then(data=>{
|
||
if(!data.ok){ alert(data.msg || '撤单失败'); return; }
|
||
if(data.exchange_tpsl) paintExchangeTpslRow(orderId, data.exchange_tpsl);
|
||
else refreshPriceSnapshotConditional();
|
||
}).catch(()=>alert('撤单请求失败'));
|
||
}
|
||
|
||
function allowManualOrderSubmit(form){
|
||
form.dataset.rrOk = "1";
|
||
if(document.body && document.body.getAttribute("data-embed-shell") === "1" && window.InstanceEmbed && window.InstanceEmbed.postFormAndReload){
|
||
window.InstanceEmbed.postFormAndReload(form, "开仓提交中…");
|
||
return;
|
||
}
|
||
if(window.FormSubmitGuard){
|
||
if(FormSubmitGuard.isLocked(form)){
|
||
FormSubmitGuard.setSubmitLabel(form, "开仓提交中…");
|
||
} else {
|
||
FormSubmitGuard.lock(form, "开仓提交中…");
|
||
}
|
||
}
|
||
form.submit();
|
||
}
|
||
|
||
let latestAvailableUsdt = null;
|
||
const lastPriceMap = {};
|
||
|
||
function formatSigned(v, digits=2){
|
||
if(v === null || typeof v === "undefined" || Number.isNaN(Number(v))) return "-";
|
||
const n = Number(v);
|
||
const sign = n > 0 ? "+" : "";
|
||
return `${sign}${n.toFixed(digits)}`;
|
||
}
|
||
|
||
function formatRrRatio(rr){
|
||
if(rr === null || typeof rr === "undefined") return "-:1";
|
||
const n = Number(rr);
|
||
if(Number.isNaN(n)) return "-:1";
|
||
const body = Number.isInteger(n) ? String(n) : String(parseFloat(n.toFixed(2)));
|
||
return `${body}:1`;
|
||
}
|
||
|
||
function paintBreakevenBadge(orderId, secured){
|
||
const wrap = document.getElementById(`order-be-wrap-${orderId}`);
|
||
if(!wrap) return;
|
||
wrap.style.display = secured ? "inline-flex" : "none";
|
||
}
|
||
function paintPlanTpslDisplay(orderId, snap){
|
||
if(!snap) return;
|
||
const card = document.getElementById(`order-row-${orderId}`);
|
||
const slEl = document.getElementById(`order-plan-sl-${orderId}`);
|
||
const tpEl = document.getElementById(`order-plan-tp-${orderId}`);
|
||
const slRaw = snap.stop_loss_raw != null && snap.stop_loss_raw !== "" ? snap.stop_loss_raw : snap.stop_loss;
|
||
const tpRaw = snap.take_profit_raw != null && snap.take_profit_raw !== "" ? snap.take_profit_raw : snap.take_profit;
|
||
const slDisp = snap.stop_loss_display || (slRaw != null && slRaw !== "" ? formatPriceForInput(slRaw) : null);
|
||
const tpDisp = snap.take_profit_display || (tpRaw != null && tpRaw !== "" ? formatPriceForInput(tpRaw) : null);
|
||
if(slEl) slEl.innerText = slDisp || "—";
|
||
if(tpEl) tpEl.innerText = tpDisp || "—";
|
||
if(card){
|
||
if(slRaw != null && slRaw !== "") card.setAttribute("data-plan-sl", formatPriceForInput(slRaw));
|
||
else if(slDisp) card.setAttribute("data-plan-sl", slDisp);
|
||
if(tpRaw != null && tpRaw !== "") card.setAttribute("data-plan-tp", formatPriceForInput(tpRaw));
|
||
else if(tpDisp) card.setAttribute("data-plan-tp", tpDisp);
|
||
}
|
||
}
|
||
|
||
function paintPriceTrend(el, key, value){
|
||
if(!el) return;
|
||
const prev = lastPriceMap[key];
|
||
el.classList.remove("price-up","price-down","price-flat");
|
||
if(typeof prev === "number"){
|
||
if(value > prev) el.classList.add("price-up");
|
||
else if(value < prev) el.classList.add("price-down");
|
||
else el.classList.add("price-flat");
|
||
} else {
|
||
el.classList.add("price-flat");
|
||
}
|
||
lastPriceMap[key] = value;
|
||
}
|
||
|
||
function refreshPriceSnapshot(){
|
||
fetch("/api/price_snapshot").then(r=>r.json()).then(data=>{
|
||
const updatedEl = document.getElementById("price-last-updated");
|
||
if(data.updated_at && updatedEl){
|
||
updatedEl.innerText = data.updated_at;
|
||
}
|
||
(data.key_prices || []).forEach(k=>{
|
||
const pEl = document.getElementById(`key-price-${k.id}`);
|
||
if(pEl){
|
||
pEl.innerText = k.price_display || (Number.isFinite(Number(k.price)) ? Number(k.price).toFixed(6) : "-");
|
||
paintPriceTrend(pEl, `k-${k.id}`, Number(k.price));
|
||
}
|
||
const upEl = document.getElementById(`key-up-diff-${k.id}`);
|
||
if(upEl){
|
||
upEl.innerText = `${formatSigned(k.upper_diff, 4)} (${formatSigned(k.upper_pct, 2)}%)`;
|
||
}
|
||
const lowEl = document.getElementById(`key-low-diff-${k.id}`);
|
||
if(lowEl){
|
||
lowEl.innerText = `${formatSigned(k.lower_diff, 4)} (${formatSigned(k.lower_pct, 2)}%)`;
|
||
}
|
||
const gateEl = document.getElementById(`key-gate-${k.id}`);
|
||
if(gateEl){
|
||
gateEl.innerText = k.gate_summary || "-";
|
||
gateEl.style.color = k.gate_ok ? "#4cd97f" : "#ff8f8f";
|
||
}
|
||
const gateMetricEl = document.getElementById(`key-gate-metrics-${k.id}`);
|
||
if(gateMetricEl){
|
||
gateMetricEl.innerText = k.gate_metrics || "";
|
||
}
|
||
});
|
||
(data.order_prices || []).forEach(o=>{
|
||
const pEl = document.getElementById(`order-price-${o.id}`);
|
||
if(pEl){
|
||
const hasMark = (()=>{ const x = o.exchange_mark_price; if(x===null||x===undefined||x==="")return false; const n=Number(x); return !Number.isNaN(n); })();
|
||
let disp = "";
|
||
if(hasMark && o.exchange_mark_price_display){
|
||
disp = o.exchange_mark_price_display;
|
||
} else if(o.price_display){
|
||
disp = o.price_display;
|
||
} else {
|
||
const px = hasMark ? Number(o.exchange_mark_price) : Number(o.price);
|
||
disp = Number.isFinite(px) ? px.toFixed(6) : "-";
|
||
}
|
||
pEl.innerText = disp;
|
||
const pxNum = hasMark ? Number(o.exchange_mark_price) : Number(o.price);
|
||
paintPriceTrend(pEl, `o-${o.id}`, Number.isFinite(pxNum) ? pxNum : px);
|
||
}
|
||
const exM = document.getElementById(`order-ex-margin-${o.id}`);
|
||
if(exM){
|
||
const mv = o.exchange_initial_margin;
|
||
const mn = (mv === null || mv === undefined || mv === "") ? NaN : Number(mv);
|
||
if(!Number.isNaN(mn)){
|
||
exM.innerText = `${mn.toFixed(2)}U`;
|
||
} else {
|
||
const prc = (typeof data.positions_raw_count === "number") ? data.positions_raw_count : null;
|
||
exM.innerText = (prc === 0) ? "无仓数据" : "-";
|
||
}
|
||
}
|
||
const pnlEl = document.getElementById(`order-pnl-${o.id}`);
|
||
if(pnlEl){
|
||
pnlEl.innerText = `${formatSigned(o.float_pnl, 2)}U (${formatSigned(o.float_pct, 2)}%)`;
|
||
pnlEl.classList.remove("price-up","price-down","price-flat");
|
||
if(Number(o.float_pnl) > 0) pnlEl.classList.add("price-up");
|
||
else if(Number(o.float_pnl) < 0) pnlEl.classList.add("price-down");
|
||
else pnlEl.classList.add("price-flat");
|
||
}
|
||
const rrEl = document.getElementById(`order-rr-${o.id}`);
|
||
if(rrEl){
|
||
rrEl.innerText = formatRrRatio(o.rr_ratio);
|
||
}
|
||
paintBreakevenBadge(o.id, o.sl_breakeven_secured);
|
||
if(o.exchange_tpsl) paintExchangeTpslRow(o.id, o.exchange_tpsl);
|
||
paintPlanTpslDisplay(o.id, o);
|
||
if(window.TimeCloseUI) TimeCloseUI.paintOrderTimeClose(o);
|
||
});
|
||
}).catch(()=>{});
|
||
}
|
||
|
||
function refreshOrderDefaults(){
|
||
const symbolEl = document.getElementById("order-symbol");
|
||
const directionEl = document.getElementById("order-direction");
|
||
if(!symbolEl || !directionEl){ return; }
|
||
const symbol = (symbolEl.value || "").trim();
|
||
const direction = directionEl.value || "long";
|
||
if(!symbol || !direction){ return; }
|
||
fetch(`/api/order_defaults?symbol=${encodeURIComponent(symbol)}&direction=${encodeURIComponent(direction)}`)
|
||
.then(r=>r.json())
|
||
.then(data=>{
|
||
if(!data.ok){ return; }
|
||
if(data.leverage){
|
||
const levEl = document.getElementById("order-leverage");
|
||
if(levEl) levEl.value = data.leverage;
|
||
}
|
||
if(typeof data.available_trading_usdt !== "undefined" && data.available_trading_usdt !== null){
|
||
latestAvailableUsdt = Number(data.available_trading_usdt);
|
||
const fullEl = document.getElementById("use-full-margin");
|
||
const marginEl = document.getElementById("order-margin");
|
||
if(fullEl && marginEl && fullEl.checked){
|
||
const m = Math.max(latestAvailableUsdt * {{ full_margin_buffer_ratio }}, 0).toFixed(2);
|
||
marginEl.value = m;
|
||
}
|
||
}
|
||
const px = data.last_price || data.price;
|
||
if(px) refreshOrderTpPreview(px);
|
||
if(window.ManualOrderRrPreview) ManualOrderRrPreview.schedule();
|
||
}).catch(()=>{});
|
||
}
|
||
|
||
function refreshAccountSnapshot(){
|
||
fetch("/api/account_snapshot").then(r=>r.json()).then(data=>{
|
||
if (typeof data.funding_usdt !== "undefined") {
|
||
const el = document.getElementById("total-capital");
|
||
if(el) el.innerText = (data.funding_usdt === null || data.funding_usdt === undefined) ? "—" : `${Number(data.funding_usdt).toFixed(2)}U`;
|
||
}
|
||
if (typeof data.current_capital !== "undefined") {
|
||
const el = document.getElementById("current-capital");
|
||
if(el) el.innerText = `${Number(data.current_capital).toFixed(2)}U`;
|
||
}
|
||
if (typeof data.available_trading_usdt !== "undefined" && data.available_trading_usdt !== null) {
|
||
latestAvailableUsdt = Number(data.available_trading_usdt);
|
||
}
|
||
if (data.risk_status) {
|
||
const badge = document.getElementById("account-risk-badge");
|
||
if (badge) {
|
||
if (window.AccountRiskBadge) {
|
||
AccountRiskBadge.applyToElement(badge, data.risk_status);
|
||
} else {
|
||
const st = data.risk_status.status || "normal";
|
||
badge.className = "risk-status-badge risk-status-" + st;
|
||
badge.innerText = data.risk_status.status_label || "正常";
|
||
badge.title = data.risk_status.reason || "";
|
||
}
|
||
}
|
||
}
|
||
let canTradeText = "可开仓";
|
||
if (!data.can_trade) {
|
||
const parts = [];
|
||
if (data.risk_status && data.risk_status.can_trade === false && data.risk_status.reason) {
|
||
parts.push(data.risk_status.reason);
|
||
}
|
||
const ac = Number(data.active_count || 0);
|
||
const max = Number(data.max_active_positions || {{ max_active_positions }});
|
||
if (ac >= max) parts.push(`持仓 ${ac}/${max}`);
|
||
const hard = Number(data.daily_open_hard_limit != null ? data.daily_open_hard_limit : {{ daily_open_hard_limit }});
|
||
const opens = Number(data.opens_today);
|
||
if (hard > 0 && !Number.isNaN(opens) && opens >= hard) parts.push(`本交易日开仓 ${opens}/${hard} 已达上限`);
|
||
if (!parts.length) parts.push(`未到北京时间 {{ reset_hour }}:00`);
|
||
else parts.push(`或未到北京时间 {{ reset_hour }}:00`);
|
||
canTradeText = `不可开仓(${parts.join(";")})`;
|
||
}
|
||
const opensToday = Number(data.opens_today);
|
||
const hardLim = Number(data.daily_open_hard_limit != null ? data.daily_open_hard_limit : {{ daily_open_hard_limit }});
|
||
const alertLim = Number(data.daily_open_alert_threshold != null ? data.daily_open_alert_threshold : {{ daily_open_alert_threshold }});
|
||
const openCntTxt = !Number.isNaN(opensToday)
|
||
? `本交易日开仓 ${opensToday}${hardLim > 0 ? ` / 硬上限 ${hardLim}` : ""}(AI 提醒 ${alertLim})`
|
||
: "";
|
||
const tip = document.getElementById("order-rule-tip");
|
||
const avail = (latestAvailableUsdt !== null && !Number.isNaN(latestAvailableUsdt)) ? `;交易账户可用约${latestAvailableUsdt.toFixed(2)}U` : "";
|
||
if(tip){
|
||
tip.innerText = `规则:最多 ${data.max_active_positions || {{ max_active_positions }}} 仓;BTC {{ btc_leverage }}x / 山寨 {{ alt_leverage }}x;${openCntTxt ? openCntTxt + ";" : ""}${canTradeText}${avail};人工开仓盈亏比不得低于 {{ manual_min_planned_rr }}:1`;
|
||
}
|
||
}).catch(()=>{});
|
||
}
|
||
|
||
const orderSymbolEl = document.getElementById("order-symbol");
|
||
const orderDirectionEl = document.getElementById("order-direction");
|
||
const fullMarginEl = document.getElementById("use-full-margin");
|
||
if(orderSymbolEl) orderSymbolEl.addEventListener("change", refreshOrderDefaults);
|
||
if(orderDirectionEl) orderDirectionEl.addEventListener("change", refreshOrderDefaults);
|
||
if(fullMarginEl){
|
||
fullMarginEl.addEventListener("change", function(){
|
||
const marginEl = document.getElementById("order-margin");
|
||
if(marginEl && this.checked && latestAvailableUsdt !== null && !Number.isNaN(latestAvailableUsdt)){
|
||
marginEl.value = Math.max(latestAvailableUsdt * {{ full_margin_buffer_ratio }}, 0).toFixed(2);
|
||
}
|
||
});
|
||
}
|
||
|
||
const sltpModeEl = document.getElementById("sltp-mode");
|
||
function toggleSltpMode(){
|
||
const mode = sltpModeEl ? sltpModeEl.value : "fixed_rr";
|
||
const slEl = document.getElementById("order-sl");
|
||
const tpEl = document.getElementById("order-tp");
|
||
const fixedRrEl = document.getElementById("order-fixed-rr");
|
||
const rrPreviewEl = document.getElementById("order-rr-preview");
|
||
const slPctEl = document.getElementById("order-sl-pct");
|
||
const tpPctEl = document.getElementById("order-tp-pct");
|
||
if(!slEl || !tpEl || !slPctEl || !tpPctEl){ return; }
|
||
const pct = mode === "pct";
|
||
const fixed = mode === "fixed_rr";
|
||
if(rrPreviewEl) rrPreviewEl.style.display = fixed ? "none" : "";
|
||
slEl.style.display = pct ? "none" : "";
|
||
tpEl.style.display = (pct || fixed) ? "none" : "";
|
||
if(fixedRrEl) fixedRrEl.style.display = fixed ? "" : "none";
|
||
slEl.required = !pct;
|
||
tpEl.required = !pct && !fixed;
|
||
if(fixedRrEl) fixedRrEl.required = fixed;
|
||
slPctEl.style.display = pct ? "" : "none";
|
||
tpPctEl.style.display = pct ? "" : "none";
|
||
slPctEl.required = pct;
|
||
tpPctEl.required = pct;
|
||
refreshOrderTpPreview();
|
||
if(window.ManualOrderRrPreview) ManualOrderRrPreview.schedule();
|
||
}
|
||
if(sltpModeEl){
|
||
sltpModeEl.addEventListener("change", toggleSltpMode);
|
||
loadFixedRrPref();
|
||
toggleSltpMode();
|
||
}
|
||
if(window.ManualOrderRrPreview){
|
||
ManualOrderRrPreview.wire({ minRr: MANUAL_MIN_PLANNED_RR });
|
||
}
|
||
["order-sl","order-fixed-rr","order-direction"].forEach(function(id){
|
||
const el = document.getElementById(id);
|
||
if(el) el.addEventListener("input", function(){ refreshOrderTpPreview(); });
|
||
if(el) el.addEventListener("change", function(){ refreshOrderTpPreview(); });
|
||
});
|
||
|
||
refreshAccountSnapshot();
|
||
if (window.AccountRiskBadge) AccountRiskBadge.startTicker();
|
||
const _journalFormEl = document.getElementById("journal-form");
|
||
if(_journalFormEl){
|
||
_journalFormEl.addEventListener("submit", function(ev){
|
||
if(!validateJournalEntryReason()) ev.preventDefault();
|
||
});
|
||
const _jErSel = _journalFormEl.querySelector('[name="entry_reason"]');
|
||
if(_jErSel) _jErSel.addEventListener("change", syncJournalEntryReasonOtherUi);
|
||
syncJournalEntryReasonOtherUi();
|
||
}
|
||
|
||
const addOrderForm = document.getElementById("add-order-form");
|
||
if(addOrderForm){
|
||
addOrderForm.addEventListener("submit", function(ev){
|
||
if(addOrderForm.dataset.rrOk === "1"){
|
||
addOrderForm.dataset.rrOk = "0";
|
||
return;
|
||
}
|
||
ev.preventDefault();
|
||
if(window.FormSubmitGuard && FormSubmitGuard.isLocked(addOrderForm)) return;
|
||
const direction = (document.getElementById("order-direction")||{}).value || "long";
|
||
const mode = (document.getElementById("sltp-mode")||{}).value || "fixed_rr";
|
||
const symbol = ((document.getElementById("order-symbol")||{}).value || "").trim();
|
||
if(mode === "fixed_rr"){
|
||
saveFixedRrPref();
|
||
const rr = Number((document.getElementById("order-fixed-rr")||{}).value);
|
||
if(!Number.isFinite(rr) || rr <= 0){
|
||
alert("请填写正数盈亏比");
|
||
return;
|
||
}
|
||
if(window.FormSubmitGuard) FormSubmitGuard.lock(addOrderForm, "校验盈亏比…");
|
||
if(rejectManualOrderRr(rr)){
|
||
if(window.FormSubmitGuard) FormSubmitGuard.unlock(addOrderForm);
|
||
return;
|
||
}
|
||
allowManualOrderSubmit(addOrderForm);
|
||
return;
|
||
}
|
||
if(mode === "pct"){
|
||
if(window.FormSubmitGuard) FormSubmitGuard.lock(addOrderForm, "校验盈亏比…");
|
||
const rr = calcClientRrFromPct(
|
||
(document.getElementById("order-sl-pct")||{}).value,
|
||
(document.getElementById("order-tp-pct")||{}).value
|
||
);
|
||
if(rejectManualOrderRr(rr)){
|
||
if(window.FormSubmitGuard) FormSubmitGuard.unlock(addOrderForm);
|
||
return;
|
||
}
|
||
allowManualOrderSubmit(addOrderForm);
|
||
return;
|
||
}
|
||
const sl = Number((document.getElementById("order-sl")||{}).value);
|
||
const tp = Number((document.getElementById("order-tp")||{}).value);
|
||
let entry = sl;
|
||
if(window.FormSubmitGuard) FormSubmitGuard.lock(addOrderForm, "校验盈亏比…");
|
||
if(!symbol){
|
||
if(rejectManualOrderRr(calcClientRr(direction, entry, sl, tp))){
|
||
if(window.FormSubmitGuard) FormSubmitGuard.unlock(addOrderForm);
|
||
return;
|
||
}
|
||
allowManualOrderSubmit(addOrderForm);
|
||
return;
|
||
}
|
||
fetch(`/api/order_defaults?symbol=${encodeURIComponent(symbol)}&direction=${encodeURIComponent(direction)}`)
|
||
.then(r=>r.json())
|
||
.then(data=>{
|
||
const px = data.last_price || data.price;
|
||
if(px) entry = Number(px);
|
||
if(rejectManualOrderRr(calcClientRr(direction, entry, sl, tp))){
|
||
if(window.FormSubmitGuard) FormSubmitGuard.unlock(addOrderForm);
|
||
return;
|
||
}
|
||
allowManualOrderSubmit(addOrderForm);
|
||
})
|
||
.catch(()=>{
|
||
alert("无法校验盈亏比,请稍后重试");
|
||
if(window.FormSubmitGuard) FormSubmitGuard.unlock(addOrderForm);
|
||
});
|
||
});
|
||
}
|
||
|
||
refreshOrderDefaults();
|
||
refreshPriceSnapshotConditional();
|
||
setInterval(refreshAccountSnapshot, Number(document.body.dataset.balanceRefreshMs || 30000));
|
||
function refreshPriceSnapshotConditional(){
|
||
const page = document.body.getAttribute("data-page") || "";
|
||
fetch("/api/price_snapshot").then(r=>r.json()).then(data=>{
|
||
const updatedEl = document.getElementById("price-last-updated");
|
||
if(data.updated_at && updatedEl) updatedEl.innerText = data.updated_at;
|
||
if(page === "key_monitor"){
|
||
(data.key_prices || []).forEach(k=>{
|
||
const pEl = document.getElementById(`key-price-${k.id}`);
|
||
if(pEl){ pEl.innerText = k.price_display || (Number.isFinite(Number(k.price)) ? Number(k.price).toFixed(6) : "-"); paintPriceTrend(pEl, `k-${k.id}`, Number(k.price)); }
|
||
const upEl = document.getElementById(`key-up-diff-${k.id}`);
|
||
if(upEl) upEl.innerText = `${formatSigned(k.upper_diff, 4)} (${formatSigned(k.upper_pct, 2)}%)`;
|
||
const lowEl = document.getElementById(`key-low-diff-${k.id}`);
|
||
if(lowEl) lowEl.innerText = `${formatSigned(k.lower_diff, 4)} (${formatSigned(k.lower_pct, 2)}%)`;
|
||
const gateEl = document.getElementById(`key-gate-${k.id}`);
|
||
if(gateEl){ gateEl.innerText = k.gate_summary || "-"; gateEl.style.color = k.gate_ok ? "#4cd97f" : "#ff8f8f"; }
|
||
const gateMetricEl = document.getElementById(`key-gate-metrics-${k.id}`);
|
||
if(gateMetricEl) gateMetricEl.innerText = k.gate_metrics || "";
|
||
if(typeof paintKeyMonitorSummary === "function") paintKeyMonitorSummary(k.id, k);
|
||
});
|
||
}
|
||
if(page === "trade"){
|
||
(data.order_prices || []).forEach(o=>{
|
||
const pEl = document.getElementById(`order-price-${o.id}`);
|
||
if(pEl){
|
||
const hasMark = (()=>{ const x = o.exchange_mark_price; if(x===null||x===undefined||x==="")return false; const n=Number(x); return !Number.isNaN(n); })();
|
||
let disp = "";
|
||
if(hasMark && o.exchange_mark_price_display) disp = o.exchange_mark_price_display;
|
||
else if(o.price_display) disp = o.price_display;
|
||
else { const px = hasMark ? Number(o.exchange_mark_price) : Number(o.price); disp = Number.isFinite(px) ? px.toFixed(6) : "-"; }
|
||
pEl.innerText = disp;
|
||
const pxNum = hasMark ? Number(o.exchange_mark_price) : Number(o.price);
|
||
paintPriceTrend(pEl, `o-${o.id}`, Number.isFinite(pxNum) ? pxNum : px);
|
||
}
|
||
const exM = document.getElementById(`order-ex-margin-${o.id}`);
|
||
if(exM){
|
||
const mv = o.exchange_initial_margin;
|
||
const mn = (mv === null || mv === undefined || mv === "") ? NaN : Number(mv);
|
||
if(!Number.isNaN(mn)) exM.innerText = `${mn.toFixed(2)}U`;
|
||
else { const prc = (typeof data.positions_raw_count === "number") ? data.positions_raw_count : null; exM.innerText = (prc === 0) ? "无仓数据" : "-"; }
|
||
}
|
||
const pnlEl = document.getElementById(`order-pnl-${o.id}`);
|
||
if(pnlEl){
|
||
pnlEl.innerText = `${formatSigned(o.float_pnl, 2)}U (${formatSigned(o.float_pct, 2)}%)`;
|
||
pnlEl.classList.remove("price-up","price-down","price-flat");
|
||
if(Number(o.float_pnl) > 0) pnlEl.classList.add("price-up");
|
||
else if(Number(o.float_pnl) < 0) pnlEl.classList.add("price-down");
|
||
else pnlEl.classList.add("price-flat");
|
||
}
|
||
const rrEl = document.getElementById(`order-rr-${o.id}`);
|
||
if(rrEl) rrEl.innerText = formatRrRatio(o.rr_ratio);
|
||
paintBreakevenBadge(o.id, o.sl_breakeven_secured);
|
||
paintExchangeTpslRow(o.id, o.exchange_tpsl || {});
|
||
paintPlanTpslDisplay(o.id, o);
|
||
if(window.TimeCloseUI) TimeCloseUI.paintOrderTimeClose(o);
|
||
const holdEl = document.getElementById(`order-hold-duration-${o.id}`);
|
||
if(holdEl && o.opened_at_ms != null && o.opened_at_ms !== ""){
|
||
holdEl.setAttribute("data-order-opened-ms", String(o.opened_at_ms));
|
||
}
|
||
});
|
||
tickOrderHoldDurations();
|
||
}
|
||
}).catch(()=>{});
|
||
}
|
||
function formatLiveHoldDurationFromMs(openedMs, nowMs){
|
||
if(openedMs == null || openedMs === "" || !Number.isFinite(Number(openedMs))) return "—";
|
||
const ms = Number(openedMs);
|
||
const now = (nowMs != null) ? nowMs : Date.now();
|
||
let sec = Math.floor((now - ms) / 1000);
|
||
if(sec < 0) sec = 0;
|
||
if(sec <= 0) return "0分钟";
|
||
const d = Math.floor(sec / 86400); sec %= 86400;
|
||
const h = Math.floor(sec / 3600); sec %= 3600;
|
||
const m = Math.floor(sec / 60);
|
||
const parts = [];
|
||
if(d) parts.push(`${d}天`);
|
||
if(h) parts.push(`${h}小时`);
|
||
if(m || !parts.length) parts.push(`${m}分钟`);
|
||
return parts.join("");
|
||
}
|
||
function tickOrderHoldDurations(){
|
||
const now = Date.now();
|
||
document.querySelectorAll(".order-hold-duration[data-order-opened-ms]").forEach(el=>{
|
||
const ms = Number(el.getAttribute("data-order-opened-ms"));
|
||
if(!Number.isFinite(ms) || ms <= 0) return;
|
||
el.textContent = formatLiveHoldDurationFromMs(ms, now);
|
||
});
|
||
}
|
||
setInterval(tickOrderHoldDurations, 1000);
|
||
tickOrderHoldDurations();
|
||
setInterval(refreshPriceSnapshotConditional, Number(document.body.dataset.priceRefreshMs || 5000));
|
||
</script>
|