Slim embed tab rendering to cut memory use and restore calculator.
Load only per-tab data for embed fragments, skip exchange capital fetches on tab switches, and harden calculator market imports/timeouts. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6836,37 +6836,73 @@ def render_main_page(page="trade", embed_mode=None):
|
||||
conn = get_db()
|
||||
session_row = ensure_session(conn, trading_day)
|
||||
local_current_capital = float(session_row["current_capital"])
|
||||
funding_capital, trading_capital = get_exchange_capitals()
|
||||
from instance_embed_context_lib import (
|
||||
embed_render_plan,
|
||||
minimal_stats_bundle,
|
||||
trade_records_summary,
|
||||
)
|
||||
|
||||
plan = embed_render_plan(page, embed_mode)
|
||||
if plan.exchange_capitals:
|
||||
funding_capital, trading_capital = get_exchange_capitals()
|
||||
else:
|
||||
funding_capital, trading_capital = None, None
|
||||
# 资金账户:仅展示交易所读取结果(含 0)。不可用 TOTAL_CAPITAL 兜底,否则会与实盘不符。
|
||||
funding_usdt = round(funding_capital, FUNDS_DECIMALS) if funding_capital is not None else None
|
||||
current_capital = round(trading_capital, FUNDS_DECIMALS) if trading_capital is not None else round(local_current_capital, FUNDS_DECIMALS)
|
||||
recommended_capital = get_recommended_capital(current_capital)
|
||||
key_list = conn.execute("SELECT * FROM key_monitors").fetchall()
|
||||
key_history = conn.execute(
|
||||
"SELECT * FROM key_monitor_history WHERE closed_at >= ? AND closed_at <= ? ORDER BY id DESC LIMIT 500",
|
||||
(start_bj, end_bj),
|
||||
).fetchall()
|
||||
stats_bundle = compute_stats_bundle(conn, trading_day, now)
|
||||
raw_order_list = conn.execute("SELECT * FROM order_monitors WHERE status='active'").fetchall()
|
||||
order_list = []
|
||||
for o in raw_order_list:
|
||||
order_list.append(enrich_order_item(row_to_dict(o), current_capital))
|
||||
tr_ts = sql_list_time_field("closed_at", "created_at", "opened_at")
|
||||
raw_records = conn.execute(
|
||||
f"SELECT * FROM trade_records WHERE {tr_ts} >= ? AND {tr_ts} <= ? ORDER BY id DESC LIMIT 1000",
|
||||
(start_bj, end_bj),
|
||||
).fetchall()
|
||||
records = [to_effective_trade_dict(r) for r in raw_records]
|
||||
total = len(records)
|
||||
miss_count = sum(1 for r in records if (r.get("effective_result") or "") == "错过")
|
||||
win = sum(1 for r in records if (r.get("effective_result") or "") in ("止盈", "保本止盈", "移动止盈"))
|
||||
occupied_miss_total = sum(
|
||||
1
|
||||
for r in records
|
||||
if (r.get("effective_result") or "") == "错过"
|
||||
and ("持仓占用" in str(r.get("effective_miss_reason") or ""))
|
||||
key_list = (
|
||||
conn.execute("SELECT * FROM key_monitors").fetchall() if plan.key_list else []
|
||||
)
|
||||
rate = round(win/total*100,2) if total else 0
|
||||
key_history = (
|
||||
conn.execute(
|
||||
"SELECT * FROM key_monitor_history WHERE closed_at >= ? AND closed_at <= ? ORDER BY id DESC LIMIT 500",
|
||||
(start_bj, end_bj),
|
||||
).fetchall()
|
||||
if plan.key_history
|
||||
else []
|
||||
)
|
||||
stats_bundle = (
|
||||
compute_stats_bundle(conn, trading_day, now)
|
||||
if plan.stats_bundle
|
||||
else minimal_stats_bundle(TRADING_DAY_RESET_HOUR)
|
||||
)
|
||||
order_list = []
|
||||
if plan.orders:
|
||||
raw_order_list = conn.execute("SELECT * FROM order_monitors WHERE status='active'").fetchall()
|
||||
for o in raw_order_list:
|
||||
order_list.append(enrich_order_item(row_to_dict(o), current_capital))
|
||||
tr_ts = sql_list_time_field("closed_at", "created_at", "opened_at")
|
||||
if plan.records_rows:
|
||||
raw_records = conn.execute(
|
||||
f"SELECT * FROM trade_records WHERE {tr_ts} >= ? AND {tr_ts} <= ? ORDER BY id DESC LIMIT 1000",
|
||||
(start_bj, end_bj),
|
||||
).fetchall()
|
||||
records = [to_effective_trade_dict(r) for r in raw_records]
|
||||
total = len(records)
|
||||
miss_count = sum(1 for r in records if (r.get("effective_result") or "") == "错过")
|
||||
win = sum(
|
||||
1
|
||||
for r in records
|
||||
if (r.get("effective_result") or "") in ("止盈", "保本止盈", "移动止盈")
|
||||
)
|
||||
occupied_miss_total = sum(
|
||||
1
|
||||
for r in records
|
||||
if (r.get("effective_result") or "") == "错过"
|
||||
and ("持仓占用" in str(r.get("effective_miss_reason") or ""))
|
||||
)
|
||||
rate = round(win / total * 100, 2) if total else 0
|
||||
elif plan.records_summary:
|
||||
summary = trade_records_summary(conn, start_bj, end_bj, tr_ts)
|
||||
records = summary["records"]
|
||||
total = summary["total"]
|
||||
miss_count = summary["miss_count"]
|
||||
rate = summary["rate"]
|
||||
occupied_miss_total = summary["occupied_miss_total"]
|
||||
else:
|
||||
records = []
|
||||
total = miss_count = rate = occupied_miss_total = 0
|
||||
active_count = len(order_list)
|
||||
opens_today = count_opens_for_trading_day(conn, trading_day)
|
||||
risk_status = hub_account_risk_status(conn)
|
||||
@@ -6895,7 +6931,7 @@ def render_main_page(page="trade", embed_mode=None):
|
||||
trigger_entry_validity_hours=TRIGGER_ENTRY_VALIDITY_HOURS,
|
||||
)
|
||||
strategy_extra = {}
|
||||
if page in ("strategy", "strategy_trend", "strategy_roll", "strategy_records"):
|
||||
if plan.strategy:
|
||||
from strategy_ui import strategy_render_extras
|
||||
|
||||
strategy_extra = strategy_render_extras(
|
||||
@@ -6906,7 +6942,7 @@ def render_main_page(page="trade", embed_mode=None):
|
||||
trend_cfg=app.extensions.get("strategy_trend_cfg"),
|
||||
)
|
||||
orphan_live_positions = []
|
||||
if not order_list and exchange_private_api_configured():
|
||||
if plan.orphan_live and not order_list and exchange_private_api_configured():
|
||||
orphan_live_positions = list_orphan_live_positions(conn)
|
||||
conn.close()
|
||||
from instance_embed_lib import embed_context_extras
|
||||
|
||||
Reference in New Issue
Block a user