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:
@@ -1,5 +1,8 @@
|
||||
from order_monitor_display_lib import (
|
||||
apply_order_price_display_fields,
|
||||
is_sl_breakeven_secured,
|
||||
order_monitor_tpsl_needs_sync,
|
||||
resolve_live_tpsl_prices,
|
||||
sl_breakeven_from_exchange_tpsl,
|
||||
snapshot_rr,
|
||||
snapshot_stop_loss,
|
||||
@@ -48,3 +51,46 @@ def test_sl_breakeven_from_exchange_tpsl():
|
||||
{"sl": {"trigger_price": 2.735}, "tp": {"trigger_price": 3.3}},
|
||||
)
|
||||
assert ok is True
|
||||
|
||||
|
||||
def test_resolve_live_tpsl_prefers_exchange():
|
||||
disp_sl, disp_tp, ex_sl, ex_tp = resolve_live_tpsl_prices(
|
||||
1674,
|
||||
1647.65,
|
||||
{"sl": {"trigger_price": 1661}, "tp": {"trigger_price": 1647.65}},
|
||||
)
|
||||
assert disp_sl == 1661
|
||||
assert disp_tp == 1647.65
|
||||
assert ex_sl == 1661
|
||||
assert ex_tp == 1647.65
|
||||
|
||||
|
||||
def test_order_monitor_tpsl_needs_sync_detects_sl_change():
|
||||
new_sl, new_tp, changed = order_monitor_tpsl_needs_sync(
|
||||
1674,
|
||||
1647.65,
|
||||
{"sl": {"trigger_price": 1661}, "tp": {"trigger_price": 1647.65}},
|
||||
)
|
||||
assert changed is True
|
||||
assert new_sl == 1661
|
||||
assert new_tp == 1647.65
|
||||
|
||||
|
||||
def test_apply_order_price_display_fields_live_sl():
|
||||
payload = {}
|
||||
apply_order_price_display_fields(
|
||||
payload,
|
||||
direction="short",
|
||||
entry_price=1663.45,
|
||||
initial_stop_loss=1674,
|
||||
stop_loss=1674,
|
||||
take_profit=1647.65,
|
||||
calc_rr_ratio_fn=_calc_rr,
|
||||
exchange_tpsl={"sl": {"trigger_price": 1661}, "tp": {"trigger_price": 1647.65}},
|
||||
format_price_fn=lambda _s, v: f"{v:.2f}",
|
||||
symbol="ETH/USDT:USDT",
|
||||
)
|
||||
assert payload["stop_loss"] == 1661
|
||||
assert payload["stop_loss_display"] == "1661.00"
|
||||
assert payload["sl_breakeven_secured"] is True
|
||||
assert payload["rr_ratio"] is not None
|
||||
|
||||
Reference in New Issue
Block a user