dsh-stock-analysis 0.2.12 → 0.2.14

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
@@ -390,12 +362,16 @@ def _tencent_us_code(symbol: str) -> str:
390
362
  return "us" + symbol.upper().replace("-", ".")
391
363
 
392
364
 
393
- def _tencent_fields(code: str) -> list:
365
+ def _tencent_raw(codes: str) -> bytes:
394
366
  import requests
395
367
 
396
- resp = requests.get(f"https://qt.gtimg.cn/q={code}", timeout=10)
368
+ resp = requests.get(f"https://qt.gtimg.cn/q={codes}", timeout=10)
397
369
  resp.raise_for_status()
398
- return _gbk_var_payload(resp.content).split("~")
370
+ return resp.content
371
+
372
+
373
+ def _tencent_fields(code: str) -> list:
374
+ return _gbk_var_payload(_tencent_raw(code)).split("~")
399
375
 
400
376
 
401
377
  def _quote_tencent(symbol: str) -> dict:
@@ -419,7 +395,7 @@ def _quote_tencent(symbol: str) -> dict:
419
395
  raise ValueError(f"tencent returned no quote for {symbol}")
420
396
  if normalize_stock_code(symbol)["is_etf"]:
421
397
  # f[73] is total fund shares and f[78] the intraday IOPV (blank for money/bond ETFs);
422
- # premium matches _quote_akshare_etf.
398
+ # premium matches _quote_eastmoney.
423
399
  iopv = _parse_float(f[78]) if len(f) > 78 else None
424
400
  price = row.get("price")
