stock-analysis-plugin 0.2.12 → 0.2.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ import math
8
8
  import os
9
9
  import re
10
10
  import sys
11
+ import threading
11
12
  import time
12
13
  from datetime import date, datetime, timedelta, timezone
13
14
  from pathlib import Path
@@ -221,6 +222,9 @@ def _kline_efinance(symbol: str, period: str, count: int) -> list:
221
222
  return [_clean_row(r) for r in df.to_dict("records")]
222
223
 
223
224
 
225
+ _BAOSTOCK_LOCK = threading.Lock()
226
+
227
+
224
228
  def _kline_baostock(symbol: str, period: str, count: int) -> list:
225
229
  import io
226
230
 
@@ -232,24 +236,26 @@ def _kline_baostock(symbol: str, period: str, count: int) -> list:
232
236
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
233
237
  # baostock login/logout print status lines to stdout — silence them so the
234
238
  # tool's JSON output on stdout stays clean for the calling host.
235
- with contextlib.redirect_stdout(io.StringIO()):
236
- bs.login()
237
- try:
238
- rs = bs.query_history_k_data_plus(
239
- _to_baostock_code(symbol),
240
- "date,open,high,low,close,volume,amount,pctChg",
241
- start_date=start.strftime("%Y-%m-%d"),
242
- end_date=end.strftime("%Y-%m-%d"),
243
- frequency=freq_map.get(period, "d"),
244
- adjustflag="2",
245
- )
246
- rows = []
247
- while (rs.error_code == "0") and rs.next():
248
- rows.append(rs.get_row_data())
249
- df = pd.DataFrame(rows, columns=rs.fields)
250
- finally:
239
+ # One global baostock session per process: `klines` threads must not interleave login/query/logout.
240
+ with _BAOSTOCK_LOCK:
251
241
  with contextlib.redirect_stdout(io.StringIO()):
252
- bs.logout()
242
+ bs.login()
243
+ try:
244
+ rs = bs.query_history_k_data_plus(
245
+ _to_baostock_code(symbol),
246
+ "date,open,high,low,close,volume,amount,pctChg",
247
+ start_date=start.strftime("%Y-%m-%d"),
248
+ end_date=end.strftime("%Y-%m-%d"),
249
+ frequency=freq_map.get(period, "d"),
250
+ adjustflag="2",
251
+ )
252
+ rows = []
253
+ while (rs.error_code == "0") and rs.next():
254
+ rows.append(rs.get_row_data())
255
+ df = pd.DataFrame(rows, columns=rs.fields)
256
+ finally:
257
+ with contextlib.redirect_stdout(io.StringIO()):
258
+ bs.logout()
253
259
  if df.empty:
254
260
  raise ValueError("baostock returned empty data")
255
261
  col_map = {"amount": "turnover", "pctChg": "change_pct"}
@@ -263,40 +269,6 @@ def _kline_baostock(symbol: str, period: str, count: int) -> list:
263
269
  return [_clean_row(r) for r in df.to_dict("records")]
264
270
 
265
271
 
266
- def _quote_efinance(symbol: str) -> dict:
267
- import efinance as ef
268
-
269
- df = ef.stock.get_realtime_quotes([symbol])
270
- if df is None or df.empty:
271
- raise ValueError("efinance quote returned empty")
272
- r = df.iloc[0]
273
- col_map = {
274
- "股票代码": "symbol",
275
- "股票名称": "name",
276
- "最新价": "price",
277
- "涨跌额": "change",
278
- "涨跌幅": "change_pct",
279
- "成交量": "volume",
280
- "成交额": "turnover",
281
- "最高": "high",
282
- "最低": "low",
283
- "今开": "open",
284
- "昨收": "prev_close",
285
- "总市值": "market_cap",
286
- "市盈率": "pe",
287
- "换手率": "turnover_rate",
288
- }
289
- result = {}
290
- for cn_key, en_key in col_map.items():
291
- val = r.get(cn_key)
292
- if val is not None and val != "":
293
- with contextlib.suppress(ValueError, TypeError):
294
- val = float(val) if en_key not in ("symbol", "name") else val
295
- result[en_key] = val
296
- result["pe_basis"] = "dynamic"
297
- return _clean_row(result)
298
-
299
-
300
272
  # --------------- tencent / sina (non-eastmoney A-share fallbacks, issue #25) ---------------
301
273
  #
302
274
  # akshare/efinance both resolve to eastmoney hosts, so an eastmoney-blocked network
@@ -343,7 +315,7 @@ def _quote_time(value: str, fmt: str, tz=timezone(timedelta(hours=8))) -> str |
343
315
 
344
316
  def _tencent_payload_row(f: list, symbol: str) -> dict:
345
317
  """Build the quote row from one Tencent `~`-separated payload (already split).
346
- Shared by _quote_tencent (single) and _tencent_batch_quotes (snapshot leg)."""
318
+ Used by _quote_tencent and the cn-indices batch fallback."""
347
319
  f += [""] * (50 - len(f)) # pad optional tail fields (pe/pb/market_cap/volume_ratio…) on short payloads
348
320
  market = detect_market(symbol)
349
321
  foreign = market in ("HK", "US") # HK and US share currency-unit turnover and lack the A-share ratio fields
@@ -419,7 +391,7 @@ def _quote_tencent(symbol: str) -> dict:
419
391
  raise ValueError(f"tencent returned no quote for {symbol}")
420
392
  if normalize_stock_code(symbol)["is_etf"]:
421
393
  # f[73] is total fund shares and f[78] the intraday IOPV (blank for money/bond ETFs);
422
- # premium matches _quote_akshare_etf.
394
+ # premium matches _quote_eastmoney.
423
395
  iopv = _parse_float(f[78]) if len(f) > 78 else None
424
396
  price = row.get("price")
425
397
  row.update(
@@ -1071,15 +1043,21 @@ def kline_yf(symbol: str, period: str, count: int, evidence: bool = False) -> li
1071
1043
 
1072
1044
  def cmd_kline(args):
1073
1045
  market = detect_market(args.symbol)
1074
- try:
1046
+ # Round counts up so 30/60/80/120/250-bar callers share one fetch; the last intraday bar may be up to 60 s stale.
1047
+ bucket = next((b for b in (60, 120, 250) if args.count <= b), args.count)
1048
+
1049
+ def fetch():
1075
1050
  if market == "A":
1076
- data = kline_a(args.symbol, args.period, args.count, evidence=True)
1051
+ data = kline_a(args.symbol, args.period, bucket, evidence=True)
1077
1052
  if data:
1078
1053
  adjusted = "none" if _A_INDEX_RE.fullmatch(args.symbol) else "qfq"
1079
1054
  data = [dict(row, adjusted=adjusted) for row in data]
1080
- else:
1081
- data = kline_yf(args.symbol, args.period, args.count, evidence=True)
1082
- return data
1055
+ return data
1056
+ return kline_yf(args.symbol, args.period, bucket, evidence=True)
1057
+
1058
+ try:
1059
+ data = cached(f"kline:v1:{args.symbol}:{args.period}:{bucket}", 60, fetch)
1060
+ return data[-args.count :] if isinstance(data, list) else data
1083
1061
  except Exception as e:
1084
1062
  return {"error": error_message(e)}
1085
1063
 
@@ -1087,27 +1065,136 @@ def cmd_kline(args):
1087
1065
  # --------------- quote ---------------
1088
1066
 
1089
1067
 
1090
- def quote_a(symbol: str, evidence: bool = False) -> dict:
1091
- """A-share quote with failover: tencent → akshare → tushare → efinance → sina; ETFs add fund spot after tencent.
1068
+ # Eastmoney answers per-code (ulist), ranked-page (clist) and server-filtered (datacenter)
1069
+ # queries, so no caller needs a whole-market spot table to read a few rows.
1070
+ _EM_HEADERS = {"User-Agent": "Mozilla/5.0"}
1071
+ _EM_PUSH2 = "https://push2.eastmoney.com/api/qt"
1072
+ _EM_DATACENTER = "https://datacenter-web.eastmoney.com/api/data/v1/get"
1073
+ _EM_FS = {
1074
+ "A": "m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048",
1075
+ "HK": "m:128+t:3,m:128+t:4,m:128+t:1,m:128+t:2",
1076
+ "US": "m:105,m:106,m:107",
1077
+ }
1078
+ _EM_PAGE = 100 # clist caps pz at 100 rows
1079
+
1092
1080
 
