fix: sync live TP/SL to position cards after entrust changes

Use exchange TP/SL for display and DB sync on price_snapshot polls, refresh instance UI cells on each tick, and merge live values into hub monitor board.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-09 11:06:27 +08:00
parent 7cb55f6557
commit f7d94f67d7
12 changed files with 291 additions and 89 deletions
+20 -1
View File
@@ -140,6 +140,7 @@ from key_monitor_lib import (
from order_monitor_display_lib import (
apply_order_price_display_fields,
enrich_order_display_fields,
order_monitor_tpsl_needs_sync,
)
from wechat_notify_lib import build_wechat_rs_level_message, send_wechat_webhook
from hub_auth import request_allowed as hub_request_allowed
@@ -5935,7 +5936,6 @@ def api_price_snapshot():
order_rows = conn.execute(
"SELECT id,symbol,exchange_symbol,direction,trigger_price,stop_loss,initial_stop_loss,take_profit,margin_capital,leverage FROM order_monitors WHERE status='active'"
).fetchall()
conn.close()
try:
ensure_markets_loaded()
@@ -6122,9 +6122,28 @@ def api_price_snapshot():
take_profit=r["take_profit"],
calc_rr_ratio_fn=calc_rr_ratio,
exchange_tpsl=exchange_tpsl,
format_price_fn=format_price_for_symbol,
symbol=r["symbol"],
)
new_sl, new_tp, changed = order_monitor_tpsl_needs_sync(
r["stop_loss"], r["take_profit"], exchange_tpsl
)
if changed:
try:
conn.execute(
"UPDATE order_monitors SET stop_loss=?, take_profit=? WHERE id=?",
(new_sl, new_tp, int(r["id"])),
)
except Exception:
pass
order_prices.append(payload)
try:
conn.commit()
except Exception:
pass
conn.close()
from hub_position_metrics import build_position_marks_list
position_marks = build_position_marks_list(
+22 -10
View File
@@ -550,19 +550,11 @@
</div>
<div class="pos-cell">
<span class="pos-label">止损</span>
{% if o.stop_loss %}
<span class="pos-value">{{ price_fmt(o.symbol, o.stop_loss) }}</span>
{% else %}
<span class="pos-value pos-val-dash"></span>
{% endif %}
<span class="pos-value" id="order-plan-sl-{{ o.id }}">{{ price_fmt(o.symbol, o.stop_loss) if o.stop_loss else '—' }}</span>
</div>
<div class="pos-cell">
<span class="pos-label">止盈</span>
{% if o.take_profit %}
<span class="pos-value">{{ price_fmt(o.symbol, o.take_profit) }}</span>
{% else %}
<span class="pos-value pos-val-dash"></span>
{% endif %}
<span class="pos-value" id="order-plan-tp-{{ o.id }}">{{ price_fmt(o.symbol, o.take_profit) if o.take_profit else '—' }}</span>
</div>
<div class="pos-cell">
<span class="pos-label">盈亏比</span>
@@ -1924,6 +1916,24 @@ function paintBreakevenBadge(orderId, secured){
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;
@@ -2011,6 +2021,7 @@ function refreshPriceSnapshot(){
}
paintBreakevenBadge(o.id, o.sl_breakeven_secured);
if(o.exchange_tpsl) paintExchangeTpslRow(o.id, o.exchange_tpsl);
paintPlanTpslDisplay(o.id, o);
});
}).catch(()=>{});
}
@@ -2281,6 +2292,7 @@ function refreshPriceSnapshotConditional(){
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);
});
}
}).catch(()=>{});