425
401
  row.update(
@@ -654,17 +630,32 @@ def _kline_akshare(symbol: str, period: str, count: int) -> list:
654
630
  return [_clean_row(r) for r in df.to_dict("records")]
655
631
 
656
632
 
657
- def _kline_tushare(symbol: str, period: str, count: int) -> list:
658
- """Tushare qfq kline via daily factors or the adjusted week/month endpoint."""
633
+ def _tushare(api_name: str, params: dict, fields: str | None = None, error: str = "tushare returned no data"):
634
+ """POST one tushare HTTP API call; returns (fields, items) or raises with the API message."""
659
635
  import os
660
636
 
661
- import pandas as pd
662
637
  import requests
663
638
 
664
639
  token = os.environ.get("TUSHARE_TOKEN")
665
640
  if not token:
666
641
  raise ValueError("TUSHARE_TOKEN not set")
642
+ body = {"api_name": api_name, "token": token, "params": params}
643
+ if fields:
644
+ body["fields"] = fields
645
+ data = requests.post("http://api.tushare.pro", json=body, timeout=15).json()
646
+ if data.get("code") != 0 or not data.get("data", {}).get("items"):
647
+ raise ValueError(f"{error}: {data.get('msg', '')}")
648
+ return data["data"]["fields"], data["data"]["items"]
667
649
 
650
+
651
+ def _kline_tushare(symbol: str, period: str, count: int) -> list:
652
+ """Tushare qfq kline via daily factors or the adjusted week/month endpoint."""
653
+ import os
654
+
655
+ import pandas as pd
656
+
657
+ if not os.environ.get("TUSHARE_TOKEN"):
658
+ raise ValueError("TUSHARE_TOKEN not set")
668
659
  if normalize_stock_code(symbol)["is_etf"]:
669
660
  raise ValueError("tushare stock adjustment does not cover ETFs")
670
661
  end = _cn_now()
@@ -681,33 +672,17 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
681
672
  params["freq"] = "week" if period == "weekly" else "month"
682
673
  fields = prices if period == "daily" else [f"{c}_qfq" for c in prices]
683
674
 
684
- resp = requests.post(
685
- "http://api.tushare.pro",
686
- json={
687
- "api_name": "daily" if period == "daily" else "stk_week_month_adj",
688
- "token": token,
689
- "params": params,
690
- "fields": ",".join(["trade_date", *fields, "vol", "amount", "pct_chg"]),
691
- },
692
- timeout=15,
693
- )
694
- data = resp.json()
695
- if data.get("code") != 0 or not data.get("data", {}).get("items"):
696
- raise ValueError(f"tushare returned no data: {data.get('msg', '')}")
697
-
698
- df = pd.DataFrame(data["data"]["items"], columns=data["data"]["fields"])
675
+ api = "daily" if period == "daily" else "stk_week_month_adj"
676
+ columns, items = _tushare(api, params, ",".join(["trade_date", *fields, "vol", "amount", "pct_chg"]))
677
+ df = pd.DataFrame(items, columns=columns)
699
678
  if not set(fields).issubset(df.columns):
700
679
  raise ValueError("tushare returned incomplete adjusted prices")
701
680
  df[fields] = df[fields].apply(pd.to_numeric, errors="coerce")
702
681
  if period == "daily":
703
- factor_resp = requests.post(
704
- "http://api.tushare.pro",
705
- json={"api_name": "adj_factor", "token": token, "params": params, "fields": "trade_date,adj_factor"},
706
- timeout=15,
707
- ).json()
708
- if factor_resp.get("code") != 0 or not factor_resp.get("data", {}).get("items"):
709
- raise ValueError("tushare returned no adjustment factors")
710
- factors = pd.DataFrame(factor_resp["data"]["items"], columns=factor_resp["data"]["fields"])
682
+ columns, items = _tushare(
683
+ "adj_factor", params, "trade_date,adj_factor", "tushare returned no adjustment factors"
684
+ )
685
+ factors = pd.DataFrame(items, columns=columns)
711
686
  factors["adj_factor"] = pd.to_numeric(factors["adj_factor"], errors="coerce")
712
687
  if not (factors["adj_factor"].gt(0) & factors["adj_factor"].lt(float("inf"))).all():
713
688
  raise ValueError("tushare returned invalid adjustment factors")
@@ -741,30 +716,10 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
741
716
 
742
717
  def _quote_tushare(symbol: str) -> dict:
743
718
  """Tushare realtime quote via HTTP API."""
744
- import os
745
-
746
- import requests
747
-
748
- token = os.environ.get("TUSHARE_TOKEN")
749
- if not token:
750
- raise ValueError("TUSHARE_TOKEN not set")
751
-
752
719
  ts_code = f"{symbol}.SH" if symbol.startswith(("6", "9", "5")) else f"{symbol}.SZ"
753
720
 
754
- resp = requests.post(
755
- "http://api.tushare.pro",
756
- json={
757
- "api_name": "realtime_quote",
758
- "token": token,
759
- "params": {"ts_code": ts_code},
760
- },
761
- timeout=15,
762
- )
763
- data = resp.json()
764
- if data.get("code") != 0 or not data.get("data", {}).get("items"):
765
- raise ValueError(f"tushare quote failed: {data.get('msg', '')}")
766
-
767
- row = dict(zip(data["data"]["fields"], data["data"]["items"][0]))
721
+ columns, items = _tushare("realtime_quote", {"ts_code": ts_code}, error="tushare quote failed")
722
+ row = dict(zip(columns, items[0]))
768
723
  return _clean_row(
769
724
  {
770
725
  "symbol": symbol,
@@ -1071,15 +1026,21 @@ def kline_yf(symbol: str, period: str, count: int, evidence: bool = False) -> li
1071
1026
 
1072
1027
  def cmd_kline(args):
1073
1028
  market = detect_market(args.symbol)
1074
- try:
1029
+ # 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.
1030
+ bucket = next((b for b in (60, 120, 250) if args.count <= b), args.count)
1031
+
1032
+ def fetch():
1075
1033
  if market == "A":
1076
- data = kline_a(args.symbol, args.period, args.count, evidence=True)
1034
+ data = kline_a(args.symbol, args.period, bucket, evidence=True)
1077
1035
  if data:
1078
1036
  adjusted = "none" if _A_INDEX_RE.fullmatch(args.symbol) else "qfq"
1079
1037
  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
1038
+ return data
1039
+ return kline_yf(args.symbol, args.period, bucket, evidence=True)
1040
+
1041
+ try:
1042
+ data = cached(f"kline:v1:{args.symbol}:{args.period}:{bucket}", 60, fetch)
1043
+ return data[-args.count :] if isinstance(data, list) else data
1083
1044
  except Exception as e:
1084
1045
  return {"error": error_message(e)}
1085
1046
 
@@ -1087,27 +1048,136 @@ def cmd_kline(args):
1087
1048
  # --------------- quote ---------------
1088
1049
 
1089
1050
 
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.
1051
+ # Eastmoney answers per-code (ulist), ranked-page (clist) and server-filtered (datacenter)
1052
+ # queries, so no caller needs a whole-market spot table to read a few rows.
1053
+ _EM_HEADERS = {"User-Agent": "Mozilla/5.0"}
1054
+ _EM_PUSH2 = "https://push2.eastmoney.com/api/qt"
1055
+ _EM_DATACENTER = "https://datacenter-web.eastmoney.com/api/data/v1/get"
1056
+ _EM_FS = {
1057
+ "A": "m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048",
1058
+ "HK": "m:128+t:3,m:128+t:4,m:128+t:1,m:128+t:2",
1059
+ "US": "m:105,m:106,m:107",
1060
+ }
1061
+ _EM_PAGE = 100 # clist caps pz at 100 rows
1062
+
1063
+
1064
+ def _get_json(url: str, params: dict) -> dict:
1065
+ """GET an eastmoney JSON API; relative paths are push2 qt endpoints."""
1066
+ import requests
1067
+
1068
+ url = url if "://" in url else f"{_EM_PUSH2}/{url}"
1069
+ resp = requests.get(url, params=params, headers=_EM_HEADERS, timeout=10)
1070
+ resp.raise_for_status()
1071
+ return resp.json()
1072
+
1073
+
1074
+ def _em_secid(symbol: str) -> str:
1075
+ code = _cn_code(symbol)
1076
+ return f"{1 if code[:2] == 'sh' else 0}.{code[2:]}"
1077
+
1078
+
1079
+ def _em_ulist(secids: list, fields: str) -> list:
1080
+ data = _get_json("ulist.np/get", {"fltt": 2, "invt": 2, "secids": ",".join(secids), "fields": fields}).get("data")
1081
+ return (data or {}).get("diff") or []
1082
+
1083
+
1084
+ def _em_clist(market: str, fid: str, fields: str, page: int = 1, size: int = _EM_PAGE, desc: bool = True):
1085
+ """One ranked page of a market list → (universe total, rows); pn is a page index of size pz."""
1086
+ params = {"np": 1, "fltt": 2, "invt": 2, "fs": _EM_FS[market], "fid": fid, "po": int(desc), "pn": page, "pz": size}
1087
+ data = _get_json("clist/get", {**params, "fields": fields}).get("data") or {}
1088
+ return int(data.get("total") or 0), data.get("diff") or []
1089
+
1090
+
1091
+ def _em_datacenter(report: str, filter_: str, sort: str, size: int, order: str = "-1", url: str = _EM_DATACENTER):
1092
+ """One server-filtered datacenter page → (match count, rows); no match is an empty page."""
1093
+ body = _get_json(
1094
+ url,
1095
+ {
1096
+ "reportName": report,
1097
+ "columns": "ALL",
1098
+ "filter": filter_,
1099
+ "sortColumns": sort,
1100
+ "sortTypes": order,
1101
+ "pageSize": size,
1102
+ "pageNumber": 1,
1103
+ "source": "WEB",
1104
+ "client": "WEB",
1105
+ },
1106
+ )
1107
+ if body.get("result") is None:
1108
+ if body.get("code") == 9201: # 返回数据为空
1109
+ return 0, []
1110
+ raise ValueError(f"eastmoney {report} unavailable: {str(body.get('message'))[:100]}")
1111
+ return int(body["result"].get("count") or 0), body["result"].get("data") or []
1112
+
1092
1113
 
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.
1114
+ _EM_CODETABLE = "https://search-codetable.eastmoney.com/codetable/search/web"
1115
+
1116
+
1117
+ def _em_codetable(keyword: str, count: int) -> list:
1118
+ """Eastmoney code-table search rows (stocks, funds, indices and BK boards) matching a keyword."""
1119
+ params = {"keyword": keyword, "pageIndex": 1, "pageSize": count, "client": "web", "clientType": "webSuggest"}
1120
+ body = _get_json(_EM_CODETABLE, {**params, "clientVersion": "lastest"})
1121
+ if str(body.get("code")) != "0":
1122
+ raise ValueError(f"eastmoney search unavailable: {str(body.get('msg'))[:100]}")
1123
+ return body.get("result") or []
1124
+
1125
+
1126
+ _EM_QUOTE_FIELDS = "f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f14,f15,f16,f17,f18,f20,f23,f24,f38,f441"
1127
+
1128
+
1129
+ def _quote_eastmoney(symbol: str) -> dict:
1130
+ """One-code eastmoney quote. ETFs add iopv (盘中实时估值), fund_shares (最新份额) and
1131
+ premium_discount_rate = (price - iopv) / iopv * 100 (positive = 溢价); None without an IOPV."""
1132
+ row = next((r for r in _em_ulist([_em_secid(symbol)], _EM_QUOTE_FIELDS) if str(r.get("f12")) == symbol), {})
1133
+
1134
+ def value(key):
1135
+ return _parse_float(row.get(key))
1136
+
1137
+ if value("f2") is None:
1138
+ raise ValueError(f"eastmoney returned no quote for {symbol}")
1139
+ result = {
1140
+ "symbol": symbol,
1141
+ "name": row.get("f14"),
1142
+ "price": value("f2"),
1143
+ "change": value("f4"),
1144
+ "change_pct": value("f3"),
1145
+ "volume": value("f5"),
1146
+ "turnover": value("f6"),
1147
+ "high": value("f15"),
1148
+ "low": value("f16"),
1149
+ "open": value("f17"),
1150
+ "prev_close": value("f18"),
1151
+ "market_cap": value("f20"),
1152
+ "turnover_rate": value("f8"),
1153
+ "volume_ratio": value("f10"),
1154
+ }
1155
+ if normalize_stock_code(symbol)["is_etf"]:
1156
+ price, iopv = result["price"], value("f441")
1157
+ premium = round((price - iopv) / iopv * 100, 4) if iopv else None
1158
+ result.update(is_etf=True, iopv=iopv, fund_shares=value("f38"), premium_discount_rate=premium)
1159
+ else:
1160
+ result.update(
1161
+ pe=value("f9"), pe_basis="dynamic", pb=value("f23"), amplitude=value("f7"), change_pct_60d=value("f24")
1162
+ )
1163
+ return _clean_row(result)
1164
+
1165
+
1166
+ def quote_a(symbol: str, evidence: bool = False) -> dict:
1167
+ """A-share quote with failover: tencent → eastmoney → tushare → sina, one code per request.
1095
1168
 
1096
1169
  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."""
1170
+ tencent/sina — a bare secid would be ambiguous (sh000001 vs sz000001). ETF quotes use their own
1171
+ chain key (quote_a_etf) so a stock-side winner never reorders the ETF chain."""
1099
1172
  if _A_PREFIXED_RE.fullmatch(symbol):
1100
1173
  sources = [("tencent", lambda: _quote_tencent(symbol)), ("sina", lambda: _quote_sina(symbol))]
1101
1174
  with socket_timeout(25):
1102
1175
  return _failover(sources, label=f"quote_a:{symbol}", evidence=evidence)
1103
1176
  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)),
1177
+ sources = [
1178
+ ("tencent", lambda: _quote_tencent(symbol)),
1179
+ ("eastmoney", lambda: _quote_eastmoney(symbol)),
1109
1180
  ("tushare", lambda: _quote_tushare(symbol)),
1110
- ("efinance", lambda: _quote_efinance(symbol)),
1111
1181
  ("sina", lambda: _quote_sina(symbol)),
1112
1182
  ]
1113
1183
  chain = "quote_a_etf" if is_etf else "quote_a"
@@ -1115,80 +1185,6 @@ def quote_a(symbol: str, evidence: bool = False) -> dict:
1115
1185
  return _failover(sources, label=f"{chain}:{symbol}", evidence=evidence)
1116
1186
 
1117
1187
 
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
1188
  def _quote_yfinance(symbol: str) -> dict:
1193
1189
  import yfinance as yf
1194
1190
 
@@ -1449,6 +1445,48 @@ def cmd_quote(args):
1449
1445
  return result
1450
1446
 
1451
1447
 
1448
+ QUOTES_DEADLINE = 30 # dsh/dashboard.ts allows the whole process 40 s
1449
+ QUOTES_MAX = 40
1450
+
1451
+
1452
+ def _batch(symbols_arg: str, one, label: str) -> list | dict:
1453
+ """Run `one(symbol)` for several symbols in one process for the dsh dashboard; rows follow input order,
1454
+ a failure or a row still running at the deadline stays per row."""
1455
+ symbols = [symbol.strip() for symbol in symbols_arg.split(",") if symbol.strip()]
1456
+ if not 0 < len(symbols) <= QUOTES_MAX:
1457
+ return {"error": f"{label}s takes 1-{QUOTES_MAX} comma-separated symbols"}
1458
+ rows = [{"error": f"{label} timed out"}] * len(symbols)
1459
+ workers = threading.BoundedSemaphore(8)
1460
+
1461
+ def run(index, symbol):
1462
+ with workers:
1463
+ try:
1464
+ rows[index] = one(symbol)
1465
+ except Exception as e:
1466
+ rows[index] = {"error": error_message(e)}
1467
+
1468
+ # Daemon threads: a provider that hangs past the deadline cannot keep the process (and every other row) alive.
1469
+ threads = [threading.Thread(target=run, args=item, daemon=True) for item in enumerate(symbols)]
1470
+ for thread in threads:
1471
+ thread.start()
1472
+ deadline = time.monotonic() + QUOTES_DEADLINE
1473
+ for thread in threads:
1474
+ thread.join(max(0, deadline - time.monotonic()))
1475
+ return list(rows)
1476
+
1477
+
1478
+ def cmd_quotes(args):
1479
+ return _batch(args.symbols, lambda symbol: cmd_quote(argparse.Namespace(symbol=symbol)), "quote")
1480
+
1481
+
1482
+ def cmd_klines(args):
1483
+ return _batch(
1484
+ args.symbols,
1485
+ lambda symbol: cmd_kline(argparse.Namespace(symbol=symbol, period="daily", count=args.count)),
1486
+ "kline",
1487
+ )
1488
+
1489
+
1452
1490
  # --------------- open-end fund quote ---------------
1453
1491
 
1454
1492
 
@@ -1466,28 +1504,13 @@ def _fund_nav_date(stamp) -> str:
1466
1504
  return datetime.fromtimestamp(stamp / 1000, timezone.utc).astimezone(ZoneInfo("Asia/Shanghai")).date().isoformat()
1467
1505
 
1468
1506
 
1469
- def _fund_estimate(symbol: str) -> dict:
1470
- import akshare as ak
1507
+ def fund_pingzhong_js(symbol: str) -> str:
1508
+ """Eastmoney's per-fund JS bundle (NAV, scale, holdings series)."""
1509
+ import requests
1471
1510
 
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
- }
1511
+ response = requests.get(f"https://fund.eastmoney.com/pingzhongdata/{symbol}.js", timeout=15)
1512
+ response.raise_for_status()
1513
+ return response.text
1491
1514
 
1492
1515
 
1493
1516
  def cmd_fund_quote(args):
@@ -1495,11 +1518,7 @@ def cmd_fund_quote(args):
1495
1518
  if not re.fullmatch(r"\d{6}", symbol):
1496
1519
  return {"error": "open-end fund symbol must be exactly 6 digits"}
1497
1520
  try:
1498
- import requests
1499
-
1500
- response = requests.get(f"https://fund.eastmoney.com/pingzhongdata/{symbol}.js", timeout=15)
1501
- response.raise_for_status()
1502
- text = response.text
1521
+ text = fund_pingzhong_js(symbol)
1503
1522
  if _fund_js_value(text, "fS_code") != symbol:
1504
1523
  raise ValueError(f"eastmoney returned a different fund for {symbol}")
1505
1524
  name = _fund_js_value(text, "fS_name")
@@ -1511,17 +1530,6 @@ def cmd_fund_quote(args):
1511
1530
  nav_date = _fund_nav_date(latest["x"])
1512
1531
  accumulated_by_date = {_fund_nav_date(row[0]): row[1] for row in accumulated_rows}
1513
1532
  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
1533
  result = _clean_row(
1526
1534
  {
1527
1535
  "symbol": symbol,
@@ -1540,13 +1548,16 @@ def cmd_fund_quote(args):
1540
1548
  "fetched_at": datetime.now(timezone.utc).isoformat(),
1541
1549
  "note": (
1542
1550
  "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"
1551
+ "1-2 trading days; no intraday estimated NAV is provided"
1544
1552
  ),
1545
- **estimate,
1553
+ # Eastmoney retired its per-fund intraday estimate (fundgz); its remaining estimate
1554
+ # page lists every fund at once, so the estimate keys stay but are always unavailable.
1555
+ "estimated_nav": None,
1556
+ "estimated_change_pct": None,
1557
+ "estimated_at": None,
1558
+ "estimate_status": "unavailable",
1546
1559
  }
1547
1560
  )
1548
- if warnings:
1549
- result["warnings"] = warnings
1550
1561
  return result
1551
1562
  except Exception as e:
1552
1563
  return {"error": error_message(e)}
@@ -1758,18 +1769,31 @@ def _yf_info(symbol, ticker):
1758
1769
  return cached(f"yf-basic-info:{_yf_hk_symbol(symbol)}", DAY, fetch)
1759
1770
 
1760
1771
 
1761
- def _financials_a_frame(symbol):
1772
+ def _financials_a_frame(symbol, periods=1, annual=False):
1773
+ """Sina serves one page per report year, and akshare's default start_year=1900 walks every year
1774
+ since listing (~30 requests). Start at the current year and step back only until `periods` rows exist."""
1762
1775
  import akshare as ak
1776
+ import pandas as pd
1763
1777
 
1764
- df = _cached_frame(
1765
- f"financial-source:{symbol}", lambda: _akshare_retry(ak.stock_financial_analysis_indicator, symbol=symbol)
1766
- )
1778
+ year = _cn_now().year
1779
+ # This year's annual report never exists within this year; one step back covers reports not yet out.
1780
+ first = year - periods if annual else year - -(-periods // 4) + 1
1781
+ for start in (first, first - 1):
1782
+ df = _cached_frame(
1783
+ f"financial-source:{symbol}:{start}",
1784
+ lambda start=start: _akshare_retry(
1785
+ ak.stock_financial_analysis_indicator, symbol=symbol, start_year=str(start)
1786
+ ),
1787
+ )
1788
+ dates = df["日期"].astype(str) if "日期" in df.columns else pd.Series(dtype=str)
1789
+ if (dates.str.endswith("12-31").sum() if annual else len(dates)) >= periods:
1790
+ break
1767
1791
  return df.sort_values("日期", ascending=False) if "日期" in df.columns else df
1768
1792
 
1769
1793
 
1770
1794
  def financials_a(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1771
1795
  try:
1772
- df = _financials_a_frame(symbol)
1796
+ df = _financials_a_frame(symbol, periods, annual)
1773
1797
  if annual and not df.empty:
1774
1798
  df = df[df["日期"].astype(str).str.endswith("12-31")]
1775
1799
  if df is None or df.empty:
@@ -2101,193 +2125,168 @@ def _financials_without_dcf(args, periods, annual, market):
2101
2125
 
2102
2126
  # --------------- market_snapshot ---------------
2103
2127
 
2128
+ # The screener only scores stocks that can pass its filters, so every leg walks the market
2129
+ # largest-cap first and stops at market_cap_min; the row cap bounds a loose (or zero) floor.
2130
+ _SNAPSHOT_MAX_ROWS = 3000
2131
+
2132
+ # screener filter field → eastmoney xuangu (server-side A-share condition screener) column
2133
+ _XUANGU_FILTER_COLUMNS = {
2134
+ "market_cap": "TOTAL_MARKET_CAP",
2135
+ "pe": "PE9",
2136
+ "pb": "PBNEWMRQ",
2137
+ "turnover_rate": "TURNOVERRATE",
2138
+ "change_pct": "CHANGE_RATE",
2139
+ "volume_ratio": "VOLUME_RATIO",
2140
+ }
2141
+ _XUANGU_COLUMNS = {
2142
+ "SECURITY_CODE": "symbol",
2143
+ "SECURITY_NAME_ABBR": "name",
2144
+ "NEW_PRICE": "price",
2145
+ "CHANGE_RATE": "change_pct",
2146
+ "VOLUME": "volume",
2147
+ "DEAL_AMOUNT": "turnover",
2148
+ "PE9": "pe",
2149
+ "PBNEWMRQ": "pb",
2150
+ "TOTAL_MARKET_CAP": "market_cap",
2151
+ "TURNOVERRATE": "turnover_rate",
2152
+ "VOLUME_RATIO": "volume_ratio",
2153
+ }
2154
+ _XUANGU_URL = "https://data.eastmoney.com/dataapi/xuangu/list"
2155
+
2156
+
2157
+ def _xuangu_page(conditions: str, page: int, size: int) -> dict:
2158
+ params = {"st": "TOTAL_MARKET_CAP", "sr": -1, "ps": size, "p": page, "sty": ",".join(_XUANGU_COLUMNS)}
2159
+ body = _get_json(_XUANGU_URL, {**params, "filter": conditions, "source": "SELECT_SECURITIES", "client": "WEB"})
2160
+ if not isinstance(body.get("result"), dict):
2161
+ raise ValueError(f"eastmoney screener unavailable: {str(body.get('message'))[:100]}")
2162
+ return body["result"]
2163
+
2164
+
2165
+ def _snapshot_xuangu(filters: dict):
2166
+ """A-share candidates filtered server-side (PE9 is a TTM PE), largest market cap first."""
2167
+ conditions = ""
2168
+ for key, value in sorted(filters.items()):
2169
+ field, _, bound = key.rpartition("_")
2170
+ if field in _XUANGU_FILTER_COLUMNS and bound in ("min", "max"):
2171
+ conditions += f"({_XUANGU_FILTER_COLUMNS[field]}{'>=' if bound == 'min' else '<='}{value})"
2172
+ rows, page = [], 1
2173
+ while len(rows) < _SNAPSHOT_MAX_ROWS:
2174
+ result = _xuangu_page(conditions, page, 1000)
2175
+ for item in result.get("data") or []:
2176
+ rows.append(_clean_row({key: item.get(col) for col, key in _XUANGU_COLUMNS.items()} | {"pe_basis": "ttm"}))
2177
+ if not result.get("nextpage"):
2178
+ break
2179
+ page += 1
2180
+ total = None
2181
+ with contextlib.suppress(Exception):
2182
+ total = _xuangu_page("", 1, 1).get("count")
2183
+ return total, rows
2184
+
2185
+
2186
+ def _snapshot_tencent(filters: dict):
2187
+ """Non-eastmoney A-share leg (issue #25): Tencent's board rank sorted by market cap."""
2188
+ floor = filters.get("market_cap_min") or 0
2189
+ rows, total = [], None
2190
+ while len(rows) < _SNAPSHOT_MAX_ROWS:
2191
+ params = {"_appver": "11.17.0", "board_code": "aStock", "sort_type": "marketValue", "direct": "down"}
2192
+ body = _get_json(
2193
+ "https://proxy.finance.qq.com/cgi/cgi-bin/rank/hs/getBoardRankList",
2194
+ {**params, "offset": len(rows), "count": 200},
2195
+ )
2196
+ data = body.get("data") or {}
2197
+ total, ranked = data.get("total"), data.get("rank_list") or []
2198
+ for item in ranked:
2199
+ cap = _parse_float(item.get("zsz")) # 亿元
2200
+ if cap is None or cap * 1e8 < floor:
2201
+ return total, rows
2202
+ turnover = _parse_float(item.get("turnover")) # 万元
2203
+ row = {
2204
+ "symbol": str(item.get("code", ""))[2:],
2205
+ "name": item.get("name"),
2206
+ "price": _parse_float(item.get("zxj")),
2207
+ "change_pct": _parse_float(item.get("zdf")),
2208
+ "volume": _parse_float(item.get("volume")),
2209
+ "turnover": turnover * 1e4 if turnover is not None else None,
2210
+ "pe": _parse_float(item.get("pe_ttm")),
2211
+ "pe_basis": "ttm",
2212
+ "pb": _parse_float(item.get("pn")),
2213
+ "market_cap": cap * 1e8,
2214
+ "turnover_rate": _parse_float(item.get("hsl")),
2215
+ "volume_ratio": _parse_float(item.get("lb")),
2216
+ }
2217
+ rows.append(_clean_row(row))
2218
+ if len(ranked) < 200:
2219
+ break
2220
+ if not rows and not total:
2221
+ raise ValueError("tencent rank returned empty data")
2222
+ return total, rows
2223
+
2224
+
2225
+ # HK/US keep their historical column set: adding turnover_rate/volume_ratio (or US pb) would
2226
+ # silently start applying A-share-tuned default filters to these markets.
2227
+ _CLIST_SNAPSHOT_COLUMNS = {
2228
+ "HK": {"f12": "symbol", "f14": "name", "f2": "price", "f3": "change_pct", "f5": "volume", "f6": "turnover"}
2229
+ | {"f9": "pe", "f23": "pb", "f20": "market_cap"},
2230
+ "US": {"f12": "symbol", "f14": "name", "f2": "price", "f3": "change_pct", "f5": "volume", "f6": "turnover"}
2231
+ | {"f9": "pe", "f20": "market_cap"},
2232
+ }
2104
2233
 
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
2234
 
2235
+ def _snapshot_clist(market: str, floor: float):
2236
+ columns = _CLIST_SNAPSHOT_COLUMNS[market]
2237
+ rows, total, page = [], 0, 1
2238
+ while len(rows) < _SNAPSHOT_MAX_ROWS:
2239
+ total, ranked = _em_clist(market, "f20", ",".join(columns), page=page)
2240
+ for item in ranked:
2241
+ cap = _parse_float(item.get("f20"))
2242
+ if cap is None or cap < floor:
2243
+ return total, rows
2244
+ row = {key: _parse_float(item.get(col)) for col, key in columns.items()}
2245
+ row.update(symbol=_clist_symbol(market, item.get("f12")), name=item.get("f14"))
2246
+ rows.append(_clean_row(row | {"pe_basis": "unknown"}))
2247
+ if len(ranked) < _EM_PAGE:
2248
+ break
2249
+ page += 1
2250
+ return total, rows
2176
2251
 
2177
- # qt.gtimg.cn accepts comma-joined multi-code batches; 60 keeps URLs well short
2178
- _TENCENT_BATCH = 60
2179
2252
 
2253
+ # Non-eastmoney HK/US leg: TradingView's public scanner, same columns as _CLIST_SNAPSHOT_COLUMNS.
2254
+ _TV_BASE = ["name", "description", "close", "change", "volume", "Value.Traded", "price_earnings_ttm"]
2255
+ _TV_SNAPSHOT_COLUMNS = {
2256
+ "HK": ("hongkong", [*_TV_BASE, "price_book_fq", "market_cap_basic"]),
2257
+ "US": ("america", [*_TV_BASE, "market_cap_basic"]),
2258
+ }
2259
+ _TV_KEYS = {"close": "price", "change": "change_pct", "Value.Traded": "turnover"}
2260
+ _TV_KEYS |= {"price_earnings_ttm": "pe", "price_book_fq": "pb", "market_cap_basic": "market_cap"}
2180
2261
 
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
2262
 
2263
+ def _snapshot_tradingview(market: str, floor: float):
2189
2264
  import requests
2190
2265
 
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)
2266
+ region, columns = _TV_SNAPSHOT_COLUMNS[market]
2267
+ filters = [{"left": "type", "operation": "equal", "right": "stock"}]
2268
+ filters += [{"left": "is_primary", "operation": "equal", "right": True}]
2269
+ rows, total = [], None
2270
+ while len(rows) < _SNAPSHOT_MAX_ROWS:
2271
+ body = {"columns": columns, "filter": filters, "range": [len(rows), len(rows) + 1000]}
2272
+ body["sort"] = {"sortBy": "market_cap_basic", "sortOrder": "desc"}
2273
+ resp = requests.post(f"https://scanner.tradingview.com/{region}/scan", json=body, timeout=15)
2274
+ resp.raise_for_status()
2275
+ payload = resp.json()
2276
+ total, data = payload.get("totalCount"), payload.get("data") or []
2277
+ for item in data:
2278
+ values = dict(zip(columns, item.get("d") or []))
2279
+ cap = _parse_float(values.get("market_cap_basic"))
2280
+ if cap is None or cap < floor:
2281
+ return total, rows
2282
+ row = {_TV_KEYS.get(col, col): _parse_float(values.get(col)) for col in columns[2:]}
2283
+ row.update(symbol=_clist_symbol(market, values.get("name")), name=values.get("description"))
2284
+ rows.append(_clean_row(row | {"pe_basis": "ttm"}))
2285
+ if len(data) < 1000:
2286
+ break
2229
2287
  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
-
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]}]
2288
+ raise ValueError("tradingview scanner returned empty data")
2289
+ return total, rows
2291
2290
 
