Fix dashboard position limit flicker by unifying active count across APIs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-02 14:53:04 +08:00
parent 658982d2b9
commit 98d63f38bf
4 changed files with 102 additions and 24 deletions
+52
View File
@@ -274,6 +274,58 @@ def count_active_trade_monitors(conn) -> int:
return 0
def _position_keys_from_rows(rows: list) -> set[tuple[str, str]]:
keys: set[tuple[str, str]] = set()
for p in rows or []:
lots = int(p.get("lots") or 0)
if lots <= 0:
continue
sym = (
p.get("symbol")
or p.get("symbol_code")
or p.get("ths_code")
or ""
).strip().lower()
direction = (p.get("direction") or "long").strip().lower()
if sym:
keys.add((sym, direction))
return keys
def effective_active_position_count(
conn,
mode: str,
*,
ctp_connected: Optional[bool] = None,
) -> int:
"""风控持仓数:柜台/快照实际持仓优先,本地监控作兜底。"""
monitor_count = count_active_trade_monitors(conn)
if ctp_connected is None:
try:
from modules.ctp.vnpy_bridge import ctp_status
ctp_connected = bool(ctp_status(mode).get("connected"))
except Exception:
ctp_connected = False
if not ctp_connected:
return monitor_count
keys: set[tuple[str, str]] = set()
try:
from modules.ctp.ctp_trading_state import trading_state
keys |= _position_keys_from_rows(trading_state.get_positions())
except Exception:
pass
try:
from modules.trading.position_stream import position_hub
snap = position_hub.get_snapshot() or {}
keys |= _position_keys_from_rows(snap.get("rows"))
except Exception:
pass
return max(monitor_count, len(keys))
def parse_mood_issues(raw: Any) -> list[str]:
if raw is None:
return []