1093
- Tencent answers one symbol per request; the eastmoney spot sources page the whole
1094
- market (~55 requests) to return one row, so they only serve as fallbacks.
1081
+ def _get_json(url: str, params: dict) -> dict:
1082
+ """GET an eastmoney JSON API; relative paths are push2 qt endpoints."""
1083
+ import requests
1084
+
1085
+ url = url if "://" in url else f"{_EM_PUSH2}/{url}"
1086
+ resp = requests.get(url, params=params, headers=_EM_HEADERS, timeout=10)
1087
+ resp.raise_for_status()
1088
+ return resp.json()
1089
+
1090
+
1091
+ def _em_secid(symbol: str) -> str:
1092
+ code = _cn_code(symbol)
1093
+ return f"{1 if code[:2] == 'sh' else 0}.{code[2:]}"
1094
+
1095
+
1096
+ def _em_ulist(secids: list, fields: str) -> list:
1097
+ data = _get_json("ulist.np/get", {"fltt": 2, "invt": 2, "secids": ",".join(secids), "fields": fields}).get("data")
1098
+ return (data or {}).get("diff") or []
1099
+
1100
+
1101
+ def _em_clist(market: str, fid: str, fields: str, page: int = 1, size: int = _EM_PAGE, desc: bool = True):
1102
+ """One ranked page of a market list → (universe total, rows); pn is a page index of size pz."""
1103
+ params = {"np": 1, "fltt": 2, "invt": 2, "fs": _EM_FS[market], "fid": fid, "po": int(desc), "pn": page, "pz": size}
1104
+ data = _get_json("clist/get", {**params, "fields": fields}).get("data") or {}
1105
+ return int(data.get("total") or 0), data.get("diff") or []
1106
+
1107
+
1108
+ def _em_datacenter(report: str, filter_: str, sort: str, size: int, order: str = "-1", url: str = _EM_DATACENTER):
1109
+ """One server-filtered datacenter page → (match count, rows); no match is an empty page."""
1110
+ body = _get_json(
1111
+ url,
1112
+ {
1113
+ "reportName": report,
1114
+ "columns": "ALL",
1115
+ "filter": filter_,
1116
+ "sortColumns": sort,
1117
+ "sortTypes": order,
1118
+ "pageSize": size,
1119
+ "pageNumber": 1,
1120
+ "source": "WEB",
1121
+ "client": "WEB",
1122
+ },
1123
+ )
1124
+ if body.get("result") is None:
1125
+ if body.get("code") == 9201: # 返回数据为空
1126
+ return 0, []
1127
+ raise ValueError(f"eastmoney {report} unavailable: {str(body.get('message'))[:100]}")
1128
+ return int(body["result"].get("count") or 0), body["result"].get("data") or []
1129
+
1130
+
1131
+ _EM_CODETABLE = "https://search-codetable.eastmoney.com/codetable/search/web"
1132
+
1133
+
1134
+ def _em_codetable(keyword: str, count: int) -> list:
1135
+ """Eastmoney code-table search rows (stocks, funds, indices and BK boards) matching a keyword."""
1136
+ params = {"keyword": keyword, "pageIndex": 1, "pageSize": count, "client": "web", "clientType": "webSuggest"}
1137
+ body = _get_json(_EM_CODETABLE, {**params, "clientVersion": "lastest"})
1138
+ if str(body.get("code")) != "0":
1139
+ raise ValueError(f"eastmoney search unavailable: {str(body.get('msg'))[:100]}")
1140
+ return body.get("result") or []
1141
+
1142
+
1143
+ _EM_QUOTE_FIELDS = "f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f14,f15,f16,f17,f18,f20,f23,f24,f38,f441"
1144
+
1145
+
1146
+ def _quote_eastmoney(symbol: str) -> dict:
1147
+ """One-code eastmoney quote. ETFs add iopv (盘中实时估值), fund_shares (最新份额) and
1148
+ premium_discount_rate = (price - iopv) / iopv * 100 (positive = 溢价); None without an IOPV."""
1149
+ row = next((r for r in _em_ulist([_em_secid(symbol)], _EM_QUOTE_FIELDS) if str(r.get("f12")) == symbol), {})
1150
+
1151
+ def value(key):
1152
+ return _parse_float(row.get(key))
1153
+
1154
+ if value("f2") is None:
1155
+ raise ValueError(f"eastmoney returned no quote for {symbol}")
1156
+ result = {
1157
+ "symbol": symbol,
1158
+ "name": row.get("f14"),
1159
+ "price": value("f2"),
1160
+ "change": value("f4"),
1161
+ "change_pct": value("f3"),
1162
+ "volume": value("f5"),
1163
+ "turnover": value("f6"),
1164
+ "high": value("f15"),
1165
+ "low": value("f16"),
1166
+ "open": value("f17"),
1167
+ "prev_close": value("f18"),
1168
+ "market_cap": value("f20"),
1169
+ "turnover_rate": value("f8"),
1170
+ "volume_ratio": value("f10"),
1171
+ }
1172
+ if normalize_stock_code(symbol)["is_etf"]:
1173
+ price, iopv = result["price"], value("f441")
1174
+ premium = round((price - iopv) / iopv * 100, 4) if iopv else None
1175
+ result.update(is_etf=True, iopv=iopv, fund_shares=value("f38"), premium_discount_rate=premium)
1176
+ else:
1177
+ result.update(
1178
+ pe=value("f9"), pe_basis="dynamic", pb=value("f23"), amplitude=value("f7"), change_pct_60d=value("f24")
1179
+ )
1180
+ return _clean_row(result)
1181
+
1182
+
1183
+ def quote_a(symbol: str, evidence: bool = False) -> dict:
1184
+ """A-share quote with failover: tencent → eastmoney → tushare → sina, one code per request.
1095
1185
 
1096
1186
  Explicitly-prefixed symbols (sh000001, typically indices) go straight to
1097
- tencent/sina — the eastmoney sources take bare 6-digit codes only. ETF quotes use their own sticky chain key
1098
- (quote_a_etf) so a stock-side winner never demotes the fund-spot source."""
1187
+ tencent/sina — a bare secid would be ambiguous (sh000001 vs sz000001). ETF quotes use their own
1188
+ chain key (quote_a_etf) so a stock-side winner never reorders the ETF chain."""
1099
1189
  if _A_PREFIXED_RE.fullmatch(symbol):
1100
1190
  sources = [("tencent", lambda: _quote_tencent(symbol)), ("sina", lambda: _quote_sina(symbol))]
1101
1191
  with socket_timeout(25):
1102
1192
  return _failover(sources, label=f"quote_a:{symbol}", evidence=evidence)
1103
1193
  is_etf = normalize_stock_code(symbol)["is_etf"]
1104
- sources = [("tencent", lambda: _quote_tencent(symbol))]
1105
- if is_etf:
1106
- sources.append(("akshare_etf", lambda: _quote_akshare_etf(symbol)))
1107
- sources += [
1108
- ("akshare", lambda: _quote_akshare(symbol)),
1194
+ sources = [
1195
+ ("tencent", lambda: _quote_tencent(symbol)),
1196
+ ("eastmoney", lambda: _quote_eastmoney(symbol)),
1109
1197
  ("tushare", lambda: _quote_tushare(symbol)),
1110
- ("efinance", lambda: _quote_efinance(symbol)),
1111
1198
  ("sina", lambda: _quote_sina(symbol)),
1112
1199
  ]
1113
1200
  chain = "quote_a_etf" if is_etf else "quote_a"
@@ -1115,80 +1202,6 @@ def quote_a(symbol: str, evidence: bool = False) -> dict:
1115
1202
  return _failover(sources, label=f"{chain}:{symbol}", evidence=evidence)
1116
1203
 
1117
1204
 
1118
- def _quote_akshare_etf(symbol: str) -> dict:
1119
- """A-share ETF realtime quote via akshare fund_etf_spot_em.
1120
-
1121
- On top of the stock-shaped fields, the same spot row carries fund-native
1122
- columns: iopv (盘中实时估值) and fund_shares (最新份额). premium_discount_rate
1123
- is derived here as (price - iopv) / iopv * 100 — positive = 溢价 (price above
1124
- IOPV), negative = 折价; None when the fund publishes no intraday IOPV (money/
1125
- bond ETFs)."""
1126
- import akshare as ak
1127
-
1128
- df = _akshare_retry(ak.fund_etf_spot_em)
1129
- row = df[df["代码"] == symbol]
1130
- if row.empty:
1131
- raise ValueError(f"ETF {symbol} not found in akshare fund spot")
1132
- r = row.iloc[0]
1133
- result = _clean_row(
1134
- {
1135
- "symbol": symbol,
1136
- "name": r.get("名称"),
1137
- "price": r.get("最新价"),
1138
- "change": r.get("涨跌额"),
1139
- "change_pct": r.get("涨跌幅"),
1140
- "volume": r.get("成交量"),
1141
- "turnover": r.get("成交额"),
1142
- "high": r.get("最高价"),
1143
- "low": r.get("最低价"),
1144
- "open": r.get("开盘价"),
1145
- "prev_close": r.get("昨收"),
1146
- "market_cap": r.get("总市值"),
1147
- "turnover_rate": r.get("换手率"),
1148
- "volume_ratio": r.get("量比"),
1149
- "is_etf": True,
1150
- "iopv": r.get("IOPV实时估值"),
1151
- "fund_shares": r.get("最新份额"),
1152
- }
1153
- )
1154
- price, iopv = result.get("price"), result.get("iopv")
1155
- result["premium_discount_rate"] = round((price - iopv) / iopv * 100, 4) if price is not None and iopv else None
1156
- return result
1157
-
1158
-
1159
- def _quote_akshare(symbol: str) -> dict:
1160
- import akshare as ak
1161
-
1162
- df = _akshare_retry(ak.stock_zh_a_spot_em)
1163
- row = df[df["代码"] == symbol]
1164
- if row.empty:
1165
- raise ValueError(f"Symbol {symbol} not found in akshare")
1166
- r = row.iloc[0]
1167
- return _clean_row(
1168
- {
1169
- "symbol": symbol,
1170
- "name": r.get("名称"),
1171
- "price": r.get("最新价"),
1172
- "change": r.get("涨跌额"),
1173
- "change_pct": r.get("涨跌幅"),
1174
- "volume": r.get("成交量"),
1175
- "turnover": r.get("成交额"),
1176
- "high": r.get("最高"),
1177
- "low": r.get("最低"),
1178
- "open": r.get("今开"),
1179
- "prev_close": r.get("昨收"),
1180
- "market_cap": r.get("总市值"),
1181
- "pe": r.get("市盈率-动态"),
1182
- "pe_basis": "dynamic",
1183
- "pb": r.get("市净率"),
1184
- "turnover_rate": r.get("换手率"),
1185
- "volume_ratio": r.get("量比"),
1186
- "amplitude": r.get("振幅"),
1187
- "change_pct_60d": r.get("60日涨跌幅"),
1188
- }
1189
- )
1190
-
1191
-
1192
1205
  def _quote_yfinance(symbol: str) -> dict:
1193
1206
  import yfinance as yf
1194
1207
 
@@ -1449,6 +1462,48 @@ def cmd_quote(args):
1449
1462
  return result
1450
1463
 
1451
1464
 
1465
+ QUOTES_DEADLINE = 30 # dsh/dashboard.ts allows the whole process 40 s
1466
+ QUOTES_MAX = 40
1467
+
1468
+
1469
+ def _batch(symbols_arg: str, one, label: str) -> list | dict:
1470
+ """Run `one(symbol)` for several symbols in one process for the dsh dashboard; rows follow input order,
1471
+ a failure or a row still running at the deadline stays per row."""
1472
+ symbols = [symbol.strip() for symbol in symbols_arg.split(",") if symbol.strip()]
1473
+ if not 0 < len(symbols) <= QUOTES_MAX:
1474
+ return {"error": f"{label}s takes 1-{QUOTES_MAX} comma-separated symbols"}
1475
+ rows = [{"error": f"{label} timed out"}] * len(symbols)
1476
+ workers = threading.BoundedSemaphore(8)
1477
+
1478
+ def run(index, symbol):
1479
+ with workers:
1480
+ try:
1481
+ rows[index] = one(symbol)
1482
+ except Exception as e:
1483
+ rows[index] = {"error": error_message(e)}
1484
+
1485
+ # Daemon threads: a provider that hangs past the deadline cannot keep the process (and every other row) alive.
1486
+ threads = [threading.Thread(target=run, args=item, daemon=True) for item in enumerate(symbols)]
1487
+ for thread in threads:
1488
+ thread.start()
1489
+ deadline = time.monotonic() + QUOTES_DEADLINE
1490
+ for thread in threads:
1491
+ thread.join(max(0, deadline - time.monotonic()))
1492
+ return list(rows)
1493
+
1494
+
1495
+ def cmd_quotes(args):
1496
+ return _batch(args.symbols, lambda symbol: cmd_quote(argparse.Namespace(symbol=symbol)), "quote")
1497
+
1498
+
1499
+ def cmd_klines(args):
1500
+ return _batch(
1501
+ args.symbols,
1502
+ lambda symbol: cmd_kline(argparse.Namespace(symbol=symbol, period="daily", count=args.count)),
1503
+ "kline",
1504
+ )
1505
+
1506
+
1452
1507
  # --------------- open-end fund quote ---------------
1453
1508
 
1454
1509
 
@@ -1466,30 +1521,6 @@ def _fund_nav_date(stamp) -> str:
1466
1521
  return datetime.fromtimestamp(stamp / 1000, timezone.utc).astimezone(ZoneInfo("Asia/Shanghai")).date().isoformat()
1467
1522
 
1468
1523
 
1469
- def _fund_estimate(symbol: str) -> dict:
1470
- import akshare as ak
1471
-
1472
- with socket_timeout(25):
1473
- df = ak.fund_value_estimation_em()
1474
- if df is None or df.empty or "基金代码" not in df.columns:
1475
- return {}
1476
- rows = df[df["基金代码"] == symbol]
1477
- value_col = next((c for c in df.columns if c.endswith("-估算数据-估算值")), None)
1478
- change_col = next((c for c in df.columns if c.endswith("-估算数据-估算增长率")), None)
1479
- if rows.empty or not value_col:
1480
- return {}
1481
- row = rows.iloc[0]
1482
- estimated_nav = _parse_float(row.get(value_col))
1483
- if estimated_nav is None:
1484
- return {}
1485
- return {
1486
- "estimated_nav": estimated_nav,
1487
- "estimated_change_pct": _parse_float(str(row.get(change_col)).rstrip("%")) if change_col else None,
1488
- "estimated_at": value_col.split("-估算数据-", 1)[0],
1489
- "estimate_status": "available",
1490
- }
1491
-
1492
-
1493
1524
  def cmd_fund_quote(args):
1494
1525
  symbol = args.symbol
1495
1526
  if not re.fullmatch(r"\d{6}", symbol):
@@ -1511,17 +1542,6 @@ def cmd_fund_quote(args):
1511
1542
  nav_date = _fund_nav_date(latest["x"])
1512
1543
  accumulated_by_date = {_fund_nav_date(row[0]): row[1] for row in accumulated_rows}
1513
1544
  today = _cn_now().date().isoformat()
1514
- estimate = {
1515
- "estimated_nav": None,
1516
- "estimated_change_pct": None,
1517
- "estimated_at": None,
1518
- "estimate_status": "unavailable",
1519
- }
1520
- warnings = []
1521
- try:
1522
- estimate.update(_fund_estimate(symbol))
1523
- except Exception:
1524
- warnings.append("estimated NAV provider request failed; published NAV remains available")
1525
1545
  result = _clean_row(
1526
1546
  {
1527
1547
  "symbol": symbol,
@@ -1540,13 +1560,16 @@ def cmd_fund_quote(args):
1540
1560
  "fetched_at": datetime.now(timezone.utc).isoformat(),
1541
1561
  "note": (
1542
1562
  "nav is published end-of-day NAV, not a real-time price; QDII disclosure may lag "
1543
- "1-2 trading days; estimated_nav is separate and may be unavailable"
1563
+ "1-2 trading days; no intraday estimated NAV is provided"
1544
1564
  ),
1545
- **estimate,
1565
+ # Eastmoney retired its per-fund intraday estimate (fundgz); its remaining estimate
1566
+ # page lists every fund at once, so the estimate keys stay but are always unavailable.
1567
+ "estimated_nav": None,
1568
+ "estimated_change_pct": None,
1569
+ "estimated_at": None,
1570
+ "estimate_status": "unavailable",
1546
1571
  }
1547
1572
  )
1548
- if warnings:
1549
- result["warnings"] = warnings
1550
1573
  return result
1551
1574
  except Exception as e:
1552
1575
  return {"error": error_message(e)}
@@ -1758,18 +1781,31 @@ def _yf_info(symbol, ticker):
1758
1781
  return cached(f"yf-basic-info:{_yf_hk_symbol(symbol)}", DAY, fetch)
1759
1782
 
1760
1783
 
1761
- def _financials_a_frame(symbol):
1784
+ def _financials_a_frame(symbol, periods=1, annual=False):
1785
+ """Sina serves one page per report year, and akshare's default start_year=1900 walks every year
1786
+ since listing (~30 requests). Start at the current year and step back only until `periods` rows exist."""
1762
1787
  import akshare as ak
1788
+ import pandas as pd
1763
1789
 
1764
- df = _cached_frame(
1765
- f"financial-source:{symbol}", lambda: _akshare_retry(ak.stock_financial_analysis_indicator, symbol=symbol)
1766
- )
1790
+ year = _cn_now().year
1791
+ # This year's annual report never exists within this year; one step back covers reports not yet out.
1792
+ first = year - periods if annual else year - -(-periods // 4) + 1
1793
+ for start in (first, first - 1):
1794
+ df = _cached_frame(
1795
+ f"financial-source:{symbol}:{start}",
1796
+ lambda start=start: _akshare_retry(
1797
+ ak.stock_financial_analysis_indicator, symbol=symbol, start_year=str(start)
1798
+ ),
1799
+ )
1800
+ dates = df["日期"].astype(str) if "日期" in df.columns else pd.Series(dtype=str)
1801
+ if (dates.str.endswith("12-31").sum() if annual else len(dates)) >= periods:
1802
+ break
1767
1803
  return df.sort_values("日期", ascending=False) if "日期" in df.columns else df
1768
1804
 
1769
1805
 
1770
1806
  def financials_a(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1771
1807
  try:
1772
- df = _financials_a_frame(symbol)
1808
+ df = _financials_a_frame(symbol, periods, annual)
1773
1809
  if annual and not df.empty:
1774
1810
  df = df[df["日期"].astype(str).str.endswith("12-31")]
1775
1811
  if df is None or df.empty:
@@ -2101,193 +2137,129 @@ def _financials_without_dcf(args, periods, annual, market):
2101
2137
 
2102
2138
  # --------------- market_snapshot ---------------
2103
2139
 
2140
+ # The screener only scores stocks that can pass its filters, so every leg walks the market
2141
+ # largest-cap first and stops at market_cap_min; the row cap bounds a loose (or zero) floor.
2142
+ _SNAPSHOT_MAX_ROWS = 3000
2143
+
2144
+ # screener filter field → eastmoney xuangu (server-side A-share condition screener) column
2145
+ _XUANGU_FILTER_COLUMNS = {
2146
+ "market_cap": "TOTAL_MARKET_CAP",
2147
+ "pe": "PE9",
2148
+ "pb": "PBNEWMRQ",
2149
+ "turnover_rate": "TURNOVERRATE",
2150
+ "change_pct": "CHANGE_RATE",
2151
+ "volume_ratio": "VOLUME_RATIO",
2152
+ }
2153
+ _XUANGU_COLUMNS = {
2154
+ "SECURITY_CODE": "symbol",
2155
+ "SECURITY_NAME_ABBR": "name",
2156
+ "NEW_PRICE": "price",
2157
+ "CHANGE_RATE": "change_pct",
2158
+ "VOLUME": "volume",
2159
+ "DEAL_AMOUNT": "turnover",
2160
+ "PE9": "pe",
2161
+ "PBNEWMRQ": "pb",
2162
+ "TOTAL_MARKET_CAP": "market_cap",
2163
+ "TURNOVERRATE": "turnover_rate",
2164
+ "VOLUME_RATIO": "volume_ratio",
2165
+ }
2166
+ _XUANGU_URL = "https://data.eastmoney.com/dataapi/xuangu/list"
2167
+
2168
+
2169
+ def _xuangu_page(conditions: str, page: int, size: int) -> dict:
2170
+ params = {"st": "TOTAL_MARKET_CAP", "sr": -1, "ps": size, "p": page, "sty": ",".join(_XUANGU_COLUMNS)}
2171
+ body = _get_json(_XUANGU_URL, {**params, "filter": conditions, "source": "SELECT_SECURITIES", "client": "WEB"})
2172
+ if not isinstance(body.get("result"), dict):
2173
+ raise ValueError(f"eastmoney screener unavailable: {str(body.get('message'))[:100]}")
2174
+ return body["result"]
2175
+
2176
+
2177
+ def _snapshot_xuangu(filters: dict):
2178
+ """A-share candidates filtered server-side (PE9 is a TTM PE), largest market cap first."""
2179
+ conditions = ""
2180
+ for key, value in sorted(filters.items()):
2181
+ field, _, bound = key.rpartition("_")
2182
+ if field in _XUANGU_FILTER_COLUMNS and bound in ("min", "max"):
2183
+ conditions += f"({_XUANGU_FILTER_COLUMNS[field]}{'>=' if bound == 'min' else '<='}{value})"
2184
+ rows, page = [], 1
2185
+ while len(rows) < _SNAPSHOT_MAX_ROWS:
2186
+ result = _xuangu_page(conditions, page, 1000)
2187
+ for item in result.get("data") or []:
2188
+ rows.append(_clean_row({key: item.get(col) for col, key in _XUANGU_COLUMNS.items()} | {"pe_basis": "ttm"}))
2189
+ if not result.get("nextpage"):
2190
+ break
2191
+ page += 1
2192
+ total = None
2193
+ with contextlib.suppress(Exception):
2194
+ total = _xuangu_page("", 1, 1).get("count")
2195
+ return total, rows
2196
+
2197
+
2198
+ def _snapshot_tencent(filters: dict):
2199
+ """Non-eastmoney A-share leg (issue #25): Tencent's board rank sorted by market cap."""
2200
+ floor = filters.get("market_cap_min") or 0
2201
+ rows, total = [], None
2202
+ while len(rows) < _SNAPSHOT_MAX_ROWS:
2203
+ params = {"_appver": "11.17.0", "board_code": "aStock", "sort_type": "marketValue", "direct": "down"}
2204
+ body = _get_json(
2205
+ "https://proxy.finance.qq.com/cgi/cgi-bin/rank/hs/getBoardRankList",
2206
+ {**params, "offset": len(rows), "count": 200},
2207
+ )
2208
+ data = body.get("data") or {}
2209
+ total, ranked = data.get("total"), data.get("rank_list") or []
2210
+ for item in ranked:
2211
+ cap = _parse_float(item.get("zsz")) # 亿元
2212
+ if cap is None or cap * 1e8 < floor:
2213
+ return total, rows
2214
+ turnover = _parse_float(item.get("turnover")) # 万元
2215
+ row = {
2216
+ "symbol": str(item.get("code", ""))[2:],
2217
+ "name": item.get("name"),
2218
+ "price": _parse_float(item.get("zxj")),
2219
+ "change_pct": _parse_float(item.get("zdf")),
2220
+ "volume": _parse_float(item.get("volume")),
2221
+ "turnover": turnover * 1e4 if turnover is not None else None,
2222
+ "pe": _parse_float(item.get("pe_ttm")),
2223
+ "pe_basis": "ttm",
2224
+ "pb": _parse_float(item.get("pn")),
2225
+ "market_cap": cap * 1e8,
2226
+ "turnover_rate": _parse_float(item.get("hsl")),
2227
+ "volume_ratio": _parse_float(item.get("lb")),
2228
+ }
2229
+ rows.append(_clean_row(row))
2230
+ if len(ranked) < 200:
2231
+ break
2232
+ if not rows and not total:
2233
+ raise ValueError("tencent rank returned empty data")
2234
+ return total, rows
2235
+
2236
+
2237
+ # HK/US keep their historical column set: adding turnover_rate/volume_ratio (or US pb) would
2238
+ # silently start applying A-share-tuned default filters to these markets.
2239
+ _CLIST_SNAPSHOT_COLUMNS = {
2240
+ "HK": {"f12": "symbol", "f14": "name", "f2": "price", "f3": "change_pct", "f5": "volume", "f6": "turnover"}
2241
+ | {"f9": "pe", "f23": "pb", "f20": "market_cap"},
2242
+ "US": {"f12": "symbol", "f14": "name", "f2": "price", "f3": "change_pct", "f5": "volume", "f6": "turnover"}
2243
+ | {"f9": "pe", "f20": "market_cap"},
2244
+ }
2104
2245
 
2105
- def snapshot_a() -> list:
2106
- # akshare/efinance leave requests timeout-less — the chain-wide socket bound
2107
- # (socket_timeout) is what lets the tencent leg get its turn on a dead network.
2108
- try:
2109
- with socket_timeout(25):
2110
- return _failover(
2111
- [
2112
- ("akshare", _snapshot_akshare),
2113
- ("efinance", _snapshot_efinance),
2114
- ("sina", _snapshot_sina),
2115
- ("tencent", _snapshot_tencent),
2116
- ],
2117
- label="snapshot_a",
2118
- )
2119
- except Exception as e:
2120
- return [{"error": f"A-share snapshot unavailable from all sources: {error_message(e)}"}]
2121
-
2122
-
2123
- def _snapshot_efinance() -> list:
2124
- import efinance as ef
2125
-
2126
- df = ef.stock.get_realtime_quotes()
2127
- if df is None or df.empty:
2128
- raise ValueError("efinance snapshot empty")
2129
- col_map = {
2130
- "股票代码": "symbol",
2131
- "股票名称": "name",
2132
- "最新价": "price",
2133
- "涨跌幅": "change_pct",
2134
- "涨跌额": "change",
2135
- "成交量": "volume",
2136
- "成交额": "turnover",
2137
- "市盈率": "pe",
2138
- "换手率": "turnover_rate",
2139
- "最高": "high",
2140
- "最低": "low",
2141
- "今开": "open",
2142
- "昨收": "prev_close",
2143
- }
2144
- df = df.rename(columns=col_map)
2145
- df["pe_basis"] = "dynamic"
2146
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
2147
- return [_clean_row(r) for r in df[keep].to_dict("records")]
2148
-
2149
-
2150
- def _snapshot_sina() -> list:
2151
- import akshare as ak
2152
- import pandas as pd
2153
-
2154
- df = _akshare_retry(getattr(ak, "stock_zh_a_spot_sina", None) or ak.stock_zh_a_spot)
2155
- if df is None or df.empty:
2156
- raise ValueError("sina snapshot empty")
2157
- df["代码"] = df["代码"].astype(str).str.replace(r"^(?:sh|sz|bj)", "", regex=True)
2158
- df["成交量"] = pd.to_numeric(df["成交量"], errors="coerce") / 100
2159
- col_map = {
2160
- "代码": "symbol",
2161
- "名称": "name",
2162
- "最新价": "price",
2163
- "涨跌额": "change",
2164
- "涨跌幅": "change_pct",
2165
- "成交量": "volume",
2166
- "成交额": "turnover",
2167
- "昨收": "prev_close",
2168
- "今开": "open",
2169
- "最高": "high",
2170
- "最低": "low",
2171
- }
2172
- df = df.rename(columns=col_map)
2173
- keep = [c for c in col_map.values() if c in df.columns]
2174
- return [_clean_row(r) for r in df[keep].to_dict("records")]
2175
-
2176
-
2177
- # qt.gtimg.cn accepts comma-joined multi-code batches; 60 keeps URLs well short
2178
- _TENCENT_BATCH = 60
2179
-
2180
-
2181
- def _tencent_batch_quotes(codes: list) -> list:
2182
- """Batch quotes from qt.gtimg.cn for exchange-prefixed codes, a few requests in
2183
- flight (this leg fires only when the eastmoney/sina legs are all dead, and ~15k
2184
- enumerated codes serialized would take minutes). Unknown codes are simply absent
2185
- from the response; delisted/suspended ones come back frozen with volume=0 and
2186
- are dropped here."""
2187
- import concurrent.futures
2188
-
2189
- import requests
2190
-
2191
- def fetch(batch):
2192
- try:
2193
- resp = requests.get(f"https://qt.gtimg.cn/q={','.join(batch)}", timeout=10)
2194
- except Exception:
2195
- return [] # a lost batch loses 60 codes — a partial snapshot still beats none
2196
- rows = []
2197
- for part in resp.content.decode("gbk", errors="replace").split(";"):
2198
- vname, _, payload = part.strip().partition('="')
2199
- f = payload.rstrip('"').split("~")
2200
- if len(f) < 35 or not f[1]:
2201
- continue
2202
- if not _parse_float(f[6]): # volume 0/empty → frozen payload (delisted/suspended)
2203
- continue
2204
- prefixed = vname[2:] # strip the leading "v_"
2205
- bare = prefixed[2:] if prefixed[:2] in ("sh", "sz", "bj") else prefixed
2206
- rows.append(_clean_row(_tencent_payload_row(f, bare)))
2207
- return rows
2208
-
2209
- batches = [codes[i : i + _TENCENT_BATCH] for i in range(0, len(codes), _TENCENT_BATCH)]
2210
- rows = []
2211
- with concurrent.futures.ThreadPoolExecutor(max_workers=6) as pool:
2212
- for batch_rows in pool.map(fetch, batches):
2213
- rows.extend(batch_rows)
2214
- return rows
2215
-
2216
-
2217
- def _snapshot_tencent() -> list:
2218
- """Tencent full-market snapshot — the plain-HTTPS last-resort leg for when the
2219
- eastmoney/sina legs are all blocked (datacenter IPs, issue #25).
2220
-
2221
- Codes are enumerated over the known SH (600-605, 688-689) and SZ (000-003,
2222
- 300-302) ranges and validated by the batch quotes themselves — no list API is
2223
- reliable from blocked networks (www.szse.cn resets datacenter connections,
2224
- query.sse.com.cn flaps, akshare's wrapper has no timeout). BSE and suspended
2225
- stocks stay uncovered by this leg."""
2226
- codes = [f"sh{i:06d}" for lo, hi in ((600000, 605999), (688000, 689999)) for i in range(lo, hi + 1)]
2227
- codes += [f"sz{i:06d}" for lo, hi in ((1, 3999), (300000, 302999)) for i in range(lo, hi + 1)]
2228
- rows = _tencent_batch_quotes(codes)
2229
- if not rows:
2230
- raise ValueError("tencent snapshot returned empty data")
2231
- return rows
2232
-
2233
-
2234
- def _snapshot_akshare() -> list:
2235
- import akshare as ak
2236
-
2237
- df = _akshare_retry(ak.stock_zh_a_spot_em)
2238
- col_map = {
2239
- "代码": "symbol",
2240
- "名称": "name",
2241
- "最新价": "price",
2242
- "涨跌幅": "change_pct",
2243
- "涨跌额": "change",
2244
- "成交量": "volume",
2245
- "成交额": "turnover",
2246
- "市盈率-动态": "pe",
2247
- "市净率": "pb",
2248
- "总市值": "market_cap",
2249
- "换手率": "turnover_rate",
2250
- "量比": "volume_ratio",
2251
- "最高": "high",
2252
- "最低": "low",
2253
- "今开": "open",
2254
- "昨收": "prev_close",
2255
- "60日涨跌幅": "change_pct_60d",
2256
- }
2257
- df = df.rename(columns=col_map)
2258
- df["pe_basis"] = "dynamic"
2259
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
2260
- df = df[keep]
2261
- return [_clean_row(r) for r in df.to_dict("records")]
2262
-
2263
-
2264
- def snapshot_hk() -> list:
2265
- try:
2266
- import akshare as ak
2267
2246
 
2268
- with socket_timeout(25):
2269
- df = _akshare_retry(ak.stock_hk_spot_em)
2270
- col_map = {
2271
- "代码": "symbol",
2272
- "名称": "name",
2273
- "最新价": "price",
2274
- "涨跌幅": "change_pct",
2275
- "成交量": "volume",
2276
- "成交额": "turnover",
2277
- "市盈率": "pe",
2278
- "市净率": "pb",
2279
- "总市值": "market_cap",
2280
- }
2281
- df = df.rename(columns=col_map)
2282
- if "symbol" in df.columns:
2283
- symbols = df["symbol"].astype(str).str.upper()
2284
- df["symbol"] = symbols.map(lambda s: s if s.endswith(".HK") else f"{s}.HK")
2285
- df["pe_basis"] = "unknown"
2286
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
2287
- return [_clean_row(r) for r in df[keep].to_dict("records")]
2288
- except Exception as e:
2289
- # summary only (type + short message) — never a stack trace or response body
2290
- return [{"error": f"HK snapshot unavailable: {type(e).__name__}: {scrub_secrets(str(e))}"[:200]}]
2247
+ def _snapshot_clist(market: str, floor: float):
2248
+ columns = _CLIST_SNAPSHOT_COLUMNS[market]
2249
+ rows, total, page = [], 0, 1
2250
+ while len(rows) < _SNAPSHOT_MAX_ROWS:
2251
+ total, ranked = _em_clist(market, "f20", ",".join(columns), page=page)
2252
+ for item in ranked:
2253
+ cap = _parse_float(item.get("f20"))
2254
+ if cap is None or cap < floor:
2255
+ return total, rows
2256
+ row = {key: _parse_float(item.get(col)) for col, key in columns.items()}
2257
+ row.update(symbol=_clist_symbol(market, item.get("f12")), name=item.get("f14"))
2258
+ rows.append(_clean_row(row | {"pe_basis": "unknown"}))
2259
+ if len(ranked) < _EM_PAGE:
2260
+ break
2261
+ page += 1
2262
+ return total, rows
2291
2263
 
2292
2264
 
2293
2265
  def normalize_us_symbol(value) -> str:
@@ -2297,44 +2269,28 @@ def normalize_us_symbol(value) -> str:
2297
2269
  return re.sub(r"[._]", "-", symbol) # Eastmoney writes class shares as BRK_B; yfinance wants BRK-B
2298
2270
 
2299
2271
 
2300
- def snapshot_us() -> list:
2301
- try:
2302
- import akshare as ak
2303
-
2304
- with socket_timeout(25):
2305
- df = _akshare_retry(ak.stock_us_spot_em)
2306
- col_map = {
2307
- "代码": "symbol",
2308
- "名称": "name",
2309
- "最新价": "price",
2310
- "涨跌幅": "change_pct",
2311
- "成交量": "volume",
2312
- "成交额": "turnover",
2313
- "市盈率": "pe",
2314
- "总市值": "market_cap",
2315
- }
2316
- df = df.rename(columns=col_map)
2317
- if "symbol" in df.columns:
2318
- df["symbol"] = df["symbol"].map(normalize_us_symbol)
2319
- df["pe_basis"] = "unknown"
2320
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
2321
- return [_clean_row(r) for r in df[keep].to_dict("records")]
2322
- except Exception as e:
2323
- return [{"error": f"US snapshot unavailable: {type(e).__name__}: {scrub_secrets(str(e))}"[:200]}]
2324
-
2325
-
2326
2272
  def cmd_market_snapshot(args):
2273
+ """Screener input: the stocks at or above market_cap_min (largest first), never the whole market."""
2274
+ market = args.market.upper()
2327
2275
  try:
2328
- m = args.market.upper()
2329
- if m == "A":
2330
- return snapshot_a()
2331
- elif m == "HK":
2332
- return snapshot_hk()
2333
- elif m == "US":
2334
- return snapshot_us()
2335
- return {"error": f"Unknown market: {m}"}
2276
+ filters = json.loads(getattr(args, "filters", None) or "{}")
2277
+ if not isinstance(filters, dict):
2278
+ return {"error": "filters must be a JSON object"}
2279
+ floor = filters.get("market_cap_min") or 0
2280
+ if market == "A":
2281
+ legs = [("eastmoney", lambda: _snapshot_xuangu(filters)), ("tencent", lambda: _snapshot_tencent(filters))]
2282
+ elif market in _CLIST_SNAPSHOT_COLUMNS:
2283
+ legs = [("eastmoney", lambda: _snapshot_clist(market, floor))]
2284
+ else:
2285
+ return {"error": f"Unknown market: {market}"}
2286
+ with socket_timeout(25):
2287
+ total, rows = _failover(legs, label=f"snapshot_{market.lower()}")
2288
+ result = {"market": market, "total_stocks": total, "rows": rows[:_SNAPSHOT_MAX_ROWS]}
2289
+ if len(rows) >= _SNAPSHOT_MAX_ROWS:
2290
+ result["truncated"] = True
2291
+ return result
2336
2292
  except Exception as e:
2337
- return {"error": error_message(e)}
2293
+ return {"error": f"{market} snapshot unavailable: {error_message(e)}"}
2338
2294
 
2339
2295
 
2340
2296
  # --------------- market_indices ---------------
@@ -2374,53 +2330,70 @@ def _quote_tencent_hk_index(symbol: str) -> dict:
2374
2330
  }