2292
2291
 
2293
2292
  def normalize_us_symbol(value) -> str:
@@ -2297,44 +2296,31 @@ def normalize_us_symbol(value) -> str:
2297
2296
  return re.sub(r"[._]", "-", symbol) # Eastmoney writes class shares as BRK_B; yfinance wants BRK-B
2298
2297
 
2299
2298
 
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
2299
  def cmd_market_snapshot(args):
2300
+ """Screener input: the stocks at or above market_cap_min (largest first), never the whole market."""
2301
+ market = args.market.upper()
2327
2302
  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}"}
2303
+ filters = json.loads(getattr(args, "filters", None) or "{}")
2304
+ if not isinstance(filters, dict):
2305
+ return {"error": "filters must be a JSON object"}
2306
+ floor = filters.get("market_cap_min") or 0
2307
+ if market == "A":
2308
+ legs = [("eastmoney", lambda: _snapshot_xuangu(filters)), ("tencent", lambda: _snapshot_tencent(filters))]
2309
+ elif market in _CLIST_SNAPSHOT_COLUMNS:
2310
+ legs = [
2311
+ ("eastmoney", lambda: _snapshot_clist(market, floor)),
2312
+ ("tradingview", lambda: _snapshot_tradingview(market, floor)),
2313
+ ]
2314
+ else:
2315
+ return {"error": f"Unknown market: {market}"}
2316
+ with socket_timeout(25):
2317
+ total, rows = _failover(legs, label=f"snapshot_{market.lower()}")
2318
+ result = {"market": market, "total_stocks": total, "rows": rows[:_SNAPSHOT_MAX_ROWS]}
2319
+ if len(rows) >= _SNAPSHOT_MAX_ROWS:
2320
+ result["truncated"] = True
2321
+ return result
2336
2322
  except Exception as e:
