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
+12 -26
View File
@@ -66,6 +66,7 @@ from order_monitor_display_lib import (
apply_order_live_price_display,
apply_order_price_display_fields,
enrich_order_display_fields,
order_monitor_tpsl_needs_sync,
stop_is_profit_protecting,
tpsl_slot_trigger_price,
tpsl_update_passes_rr_gate,
@@ -5699,45 +5700,30 @@ def api_price_snapshot():
except Exception:
exchange_tpsl = {"sl": None, "tp": None}
payload["exchange_tpsl"] = exchange_tpsl
live_sl = tpsl_slot_trigger_price(exchange_tpsl.get("sl"))
live_tp = tpsl_slot_trigger_price(exchange_tpsl.get("tp"))
disp_sl = live_sl if live_sl is not None else r["stop_loss"]
disp_tp = live_tp if live_tp is not None else r["take_profit"]
sym = r["symbol"]
payload["stop_loss_raw"] = disp_sl
payload["take_profit_raw"] = disp_tp
payload["stop_loss_display"] = (
format_price_for_symbol(sym, disp_sl) if disp_sl not in (None, "") else ""
)
payload["take_profit_display"] = (
format_price_for_symbol(sym, disp_tp) if disp_tp not in (None, "") else ""
)
apply_order_price_display_fields(
payload,
direction=r["direction"],
entry_price=entry,
initial_stop_loss=r["initial_stop_loss"],
stop_loss=disp_sl,
take_profit=disp_tp,
stop_loss=r["stop_loss"],
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"],
)
order_prices.append(payload)
if live_sl is not None or live_tp is not None:
new_sl, new_tp, changed = order_monitor_tpsl_needs_sync(
r["stop_loss"], r["take_profit"], exchange_tpsl
)
if changed:
try:
cur_sl = float(r["stop_loss"] or 0)
cur_tp = float(r["take_profit"] or 0)
except (TypeError, ValueError):
cur_sl, cur_tp = 0.0, 0.0
new_sl = live_sl if live_sl is not None else cur_sl
new_tp = live_tp if live_tp is not None else cur_tp
if (live_sl is not None and abs(new_sl - cur_sl) > 1e-12) or (
live_tp is not None and abs(new_tp - cur_tp) > 1e-12
):
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()
+8 -6
View File
@@ -1723,14 +1723,16 @@ function paintPlanTpslDisplay(orderId, snap){
const slEl = document.getElementById(`order-plan-sl-${orderId}`);
const tpEl = document.getElementById(`order-plan-tp-${orderId}`);
const rrEl = document.getElementById(`order-rr-${orderId}`);
const slDisp = snap.stop_loss_display;
const tpDisp = snap.take_profit_display;
if(slEl && slDisp) slEl.innerText = slDisp;
if(tpEl && tpDisp) tpEl.innerText = tpDisp;
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(snap.stop_loss_raw != null && snap.stop_loss_raw !== "") card.setAttribute('data-plan-sl', formatPriceForInput(snap.stop_loss_raw));
if(slRaw != null && slRaw !== "") card.setAttribute('data-plan-sl', formatPriceForInput(slRaw));
else if(slDisp) card.setAttribute('data-plan-sl', slDisp);
if(snap.take_profit_raw != null && snap.take_profit_raw !== "") card.setAttribute('data-plan-tp', formatPriceForInput(snap.take_profit_raw));
if(tpRaw != null && tpRaw !== "") card.setAttribute('data-plan-tp', formatPriceForInput(tpRaw));
else if(tpDisp) card.setAttribute('data-plan-tp', tpDisp);
}
if(rrEl && typeof snap.rr_ratio !== "undefined") rrEl.innerText = formatRrRatio(snap.rr_ratio);