2375
2331
 
2376
2332
 
2333
+ _CN_INDICES = [
2334
+ ("sh000001", "上证指数"),
2335
+ ("sz399001", "深证成指"),
2336
+ ("sz399006", "创业板指"),
2337
+ ("sh000688", "科创50"),
2338
+ ("sh000300", "沪深300"),
2339
+ ("sh000016", "上证50"),
2340
+ ]
2341
+
2342
+
2343
+ _EM_INDEX_FIELDS = (
2344
+ ("price", "f2"),
2345
+ ("change", "f4"),
2346
+ ("change_pct", "f3"),
2347
+ ("volume", "f5"),
2348
+ ("turnover", "f6"),
2349
+ ("high", "f15"),
2350
+ ("low", "f16"),
2351
+ ("open", "f17"),
2352
+ ("prev_close", "f18"),
2353
+ )
2354
+
2355
+
2356
+ def _cn_indices_eastmoney() -> dict:
2357
+ secids = {f"{1 if code[:2] == 'sh' else 0}.{code[2:]}": code for code, _ in _CN_INDICES}
2358
+ quotes = {}
2359
+ for row in _em_ulist(list(secids), "f2,f3,f4,f5,f6,f12,f13,f14,f15,f16,f17,f18"):
2360
+ code = secids.get(f"{row.get('f13')}.{row.get('f12')}")
2361
+ if code and _parse_float(row.get("f2")) is not None:
2362
+ quotes[code] = {"name": row.get("f14")} | {key: _parse_float(row.get(col)) for key, col in _EM_INDEX_FIELDS}
2363
+ return quotes
2364
+
2365
+
2366
+ def _cn_indices_tencent() -> dict:
2367
+ """One qt.gtimg.cn batch request for every CN index (eastmoney refuses some datacenter IPs)."""
2368
+ import requests
2369
+
2370
+ resp = requests.get(f"https://qt.gtimg.cn/q={','.join(code for code, _ in _CN_INDICES)}", timeout=10)
2371
+ resp.raise_for_status()
2372
+ quotes = {}
2373
+ for part in resp.content.decode("gbk", errors="replace").split(";"):
2374
+ name, _, payload = part.strip().partition('="')
2375
+ fields = payload.rstrip('"').split("~")
2376
+ if len(fields) > 37 and _parse_float(fields[3]):
2377
+ row = _tencent_payload_row(fields, name.removeprefix("v_"))
2378
+ keys = ("name", "price", "change", "change_pct", "volume", "turnover", "high", "low", "open", "prev_close")
2379
+ quotes[name.removeprefix("v_")] = {key: row[key] for key in keys}
2380
+ return quotes
2381
+
2382
+
2377
2383
  def cmd_market_indices(args):