2337
- return {"error": error_message(e)}
2323
+ return {"error": f"{market} snapshot unavailable: {error_message(e)}"}
2338
2324
 
2339
2325
 
2340
2326
  # --------------- market_indices ---------------
@@ -2352,11 +2338,7 @@ _REGION_INDEX_SYMBOLS = {
2352
2338
  def _quote_tencent_hk_index(symbol: str) -> dict:
2353
2339
  """Tencent r_hk index quote, using the same `~` field positions as _tencent_payload_row.
2354
2340
  Volume is omitted: the index feed's volume field has no confirmed unit."""
2355
- import requests
2356
-
2357
- resp = requests.get(f"https://qt.gtimg.cn/q=r_{_TENCENT_HK_INDEX[symbol]}", timeout=10)
2358
- resp.raise_for_status()
2359
- f = _gbk_var_payload(resp.content).split("~")
2341
+ f = _tencent_fields(f"r_{_TENCENT_HK_INDEX[symbol]}")
2360
2342
  price = _parse_float(f[3]) if len(f) > 34 else None
2361
2343
  if price is None or price <= 0:
2362
2344
  raise ValueError(f"tencent returned no quote for {symbol}")
@@ -2374,53 +2356,67 @@ def _quote_tencent_hk_index(symbol: str) -> dict:
2374
2356
  }
