fix(trend): align hub and four-exchange trend plan display

Unify gate_bot with shared enrich_trend_plan for strategy pages and hub monitor, reconcile DCA avg with live entry price, and fix missing fill price display.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-07 18:05:30 +08:00
parent e5576eaaed
commit 6a4ec69dba
7 changed files with 287 additions and 127 deletions
+45 -10
View File
@@ -456,11 +456,21 @@ def _trend_add_leg_fields(cfg: dict, d: dict) -> dict:
except Exception:
grid = []
add_prices: list[float] = []
for x in grid[:legs_done]:
try:
add_prices.append(float(x))
except (TypeError, ValueError):
pass
try:
from strategy_trend_lib import reconcile_trend_leg_fill_prices
fills = reconcile_trend_leg_fill_prices(out)
for i in range(1, legs_done + 1):
if i < len(fills):
add_prices.append(float(fills[i]))
except Exception:
fills = []
if not add_prices:
for x in grid[:legs_done]:
try:
add_prices.append(float(x))
except (TypeError, ValueError):
pass
sym = out.get("exchange_symbol") or out.get("symbol") or ""
out["add_count"] = legs_done
out["add_count_total"] = dca_legs
@@ -504,6 +514,11 @@ def _patch_hub_trend_views(app: Flask) -> None:
app.config["HUB_CTX"] = ctx
def patch_trend_hub_enrich(app: Flask, cfg: dict) -> None:
"""hub_bridge install 之后调用:四所 /api/hub/monitor 趋势字段与策略页一致。"""
_patch_hub_monitor_enrich(app, cfg)
def _patch_hub_monitor_enrich(app: Flask, cfg: dict) -> None:
ctx = dict(app.config.get("HUB_CTX") or {})
prev = ctx.get("enrich_monitor")
@@ -530,7 +545,6 @@ def _patch_hub_monitor_enrich(app: Flask, cfg: dict) -> None:
def enrich_trend_plan(cfg: dict, row) -> dict:
m = _m(cfg)
d = _row(cfg, row)
d = _trend_add_leg_fields(cfg, d)
try:
d["breakeven_applied"] = int(d.get("breakeven_applied") or 0) != 0
except Exception:
@@ -538,6 +552,7 @@ def enrich_trend_plan(cfg: dict, row) -> dict:
ex_sym = d.get("exchange_symbol") or m.normalize_exchange_symbol(d.get("symbol") or "")
direction = (d.get("direction") or "long").lower()
metrics_fn = getattr(m, "get_live_position_exchange_metrics", None)
met = None
if callable(metrics_fn):
try:
lev = int(d.get("leverage") or 0) or None
@@ -547,6 +562,13 @@ def enrich_trend_plan(cfg: dict, row) -> dict:
met = metrics_fn(ex_sym, direction, order_leverage=lev)
except TypeError:
met = metrics_fn(ex_sym, direction)
if met and met.get("entry_price") is not None:
try:
live_entry = float(met["entry_price"])
if live_entry > 0:
d["avg_entry_price"] = live_entry
except (TypeError, ValueError):
pass
if met and met.get("unrealized_pnl") is not None:
d["floating_pnl"] = float(met["unrealized_pnl"])
elif (
@@ -587,6 +609,7 @@ def enrich_trend_plan(cfg: dict, row) -> dict:
d["contract_size"] = float(get_cs(ex_sym))
except (TypeError, ValueError):
pass
d = _trend_add_leg_fields(cfg, d)
from strategy_snapshot_lib import attach_trend_dca_levels
from strategy_trend_lib import calc_trend_plan_money_metrics
@@ -1256,13 +1279,24 @@ def load_trend_page_context(conn, request_obj, cfg: dict) -> dict[str, Any]:
or 0
)
trend_plans = []
for r in conn.execute(
trend_dca_probes = []
raw_plans = conn.execute(
"SELECT * FROM trend_pullback_plans WHERE status='active' ORDER BY id DESC"
).fetchall():
).fetchall()
for r in raw_plans:
try:
trend_plans.append(enrich_trend_plan(cfg, r))
enriched = enrich_trend_plan(cfg, r)
trend_plans.append(enriched)
except Exception:
trend_plans.append(_row(cfg, r))
enriched = _row(cfg, r)
trend_plans.append(enriched)
try:
probe = summarize_trend_dca_probe(cfg, r)
trend_dca_probes.append(probe)
if isinstance(enriched, dict):
enriched["dca_probe"] = probe
except Exception:
pass
now = m.app_now()
active_count = m.get_active_position_count(conn)
can_trade_trend = (
@@ -1298,6 +1332,7 @@ def load_trend_page_context(conn, request_obj, cfg: dict) -> dict[str, Any]:
trend_preview_expired = True
return {
"trend_plans": trend_plans,
"trend_dca_probes": trend_dca_probes,
"trend_active": trend_active,
"can_trade_trend": can_trade_trend,
"trend_preview": trend_preview,