feat: 持仓快照盈亏比与交易所止损已保本标识
盈亏比固定用开仓 initial_stop_loss 计算,人工改委托后不变化;轮询交易所止损触发价相对成交价判定已保本,四所实例与中控统一显示绿色标识。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -601,6 +601,31 @@ def _find_exchange_tpsl_for_position(
|
||||
return None
|
||||
|
||||
|
||||
def _merge_flask_order_price_fields(hub_mon: dict | None, snap: dict | None) -> None:
|
||||
"""将 price_snapshot 中的快照盈亏比、已保本状态合并进 hub_monitor.orders。"""
|
||||
if not isinstance(hub_mon, dict) or not isinstance(snap, dict):
|
||||
return
|
||||
order_prices = snap.get("order_prices") or []
|
||||
op_by_id = {
|
||||
op.get("id"): op
|
||||
for op in order_prices
|
||||
if isinstance(op, dict) and op.get("id") is not None
|
||||
}
|
||||
orders = hub_mon.get("orders") or []
|
||||
if not isinstance(orders, list):
|
||||
return
|
||||
for o in orders:
|
||||
if not isinstance(o, dict):
|
||||
continue
|
||||
op = op_by_id.get(o.get("id"))
|
||||
if not isinstance(op, dict):
|
||||
continue
|
||||
if op.get("rr_ratio") is not None:
|
||||
o["rr_ratio"] = op["rr_ratio"]
|
||||
if "sl_breakeven_secured" in op:
|
||||
o["sl_breakeven_secured"] = bool(op["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")
|
||||
@@ -656,6 +681,8 @@ async def _assemble_board_row(
|
||||
client: httpx.AsyncClient, ex: dict, agent_row: dict
|
||||
) -> dict:
|
||||
hub_mon, meta, key_prices, snap = await _fetch_exchange_flask_bundle(client, ex)
|
||||
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)
|
||||
flask_ok = isinstance(hub_mon, dict) and hub_mon.get("ok") is not False
|
||||
raw_review = (ex.get("review_url") or "").strip()
|
||||
|
||||
@@ -847,6 +847,17 @@ body.market-chart-fs-open {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.hub-pos-card .pos-breakeven-badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 2px 8px;
|
||||
border-radius: 6px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
background: #1a3d2e;
|
||||
color: #4cd97f;
|
||||
}
|
||||
|
||||
.hub-pos-card .pos-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
|
||||
@@ -403,6 +403,28 @@
|
||||
return reward / risk;
|
||||
}
|
||||
|
||||
function resolveSnapshotRr(mo, side, entry, sl, tp, tpMonitored) {
|
||||
if (tpMonitored) return null;
|
||||
const snap = mo && mo.rr_ratio;
|
||||
if (snap != null && snap !== "") {
|
||||
const n = Number(snap);
|
||||
if (Number.isFinite(n)) return n;
|
||||
}
|
||||
const initSl = mo && (mo.initial_stop_loss != null ? mo.initial_stop_loss : mo.stop_loss);
|
||||
return calcRrRatio(side, entry, initSl || sl, tp);
|
||||
}
|
||||
|
||||
function isBreakevenSecured(side, entry, monitorOrder, cond) {
|
||||
const mo = monitorOrder || {};
|
||||
if (mo.sl_breakeven_secured === true || mo.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);
|
||||
if (!Number.isFinite(trig) || !Number.isFinite(e)) return false;
|
||||
if ((side || "long").toLowerCase() === "short") return trig <= e;
|
||||
return trig >= e;
|
||||
}
|
||||
|
||||
async function loadMonitorBoard() {
|
||||
const box = document.getElementById("monitor-grid");
|
||||
const showLoading = !lastMonitorRows.length;
|
||||
@@ -932,7 +954,8 @@
|
||||
const sl = tpsl.sl;
|
||||
const tp = tpsl.tp;
|
||||
const tpMonitored = tpsl.tp_monitored;
|
||||
const rr = tpMonitored ? null : calcRrRatio(side, entry, sl, tp);
|
||||
const rr = resolveSnapshotRr(mo, side, entry, sl, tp, tpMonitored);
|
||||
const beSecured = isBreakevenSecured(side, entry, mo, cond);
|
||||
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) {
|
||||
@@ -956,6 +979,9 @@
|
||||
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 mktAttrs = marketOpenBtnAttrs(exchangeId, exchangeKey, symbol, pos, monitorOrder, trendPlan);
|
||||
return `<div class="pos-card hub-pos-card">
|
||||
<div class="pos-card-head">
|
||||
|
||||
Reference in New Issue
Block a user