2378
2384
  region = args.region.lower()
2379
2385
  try:
2380
2386
  if region == "cn":
2381
- import akshare as ak
2382
-
2383
- indices = [
2384
- ("sh000001", "上证指数"),
2385
- ("sz399001", "深证成指"),
2386
- ("sz399006", "创业板指"),
2387
- ("sh000688", "科创50"),
2388
- ("sh000300", "沪深300"),
2389
- ("sh000016", "上证50"),
2390
- ]
2391
- results = []
2392
- try:
2393
- df = _akshare_retry(getattr(ak, "index_zh_spot_em", None) or ak.stock_zh_index_spot_em)
2394
- except Exception:
2395
- # eastmoney refuses datacenter/proxy IPs (RemoteDisconnected) — sina is the fallback
2396
- df = _akshare_retry(getattr(ak, "index_zh_spot_sina", None) or ak.stock_zh_index_spot_sina)
2397
- for code, name in indices:
2398
- clean_code = code[2:] if code[:2] in ("sh", "sz") else code
2399
- row = df[df["代码"] == clean_code]
2400
- if row.empty:
2401
- row = df[df["代码"] == code]
2402
- if row.empty:
2403
- results.append({"code": code, "name": name, "note": "data unavailable"})
2404
- continue
2405
- r = row.iloc[0]
2406
- results.append(
2407
- _clean_row(
2408
- {
2409
- "code": code,
2410
- "name": r.get("名称", name),
2411
- "price": r.get("最新价"),
2412
- "change": r.get("涨跌额"),
2413
- "change_pct": r.get("涨跌幅"),
2414
- "volume": r.get("成交量"),
2415
- "turnover": r.get("成交额"),
2416
- "high": r.get("最高"),
2417
- "low": r.get("最低"),
2418
- "open": r.get("今开"),
2419
- "prev_close": r.get("昨收"),
2420
- }
2421
- )
2387
+ with socket_timeout(25):
2388
+ quotes = _failover(
2389
+ [("eastmoney", _cn_indices_eastmoney), ("tencent", _cn_indices_tencent)], label="market_indices_cn"
2422
2390
  )
2423
- return results
2391
+ return [
2392
+ _clean_row({"code": code, **quotes[code]})
2393
+ if code in quotes
2394
+ else {"code": code, "name": name, "note": "data unavailable"}
2395
+ for code, name in _CN_INDICES
2396
+ ]
2424
2397
  elif region in _REGION_INDEX_SYMBOLS:
2425
2398
  from concurrent.futures import ThreadPoolExecutor
2426
2399
 
@@ -2729,17 +2702,20 @@ def _constituents_em(sector: str, board_kind: str) -> dict:
2729
2702
  """Eastmoney industry/concept board constituents via akshare, fuzzy-resolved."""
2730
2703
  import akshare as ak
2731
2704
 
2732
- if board_kind == "industry":
2733
- name_fn, cons_fn = ak.stock_board_industry_name_em, ak.stock_board_industry_cons_em
2734
- else:
2735
- name_fn, cons_fn = ak.stock_board_concept_name_em, ak.stock_board_concept_cons_em
2736
- names_df = _akshare_retry(name_fn)
2737
- if names_df is None or names_df.empty or "板块名称" not in names_df.columns:
2738
- raise ValueError(f"eastmoney {board_kind} board list unavailable")
2739
- exact = _fuzzy_match_sector(sector, [str(n) for n in names_df["板块名称"].tolist()])
2705
+ cons_fn = ak.stock_board_industry_cons_em if board_kind == "industry" else ak.stock_board_concept_cons_em
2706
+ kind = "行业" if board_kind == "industry" else "概念"
2707
+ # Search the board by name (one request) instead of paging the whole board list (~5s per kind).
2708
+ keyword = re.sub(r"(板块|行业|概念)$", "", re.sub(r"\s+", "", sector)) or sector
2709
+ boards = {
2710
+ str(r.get("shortName")): str(r.get("code"))
2711
+ for r in _akshare_retry(_em_codetable, keyword, 50)
2712
+ if r.get("market") == 90 and r.get("securityTypeName") == kind and str(r.get("code", "")).startswith("BK")
2713
+ }
2714
+ exact = _fuzzy_match_sector(sector, list(boards))
2740
2715
  if not exact:
2741
2716
  raise ValueError(f"no eastmoney {board_kind} board matching '{sector}'")
2742
- df = _akshare_retry(cons_fn, symbol=exact)
2717
+ # A BK code also skips akshare's own name→code lookup, which pages the whole board list again.
2718
+ df = _akshare_retry(cons_fn, symbol=boards[exact])
2743
2719
  if df is None or df.empty:
2744
2720
  raise ValueError(f"eastmoney {board_kind} board '{exact}' returned no constituents")
2745
2721
  constituents = _constituent_rows(df)
@@ -2978,8 +2954,7 @@ def _resolve_leg_down(name: str) -> bool:
2978
2954
 
2979
2955
 
2980
2956
  def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
2981
- if info_map is not None:
2982
- return _resolve_stock_sectors(symbol, info_map)
2957
+ # info_map only saves the em leg's fetch on a miss; the result is the same, so share the cache.
2983
2958
  return cached(f"stock-sectors:{symbol}", WEEK, lambda: _resolve_stock_sectors(symbol, info_map))
2984
2959
 
2985
2960
 
@@ -3206,93 +3181,132 @@ def cmd_chip_distribution(args):
3206
3181
  # --------------- market_stats ---------------
3207
3182
 
3208
3183
 
3209
- def compute_market_stats(data: list) -> dict:
3210
- """Compute breadth/limit/turnover statistics from a market snapshot list."""
3211
- import numpy as np
3184
+ def _clist_symbol(market: str, code) -> str:
3185
+ code = str(code)
3186
+ return f"{code.zfill(5)}.HK" if market == "HK" else normalize_us_symbol(code) if market == "US" else code
3212
3187
 
3213
- changes = []
3214
- turnovers = []
3215
- limit_ups = []
3216
- limit_downs = []
3217
- up_count = down_count = flat_count = 0
3218
-
3219
- for s in data:
3220
- chg = s.get("change_pct")
3221
- if chg is not None:
3222
- changes.append(float(chg))
3223
- if chg > 0:
3224
- up_count += 1
3225
- elif chg < 0:
3226
- down_count += 1
3227
- else:
3228
- flat_count += 1
3229
- t = s.get("turnover")
3230
- if t is not None:
3231
- turnovers.append(float(t))
3232
-
3233
- price = s.get("price")
3234
- prev = s.get("prev_close")
3235
- sym = s.get("symbol", "")
3236
- if price and prev and prev > 0:
3237
- code_info = normalize_stock_code(sym)
3238
- lp = code_info.get("limit_pct")
3239
- if lp:
3240
- name = s.get("name", "")
3241
- if name and "ST" in name.upper() and code_info["board"] == "main":
3242
- lp = 0.05
3243
- limit_up = calc_limit_price(float(prev), lp, "up")
3244
- limit_down = calc_limit_price(float(prev), lp, "down")
3245
- if abs(float(price) - limit_up) < 0.01:
3246
- limit_ups.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
3247
- elif abs(float(price) - limit_down) < 0.01:
3248
- limit_downs.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
3249
-
3250
- changes_arr = np.array(changes) if changes else np.array([0])
3251
- sorted_data = sorted(data, key=lambda x: x.get("change_pct") or 0, reverse=True)
3252
3188
 
3253
- return _clean_row(
3189
+ def _clist_change_at(market: str, rank: int, desc: bool = True):
3190
+ _, rows = _em_clist(market, "f3", "f3", page=rank, size=1, desc=desc)
3191
+ return _parse_float(rows[0].get("f3")) if rows else None
3192
+
3193
+
3194
+ def _leading_count(market: str, total: int, desc: bool) -> int:
3195
+ """Ranks with a strictly positive (desc) / negative (asc) change, by binary search over single-row pages."""
3196
+ lo, hi = 0, total
3197
+ while lo < hi:
3198
+ mid = (lo + hi + 1) // 2
3199
+ change = _clist_change_at(market, mid, desc) or 0
3200
+ if change > 0 if desc else change < 0:
3201
+ lo = mid
3202
+ else:
3203
+ hi = mid - 1
3204
+ return lo
3205
+
3206
+
3207
+ def _movers(market: str, desc: bool) -> tuple:
3208
+ total, rows = _em_clist(market, "f3", "f12,f14,f3", size=5, desc=desc)
3209
+ movers = [
3210
+ {"symbol": _clist_symbol(market, r.get("f12")), "name": r.get("f14"), "change_pct": _parse_float(r.get("f3"))}
3211
+ for r in rows
3212
+ ]
3213
+ return total, movers if desc else movers[::-1] # losers keep the old order: least negative first
3214
+
3215
+
3216
+ def _median_change(market: str, total: int, up: int, flat: int, counted: int):
3217
+ """Median of the counted stocks; unchanged and unquoted rows rank between gainers and losers."""
3218
+ if counted <= 0:
3219
+ return None
3220
+ k = (counted + 1) // 2
3221
+ if up < k <= up + flat:
3222
+ return 0.0
3223
+ return _clist_change_at(market, k if k <= up else k + total - counted)
3224
+
3225
+
3226
+ _EM_LIMIT_POOL = {"ut": "7eea3edcaed734bea9cbfc24409ed989", "dpt": "wz.ztzt"}
3227
+
3228
+
3229
+ def _limit_samples(endpoint: str, sort: str, trade_date: str) -> list:
3230
+ params = {**_EM_LIMIT_POOL, "Pageindex": 0, "pagesize": 5, "sort": sort, "date": trade_date}
3231
+ pool = (_get_json(f"https://push2ex.eastmoney.com/{endpoint}", params).get("data") or {}).get("pool") or []
3232
+ return [
3254
3233
  {
3255
- "total_stocks": len(data),
3256
- "up_count": up_count,
3257
- "down_count": down_count,
3258
- "flat_count": flat_count,
3259
- "limit_up_count": len(limit_ups),
3260
- "limit_down_count": len(limit_downs),
3261
- "avg_change_pct": round(float(changes_arr.mean()), 2),
3262
- "median_change_pct": round(float(np.median(changes_arr)), 2),
3263
- "total_turnover": round(sum(turnovers), 0) if turnovers else None,
3264
- "top5_gainers": [
3265
- {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
3266
- for s in sorted_data[:5]
3267
- ],
3268
- "top5_losers": [
3269
- {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
3270
- for s in sorted_data[-5:]
3271
- ],
3272
- "limit_up_samples": limit_ups[:5],
3273
- "limit_down_samples": limit_downs[:5],
3234
+ "symbol": p.get("c"),
3235
+ "name": p.get("n"),
3236
+ "change_pct": _sanitize(round(p["zdp"], 2) if isinstance(p.get("zdp"), (int, float)) else None),
3274
3237
  }
3275
- )
3238
+ for p in pool
3239
+ ]
3240
+
3241
+
3242
+ def _market_stats_a() -> dict:
3243
+ """A-share breadth from eastmoney's 涨跌分布 (buckets ±11 are limit moves) — no per-stock rows."""
3244
+ body = _get_json("https://push2ex.eastmoney.com/getTopicZDFenBu", _EM_LIMIT_POOL).get("data") or {}
3245
+ buckets = {int(k): int(v) for item in body.get("fenbu") or [] for k, v in item.items()}
3246
+ if not buckets:
3247
+ raise ValueError("eastmoney breadth distribution unavailable")
3248
+ up = sum(v for k, v in buckets.items() if k > 0)
3249
+ down = sum(v for k, v in buckets.items() if k < 0)
3250
+ flat = buckets.get(0, 0)
3251
+ trade_date = str(body.get("qdate") or "")
3252
+ index_rows = _em_ulist(["1.000001", "0.399001", "0.899050"], "f6") # 沪 + 深 + 北 market turnover
3253
+ turnovers = [_parse_float(r.get("f6")) for r in index_rows]
3254
+ total, gainers = _movers("A", True)
3255
+ _, losers = _movers("A", False)
3256
+ return {
3257
+ "total_stocks": up + down + flat,
3258
+ "up_count": up,
3259
+ "down_count": down,
3260
+ "flat_count": flat,
3261
+ "limit_up_count": buckets.get(11, 0),
3262
+ "limit_down_count": buckets.get(-11, 0),
3263
+ "median_change_pct": _median_change("A", total, up, flat, up + down + flat),
3264
+ "total_turnover": round(sum(turnovers), 0) if turnovers and None not in turnovers else None,
3265
+ "top5_gainers": gainers,
3266
+ "top5_losers": losers,
3267
+ "limit_up_samples": _limit_samples("getTopicZTPool", "fbt:asc", trade_date),
3268
+ "limit_down_samples": _limit_samples("getTopicDTPool", "fund:asc", trade_date),
3269
+ }
3270
+
3271
+
3272
+ def _market_stats_clist(market: str) -> dict:
3273
+ """HK/US breadth: the up/down boundaries of the change-ranked list, found by binary search."""
3274
+ from concurrent.futures import ThreadPoolExecutor
3275
+
3276
+ total, gainers = _movers(market, True)
3277
+ if not total:
3278
+ raise ValueError(f"eastmoney {market} list unavailable")
3279
+ # The two binary searches (~15 sequential requests each) and the loser list are independent.
3280
+ with ThreadPoolExecutor(max_workers=3) as pool:
3281
+ losers_f = pool.submit(_movers, market, False)
3282
+ up_f, down_f = (pool.submit(_leading_count, market, total, rising) for rising in (True, False))
3283
+ _, losers = losers_f.result()
3284
+ up, down = up_f.result(), down_f.result()
3285
+ flat = total - up - down
3286
+ return {
3287
+ "total_stocks": total,
3288
+ "up_count": up,
3289
+ "down_count": down,
3290
+ "flat_count": flat,
3291
+ "median_change_pct": _median_change(market, total, up, flat, total),
3292
+ "top5_gainers": gainers,
3293
+ "top5_losers": losers,
3294
+ # No daily price limits; a turnover total would need every row.
3295
+ **dict.fromkeys(("limit_up_count", "limit_down_count", "limit_up_samples", "limit_down_samples")),
3296
+ "total_turnover": None,
3297
+ "market": market,
3298
+ "source": "eastmoney_rank",
3299
+ "turnover_currency": "HKD" if market == "HK" else "USD",
3300
+ }
3276
3301
 
3277
3302
 
3278
3303
  def cmd_market_stats(args):
3279
3304
  market = getattr(args, "market", "A")
3280
3305
  try:
3281
- data = {"A": snapshot_a, "HK": snapshot_hk, "US": snapshot_us}[market]()
3282
- if isinstance(data, dict) and "error" in data:
3283
- return data
3284
- if isinstance(data, list) and len(data) == 1 and isinstance(data[0], dict) and "error" in data[0]:
3285
- return data[0]
3286
- if not data:
3287
- return {"error": "No market data"}
3288
- stats = compute_market_stats(data)
3289
- if market != "A":
3290
- # HK/US have no daily price limits; breadth covers the provider's listed-security universe.
3291
- stats.update(
3292
- dict.fromkeys(("limit_up_count", "limit_down_count", "limit_up_samples", "limit_down_samples"))
3293
- )
3294
- stats.update(market=market, source="eastmoney_spot", turnover_currency="HKD" if market == "HK" else "USD")
3295
- return stats
3306
+ with socket_timeout(25):
3307
+ stats = _market_stats_a() if market == "A" else _market_stats_clist(market)
3308
+ # The mean needs every stock's change; the key stays for the JSON contract.
3309
+ return _clean_row({"avg_change_pct": None, **stats})
3296
3310
  except Exception as e:
3297
3311
  return {"error": error_message(e)}
3298
3312
 
@@ -3636,122 +3650,43 @@ def cmd_hot_stocks(args):
3636
3650
 
3637
3651
  # --------------- margin_trading / northbound_flow (A-share only) ---------------
3638
3652
 
3639
- # Exchange margin-detail column layouts differ: SSE carries 融资偿还额/融券偿还量,
3640
- # SZSE carries 融券余额/融资融券余额 — the output keys below follow the source.
3641
- _MARGIN_DETAIL_SSE_COLS = {
3642
- "标的证券代码": "symbol",
3643
- "标的证券简称": "name",
3644
- "融资余额": "margin_balance",
3645
- "融资买入额": "margin_buy",
3646
- "融资偿还额": "margin_repay",
3647
- "融券余量": "short_balance_shares",
3648
- "融券卖出量": "short_sell_shares",
3649
- "融券偿还量": "short_repay_shares",
3650
- }
3651
-
3652
- _MARGIN_DETAIL_SZSE_COLS = {
3653
- "证券代码": "symbol",
3654
- "证券简称": "name",
3655
- "融资买入额": "margin_buy",
3656
- "融资余额": "margin_balance",
3657
- "融券卖出量": "short_sell_shares",
3658
- "融券余量": "short_balance_shares",
3659
- "融券余额": "short_balance",
3660
- "融资融券余额": "total_balance",
3653
+ # Eastmoney per-stock margin detail (RPTA_WEB_RZRQ_GGMX) → output keys
3654
+ _MARGIN_COLUMNS = {
3655
+ "SCODE": "symbol",
3656
+ "SECNAME": "name",
3657
+ "RZYE": "margin_balance",
3658
+ "RZMRE": "margin_buy",
3659
+ "RZCHE": "margin_repay",
3660
+ "RQYL": "short_balance_shares",
3661
+ "RQMCL": "short_sell_shares",
3662
+ "RQCHL": "short_repay_shares",
3663
+ "RQYE": "short_balance",
3664
+ "RZRQYE": "total_balance",
3661
3665
  }
3662
3666
 
3663
3667
 
3664
3668
  def cmd_margin_trading(args):
3665
- """Per-stock margin trading detail (融资融券) for the last N trading days.
3666
-
3667
- akshare has no per-stock margin history endpoint — the exchange detail lists
3668
- (stock_margin_detail_sse / stock_margin_detail_szse) are per-day, all-stock
3669
- tables, so each trading day is fetched and filtered to the symbol. Daily fetches
3670
- are independent and run concurrently (5 workers; a serial 60-day run would
3671
- overrun the host's 120s subprocess budget), then re-sorted newest-first — the
3672
- output never depends on completion order. A day that fails or has no data is
3673
- skipped (两融明细 T 日晚间才发布 — a morning run always misses the latest day);
3674
- only when every day failed is an error returned. Days where the symbol is
3675
- absent (non-margin underlying that day) are skipped; if no day yields a row the
3676
- symbol is reported as not covered."""
3669
+ """Per-stock margin trading detail (融资融券) for the last N trading days, newest first.
3670
+
3671
+ One server-filtered eastmoney query returns exactly this stock's rows (两融明细 T 日晚间才发布 —
3672
+ a morning run ends at the previous day)."""
3677
3673
  if detect_market(args.symbol) != "A":
3678
3674
  return {"error": "margin_trading only available for A-shares"}
3679
3675
  days = getattr(args, "days", 10)
3680
3676
  if days < 1:
3681
3677
  return {"error": f"days must be a positive integer, got {days}"}
3682
- # One akshare call per day — cap the fan-out so a slow source can't overrun
3683
- # the host's subprocess budget (rows carry their dates; truncation is evident).
3684
3678
  days = min(days, 60)
3685
- code = _cn_code(args.symbol)
3686
- exchange, bare = code[:2], code[2:]
3679
+ bare = _cn_code(args.symbol)[2:]
3687
3680
  try:
3688
- import concurrent.futures
3689
-
3690
- from trading_calendar import prev_trading_days
3691
-
3692
- if exchange == "sh":
3693
- col_map = _MARGIN_DETAIL_SSE_COLS
3694
- elif exchange == "sz":
3695
- col_map = _MARGIN_DETAIL_SZSE_COLS
3696
- else:
3697
- return {"error": f"margin_trading: {args.symbol} is a BSE code; only SSE/SZSE margin detail is available"}
3698
- trade_days = prev_trading_days("CN", days)
3699
- if not trade_days:
3700
- return {"error": "margin_trading: trading calendar unavailable"}
3701
-
3702
- def fetch_day(date_iso):
3703
- """Cache the published exchange-day table, then select this stock."""
3704
-
3705
- def fetch_table():
3706
- import akshare as ak
3707
-
3708
- detail_fn = ak.stock_margin_detail_sse if exchange == "sh" else ak.stock_margin_detail_szse
3709
- df = _akshare_retry(detail_fn, date=date_iso.replace("-", ""))
3710
- if df is None or df.empty:
3711
- return []
3712
- df = df.rename(columns=col_map)
3713
- required = {"symbol", "margin_buy", "margin_balance"}
3714
- if not required.issubset(df.columns):
3715
- raise ValueError("margin detail table missing financial columns")
3716
- df = df[df["symbol"].astype(str).str.fullmatch(r"\d{6}")]
3717
- if "信用交易日期" in df.columns:
3718
- source_days = df["信用交易日期"].map(lambda v: re.sub(r"\D", "", str(v))[:8])
3719
- if not (source_days == date_iso.replace("-", "")).all():
3720
- raise ValueError("margin detail table date does not match requested day")
3721
- keep = [c for c in col_map.values() if c in df.columns]
3722
- table = [{"date": date_iso, **_clean_row(r)} for r in df[keep].to_dict("records")]
3723
- for row in table:
3724
- values = [_parse_float(row.get(field)) for field in ("margin_buy", "margin_balance")]
3725
- if any(value is None or not math.isfinite(value) for value in values):
3726
- # Keep usable stocks without persisting an incomplete exchange snapshot.
3727
- return {"rows": table, "warnings": ["incomplete financial values"]}
3728
- return table
3729
-
3730
- try:
3731
- today = _cn_now().date().isoformat()
3732
- table = (
3733
- cached(f"margin-table:{exchange}:{date_iso}", DAY, fetch_table)
3734
- if date_iso < today
3735
- else fetch_table()
3736
- )
3737
- if isinstance(table, dict):
3738
- table = table["rows"]
3739
- return date_iso, [r for r in table if str(r.get("symbol")) == bare], None
3740
- except Exception as e:
3741
- return date_iso, [], f"{date_iso}: {type(e).__name__}: {scrub_secrets(str(e))}"
3742
-
3743
- rows = []
3744
- failures = []
3745
- with socket_timeout(25), concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool:
3746
- for _date_iso, day_rows, failure in pool.map(fetch_day, trade_days):
3747
- if failure:
3748
- failures.append(failure)
3749
- rows.extend(day_rows)
3750
- rows.sort(key=lambda r: r["date"], reverse=True)
3681
+ with socket_timeout(25):
3682
+ _, records = _em_datacenter("RPTA_WEB_RZRQ_GGMX", f'(SCODE="{bare}")', "DATE", days)
3683
+ rows = [
3684
+ _clean_row(
3685
+ {"date": str(r.get("DATE", ""))[:10]} | {key: r.get(col) for col, key in _MARGIN_COLUMNS.items()}
3686
+ )
3687
+ for r in records
3688
+ ]
3751
3689
  if not rows:
3752
- if failures:
3753
- msg = f"margin detail fetch failed for all {len(trade_days)} day(s): {'; '.join(failures)}"
3754
- return {"symbol": args.symbol, "error": msg}
3755
3690
  return {"symbol": args.symbol, "error": f"no margin trading data for {args.symbol} (可能不是两融标的)"}
3756
3691
  if getattr(args, "include_ratio", False):
3757
3692
  try:
@@ -3860,6 +3795,13 @@ def main():
3860
3795
  p_quote = sub.add_parser("quote")
3861
3796
  p_quote.add_argument("symbol")
3862
3797
 
3798
+ p_quotes = sub.add_parser("quotes")
3799
+ p_quotes.add_argument("symbols", help=f"comma-separated, at most {QUOTES_MAX}")
3800
+
3801
+ p_klines = sub.add_parser("klines")
3802
+ p_klines.add_argument("symbols", help=f"comma-separated, at most {QUOTES_MAX}; daily bars")
3803
+ p_klines.add_argument("--count", type=int, default=30)
3804
+
3863
3805
  p_fund_quote = sub.add_parser("fund_quote")
3864
3806
  p_fund_quote.add_argument("symbol")
3865
3807
 
@@ -3879,6 +3821,7 @@ def main():
3879
3821
 
3880
3822
  p_snap = sub.add_parser("market_snapshot")
3881
3823
  p_snap.add_argument("--market", default="A", choices=["A", "HK", "US"])
3824
+ p_snap.add_argument("--filters", default="{}", help="JSON screener filters; market_cap_min bounds the scan")
3882
3825
 
3883
3826
  p_idx = sub.add_parser("market_indices")
3884
3827
  p_idx.add_argument("--region", default="cn", choices=["cn", "hk", "us", "jp", "kr", "tw"])
@@ -3935,6 +3878,8 @@ def main():
3935
3878
  dispatch = {
3936
3879
  "kline": cmd_kline,
3937
3880
  "quote": cmd_quote,
3881
+ "quotes": cmd_quotes,
3882
+ "klines": cmd_klines,
3938
3883
  "fund_quote": cmd_fund_quote,
3939
3884
  "capital_flow": cmd_capital_flow,
3940
3885
  "news": cmd_news,
@@ -3961,6 +3906,10 @@ def main():
3961
3906
  output(result)
3962
3907
  if isinstance(result, dict) and "error" in result:
3963
3908
  sys.exit(1)
3909
+ if args.command in ("quotes", "klines"):
3910
+ # Timed-out daemon threads may hold stderr; normal shutdown would then abort and drop valid rows.
3911
+ sys.stdout.flush()
3912
+ os._exit(0)
3964
3913
 
3965
3914
 
3966
3915
  if __name__ == "__main__":