From 5aba31f53013de90872ae459ed7c431768e76e9c Mon Sep 17 00:00:00 2001 From: dekun Date: Wed, 1 Jul 2026 13:27:46 +0800 Subject: [PATCH] Fix position SSE JSON serialization for datetime fields. Co-authored-by: Cursor --- install_trading.py | 15 +++++++++++---- kline_stream.py | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/install_trading.py b/install_trading.py index 44346bd..e543575 100644 --- a/install_trading.py +++ b/install_trading.py @@ -759,10 +759,17 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se has_commission = True return round(total, 2) if has_commission else None + def _time_str(val) -> str: + if val is None: + return "" + if isinstance(val, str): + return val.strip() + return str(val).strip() + def _holding_duration(open_time: str, now_iso: str) -> str: try: from app import calc_holding_duration - open_s = (open_time or "").strip().replace("T", " ")[:19] + open_s = _time_str(open_time).replace("T", " ")[:19] now_s = (now_iso or "").strip().replace("T", " ")[:19] if not open_s or not now_s: return "" @@ -1362,7 +1369,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se lots = int(mon.get("lots") or 0) entry = float(mon.get("entry_price") or 0) source_label = monitor_source_label(mon.get("monitor_type")) - open_time = (mon.get("open_time") or "").strip() + open_time = _time_str(mon.get("open_time")) open_time_source = "order" margin = mon.get("margin") position_pct = mon.get("position_pct") @@ -1376,7 +1383,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se lots = int(ctp.get("lots") or 0) entry = float(ctp.get("avg_price") or 0) source_label = "CTP 柜台" - open_time = (ctp.get("open_time") or "").strip() + open_time = _time_str(ctp.get("open_time")) open_time_source = "ctp" margin = None position_pct = None @@ -1571,7 +1578,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se pos_metrics = calc_position_metrics( direction, order_price, sl or order_price, tp or order_price, lots, order_price, capital, sym, ) - open_time = (mon.get("open_time") or "").strip() + open_time = _time_str(mon.get("open_time")) timeout_sec = get_pending_order_timeout_sec(get_setting) remain = pending_auto_cancel_remaining(mon, timeout_sec=timeout_sec) return { diff --git a/kline_stream.py b/kline_stream.py index c355a4a..db361ca 100644 --- a/kline_stream.py +++ b/kline_stream.py @@ -29,7 +29,7 @@ FAST_PERIODS = frozenset({ def sse_format(event: str, data: dict) -> str: - return f"event: {event}\ndata: {json.dumps(data, ensure_ascii=False)}\n\n" + return f"event: {event}\ndata: {json.dumps(data, ensure_ascii=False, default=str)}\n\n" @dataclass