修复中控保本状态

This commit is contained in:
dekun
2026-06-03 16:41:48 +08:00
parent cf3e2ee1c9
commit d9b1b324f9
3 changed files with 78 additions and 7 deletions
+46
View File
@@ -626,6 +626,51 @@ def _merge_flask_order_price_fields(hub_mon: dict | None, snap: dict | None) ->
o["sl_breakeven_secured"] = bool(op["sl_breakeven_secured"])
def _merge_flask_position_breakeven(agent_row: dict, snap: dict | None, hub_mon: dict | None) -> None:
"""将 price_snapshot 的已保本状态同步到 agent 持仓,供中控首页表格展示。"""
ag = agent_row.get("agent")
if not isinstance(ag, dict) or not isinstance(snap, dict):
return
positions = ag.get("positions")
if not isinstance(positions, list) or not positions:
return
order_prices = snap.get("order_prices") or []
hub_orders = []
if isinstance(hub_mon, dict):
hub_orders = hub_mon.get("orders") or []
op_by_id = {
op.get("id"): op
for op in order_prices
if isinstance(op, dict) and op.get("id") is not None
}
for p in positions:
if not isinstance(p, dict):
continue
sym = p.get("symbol") or ""
side = (p.get("side") or "").lower()
matched = None
for o in hub_orders:
if not isinstance(o, dict):
continue
o_sym = o.get("exchange_symbol") or o.get("symbol") or ""
if not _symbols_match(sym, o_sym):
continue
if (o.get("direction") or "").lower() != side:
continue
matched = op_by_id.get(o.get("id"))
break
if matched is None:
for op in order_prices:
if not isinstance(op, dict):
continue
if not _symbols_match(sym, op.get("symbol") or ""):
continue
matched = op
break
if isinstance(matched, dict) and "sl_breakeven_secured" in matched:
p["sl_breakeven_secured"] = bool(matched["sl_breakeven_secured"])
def _merge_flask_exchange_tpsl(agent_row: dict, snap: dict | None, hub_mon: dict | None) -> None:
"""子代理挂单为空时,用实例 Flask 已算好的 exchange_tpsl 补全展示。"""
ag = agent_row.get("agent")
@@ -684,6 +729,7 @@ async def _assemble_board_row(
if isinstance(hub_mon, dict):
_merge_flask_order_price_fields(hub_mon, snap)
_merge_flask_exchange_tpsl(agent_row, snap, hub_mon if isinstance(hub_mon, dict) else None)
_merge_flask_position_breakeven(agent_row, snap, hub_mon if isinstance(hub_mon, dict) else None)
flask_ok = isinstance(hub_mon, dict) and hub_mon.get("ok") is not False
raw_review = (ex.get("review_url") or "").strip()
review_link = browser_url(raw_review) if raw_review else default_review_url(
+18
View File
@@ -858,6 +858,24 @@ body.market-chart-fs-open {
color: #4cd97f;
}
.pos-breakeven-badge {
display: inline-flex;
align-items: center;
margin-left: 6px;
padding: 2px 8px;
border-radius: 6px;
font-size: 11px;
font-weight: 600;
background: #1a3d2e;
color: #4cd97f;
vertical-align: middle;
white-space: nowrap;
}
.data-table .td-symbol {
white-space: nowrap;
}
.hub-pos-card .pos-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
+14 -7
View File
@@ -414,9 +414,11 @@
return calcRrRatio(side, entry, initSl || sl, tp);
}
function isBreakevenSecured(side, entry, monitorOrder, cond) {
function isBreakevenSecured(side, entry, monitorOrder, cond, pos) {
const mo = monitorOrder || {};
const p = pos || {};
if (mo.sl_breakeven_secured === true || mo.sl_breakeven_secured === 1) return true;
if (p.sl_breakeven_secured === true || p.sl_breakeven_secured === 1) return true;
const { sl } = pickExTpslOrders(cond);
const trig = sl && sl.trigger_price != null ? Number(sl.trigger_price) : NaN;
const e = Number(entry);
@@ -425,6 +427,10 @@
return trig >= e;
}
function breakevenBadgeHtml() {
return `<span class="pos-breakeven-badge">已保本</span>`;
}
async function loadMonitorBoard() {
const box = document.getElementById("monitor-grid");
const showLoading = !lastMonitorRows.length;
@@ -955,7 +961,7 @@
const tp = tpsl.tp;
const tpMonitored = tpsl.tp_monitored;
const rr = resolveSnapshotRr(mo, side, entry, sl, tp, tpMonitored);
const beSecured = isBreakevenSecured(side, entry, mo, cond);
const beSecured = isBreakevenSecured(side, entry, mo, cond, pos);
const upnl = pos.unrealized_pnl;
let pnlText = fmt(upnl, 2) + "U";
if (pos.notional_usdt && upnl != null && Math.abs(Number(pos.notional_usdt)) > 1e-8) {
@@ -979,14 +985,12 @@
meta.push(
`<span class="${beOn ? "pos-meta-on" : "pos-meta-off"}">移动保本:${beOn ? "开" : "关"}</span>`
);
if (beSecured) {
meta.push(`<span class="pos-meta-item"><span class="pos-breakeven-badge">已保本</span></span>`);
}
const symBeBadge = beSecured ? ` ${breakevenBadgeHtml()}` : "";
const mktAttrs = marketOpenBtnAttrs(exchangeId, exchangeKey, symbol, pos, monitorOrder, trendPlan);
return `<div class="pos-card hub-pos-card">
<div class="pos-card-head">
<div class="pos-card-symbol">
<button type="button" class="btn-open-market sym-link pos-symbol-link" ${mktAttrs} title="打开行情区(含入场/止盈止损)"><strong>${esc(symbol)}</strong></button>
<button type="button" class="btn-open-market sym-link pos-symbol-link" ${mktAttrs} title="打开行情区(含入场/止盈止损)"><strong>${esc(symbol)}</strong></button>${symBeBadge}
<span class="pos-side-badge ${sideCls}">${sideCn}</span>
</div>
<div class="pos-head-actions">
@@ -1091,18 +1095,21 @@
const symAttr = esc(x.symbol || "").replace(/"/g, "&quot;");
const exKeyAttr = esc(exchangeKey || exchangeId || "").replace(/"/g, "&quot;");
const sideAttr = esc((x.side || "").toLowerCase()).replace(/"/g, "&quot;");
const side = sideAttr || "long";
const contractsAttr = esc(String(x.contracts != null ? x.contracts : "")).replace(/"/g, "&quot;");
const cond = condOrdersFromPosition(x);
const reg = Array.isArray(x.regular_orders) ? x.regular_orders : [];
const tpsl = resolvePositionTpsl(x, monitorOrder, trendPlan);
const beSecured = isBreakevenSecured(side, tpsl.entry, monitorOrder, cond, x);
const slAttr = esc(String(tpsl.sl)).replace(/"/g, "&quot;");
const tpAttr = esc(String(tpsl.tp)).replace(/"/g, "&quot;");
const mktAttrs = marketOpenBtnAttrs(exchangeId, exchangeKey, x.symbol, x, monitorOrder, trendPlan);
const symBeBadge = beSecured ? ` ${breakevenBadgeHtml()}` : "";
return `<div class="pos-block">
<div class="table-scroll">
<table class="data-table"><thead><tr><th>合约</th><th>方向</th><th>张数</th><th>浮盈</th><th>操作</th></tr></thead><tbody>
<tr>
<td><button type="button" class="btn-open-market sym-link" ${mktAttrs} title="打开行情区(含入场/止盈止损)">${esc(x.symbol)}</button></td>
<td class="td-symbol"><button type="button" class="btn-open-market sym-link" ${mktAttrs} title="打开行情区(含入场/止盈止损)">${esc(x.symbol)}</button>${symBeBadge}</td>
<td class="${sideDirCls(x.side)}">${renderDirectionHtml(x.side)}</td>
<td>${fmt(x.contracts, 4)}</td>
<td class="${pnlCls(x.unrealized_pnl)}">${fmt(x.unrealized_pnl, 2)}</td>