Fix roll position display: live contracts, TP profit, and 2-decimal qty precision.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -276,6 +276,7 @@ def apply_order_price_display_fields(
|
||||
contracts: Any = None,
|
||||
contract_size: Any = None,
|
||||
mark_price: Any = None,
|
||||
avg_entry_price: Any = None,
|
||||
funds_decimals: int = 2,
|
||||
) -> dict[str, Any]:
|
||||
disp_sl, disp_tp, _, _ = resolve_live_tpsl_prices(stop_loss, take_profit, exchange_tpsl)
|
||||
@@ -302,14 +303,17 @@ def apply_order_price_display_fields(
|
||||
payload["display_rr_ratio"] = None
|
||||
if contracts is not None:
|
||||
try:
|
||||
c = abs(float(contracts))
|
||||
from lib.hub.hub_position_metrics import normalize_contracts_qty
|
||||
|
||||
c = normalize_contracts_qty(contracts)
|
||||
if c > 0:
|
||||
payload["contracts"] = c
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
risk_entry = _positive_float(avg_entry_price) or _positive_float(entry_price)
|
||||
payload["latest_risk_amount"] = calc_latest_risk_amount(
|
||||
direction,
|
||||
entry_price,
|
||||
risk_entry,
|
||||
disp_sl if disp_sl is not None else stop_loss,
|
||||
margin_capital=margin_capital,
|
||||
leverage=leverage,
|
||||
@@ -319,6 +323,31 @@ def apply_order_price_display_fields(
|
||||
mark_price=mark_price,
|
||||
funds_decimals=funds_decimals,
|
||||
)
|
||||
tp_for_reward = disp_tp if disp_tp is not None else _positive_float(take_profit)
|
||||
qty_for_reward = payload.get("contracts")
|
||||
if qty_for_reward is None and contracts is not None:
|
||||
try:
|
||||
qty_for_reward = abs(float(contracts))
|
||||
except (TypeError, ValueError):
|
||||
qty_for_reward = None
|
||||
if risk_entry is not None and tp_for_reward is not None and qty_for_reward:
|
||||
try:
|
||||
from lib.strategy.strategy_roll_ui_lib import reward_at_tp_usdt
|
||||
|
||||
reward = reward_at_tp_usdt(
|
||||
direction,
|
||||
risk_entry,
|
||||
tp_for_reward,
|
||||
float(qty_for_reward),
|
||||
contract_size=float(contract_size or 1.0),
|
||||
)
|
||||
payload["reward_at_tp_usdt"] = (
|
||||
round(reward, funds_decimals) if reward is not None else None
|
||||
)
|
||||
except Exception:
|
||||
payload["reward_at_tp_usdt"] = None
|
||||
else:
|
||||
payload["reward_at_tp_usdt"] = None
|
||||
if format_price_fn is not None and symbol is not None:
|
||||
payload["stop_loss_display"] = (
|
||||
format_price_fn(symbol, disp_sl) if disp_sl is not None else "—"
|
||||
@@ -362,11 +391,14 @@ def enrich_active_monitor_tpsl_json(
|
||||
margin = _row_val("margin_capital")
|
||||
leverage = _row_val("leverage")
|
||||
if position_row is not None:
|
||||
from lib.hub.hub_position_metrics import position_contracts
|
||||
from lib.hub.hub_position_metrics import parse_position_entry_price, position_contracts
|
||||
|
||||
live_c = position_contracts(position_row)
|
||||
if abs(live_c) >= 1e-12:
|
||||
contracts = abs(live_c)
|
||||
avg_entry = parse_position_entry_price(position_row)
|
||||
else:
|
||||
avg_entry = None
|
||||
payload: dict[str, Any] = {
|
||||
"stop_loss": stop_loss,
|
||||
"take_profit": take_profit,
|
||||
@@ -388,6 +420,7 @@ def enrich_active_monitor_tpsl_json(
|
||||
contracts=contracts,
|
||||
contract_size=contract_size,
|
||||
mark_price=mark_price,
|
||||
avg_entry_price=avg_entry,
|
||||
funds_decimals=funds_decimals,
|
||||
)
|
||||
return payload
|
||||
|
||||
Reference in New Issue
Block a user