2375
2357
 
2376
2358
 
2359
+ _CN_INDICES = [
2360
+ ("sh000001", "上证指数"),
2361
+ ("sz399001", "深证成指"),
2362
+ ("sz399006", "创业板指"),
2363
+ ("sh000688", "科创50"),
2364
+ ("sh000300", "沪深300"),
2365
+ ("sh000016", "上证50"),
2366
+ ]
2367
+
2368
+
2369
+ _EM_INDEX_FIELDS = (
2370
+ ("price", "f2"),
2371
+ ("change", "f4"),
2372
+ ("change_pct", "f3"),
2373
+ ("volume", "f5"),
2374
+ ("turnover", "f6"),
2375
+ ("high", "f15"),
2376
+ ("low", "f16"),
2377
+ ("open", "f17"),
2378
+ ("prev_close", "f18"),
2379
+ )
2380
+
2381
+
2382
+ def _cn_indices_eastmoney() -> dict:
2383
+ secids = {f"{1 if code[:2] == 'sh' else 0}.{code[2:]}": code for code, _ in _CN_INDICES}
2384
+ quotes = {}
2385
+ for row in _em_ulist(list(secids), "f2,f3,f4,f5,f6,f12,f13,f14,f15,f16,f17,f18"):
2386
+ code = secids.get(f"{row.get('f13')}.{row.get('f12')}")
2387
+ if code and _parse_float(row.get("f2")) is not None:
2388
+ quotes[code] = {"name": row.get("f14")} | {key: _parse_float(row.get(col)) for key, col in _EM_INDEX_FIELDS}
2389
+ return quotes
2390
+
2391
+
2392
+ def _cn_indices_tencent() -> dict:
2393
+ """One qt.gtimg.cn batch request for every CN index (eastmoney refuses some datacenter IPs)."""
2394
+ content = _tencent_raw(",".join(code for code, _ in _CN_INDICES))
2395
+ quotes = {}
2396
+ for part in content.decode("gbk", errors="replace").split(";"):
2397
+ name, _, payload = part.strip().partition('="')
2398
+ fields = payload.rstrip('"').split("~")
2399
+ if len(fields) > 37 and _parse_float(fields[3]):
2400
+ row = _tencent_payload_row(fields, name.removeprefix("v_"))
2401
+ keys = ("name", "price", "change", "change_pct", "volume", "turnover", "high", "low", "open", "prev_close")
2402
+ quotes[name.removeprefix("v_")] = {key: row[key] for key in keys}
2403
+ return quotes
2404
+
2405
+
2377
2406
  def cmd_market_indices(args):
2378
2407
  region = args.region.lower()
2379
2408
  try:
2380
2409
  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
- )
2410
+ with socket_timeout(25):
2411
+ quotes = _failover(
2412
+ [("eastmoney", _cn_indices_eastmoney), ("tencent", _cn_indices_tencent)], label="market_indices_cn"
2422
2413
  )
2423
- return results
2414
+ return [
2415
+ _clean_row({"code": code, **quotes[code]})
2416
+ if code in quotes
2417
+ else {"code": code, "name": name, "note": "data unavailable"}
2418
+ for code, name in _CN_INDICES
2419
+ ]
2424
2420
  elif region in _REGION_INDEX_SYMBOLS:
2425
2421
  from concurrent.futures import ThreadPoolExecutor
2426
2422
 
@@ -2729,17 +2725,20 @@ def _constituents_em(sector: str, board_kind: str) -> dict:
2729
2725
  """Eastmoney industry/concept board constituents via akshare, fuzzy-resolved."""
2730
2726
  import akshare as ak
2731
2727
 
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()])
2728
+ cons_fn = ak.stock_board_industry_cons_em if board_kind == "industry" else ak.stock_board_concept_cons_em
2729
+ kind = "行业" if board_kind == "industry" else "概念"
2730
+ # Search the board by name (one request) instead of paging the whole board list (~5s per kind).
2731
+ keyword = re.sub(r"(板块|行业|概念)$", "", re.sub(r"\s+", "", sector)) or sector
2732
+ boards = {
2733
+ str(r.get("shortName")): str(r.get("code"))
2734
+ for r in _akshare_retry(_em_codetable, keyword, 50)
2735
+ if r.get("market") == 90 and r.get("securityTypeName") == kind and str(r.get("code", "")).startswith("BK")
2736
+ }
2737
+ exact = _fuzzy_match_sector(sector, list(boards))
2740
2738
  if not exact:
2741
2739
  raise ValueError(f"no eastmoney {board_kind} board matching '{sector}'")
2742
- df = _akshare_retry(cons_fn, symbol=exact)
2740
+ # A BK code also skips akshare's own name→code lookup, which pages the whole board list again.
2741
+ df = _akshare_retry(cons_fn, symbol=boards[exact])
2743
2742
  if df is None or df.empty:
2744
2743
  raise ValueError(f"eastmoney {board_kind} board '{exact}' returned no constituents")
2745
2744
  constituents = _constituent_rows(df)
@@ -2978,8 +2977,7 @@ def _resolve_leg_down(name: str) -> bool:
2978
2977
 
2979
2978
 
2980
2979
  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)
2980
+ # info_map only saves the em leg's fetch on a miss; the result is the same, so share the cache.
2983
2981
  return cached(f"stock-sectors:{symbol}", WEEK, lambda: _resolve_stock_sectors(symbol, info_map))
2984
2982
 
2985
2983
 
@@ -3206,93 +3204,132 @@ def cmd_chip_distribution(args):
3206
3204
  # --------------- market_stats ---------------
3207
3205
 
3208
3206
 
3209
- def compute_market_stats(data: list) -> dict:
3210
- """Compute breadth/limit/turnover statistics from a market snapshot list."""
3211
- import numpy as np
3207
+ def _clist_symbol(market: str, code) -> str:
3208
+ code = str(code)
3209
+ return f"{code.zfill(5)}.HK" if market == "HK" else normalize_us_symbol(code) if market == "US" else code
3212
3210
 
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
3211
 
3253
- return _clean_row(
3212
+ def _clist_change_at(market: str, rank: int, desc: bool = True):
3213
+ _, rows = _em_clist(market, "f3", "f3", page=rank, size=1, desc=desc)
3214
+ return _parse_float(rows[0].get("f3")) if rows else None
3215
+
3216
+
3217
+ def _leading_count(market: str, total: int, desc: bool) -> int:
3218
+ """Ranks with a strictly positive (desc) / negative (asc) change, by binary search over single-row pages."""
3219
+ lo, hi = 0, total
3220
+ while lo < hi:
3221
+ mid = (lo + hi + 1) // 2
3222
+ change = _clist_change_at(market, mid, desc) or 0
3223
+ if change > 0 if desc else change < 0:
3224
+ lo = mid
3225
+ else:
3226
+ hi = mid - 1
3227
+ return lo
3228
+
3229
+
3230
+ def _movers(market: str, desc: bool) -> tuple:
3231
+ total, rows = _em_clist(market, "f3", "f12,f14,f3", size=5, desc=desc)
3232
+ movers = [
3233
+ {"symbol": _clist_symbol(market, r.get("f12")), "name": r.get("f14"), "change_pct": _parse_float(r.get("f3"))}
3234
+ for r in rows
3235
+ ]
3236
+ return total, movers if desc else movers[::-1] # losers keep the old order: least negative first
3237
+
3238
+
3239
+ def _median_change(market: str, total: int, up: int, flat: int, counted: int):
3240
+ """Median of the counted stocks; unchanged and unquoted rows rank between gainers and losers."""
3241
+ if counted <= 0:
3242
+ return None
3243
+ k = (counted + 1) // 2
3244
+ if up < k <= up + flat:
3245
+ return 0.0
3246
+ return _clist_change_at(market, k if k <= up else k + total - counted)
3247
+
3248
+
3249
+ _EM_LIMIT_POOL = {"ut": "7eea3edcaed734bea9cbfc24409ed989", "dpt": "wz.ztzt"}
3250
+
3251
+
3252
+ def _limit_samples(endpoint: str, sort: str, trade_date: str) -> list:
3253
+ params = {**_EM_LIMIT_POOL, "Pageindex": 0, "pagesize": 5, "sort": sort, "date": trade_date}
3254
+ pool = (_get_json(f"https://push2ex.eastmoney.com/{endpoint}", params).get("data") or {}).get("pool") or []
3255
+ return [
3254
3256
  {
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],
3257
+ "symbol": p.get("c"),
3258
+ "name": p.get("n"),
3259
+ "change_pct": _sanitize(round(p["zdp"], 2) if isinstance(p.get("zdp"), (int, float)) else None),
3274
3260
  }
3275
- )
3261
+ for p in pool
3262
+ ]
3263
+
3264
+
3265
+ def _market_stats_a() -> dict:
3266
+ """A-share breadth from eastmoney's 涨跌分布 (buckets ±11 are limit moves) — no per-stock rows."""
3267
+ body = _get_json("https://push2ex.eastmoney.com/getTopicZDFenBu", _EM_LIMIT_POOL).get("data") or {}
3268
+ buckets = {int(k): int(v) for item in body.get("fenbu") or [] for k, v in item.items()}
3269
+ if not buckets:
3270
+ raise ValueError("eastmoney breadth distribution unavailable")
3271
+ up = sum(v for k, v in buckets.items() if k > 0)
3272
+ down = sum(v for k, v in buckets.items() if k < 0)
3273
+ flat = buckets.get(0, 0)
3274
+ trade_date = str(body.get("qdate") or "")
3275
+ index_rows = _em_ulist(["1.000001", "0.399001", "0.899050"], "f6") # 沪 + 深 + 北 market turnover
3276
+ turnovers = [_parse_float(r.get("f6")) for r in index_rows]
3277
+ total, gainers = _movers("A", True)
3278
+ _, losers = _movers("A", False)
3279
+ return {
3280
+ "total_stocks": up + down + flat,
3281
+ "up_count": up,
3282
+ "down_count": down,
3283
+ "flat_count": flat,
3284
+ "limit_up_count": buckets.get(11, 0),
3285
+ "limit_down_count": buckets.get(-11, 0),
3286
+ "median_change_pct": _median_change("A", total, up, flat, up + down + flat),
3287
+ "total_turnover": round(sum(turnovers), 0) if turnovers and None not in turnovers else None,
3288
+ "top5_gainers": gainers,
3289
+ "top5_losers": losers,
3290
+ "limit_up_samples": _limit_samples("getTopicZTPool", "fbt:asc", trade_date),
3291
+ "limit_down_samples": _limit_samples("getTopicDTPool", "fund:asc", trade_date),
3292
+ }
3293
+
3294
+
3295
+ def _market_stats_clist(market: str) -> dict:
3296
+ """HK/US breadth: the up/down boundaries of the change-ranked list, found by binary search."""
3297
+ from concurrent.futures import ThreadPoolExecutor
3298
+
3299
+ total, gainers = _movers(market, True)
3300
+ if not total:
3301
+ raise ValueError(f"eastmoney {market} list unavailable")
3302
+ # The two binary searches (~15 sequential requests each) and the loser list are independent.
3303
+ with ThreadPoolExecutor(max_workers=3) as pool:
3304
+ losers_f = pool.submit(_movers, market, False)
3305
+ up_f, down_f = (pool.submit(_leading_count, market, total, rising) for rising in (True, False))
3306
+ _, losers = losers_f.result()
3307
+ up, down = up_f.result(), down_f.result()
3308
+ flat = total - up - down
3309
+ return {
3310
+ "total_stocks": total,
3311
+ "up_count": up,
3312
+ "down_count": down,
3313
+ "flat_count": flat,
3314
+ "median_change_pct": _median_change(market, total, up, flat, total),
3315
+ "top5_gainers": gainers,
3316
+ "top5_losers": losers,
3317
+ # No daily price limits; a turnover total would need every row.
3318
+ **dict.fromkeys(("limit_up_count", "limit_down_count", "limit_up_samples", "limit_down_samples")),
3319
+ "total_turnover": None,
3320
+ "market": market,
3321
+ "source": "eastmoney_rank",
3322
+ "turnover_currency": "HKD" if market == "HK" else "USD",
3323
+ }
3276
3324
 
3277
3325
 
3278
3326
  def cmd_market_stats(args):
3279
3327
  market = getattr(args, "market", "A")
3280
3328
  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
3329
+ with socket_timeout(25):
3330
+ stats = _market_stats_a() if market == "A" else _market_stats_clist(market)
3331
+ # The mean needs every stock's change; the key stays for the JSON contract.
3332
+ return _clean_row({"avg_change_pct": None, **stats})
3296
3333
  except Exception as e:
3297
3334
  return {"error": error_message(e)}
3298
3335
 
@@ -3636,122 +3673,43 @@ def cmd_hot_stocks(args):
3636
3673
 
3637
3674
  # --------------- margin_trading / northbound_flow (A-share only) ---------------
3638
3675
 
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",
3676
+ # Eastmoney per-stock margin detail (RPTA_WEB_RZRQ_GGMX) → output keys
3677
+ _MARGIN_COLUMNS = {
3678
+ "SCODE": "symbol",
3679
+ "SECNAME": "name",
3680
+ "RZYE": "margin_balance",
3681
+ "RZMRE": "margin_buy",
3682
+ "RZCHE": "margin_repay",
3683
+ "RQYL": "short_balance_shares",
3684
+ "RQMCL": "short_sell_shares",
3685
+ "RQCHL": "short_repay_shares",
3686
+ "RQYE": "short_balance",
3687
+ "RZRQYE": "total_balance",
3661
3688
  }
3662
3689
 
3663
3690
 
3664
3691
  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."""
3692
+ """Per-stock margin trading detail (融资融券) for the last N trading days, newest first.
3693
+
3694
+ One server-filtered eastmoney query returns exactly this stock's rows (两融明细 T 日晚间才发布 —
3695
+ a morning run ends at the previous day)."""
3677
3696
  if detect_market(args.symbol) != "A":
3678
3697
  return {"error": "margin_trading only available for A-shares"}
3679
3698
  days = getattr(args, "days", 10)
3680
3699
  if days < 1:
3681
3700
  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
3701
  days = min(days, 60)
3685
- code = _cn_code(args.symbol)
3686
- exchange, bare = code[:2], code[2:]
3702
+ bare = _cn_code(args.symbol)[2:]
3687
3703
  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)
3704
+ with socket_timeout(25):
3705
+ _, records = _em_datacenter("RPTA_WEB_RZRQ_GGMX", f'(SCODE="{bare}")', "DATE", days)
3706
+ rows = [
3707
+ _clean_row(
3708
+ {"date": str(r.get("DATE", ""))[:10]} | {key: r.get(col) for col, key in _MARGIN_COLUMNS.items()}
3709
+ )
3710
+ for r in records
3711
+ ]
3751
3712
  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
3713
  return {"symbol": args.symbol, "error": f"no margin trading data for {args.symbol} (可能不是两融标的)"}
3756
3714
  if getattr(args, "include_ratio", False):
3757
3715
  try:
@@ -3860,6 +3818,13 @@ def main():
3860
3818
  p_quote = sub.add_parser("quote")
3861
3819
  p_quote.add_argument("symbol")
3862
3820
 
3821
+ p_quotes = sub.add_parser("quotes")
3822
+ p_quotes.add_argument("symbols", help=f"comma-separated, at most {QUOTES_MAX}")
3823
+
3824
+ p_klines = sub.add_parser("klines")
3825
+ p_klines.add_argument("symbols", help=f"comma-separated, at most {QUOTES_MAX}; daily bars")
3826
+ p_klines.add_argument("--count", type=int, default=30)
3827
+
3863
3828
  p_fund_quote = sub.add_parser("fund_quote")
3864
3829
  p_fund_quote.add_argument("symbol")
3865
3830
 
@@ -3879,6 +3844,7 @@ def main():
3879
3844
 
3880
3845
  p_snap = sub.add_parser("market_snapshot")
3881
3846
  p_snap.add_argument("--market", default="A", choices=["A", "HK", "US"])
3847
+ p_snap.add_argument("--filters", default="{}", help="JSON screener filters; market_cap_min bounds the scan")
3882
3848
 
3883
3849
  p_idx = sub.add_parser("market_indices")
3884
3850
  p_idx.add_argument("--region", default="cn", choices=["cn", "hk", "us", "jp", "kr", "tw"])
@@ -3935,6 +3901,8 @@ def main():
3935
3901
  dispatch = {
3936
3902
  "kline": cmd_kline,
3937
3903
  "quote": cmd_quote,
3904
+ "quotes": cmd_quotes,
3905
+ "klines": cmd_klines,
3938
3906
  "fund_quote": cmd_fund_quote,
3939
3907
  "capital_flow": cmd_capital_flow,
3940
3908
  "news": cmd_news,
@@ -3961,6 +3929,10 @@ def main():
3961
3929
  output(result)
3962
3930
  if isinstance(result, dict) and "error" in result:
3963
3931
  sys.exit(1)
3932
+ if args.command in ("quotes", "klines"):
3933
+ # Timed-out daemon threads may hold stderr; normal shutdown would then abort and drop valid rows.
3934
+ sys.stdout.flush()
3935
+ os._exit(0)
3964
3936
 
3965
3937
 
3966
3938
  if __name__ == "__main__":