stock-analysis-plugin 0.2.11 → 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.
Files changed (54) hide show
  1. package/README.md +234 -382
  2. package/docs/mcp.md +60 -0
  3. package/hermes/__init__.py +35 -0
  4. package/hermes/plugin.yaml +132 -0
  5. package/hermes/schemas.py +6 -0
  6. package/hermes/tools.py +647 -0
  7. package/mcp_host/__init__.py +1 -0
  8. package/mcp_host/__main__.py +3 -0
  9. package/mcp_host/server.py +222 -0
  10. package/package.json +7 -1
  11. package/pi/index.ts +52 -2906
  12. package/scripts/cli-json.mjs +23 -0
  13. package/scripts/exec-process.mjs +89 -0
  14. package/scripts/setup-python.mjs +37 -4
  15. package/scripts/tool-calls.mjs +493 -0
  16. package/skills/bull-trend/SKILL.md +2 -2
  17. package/skills/bull-trend/references/fat-tails.md +76 -3
  18. package/skills/bull-trend/scripts/tail_analysis.py +92 -3
  19. package/skills/event-driven/SKILL.md +2 -2
  20. package/skills/growth-quality/SKILL.md +1 -1
  21. package/skills/market-review/SKILL.md +1 -0
  22. package/skills/stock-analysis/SKILL.md +12 -7
  23. package/skills/stock-analysis/references/stateless-analysis.md +9 -1
  24. package/skills/stock-screener/SKILL.md +1 -1
  25. package/skills/strategy-backtest/SKILL.md +1 -1
  26. package/tools/_cache.py +13 -0
  27. package/tools/_subproc.py +46 -0
  28. package/tools/alert_rules.py +2 -2
  29. package/tools/backtest.py +3 -3
  30. package/tools/capabilities.py +33 -9
  31. package/tools/cli_budgets.json +1 -0
  32. package/tools/credit_data.py +293 -0
  33. package/tools/diagnostics.py +3 -4
  34. package/tools/gather.py +2 -6
  35. package/tools/import_parser.py +3 -4
  36. package/tools/lixinger.py +119 -27
  37. package/tools/market_regime.py +5 -2
  38. package/tools/market_review.py +5 -6
  39. package/tools/name_resolver.py +45 -33
  40. package/tools/report_renderer.py +3 -3
  41. package/tools/requirements.txt +5 -4
  42. package/tools/risk_screening.py +3 -2
  43. package/tools/screener.py +41 -30
  44. package/tools/search_intel.py +79 -10
  45. package/tools/sec_edgar.py +268 -1
  46. package/tools/sec_holders.py +6 -0
  47. package/tools/signal_tracker.py +5 -5
  48. package/tools/stateless_analysis.py +132 -19
  49. package/tools/stock_data.py +1020 -817
  50. package/tools/tiaa_disclosures.py +4 -0
  51. package/tools/tool_schemas.json +2734 -0
  52. package/tools/trading_calendar.py +65 -30
  53. package/tools/value_data.py +423 -175
  54. package/tools/watchlist.py +43 -5
@@ -3,27 +3,29 @@
3
3
 
4
4
  import argparse
5
5
  import contextlib
6
- import hashlib
7
6
  import json
8
7
  import math
9
8
  import os
10
9
  import re
11
10
  import sys
12
- import tempfile
11
+ import threading
13
12
  import time
14
13
  from datetime import date, datetime, timedelta, timezone
15
14
  from pathlib import Path
16
15
  from zoneinfo import ZoneInfo
17
16
 
18
17
  sys.path.insert(0, str(Path(__file__).resolve().parent))
19
- from _cache import DAY, WEEK, atomic_write, cached # noqa: E402
20
- from _subproc import run_tool, scrub_secrets, socket_timeout, utf8_stdio # noqa: E402
18
+ from _cache import CACHE_DIR, DAY, WEEK, atomic_write, cached, cached_values # noqa: E402
19
+ from _subproc import error_message, run_tool, scrub_secrets, socket_timeout, utf8_stdio # noqa: E402
20
+ from trading_calendar import market_now # noqa: E402
21
21
 
22
22
  # Explicitly exchange-prefixed A-share codes (sh600519 / sz000858 / bj920001).
23
23
  # Bare 6-digit codes are always stocks; the prefix is the disambiguation escape
24
24
  # hatch for index codes (sh000001 = 上证指数, not 平安银行).
25
25
  _A_PREFIXED_RE = re.compile(r"(?:sh|sz|bj)\d{6}", re.IGNORECASE)
26
26
  _A_INDEX_RE = re.compile(r"(?:sh000\d{3}|sz399\d{3})", re.IGNORECASE)
27
+ # Yahoo has no Hang Seng Tech index; Tencent serves its live quote and daily history.
28
+ _TENCENT_HK_INDEX = {"^HSTECH": "hkHSTECH"}
27
29
 
28
30
 
29
31
  def detect_market(symbol: str) -> str:
@@ -88,7 +90,6 @@ def calc_limit_price(pre_close: float, ratio: float, direction: str = "up") -> f
88
90
  _STICKY_CHAINS = {
89
91
  "kline_a",
90
92
  "quote_a",
91
- "quote_a_etf",
92
93
  "snapshot_a",
93
94
  "sector_constituents",
94
95
  "sina",
@@ -108,16 +109,19 @@ def _failover(sources: list, label: str, evidence: bool = False):
108
109
  entry costs one failed attempt, then the new winner replaces it (self-healing).
109
110
  """
110
111
  chain = label.split(":", 1)[0]
112
+ sticky = None
111
113
  if chain in _STICKY_CHAINS:
112
114
  sticky = _disk_cache_get(f"sticky-{chain}")
113
115
  if sticky:
114
- sources = sorted(sources, key=lambda s: s[0] != sticky)
116
+ # Single-symbol A quotes always try Tencent first; sticky only reorders the fallbacks behind it.
117
+ first = "tencent" if chain == "quote_a" else sticky
118
+ sources = sorted(sources, key=lambda s: (s[0] != first, s[0] != sticky))
115
119
  errors = []
116
120
  for name, fn in sources:
117
121
  try:
118
122
  result = fn()
119
123
  if result:
120
- if chain in _STICKY_CHAINS:
124
+ if chain in _STICKY_CHAINS and name != sticky:
121
125
  _disk_cache_set(f"sticky-{chain}", name)
122
126
  if evidence:
123
127
  fetched_at = datetime.now(timezone.utc).isoformat()
@@ -218,6 +222,9 @@ def _kline_efinance(symbol: str, period: str, count: int) -> list:
218
222
  return [_clean_row(r) for r in df.to_dict("records")]
219
223
 
220
224
 
225
+ _BAOSTOCK_LOCK = threading.Lock()
226
+
227
+
221
228
  def _kline_baostock(symbol: str, period: str, count: int) -> list:
222
229
  import io
223
230
 
@@ -225,28 +232,30 @@ def _kline_baostock(symbol: str, period: str, count: int) -> list:
225
232
  import pandas as pd
226
233
 
227
234
  freq_map = {"daily": "d", "weekly": "w", "monthly": "m"}
228
- end = datetime.now()
235
+ end = _cn_now()
229
236
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
230
237
  # baostock login/logout print status lines to stdout — silence them so the
231
238
  # tool's JSON output on stdout stays clean for the calling host.
232
- with contextlib.redirect_stdout(io.StringIO()):
233
- bs.login()
234
- try:
235
- rs = bs.query_history_k_data_plus(
236
- _to_baostock_code(symbol),
237
- "date,open,high,low,close,volume,amount,pctChg",
238
- start_date=start.strftime("%Y-%m-%d"),
239
- end_date=end.strftime("%Y-%m-%d"),
240
- frequency=freq_map.get(period, "d"),
241
- adjustflag="2",
242
- )
243
- rows = []
244
- while (rs.error_code == "0") and rs.next():
245
- rows.append(rs.get_row_data())
246
- df = pd.DataFrame(rows, columns=rs.fields)
247
- finally:
239
+ # One global baostock session per process: `klines` threads must not interleave login/query/logout.
240
+ with _BAOSTOCK_LOCK:
248
241
  with contextlib.redirect_stdout(io.StringIO()):
249
- 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()
250
259
  if df.empty:
251
260
  raise ValueError("baostock returned empty data")
252
261
  col_map = {"amount": "turnover", "pctChg": "change_pct"}
@@ -260,40 +269,6 @@ def _kline_baostock(symbol: str, period: str, count: int) -> list:
260
269
  return [_clean_row(r) for r in df.to_dict("records")]
261
270
 
262
271
 
263
- def _quote_efinance(symbol: str) -> dict:
264
- import efinance as ef
265
-
266
- df = ef.stock.get_realtime_quotes([symbol])
267
- if df is None or df.empty:
268
- raise ValueError("efinance quote returned empty")
269
- r = df.iloc[0]
270
- col_map = {
271
- "股票代码": "symbol",
272
- "股票名称": "name",
273
- "最新价": "price",
274
- "涨跌额": "change",
275
- "涨跌幅": "change_pct",
276
- "成交量": "volume",
277
- "成交额": "turnover",
278
- "最高": "high",
279
- "最低": "low",
280
- "今开": "open",
281
- "昨收": "prev_close",
282
- "总市值": "market_cap",
283
- "市盈率": "pe",
284
- "换手率": "turnover_rate",
285
- }
286
- result = {}
287
- for cn_key, en_key in col_map.items():
288
- val = r.get(cn_key)
289
- if val is not None and val != "":
290
- with contextlib.suppress(ValueError, TypeError):
291
- val = float(val) if en_key not in ("symbol", "name") else val
292
- result[en_key] = val
293
- result["pe_basis"] = "dynamic"
294
- return _clean_row(result)
295
-
296
-
297
272
  # --------------- tencent / sina (non-eastmoney A-share fallbacks, issue #25) ---------------
298
273
  #
299
274
  # akshare/efinance both resolve to eastmoney hosts, so an eastmoney-blocked network
@@ -331,20 +306,25 @@ def _unix_quote_time(value) -> str | None:
331
306
  return None
332
307
 
333
308
 
334
- def _quote_time(value: str, fmt: str) -> str | None:
309
+ def _quote_time(value: str, fmt: str, tz=timezone(timedelta(hours=8))) -> str | None:
335
310
  try:
336
- return datetime.strptime(value, fmt).replace(tzinfo=timezone(timedelta(hours=8))).isoformat()
311
+ return datetime.strptime(value, fmt).replace(tzinfo=tz).isoformat()
337
312
  except ValueError:
338
313
  return None
339
314
 
340
315
 
341
316
  def _tencent_payload_row(f: list, symbol: str) -> dict:
342
317
  """Build the quote row from one Tencent `~`-separated payload (already split).
343
- Shared by _quote_tencent (single) and _tencent_batch_quotes (snapshot leg)."""
318
+ Used by _quote_tencent and the cn-indices batch fallback."""
344
319
  f += [""] * (50 - len(f)) # pad optional tail fields (pe/pb/market_cap/volume_ratio…) on short payloads
345
- hk = detect_market(symbol) == "HK"
320
+ market = detect_market(symbol)
321
+ foreign = market in ("HK", "US") # HK and US share currency-unit turnover and lack the A-share ratio fields
346
322
  turnover = _parse_float(f[37])
347
323
  market_cap = _parse_float(f[45])
324
+ if market == "US":
325
+ as_of = _quote_time(f[30], "%Y-%m-%d %H:%M:%S", ZoneInfo("America/New_York"))
326
+ else:
327
+ as_of = _quote_time(f[30], "%Y/%m/%d %H:%M:%S" if foreign else "%Y%m%d%H%M%S")
348
328
  return {
349
329
  "symbol": symbol,
350
330
  "name": f[1],
@@ -352,7 +332,7 @@ def _tencent_payload_row(f: list, symbol: str) -> dict:
352
332
  "change": _parse_float(f[31]),
353
333
  "change_pct": _parse_float(f[32]),
354
334
  "volume": _parse_float(f[6]), # A: 手; HK: shares, same as yfinance
355
- "turnover": turnover * (1 if hk else 1e4) if turnover is not None else None,
335
+ "turnover": turnover * (1 if foreign else 1e4) if turnover is not None else None,
356
336
  "high": _parse_float(f[33]),
357
337
  "low": _parse_float(f[34]),
358
338
  "open": _parse_float(f[5]),
@@ -360,50 +340,94 @@ def _tencent_payload_row(f: list, symbol: str) -> dict:
360
340
  "market_cap": market_cap * 1e8 if market_cap is not None else None,
361
341
  "pe": _parse_float(f[39]),
362
342
  "pe_basis": "unknown",
363
- # Tencent's HK map labels f[47] DY and f[49] WK52_Low; neither is an A-share ratio.
364
- "pb": None if hk else _parse_float(f[46]),
365
- "turnover_rate": None if hk else _parse_float(f[38]),
366
- "volume_ratio": None if hk else _parse_float(f[49]),
343
+ # Tencent's HK map labels f[47] DY and f[49] WK52_Low, and US f[46] is the English name.
344
+ "pb": None if foreign else _parse_float(f[46]),
345
+ "turnover_rate": None if foreign else _parse_float(f[38]),
346
+ "volume_ratio": None if foreign else _parse_float(f[49]),
367
347
  "amplitude": _parse_float(f[43]),
368
- "data_as_of": _quote_time(f[30], "%Y/%m/%d %H:%M:%S" if hk else "%Y%m%d%H%M%S"),
348
+ "data_as_of": as_of,
369
349
  }
370
350
 
371
351
 
372
- def _quote_tencent(symbol: str) -> dict:
373
- """Tencent A/HK quote via qt.gtimg.cn, with a GBK-encoded `~` payload.
352
+ def _tencent_hk_code(symbol: str) -> str:
353
+ if not re.fullmatch(r"[0-9]{1,5}\.HK", symbol, re.IGNORECASE):
354
+ raise ValueError(f"invalid HK symbol: {symbol}")
355
+ return f"hk{symbol[:-3].zfill(5)}"
356
+
357
+
358
+ def _tencent_us_code(symbol: str) -> str:
359
+ """Tencent spells class shares with a dot (BRK.B) where Yahoo uses a dash (BRK-B)."""
360
+ if not re.fullmatch(r"[A-Za-z]{1,6}(?:[.-][A-Za-z])?", symbol):
361
+ raise ValueError(f"invalid US symbol: {symbol}")
362
+ return "us" + symbol.upper().replace("-", ".")
374
363
 
375
- A-share turnover is 万元; HK turnover is already HKD. Market cap is 亿元
376
- (CNY/HKD respectively); normalize monetary fields to currency units."""
364
+
365
+ def _tencent_fields(code: str) -> list:
377
366
  import requests
378
367
 
379
- hk = detect_market(symbol) == "HK"
380
- if hk and not re.fullmatch(r"[0-9]{1,5}\.HK", symbol, re.IGNORECASE):
381
- raise ValueError(f"invalid HK symbol: {symbol}")
382
- # Tencent's hk feed is delayed 15 minutes; r_hk uses the same fields for live quotes.
383
- code = f"r_hk{symbol[:-3].zfill(5)}" if hk else _cn_code(symbol)
384
368
  resp = requests.get(f"https://qt.gtimg.cn/q={code}", timeout=10)
385
369
  resp.raise_for_status()
386
- f = _gbk_var_payload(resp.content).split("~")
370
+ return _gbk_var_payload(resp.content).split("~")
371
+
372
+
373
+ def _quote_tencent(symbol: str) -> dict:
374
+ """Tencent A/HK/US quote via qt.gtimg.cn, with a GBK-encoded `~` payload.
375
+
376
+ A-share turnover is 万元; HK/US turnover is already HKD/USD. Market cap is 亿
377
+ in the quote currency; normalize monetary fields to currency units."""
378
+ market = detect_market(symbol)
379
+ if market == "HK":
380
+ # Tencent's hk feed is delayed 15 minutes; r_hk uses the same fields for live quotes.
381
+ code = f"r_{_tencent_hk_code(symbol)}"
382
+ elif market == "US":
383
+ code = _tencent_us_code(symbol)
384
+ else:
385
+ code = _cn_code(symbol)
386
+ f = _tencent_fields(code)
387
387
  if len(f) < 35 or not f[1]:
388
388
  raise ValueError(f"tencent returned no quote for {symbol}")
389
389
  row = _clean_row(_tencent_payload_row(f, symbol))
390
- if hk and (row["price"] is None or row["price"] <= 0):
390
+ if market != "A" and (row["price"] is None or row["price"] <= 0):
391
391
  raise ValueError(f"tencent returned no quote for {symbol}")
392
392
  if normalize_stock_code(symbol)["is_etf"]:
393
- row["is_etf"] = True
393
+ # f[73] is total fund shares and f[78] the intraday IOPV (blank for money/bond ETFs);
394
+ # premium matches _quote_eastmoney.
395
+ iopv = _parse_float(f[78]) if len(f) > 78 else None
396
+ price = row.get("price")
397
+ row.update(
398
+ is_etf=True,
399
+ fund_shares=_parse_float(f[73]) if len(f) > 73 else None,
400
+ iopv=iopv,
401
+ premium_discount_rate=round((price - iopv) / iopv * 100, 4) if price is not None and iopv else None,
402
+ )
394
403
  return row
395
404
 
396
405
 
397
406
  def _kline_tencent(symbol: str, period: str, count: int) -> list:
398
407
  """Tencent kline via web.ifzq.gtimg.cn. Rows are [date, open, close, high, low,
399
- volume(手)] ascending; raw series are accepted only for explicit indices."""
408
+ volume] ascending (A: 手, HK/US: shares); raw series are accepted only for explicit indices.
409
+ HK/US stocks need the hk/us endpoints: the generic one ignores qfq for them."""
400
410
  import requests
401
411
 
402
412
  p_map = {"daily": "day", "weekly": "week", "monthly": "month"}
403
413
  p = p_map.get(period, "day")
404
- code = _cn_code(symbol)
414
+ market = detect_market(symbol)
415
+ hk_index = symbol.upper() in _TENCENT_HK_INDEX
416
+ endpoint = "fqkline"
417
+ if hk_index:
418
+ code = _TENCENT_HK_INDEX[symbol.upper()]
419
+ elif market == "HK":
420
+ endpoint, code = "hkfqkline", _tencent_hk_code(symbol)
421
+ elif market == "US":
422
+ # usfqkline only adjusts the exchange-suffixed code (AAPL.OQ) that the quote API reports.
423
+ f = _tencent_fields(_tencent_us_code(symbol))
424
+ if len(f) < 3 or not f[2]:
425
+ raise ValueError(f"tencent returned no quote for {symbol}")
426
+ endpoint, code = "usfqkline", f"us{f[2]}"
427
+ else:
428
+ code = _cn_code(symbol)
405
429
  resp = requests.get(
406
- "https://web.ifzq.gtimg.cn/appstock/app/fqkline/get",
430
+ f"https://web.ifzq.gtimg.cn/appstock/app/{endpoint}/get",
407
431
  params={"param": f"{code},{p},,,{count},qfq"},
408
432
  timeout=10,
409
433
  )
@@ -411,9 +435,11 @@ def _kline_tencent(symbol: str, period: str, count: int) -> list:
411
435
  if data.get("code") != 0:
412
436
  raise ValueError(f"tencent kline failed: {data.get('msg', '')}")
413
437
  node = data.get("data", {}).get(code, {})
414
- rows = node.get(p) if _A_INDEX_RE.fullmatch(symbol) else node.get(f"qfq{p}")
438
+ raw = _A_INDEX_RE.fullmatch(symbol) or hk_index
439
+ rows = node.get(p) if raw else node.get(f"qfq{p}")
415
440
  if not rows:
416
441
  raise ValueError("tencent kline returned empty data for required adjustment")
442
+ extra = {} if market == "A" else {"adjusted": "none" if raw else "qfq"}
417
443
  return [
418
444
  _clean_row(
419
445
  {
@@ -423,6 +449,7 @@ def _kline_tencent(symbol: str, period: str, count: int) -> list:
423
449
  "low": _parse_float(r[4]),
424
450
  "close": _parse_float(r[2]),
425
451
  "volume": _parse_float(r[5]),
452
+ **extra,
426
453
  }
427
454
  )
428
455
  for r in rows[-count:]
@@ -520,6 +547,10 @@ def _sanitize(obj):
520
547
  return obj
521
548
 
522
549
 
550
+ def _cn_now() -> datetime:
551
+ return market_now("CN")
552
+
553
+
523
554
  def _clean_row(d: dict) -> dict:
524
555
  return {k: _sanitize(v) for k, v in d.items()}
525
556
 
@@ -554,11 +585,16 @@ def _kline_akshare(symbol: str, period: str, count: int) -> list:
554
585
  import akshare as ak
555
586
 
556
587
  period_map = {"daily": "daily", "weekly": "weekly", "monthly": "monthly"}
557
- end = datetime.now()
588
+ end = _cn_now()
558
589
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
559
590
 
560
- # ETFs (51/52/56/58/15/16/18xxxx) are not covered by stock_zh_a_hist
561
- hist_fn = ak.fund_etf_hist_em if normalize_stock_code(symbol)["is_etf"] else ak.stock_zh_a_hist
591
+ # ETFs (51/52/56/58/15/16/18xxxx) are not covered by stock_zh_a_hist.
592
+ # akshare 1.20.44-1.20.46 renamed several APIs; py3.10 stays below 1.20.40 with the old names.
593
+ hist_fn = (
594
+ ak.fund_etf_hist_em
595
+ if normalize_stock_code(symbol)["is_etf"]
596
+ else getattr(ak, "stock_zh_a_hist_em", None) or ak.stock_zh_a_hist
597
+ )
562
598
  df = _akshare_retry(
563
599
  hist_fn,
564
600
  symbol=symbol,
@@ -603,7 +639,7 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
603
639
 
604
640
  if normalize_stock_code(symbol)["is_etf"]:
605
641
  raise ValueError("tushare stock adjustment does not cover ETFs")
606
- end = datetime.now()
642
+ end = _cn_now()
607
643
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
608
644
 
609
645
  ts_code = f"{symbol}.{_cn_code(symbol)[:2].upper()}"
@@ -718,69 +754,6 @@ def _quote_tushare(symbol: str) -> dict:
718
754
  )
719
755
 
720
756
 
721
- # Public TDX quote servers, tried in order — a datacenter IP often gets only
722
- # some of them refused, so a single hardcoded endpoint is a needless failure mode.
723
- _TDX_SERVERS = [
724
- ("119.147.212.81", 7709), # Tencent, Shenzhen
725
- ("180.153.18.170", 7709), # Shanghai
726
- ("202.108.253.130", 7709), # Beijing
727
- ("59.173.18.69", 7709), # Wuhan
728
- ]
729
-
730
-
731
- def _pytdx_connect(api) -> None:
732
- """Connect to the first reachable TDX server.
733
-
734
- pytdx's connect() returns True/False instead of raising (and is not a context
735
- manager), so wrap it here to keep a failed connection from surfacing as a
736
- misleading TypeError downstream.
737
- """
738
- errors = []
739
- for ip, port in _TDX_SERVERS:
740
- try:
741
- if api.connect(ip, port):
742
- return
743
- errors.append(f"{ip}:{port} refused")
744
- except Exception as e:
745
- errors.append(f"{ip}:{port} {e}")
746
- raise ConnectionError(f"pytdx failed to connect to any TDX server ({'; '.join(errors)})")
747
-
748
-
749
- def _quote_pytdx(symbol: str) -> dict:
750
- """pytdx realtime quote from TDX market servers."""
751
- from pytdx.hq import TdxHq_API
752
-
753
- market = 1 if symbol.startswith(("6", "9", "5")) else 0
754
-
755
- api = TdxHq_API()
756
- try:
757
- _pytdx_connect(api)
758
- data = api.get_security_quotes([(market, symbol)])
759
- finally:
760
- api.disconnect()
761
-
762
- if not data:
763
- raise ValueError("pytdx quote returned empty")
764
-
765
- q = data[0]
766
- prev_close = q.get("last_close")
767
- price = q.get("price")
768
- return _clean_row(
769
- {
770
- "symbol": symbol,
771
- "name": q.get("name", ""),
772
- "price": price,
773
- "change": (price - prev_close) if price and prev_close else None,
774
- "change_pct": ((price / prev_close) - 1) * 100 if price and prev_close else None,
775
- "volume": q.get("vol"),
776
- "high": q.get("high"),
777
- "low": q.get("low"),
778
- "open": q.get("open"),
779
- "prev_close": prev_close,
780
- }
781
- )
782
-
783
-
784
757
  def _kline_yfinance(symbol: str, period: str, count: int) -> list:
785
758
  import yfinance as yf
786
759
 
@@ -1028,25 +1001,38 @@ def _quote_alphavantage(symbol: str) -> dict:
1028
1001
  )
1029
1002
 
1030
1003
 
1031
- def _yf_failover(symbol: str, yfinance, finnhub, longbridge, alphavantage, label: str, evidence: bool = False):
1032
- """Shared HK/US/JP/KR/TW failover chain: yfinance → finnhub/longbridge (HK/US) → alphavantage (US only).
1004
+ def _yf_failover(symbol: str, yfinance, tencent, finnhub, longbridge, alphavantage, label: str, evidence: bool = False):
1005
+ """Shared HK/US/JP/KR/TW failover chain. HK: yfinance → tencent → finnhub → longbridge;
1006
+ US: yfinance → finnhub → longbridge → tencent → alphavantage; JP/KR/TW: yfinance only.
1033
1007
 
1008
+ Keyless Tencent backs up HK first (finnhub's free tier has no HK). For US, configured
1009
+ keyed sources go first and Tencent precedes alphavantage to spare its 25 calls/day.
1034
1010
  Each provider is a zero-arg callable; callers bind their own symbol/period/count.
1035
1011
  """
1036
1012
  market = detect_market(symbol)
1037
1013
  sources = [("yfinance", yfinance)]
1038
- if market in ("HK", "US"):
1039
- sources += [("finnhub", finnhub), ("longbridge", longbridge)]
1040
- if market == "US":
1041
- sources.append(("alphavantage", alphavantage))
1014
+ if market == "HK":
1015
+ sources += [("tencent", tencent), ("finnhub", finnhub), ("longbridge", longbridge)]
1016
+ elif market == "US":
1017
+ sources += [
1018
+ ("finnhub", finnhub),
1019
+ ("longbridge", longbridge),
1020
+ ("tencent", tencent),
1021
+ ("alphavantage", alphavantage),
1022
+ ]
1042
1023
  return _failover(sources, label=label, evidence=evidence)
1043
1024
 
1044
1025
 
1045
1026
  def kline_yf(symbol: str, period: str, count: int, evidence: bool = False) -> list:
1046
- """HK/US/JP/KR/TW kline. Failover: yfinance → finnhub/longbridge (HK/US) → alphavantage (US only)."""
1027
+ """HK/US/JP/KR/TW kline; see _yf_failover for the chain."""
1028
+ if symbol.upper() in _TENCENT_HK_INDEX:
1029
+ with socket_timeout(25):
1030
+ sources = [("tencent", lambda: _kline_tencent(symbol, period, count))]
1031
+ return _failover(sources, label=f"kline_hk_index:{symbol}", evidence=evidence)
1047
1032
  return _yf_failover(
1048
1033
  symbol,
1049
1034
  lambda: _kline_yfinance(symbol, period, count),
1035
+ lambda: _kline_tencent(symbol, period, count),
1050
1036
  lambda: _kline_finnhub(symbol, period, count),
1051
1037
  lambda: _kline_longbridge(symbol, period, count),
1052
1038
  lambda: _kline_alphavantage(symbol, period, count),
@@ -1057,124 +1043,165 @@ def kline_yf(symbol: str, period: str, count: int, evidence: bool = False) -> li
1057
1043
 
1058
1044
  def cmd_kline(args):
1059
1045
  market = detect_market(args.symbol)
1060
- 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():
1061
1050
  if market == "A":
1062
- data = kline_a(args.symbol, args.period, args.count, evidence=True)
1051
+ data = kline_a(args.symbol, args.period, bucket, evidence=True)
1063
1052
  if data:
1064
1053
  adjusted = "none" if _A_INDEX_RE.fullmatch(args.symbol) else "qfq"
1065
1054
  data = [dict(row, adjusted=adjusted) for row in data]
1066
- else:
1067
- data = kline_yf(args.symbol, args.period, args.count, evidence=True)
1068
- 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
1069
1061
  except Exception as e:
1070
- return {"error": str(e)}
1062
+ return {"error": error_message(e)}
1071
1063
 
1072
1064
 
1073
1065
  # --------------- quote ---------------
1074
1066
 
1075
1067
 
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
+
1080
+
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
+
1076
1183
  def quote_a(symbol: str, evidence: bool = False) -> dict:
1077
- """A-share quote with failover: akshare → tushare → efinance → tencent → sina → pytdx. ETFs try fund spot first.
1184
+ """A-share quote with failover: tencent → eastmoney → tushare → sina, one code per request.
1078
1185
 
1079
1186
  Explicitly-prefixed symbols (sh000001, typically indices) go straight to
1080
- tencent/sina — the eastmoney sources take bare 6-digit codes only, and pytdx
1081
- can't place prefixed codes either. ETF quotes use their own sticky chain key
1082
- (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."""
1083
1189
  if _A_PREFIXED_RE.fullmatch(symbol):
1084
1190
  sources = [("tencent", lambda: _quote_tencent(symbol)), ("sina", lambda: _quote_sina(symbol))]
1085
1191
  with socket_timeout(25):
1086
1192
  return _failover(sources, label=f"quote_a:{symbol}", evidence=evidence)
1087
1193
  is_etf = normalize_stock_code(symbol)["is_etf"]
1088
- sources = []
1089
- if is_etf:
1090
- sources.append(("akshare_etf", lambda: _quote_akshare_etf(symbol)))
1091
- sources += [
1092
- ("akshare", lambda: _quote_akshare(symbol)),
1093
- ("tushare", lambda: _quote_tushare(symbol)),
1094
- ("efinance", lambda: _quote_efinance(symbol)),
1194
+ sources = [
1095
1195
  ("tencent", lambda: _quote_tencent(symbol)),
1196
+ ("eastmoney", lambda: _quote_eastmoney(symbol)),
1197
+ ("tushare", lambda: _quote_tushare(symbol)),
1096
1198
  ("sina", lambda: _quote_sina(symbol)),
1097
- ("pytdx", lambda: _quote_pytdx(symbol)),
1098
1199
  ]
1099
1200
  chain = "quote_a_etf" if is_etf else "quote_a"
1100
1201
  with socket_timeout(25):
1101
1202
  return _failover(sources, label=f"{chain}:{symbol}", evidence=evidence)
1102
1203
 
1103
1204
 
1104
- def _quote_akshare_etf(symbol: str) -> dict:
1105
- """A-share ETF realtime quote via akshare fund_etf_spot_em.
1106
-
1107
- On top of the stock-shaped fields, the same spot row carries fund-native
1108
- columns: iopv (盘中实时估值) and fund_shares (最新份额). premium_discount_rate
1109
- is derived here as (price - iopv) / iopv * 100 — positive = 溢价 (price above
1110
- IOPV), negative = 折价; None when the fund publishes no intraday IOPV (money/
1111
- bond ETFs)."""
1112
- import akshare as ak
1113
-
1114
- df = _akshare_retry(ak.fund_etf_spot_em)
1115
- row = df[df["代码"] == symbol]
1116
- if row.empty:
1117
- raise ValueError(f"ETF {symbol} not found in akshare fund spot")
1118
- r = row.iloc[0]
1119
- result = _clean_row(
1120
- {
1121
- "symbol": symbol,
1122
- "name": r.get("名称"),
1123
- "price": r.get("最新价"),
1124
- "change": r.get("涨跌额"),
1125
- "change_pct": r.get("涨跌幅"),
1126
- "volume": r.get("成交量"),
1127
- "turnover": r.get("成交额"),
1128
- "high": r.get("最高价"),
1129
- "low": r.get("最低价"),
1130
- "open": r.get("开盘价"),
1131
- "prev_close": r.get("昨收"),
1132
- "market_cap": r.get("总市值"),
1133
- "turnover_rate": r.get("换手率"),
1134
- "volume_ratio": r.get("量比"),
1135
- "is_etf": True,
1136
- "iopv": r.get("IOPV实时估值"),
1137
- "fund_shares": r.get("最新份额"),
1138
- }
1139
- )
1140
- price, iopv = result.get("price"), result.get("iopv")
1141
- result["premium_discount_rate"] = round((price - iopv) / iopv * 100, 4) if price is not None and iopv else None
1142
- return result
1143
-
1144
-
1145
- def _quote_akshare(symbol: str) -> dict:
1146
- import akshare as ak
1147
-
1148
- df = _akshare_retry(ak.stock_zh_a_spot_em)
1149
- row = df[df["代码"] == symbol]
1150
- if row.empty:
1151
- raise ValueError(f"Symbol {symbol} not found in akshare")
1152
- r = row.iloc[0]
1153
- return _clean_row(
1154
- {
1155
- "symbol": symbol,
1156
- "name": r.get("名称"),
1157
- "price": r.get("最新价"),
1158
- "change": r.get("涨跌额"),
1159
- "change_pct": r.get("涨跌幅"),
1160
- "volume": r.get("成交量"),
1161
- "turnover": r.get("成交额"),
1162
- "high": r.get("最高"),
1163
- "low": r.get("最低"),
1164
- "open": r.get("今开"),
1165
- "prev_close": r.get("昨收"),
1166
- "market_cap": r.get("总市值"),
1167
- "pe": r.get("市盈率-动态"),
1168
- "pe_basis": "dynamic",
1169
- "pb": r.get("市净率"),
1170
- "turnover_rate": r.get("换手率"),
1171
- "volume_ratio": r.get("量比"),
1172
- "amplitude": r.get("振幅"),
1173
- "change_pct_60d": r.get("60日涨跌幅"),
1174
- }
1175
- )
1176
-
1177
-
1178
1205
  def _quote_yfinance(symbol: str) -> dict:
1179
1206
  import yfinance as yf
1180
1207
 
@@ -1231,21 +1258,14 @@ def _quote_finnhub(symbol: str) -> dict:
1231
1258
 
1232
1259
 
1233
1260
  def quote_yf(symbol: str, evidence: bool = False) -> dict:
1234
- """HK adds Tencent after yfinance; US/JP/KR/TW keep their existing failover chains."""
1235
- if detect_market(symbol) == "HK":
1236
- return _failover(
1237
- [
1238
- ("yfinance", lambda: _quote_yfinance(symbol)),
1239
- ("tencent", lambda: _quote_tencent(symbol)),
1240
- ("finnhub", lambda: _quote_finnhub(symbol)),
1241
- ("longbridge", lambda: _quote_longbridge(symbol)),
1242
- ],
1243
- label=f"quote:{symbol}",
1244
- evidence=evidence,
1245
- )
1261
+ """HK/US/JP/KR/TW quote; see _yf_failover for the chain."""
1262
+ if symbol.upper() in _TENCENT_HK_INDEX:
1263
+ sources = [("tencent", lambda: {"symbol": symbol, **_quote_tencent_hk_index(symbol.upper())})]
1264
+ return _failover(sources, label=f"quote_hk_index:{symbol}", evidence=evidence)
1246
1265
  return _yf_failover(
1247
1266
  symbol,
1248
1267
  lambda: _quote_yfinance(symbol),
1268
+ lambda: _quote_tencent(symbol),
1249
1269
  lambda: _quote_finnhub(symbol),
1250
1270
  lambda: _quote_longbridge(symbol),
1251
1271
  lambda: _quote_alphavantage(symbol),
@@ -1392,11 +1412,15 @@ def _quote_tiaa(symbol: str) -> dict:
1392
1412
  def cmd_quote(args):
1393
1413
  if re.fullmatch(r"TIAA\d{6}", args.symbol, re.IGNORECASE):
1394
1414
  return _quote_tiaa(args.symbol.upper())
1395
- market = detect_market(args.symbol)
1415
+ # detect_market reads every ^index as US; known region indices keep their own calendar for freshness.
1416
+ market = next(
1417
+ (region.upper() for region, symbols in _REGION_INDEX_SYMBOLS.items() if args.symbol.upper() in symbols),
1418
+ None,
1419
+ ) or detect_market(args.symbol)
1396
1420
  try:
1397
1421
  result = quote_a(args.symbol, evidence=True) if market == "A" else quote_yf(args.symbol, evidence=True)
1398
1422
  except Exception as e:
1399
- result = {"error": str(e)}
1423
+ result = {"error": error_message(e)}
1400
1424
  # Exchange quotes cannot see open-end funds. Stock-only prefixes (SH 60/68, ChiNext 30, BSE) keep the bare error.
1401
1425
  if (
1402
1426
  isinstance(result, dict)
@@ -1438,6 +1462,48 @@ def cmd_quote(args):
1438
1462
  return result
1439
1463
 
1440
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
+
1441
1507
  # --------------- open-end fund quote ---------------
1442
1508
 
1443
1509
 
@@ -1455,30 +1521,6 @@ def _fund_nav_date(stamp) -> str:
1455
1521
  return datetime.fromtimestamp(stamp / 1000, timezone.utc).astimezone(ZoneInfo("Asia/Shanghai")).date().isoformat()
1456
1522
 
1457
1523
 
1458
- def _fund_estimate(symbol: str) -> dict:
1459
- import akshare as ak
1460
-
1461
- with socket_timeout(25):
1462
- df = ak.fund_value_estimation_em()
1463
- if df is None or df.empty or "基金代码" not in df.columns:
1464
- return {}
1465
- rows = df[df["基金代码"] == symbol]
1466
- value_col = next((c for c in df.columns if c.endswith("-估算数据-估算值")), None)
1467
- change_col = next((c for c in df.columns if c.endswith("-估算数据-估算增长率")), None)
1468
- if rows.empty or not value_col:
1469
- return {}
1470
- row = rows.iloc[0]
1471
- estimated_nav = _parse_float(row.get(value_col))
1472
- if estimated_nav is None:
1473
- return {}
1474
- return {
1475
- "estimated_nav": estimated_nav,
1476
- "estimated_change_pct": _parse_float(str(row.get(change_col)).rstrip("%")) if change_col else None,
1477
- "estimated_at": value_col.split("-估算数据-", 1)[0],
1478
- "estimate_status": "available",
1479
- }
1480
-
1481
-
1482
1524
  def cmd_fund_quote(args):
1483
1525
  symbol = args.symbol
1484
1526
  if not re.fullmatch(r"\d{6}", symbol):
@@ -1499,18 +1541,7 @@ def cmd_fund_quote(args):
1499
1541
  latest = nav_rows[-1]
1500
1542
  nav_date = _fund_nav_date(latest["x"])
1501
1543
  accumulated_by_date = {_fund_nav_date(row[0]): row[1] for row in accumulated_rows}
1502
- today = datetime.now(ZoneInfo("Asia/Shanghai")).date().isoformat()
1503
- estimate = {
1504
- "estimated_nav": None,
1505
- "estimated_change_pct": None,
1506
- "estimated_at": None,
1507
- "estimate_status": "unavailable",
1508
- }
1509
- warnings = []
1510
- try:
1511
- estimate.update(_fund_estimate(symbol))
1512
- except Exception:
1513
- warnings.append("estimated NAV provider request failed; published NAV remains available")
1544
+ today = _cn_now().date().isoformat()
1514
1545
  result = _clean_row(
1515
1546
  {
1516
1547
  "symbol": symbol,
@@ -1529,16 +1560,19 @@ def cmd_fund_quote(args):
1529
1560
  "fetched_at": datetime.now(timezone.utc).isoformat(),
1530
1561
  "note": (
1531
1562
  "nav is published end-of-day NAV, not a real-time price; QDII disclosure may lag "
1532
- "1-2 trading days; estimated_nav is separate and may be unavailable"
1563
+ "1-2 trading days; no intraday estimated NAV is provided"
1533
1564
  ),
1534
- **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",
1535
1571
  }
1536
1572
  )
1537
- if warnings:
1538
- result["warnings"] = warnings
1539
1573
  return result
1540
1574
  except Exception as e:
1541
- return {"error": str(e)}
1575
+ return {"error": error_message(e)}
1542
1576
 
1543
1577
 
1544
1578
  # --------------- capital_flow ---------------
@@ -1576,7 +1610,7 @@ def cmd_capital_flow(args):
1576
1610
  try:
1577
1611
  import akshare as ak
1578
1612
 
1579
- df = _akshare_retry(ak.stock_sector_fund_flow_rank)
1613
+ df = _akshare_retry(getattr(ak, "stock_fund_flow_sector_rank_em", None) or ak.stock_sector_fund_flow_rank)
1580
1614
  if df is None or df.empty:
1581
1615
  return {"error": "Sector fund flow data unavailable"}
1582
1616
  col_map = {
@@ -1591,7 +1625,7 @@ def cmd_capital_flow(args):
1591
1625
  keep = [c for c in col_map.values() if c in df.columns]
1592
1626
  return [_clean_row(r) for r in df[keep].head(20).to_dict("records")]
1593
1627
  except Exception as e:
1594
- return {"error": str(e)}
1628
+ return {"error": error_message(e)}
1595
1629
 
1596
1630
  if detect_market(args.symbol) != "A":
1597
1631
  return {"error": "capital_flow only available for A-shares"}
@@ -1603,7 +1637,9 @@ def cmd_capital_flow(args):
1603
1637
  # akshare wants the bare 6-digit stock plus market ∈ {sh, sz, bj}.
1604
1638
  code = _cn_code(args.symbol)
1605
1639
  market, stock = code[:2], code[2:]
1606
- df = _akshare_retry(ak.stock_individual_fund_flow, stock=stock, market=market)
1640
+ # Positional: the 1.20.45 rename also renamed `stock=` to `symbol=`.
1641
+ flow = getattr(ak, "stock_fund_flow_individual_hist_em", None) or ak.stock_individual_fund_flow
1642
+ df = _akshare_retry(flow, stock, market)
1607
1643
  col_map = {
1608
1644
  "日期": "date",
1609
1645
  "主力净流入-净额": "main_net_inflow",
@@ -1642,7 +1678,7 @@ def cmd_capital_flow(args):
1642
1678
  try:
1643
1679
  return _capital_flow_efinance(args.symbol)
1644
1680
  except Exception as e2:
1645
- return {"error": str(e2)}
1681
+ return {"error": error_message(e2)}
1646
1682
 
1647
1683
 
1648
1684
  # --------------- news ---------------
@@ -1650,7 +1686,9 @@ def cmd_capital_flow(args):
1650
1686
 
1651
1687
  def _news_search_intel_fallback(symbol: str) -> list:
1652
1688
  """Fallback: use search_intel to find news when primary sources fail."""
1653
- data = run_tool("search_intel.py", ["search", f"{symbol} 最新消息"], timeout=30)
1689
+ data = run_tool("search_intel.py", ["search", f"{symbol} 最新消息"])
1690
+ if isinstance(data, dict):
1691
+ data = data.get("results", [])
1654
1692
  if isinstance(data, list):
1655
1693
  return [{"title": item.get("title", ""), "url": item.get("url", ""), "source": "search"} for item in data[:10]]
1656
1694
  return []
@@ -1674,7 +1712,7 @@ def cmd_news(args):
1674
1712
  }
1675
1713
  df = df.rename(columns=col_map)
1676
1714
  keep = [c for c in col_map.values() if c in df.columns]
1677
- cutoff = datetime.now() - timedelta(days=args.days)
1715
+ cutoff = _cn_now() - timedelta(days=args.days)
1678
1716
  df = df[keep]
1679
1717
  if "datetime" in df.columns:
1680
1718
  import pandas as pd
@@ -1691,15 +1729,18 @@ def cmd_news(args):
1691
1729
  raise ValueError("yfinance news empty")
1692
1730
  results = []
1693
1731
  for item in news[:20]:
1732
+ content = item.get("content") or item # yfinance >= 0.2.50 nests fields under `content`
1694
1733
  results.append(
1695
1734
  {
1696
- "title": item.get("title", ""),
1697
- "publisher": item.get("publisher", ""),
1698
- "url": item.get("link", ""),
1699
- "published": item.get("providerPublishTime", ""),
1700
- "type": item.get("type", ""),
1735
+ "title": content.get("title") or item.get("title", ""),
1736
+ "publisher": (content.get("provider") or {}).get("displayName") or item.get("publisher", ""),
1737
+ "url": (content.get("canonicalUrl") or {}).get("url") or item.get("link", ""),
1738
+ "published": content.get("pubDate") or item.get("providerPublishTime", ""),
1739
+ "type": content.get("contentType") or item.get("type", ""),
1701
1740
  }
1702
1741
  )
1742
+ if not any(r["title"] for r in results):
1743
+ raise ValueError("yfinance news has no titles")
1703
1744
  return results
1704
1745
  except Exception:
1705
1746
  return _news_search_intel_fallback(args.symbol)
@@ -1740,18 +1781,31 @@ def _yf_info(symbol, ticker):
1740
1781
  return cached(f"yf-basic-info:{_yf_hk_symbol(symbol)}", DAY, fetch)
1741
1782
 
1742
1783
 
1743
- 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."""
1744
1787
  import akshare as ak
1788
+ import pandas as pd
1745
1789
 
1746
- df = _cached_frame(
1747
- f"financial-source:{symbol}", lambda: _akshare_retry(ak.stock_financial_analysis_indicator, symbol=symbol)
1748
- )
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
1749
1803
  return df.sort_values("日期", ascending=False) if "日期" in df.columns else df
1750
1804
 
1751
1805
 
1752
1806
  def financials_a(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1753
1807
  try:
1754
- df = _financials_a_frame(symbol)
1808
+ df = _financials_a_frame(symbol, periods, annual)
1755
1809
  if annual and not df.empty:
1756
1810
  df = df[df["日期"].astype(str).str.endswith("12-31")]
1757
1811
  if df is None or df.empty:
@@ -1880,19 +1934,175 @@ def financials_yf(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1880
1934
  return result
1881
1935
 
1882
1936
 
1937
+ def _yf_dcf_inputs(symbol: str, periods: int) -> dict:
1938
+ import yfinance as yf
1939
+
1940
+ if detect_market(symbol) == "A":
1941
+ code = _cn_code(symbol)
1942
+ if code.startswith("bj"):
1943
+ raise ValueError("Yahoo has no Beijing Stock Exchange statements")
1944
+ ticker = yf.Ticker(f"{code[2:]}.{'SS' if code.startswith('sh') else 'SZ'}")
1945
+ else:
1946
+ ticker = yf.Ticker(_yf_hk_symbol(symbol))
1947
+ tables = [ticker.income_stmt, ticker.cashflow, ticker.balance_sheet]
1948
+ warnings = ["Yahoo standardized annual statements; verify against filings before valuation."]
1949
+ try:
1950
+ currency, reason = _yf_info(symbol, ticker).get("financialCurrency"), "not reported"
1951
+ except Exception as e:
1952
+ currency, reason = None, error_message(e)
1953
+ if not currency:
1954
+ warnings.append(f"Yahoo financial currency unavailable ({reason}); rows cannot fill other sources")
1955
+ fields = {
1956
+ "revenue": (0, "Total Revenue"),
1957
+ "operating_income": (0, "Operating Income"),
1958
+ "pretax_income": (0, "Pretax Income"),
1959
+ "income_tax": (0, "Tax Provision"),
1960
+ "diluted_shares": (0, "Diluted Average Shares"),
1961
+ "operating_cash_flow": (1, "Operating Cash Flow"),
1962
+ "capital_expenditure": (1, "Capital Expenditure"),
1963
+ "depreciation_amortization": (1, "Depreciation And Amortization"),
1964
+ "cash": (2, "Cash And Cash Equivalents"),
1965
+ "lease_liabilities": (2, "Capital Lease Obligations"),
1966
+ "minority_interest": (2, "Minority Interest"),
1967
+ "shares_outstanding": (2, "Ordinary Shares Number"),
1968
+ }
1969
+ ends = sorted(
1970
+ {str(col)[:10] for table in tables if table is not None and not table.empty for col in table.columns},
1971
+ reverse=True,
1972
+ )[:periods]
1973
+ rows = []
1974
+ for end in ends:
1975
+ entry = {"period_end": end, "currency": currency}
1976
+
1977
+ def cell(index, label, end=end):
1978
+ table = tables[index]
1979
+ column = next((col for col in getattr(table, "columns", []) if str(col)[:10] == end), None)
1980
+ return (
1981
+ _sanitize(_parse_float(table.loc[label, column]))
1982
+ if column is not None and label in table.index
1983
+ else None
1984
+ )
1985
+
1986
+ for key, (index, label) in fields.items():
1987
+ value = cell(index, label)
1988
+ entry[key] = abs(value) if key == "capital_expenditure" and value is not None else value
1989
+ # Yahoo "Total Debt" adds lease liabilities; SEC/Lixinger total_debt excludes them.
1990
+ debt = [value for value in (cell(2, "Current Debt"), cell(2, "Long Term Debt")) if value is not None]
1991
+ entry["total_debt"] = sum(debt) if debt else None
1992
+ rows.append(entry)
1993
+ if not rows:
1994
+ raise ValueError("Yahoo annual statements unavailable")
1995
+ return {
1996
+ "source": "yfinance",
1997
+ "periods": rows,
1998
+ "warnings": warnings + ["lease_liabilities is Yahoo Capital Lease Obligations (may omit operating leases)."],
1999
+ }
2000
+
2001
+
2002
+ DCF_FIELDS = (
2003
+ "revenue",
2004
+ "operating_income",
2005
+ "pretax_income",
2006
+ "income_tax",
2007
+ "operating_cash_flow",
2008
+ "capital_expenditure",
2009
+ "depreciation_amortization",
2010
+ "cash",
2011
+ "total_debt",
2012
+ "lease_liabilities",
2013
+ "minority_interest",
2014
+ "diluted_shares",
2015
+ "shares_outstanding",
2016
+ )
2017
+
2018
+
2019
+ def _fill_dcf_rows(result: dict, fallback: dict | None) -> dict:
2020
+ """Fill null primary fields from Yahoo for the same fiscal period and currency, recording each field's source."""
2021
+ for row in result["periods"]:
2022
+ match = None
2023
+ if fallback:
2024
+ end = datetime.fromisoformat(row["period_end"]).date()
2025
+ match = next(
2026
+ (
2027
+ other
2028
+ for other in fallback["periods"]
2029
+ if abs((datetime.fromisoformat(other["period_end"]).date() - end).days) <= 10
2030
+ and other.get("currency")
2031
+ and other.get("currency") == row.get("currency")
2032
+ ),
2033
+ None,
2034
+ )
2035
+ sources = {}
2036
+ for key in DCF_FIELDS:
2037
+ # Yahoo leases are finance-lease-only "Capital Lease Obligations"; never mix them into another source.
2038
+ if row.get(key) is None and match and match.get(key) is not None and key != "lease_liabilities":
2039
+ row[key], sources[key] = match[key], fallback["source"]
2040
+ elif row.get(key) is not None:
2041
+ sources[key] = result["source"]
2042
+ else:
2043
+ row[key] = None
2044
+ pretax, tax = row["pretax_income"], row["income_tax"]
2045
+ row["effective_tax_rate"] = tax / pretax if tax is not None and pretax and pretax > 0 else None
2046
+ row["field_sources"] = sources
2047
+ row["missing_fields"] = [key for key in DCF_FIELDS if row[key] is None]
2048
+ result["units"] = {"amounts": "reported currency units", "shares": "shares", "effective_tax_rate": "ratio"}
2049
+ return result
2050
+
2051
+
2052
+ def _dcf_inputs(symbol: str, periods: int) -> dict:
2053
+ market = detect_market(symbol)
2054
+ if market not in ("A", "HK", "US"):
2055
+ return {"error": "DCF inputs support A, HK and US stocks only"}
2056
+ warnings, primary = [], None
2057
+ if market == "US" or os.environ.get("LIXINGER_TOKEN"):
2058
+ label = "SEC companyfacts" if market == "US" else f"Lixinger {market}"
2059
+ try:
2060
+ if market == "US":
2061
+ import sec_edgar
2062
+
2063
+ primary = sec_edgar.dcf_inputs(symbol, periods)
2064
+ else:
2065
+ import lixinger
2066
+
2067
+ code = _cn_code(symbol)[2:] if market == "A" else symbol.split(".")[0].zfill(5)
2068
+ primary = lixinger.dcf_inputs("cn" if market == "A" else "hk", code, periods)
2069
+ except Exception as e:
2070
+ warnings.append(f"{label} unavailable: {error_message(e)}; Yahoo fallback used")
2071
+ try:
2072
+ yahoo = _yf_dcf_inputs(symbol, periods)
2073
+ except Exception as e:
2074
+ if primary is None:
2075
+ return {"error": f"DCF inputs unavailable: {error_message(e)}", "warnings": warnings}
2076
+ yahoo = None
2077
+ warnings.append(f"Yahoo fill unavailable: {error_message(e)}")
2078
+ result = _fill_dcf_rows(primary or yahoo, yahoo if primary else None)
2079
+ result["warnings"] = warnings + result["warnings"]
2080
+ return result
2081
+
2082
+
1883
2083
  def cmd_financials(args):
1884
2084
  periods = getattr(args, "periods", 1)
1885
2085
  if not 1 <= periods <= 40:
1886
2086
  return {"error": f"periods must be between 1 and 40, got {periods}"}
1887
2087
  annual = getattr(args, "annual", False)
1888
2088
  market = detect_market(args.symbol)
2089
+ result = _financials_without_dcf(args, periods, annual, market)
2090
+ if getattr(args, "include_dcf_inputs", False) and "error" not in result:
2091
+ result["dcf_inputs"] = _dcf_inputs(args.symbol, periods)
2092
+ return result
2093
+
2094
+
2095
+ def _financials_without_dcf(args, periods, annual, market):
1889
2096
  try:
1890
2097
  if market == "HK":
1891
2098
  if os.environ.get("LIXINGER_TOKEN"):
1892
2099
  try:
1893
2100
  from lixinger import financials_hk
1894
2101
 
1895
- result = financials_hk(args.symbol, periods, annual)
2102
+ result = {
2103
+ "symbol": args.symbol,
2104
+ **financials_hk(args.symbol.split(".")[0].zfill(5), periods, annual),
2105
+ }
1896
2106
  snapshot = financials_yf(args.symbol, 1, False)
1897
2107
  result.update(
1898
2108
  {
@@ -1922,300 +2132,268 @@ def cmd_financials(args):
1922
2132
  financials_a(args.symbol, periods, annual) if market == "A" else financials_yf(args.symbol, periods, annual)
1923
2133
  )
1924
2134
  except Exception as e:
1925
- return {"error": str(e)}
2135
+ return {"error": error_message(e)}
1926
2136
 
1927
2137
 
1928
2138
  # --------------- market_snapshot ---------------
1929
2139
 
1930
-
1931
- def snapshot_a() -> list:
1932
- # akshare/efinance leave requests timeout-less — the chain-wide socket bound
1933
- # (socket_timeout) is what lets the tencent leg get its turn on a dead network.
1934
- try:
1935
- with socket_timeout(25):
1936
- return _failover(
1937
- [
1938
- ("akshare", _snapshot_akshare),
1939
- ("efinance", _snapshot_efinance),
1940
- ("sina", _snapshot_sina),
1941
- ("tencent", _snapshot_tencent),
1942
- ],
1943
- label="snapshot_a",
1944
- )
1945
- except Exception as e:
1946
- return [{"error": f"A-share snapshot unavailable from all sources: {e}"}]
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
+ }
1947
2245
 
1948
2246
 
1949
- def _snapshot_efinance() -> list:
1950
- import efinance as ef
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
1951
2263
 
1952
- df = ef.stock.get_realtime_quotes()
1953
- if df is None or df.empty:
1954
- raise ValueError("efinance snapshot empty")
1955
- col_map = {
1956
- "股票代码": "symbol",
1957
- "股票名称": "name",
1958
- "最新价": "price",
1959
- "涨跌幅": "change_pct",
1960
- "涨跌额": "change",
1961
- "成交量": "volume",
1962
- "成交额": "turnover",
1963
- "市盈率": "pe",
1964
- "换手率": "turnover_rate",
1965
- "最高": "high",
1966
- "最低": "low",
1967
- "今开": "open",
1968
- "昨收": "prev_close",
1969
- }
1970
- df = df.rename(columns=col_map)
1971
- df["pe_basis"] = "dynamic"
1972
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
1973
- return [_clean_row(r) for r in df[keep].to_dict("records")]
1974
2264
 
2265
+ def normalize_us_symbol(value) -> str:
2266
+ symbol = str(value).strip().upper()
2267
+ symbol = re.sub(r"^(?:105|106|107)[.]", "", symbol)
2268
+ symbol = re.sub(r"[.](?:OQ|N|A)$", "", symbol)
2269
+ return re.sub(r"[._]", "-", symbol) # Eastmoney writes class shares as BRK_B; yfinance wants BRK-B
1975
2270
 
1976
- def _snapshot_sina() -> list:
1977
- import akshare as ak
1978
- import pandas as pd
1979
2271
 
1980
- df = _akshare_retry(ak.stock_zh_a_spot)
1981
- if df is None or df.empty:
1982
- raise ValueError("sina snapshot empty")
1983
- df["代码"] = df["代码"].astype(str).str.replace(r"^(?:sh|sz|bj)", "", regex=True)
1984
- df["成交量"] = pd.to_numeric(df["成交量"], errors="coerce") / 100
1985
- col_map = {
1986
- "代码": "symbol",
1987
- "名称": "name",
1988
- "最新价": "price",
1989
- "涨跌额": "change",
1990
- "涨跌幅": "change_pct",
1991
- "成交量": "volume",
1992
- "成交额": "turnover",
1993
- "昨收": "prev_close",
1994
- "今开": "open",
1995
- "最高": "high",
1996
- "最低": "low",
1997
- }
1998
- df = df.rename(columns=col_map)
1999
- keep = [c for c in col_map.values() if c in df.columns]
2000
- return [_clean_row(r) for r in df[keep].to_dict("records")]
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()
2275
+ try:
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
2292
+ except Exception as e:
2293
+ return {"error": f"{market} snapshot unavailable: {error_message(e)}"}
2001
2294
 
2002
2295
 
2003
- # qt.gtimg.cn accepts comma-joined multi-code batches; 60 keeps URLs well short
2004
- _TENCENT_BATCH = 60
2296
+ # --------------- market_indices ---------------
2005
2297
 
2298
+ # region -> {index symbol -> display name}; CN is handled separately via akshare.
2299
+ _REGION_INDEX_SYMBOLS = {
2300
+ "hk": {"^HSI": "恒生指数", "^HSCE": "恒生国企指数", "^HSTECH": "恒生科技指数"},
2301
+ "us": {"^DJI": "道琼斯", "^IXIC": "纳斯达克", "^GSPC": "标普500", "^RUT": "罗素2000"},
2302
+ "jp": {"^N225": "日经225", "^TOPX": "东证指数"},
2303
+ "kr": {"^KS11": "韩国KOSPI", "^KQ11": "韩国KOSDAQ"},
2304
+ "tw": {"^TWII": "台湾加权指数"},
2305
+ }
2006
2306
 
2007
- def _tencent_batch_quotes(codes: list) -> list:
2008
- """Batch quotes from qt.gtimg.cn for exchange-prefixed codes, a few requests in
2009
- flight (this leg fires only when the eastmoney/sina legs are all dead, and ~15k
2010
- enumerated codes serialized would take minutes). Unknown codes are simply absent
2011
- from the response; delisted/suspended ones come back frozen with volume=0 and
2012
- are dropped here."""
2013
- import concurrent.futures
2014
2307
 
2308
+ def _quote_tencent_hk_index(symbol: str) -> dict:
2309
+ """Tencent r_hk index quote, using the same `~` field positions as _tencent_payload_row.
2310
+ Volume is omitted: the index feed's volume field has no confirmed unit."""
2015
2311
  import requests
2016
2312
 
2017
- def fetch(batch):
2018
- try:
2019
- resp = requests.get(f"https://qt.gtimg.cn/q={','.join(batch)}", timeout=10)
2020
- except Exception:
2021
- return [] # a lost batch loses 60 codes — a partial snapshot still beats none
2022
- rows = []
2023
- for part in resp.content.decode("gbk", errors="replace").split(";"):
2024
- vname, _, payload = part.strip().partition('="')
2025
- f = payload.rstrip('"').split("~")
2026
- if len(f) < 35 or not f[1]:
2027
- continue
2028
- if not _parse_float(f[6]): # volume 0/empty → frozen payload (delisted/suspended)
2029
- continue
2030
- prefixed = vname[2:] # strip the leading "v_"
2031
- bare = prefixed[2:] if prefixed[:2] in ("sh", "sz", "bj") else prefixed
2032
- rows.append(_clean_row(_tencent_payload_row(f, bare)))
2033
- return rows
2034
-
2035
- batches = [codes[i : i + _TENCENT_BATCH] for i in range(0, len(codes), _TENCENT_BATCH)]
2036
- rows = []
2037
- with concurrent.futures.ThreadPoolExecutor(max_workers=6) as pool:
2038
- for batch_rows in pool.map(fetch, batches):
2039
- rows.extend(batch_rows)
2040
- return rows
2041
-
2042
-
2043
- def _snapshot_tencent() -> list:
2044
- """Tencent full-market snapshot — the plain-HTTPS last-resort leg for when the
2045
- eastmoney/sina legs are all blocked (datacenter IPs, issue #25).
2046
-
2047
- Codes are enumerated over the known SH (600-605, 688-689) and SZ (000-003,
2048
- 300-302) ranges and validated by the batch quotes themselves — no list API is
2049
- reliable from blocked networks (www.szse.cn resets datacenter connections,
2050
- query.sse.com.cn flaps, akshare's wrapper has no timeout). BSE and suspended
2051
- stocks stay uncovered by this leg."""
2052
- codes = [f"sh{i:06d}" for lo, hi in ((600000, 605999), (688000, 689999)) for i in range(lo, hi + 1)]
2053
- codes += [f"sz{i:06d}" for lo, hi in ((1, 3999), (300000, 302999)) for i in range(lo, hi + 1)]
2054
- rows = _tencent_batch_quotes(codes)
2055
- if not rows:
2056
- raise ValueError("tencent snapshot returned empty data")
2057
- return rows
2058
-
2059
-
2060
- def _snapshot_akshare() -> list:
2061
- import akshare as ak
2062
-
2063
- df = _akshare_retry(ak.stock_zh_a_spot_em)
2064
- col_map = {
2065
- "代码": "symbol",
2066
- "名称": "name",
2067
- "最新价": "price",
2068
- "涨跌幅": "change_pct",
2069
- "涨跌额": "change",
2070
- "成交量": "volume",
2071
- "成交额": "turnover",
2072
- "市盈率-动态": "pe",
2073
- "市净率": "pb",
2074
- "总市值": "market_cap",
2075
- "换手率": "turnover_rate",
2076
- "量比": "volume_ratio",
2077
- "最高": "high",
2078
- "最低": "low",
2079
- "今开": "open",
2080
- "昨收": "prev_close",
2081
- "60日涨跌幅": "change_pct_60d",
2313
+ resp = requests.get(f"https://qt.gtimg.cn/q=r_{_TENCENT_HK_INDEX[symbol]}", timeout=10)
2314
+ resp.raise_for_status()
2315
+ f = _gbk_var_payload(resp.content).split("~")
2316
+ price = _parse_float(f[3]) if len(f) > 34 else None
2317
+ if price is None or price <= 0:
2318
+ raise ValueError(f"tencent returned no quote for {symbol}")
2319
+ return {
2320
+ "price": price,
2321
+ "change": _parse_float(f[31]),
2322
+ "change_pct": _parse_float(f[32]),
2323
+ "high": _parse_float(f[33]),
2324
+ "low": _parse_float(f[34]),
2325
+ "open": _parse_float(f[5]),
2326
+ "prev_close": _parse_float(f[4]),
2327
+ "source": "tencent",
2328
+ "data_as_of": _quote_time(f[30], "%Y/%m/%d %H:%M:%S"),
2329
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
2082
2330
  }
2083
- df = df.rename(columns=col_map)
2084
- df["pe_basis"] = "dynamic"
2085
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
2086
- df = df[keep]
2087
- return [_clean_row(r) for r in df.to_dict("records")]
2088
2331
 
2089
2332
 
2090
- def snapshot_hk() -> list:
2091
- try:
2092
- import akshare as ak
2093
-
2094
- with socket_timeout(25):
2095
- df = _akshare_retry(ak.stock_hk_spot_em)
2096
- col_map = {
2097
- "代码": "symbol",
2098
- "名称": "name",
2099
- "最新价": "price",
2100
- "涨跌幅": "change_pct",
2101
- "成交量": "volume",
2102
- "成交额": "turnover",
2103
- "市盈率": "pe",
2104
- "市净率": "pb",
2105
- "总市值": "market_cap",
2106
- }
2107
- df = df.rename(columns=col_map)
2108
- if "symbol" in df.columns:
2109
- symbols = df["symbol"].astype(str).str.upper()
2110
- df["symbol"] = symbols.map(lambda s: s if s.endswith(".HK") else f"{s}.HK")
2111
- df["pe_basis"] = "unknown"
2112
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
2113
- return [_clean_row(r) for r in df[keep].to_dict("records")]
2114
- except Exception as e:
2115
- # summary only (type + short message) — never a stack trace or response body
2116
- return [{"error": f"HK snapshot unavailable: {type(e).__name__}: {scrub_secrets(str(e))}"[:200]}]
2117
-
2333
+ _CN_INDICES = [
2334
+ ("sh000001", "上证指数"),
2335
+ ("sz399001", "深证成指"),
2336
+ ("sz399006", "创业板指"),
2337
+ ("sh000688", "科创50"),
2338
+ ("sh000300", "沪深300"),
2339
+ ("sh000016", "上证50"),
2340
+ ]
2118
2341
 
2119
- def snapshot_us() -> list:
2120
- try:
2121
- import akshare as ak
2122
2342
 
2123
- with socket_timeout(25):
2124
- df = _akshare_retry(ak.stock_us_spot_em)
2125
- col_map = {
2126
- "代码": "symbol",
2127
- "名称": "name",
2128
- "最新价": "price",
2129
- "涨跌幅": "change_pct",
2130
- "成交量": "volume",
2131
- "成交额": "turnover",
2132
- "市盈率": "pe",
2133
- "总市值": "market_cap",
2134
- }
2135
- df = df.rename(columns=col_map)
2136
- if "symbol" in df.columns:
2137
- symbols = df["symbol"].astype(str).str.upper().str.replace(r"\.(?:OQ|N|A)$", "", regex=True)
2138
- df["symbol"] = symbols.str.replace(".", "-", regex=False)
2139
- df["pe_basis"] = "unknown"
2140
- keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
2141
- return [_clean_row(r) for r in df[keep].to_dict("records")]
2142
- except Exception as e:
2143
- return [{"error": f"US snapshot unavailable: {type(e).__name__}: {scrub_secrets(str(e))}"[:200]}]
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
+ )
2144
2354
 
2145
2355
 
2146
- def cmd_market_snapshot(args):
2147
- try:
2148
- m = args.market.upper()
2149
- if m == "A":
2150
- return snapshot_a()
2151
- elif m == "HK":
2152
- return snapshot_hk()
2153
- elif m == "US":
2154
- return snapshot_us()
2155
- return {"error": f"Unknown market: {m}"}
2156
- except Exception as e:
2157
- return {"error": str(e)}
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
2158
2364
 
2159
2365
 
2160
- # --------------- market_indices ---------------
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
2161
2369
 
2162
- # region -> {index symbol -> display name}; CN is handled separately via akshare.
2163
- _REGION_INDEX_SYMBOLS = {
2164
- "hk": {"^HSI": "恒生指数", "^HSCE": "恒生国企指数", "^HSTECH": "恒生科技指数"},
2165
- "us": {"^DJI": "道琼斯", "^IXIC": "纳斯达克", "^GSPC": "标普500", "^RUT": "罗素2000"},
2166
- "jp": {"^N225": "日经225", "^TOPX": "东证指数"},
2167
- "kr": {"^KS11": "韩国KOSPI", "^KQ11": "韩国KOSDAQ"},
2168
- "tw": {"^TWII": "台湾加权指数"},
2169
- }
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
2170
2381
 
2171
2382
 
2172
2383
  def cmd_market_indices(args):
2173
2384
  region = args.region.lower()
2174
2385
  try:
2175
2386
  if region == "cn":
2176
- import akshare as ak
2177
-
2178
- indices = [
2179
- ("sh000001", "上证指数"),
2180
- ("sz399001", "深证成指"),
2181
- ("sz399006", "创业板指"),
2182
- ("sh000688", "科创50"),
2183
- ("sh000300", "沪深300"),
2184
- ("sh000016", "上证50"),
2185
- ]
2186
- results = []
2187
- try:
2188
- df = _akshare_retry(ak.stock_zh_index_spot_em)
2189
- except Exception:
2190
- # eastmoney refuses datacenter/proxy IPs (RemoteDisconnected) — sina is the fallback
2191
- df = _akshare_retry(ak.stock_zh_index_spot_sina)
2192
- for code, name in indices:
2193
- clean_code = code[2:] if code[:2] in ("sh", "sz") else code
2194
- row = df[df["代码"] == clean_code]
2195
- if row.empty:
2196
- row = df[df["代码"] == code]
2197
- if row.empty:
2198
- results.append({"code": code, "name": name, "note": "data unavailable"})
2199
- continue
2200
- r = row.iloc[0]
2201
- results.append(
2202
- _clean_row(
2203
- {
2204
- "code": code,
2205
- "name": r.get("名称", name),
2206
- "price": r.get("最新价"),
2207
- "change": r.get("涨跌额"),
2208
- "change_pct": r.get("涨跌幅"),
2209
- "volume": r.get("成交量"),
2210
- "turnover": r.get("成交额"),
2211
- "high": r.get("最高"),
2212
- "low": r.get("最低"),
2213
- "open": r.get("今开"),
2214
- "prev_close": r.get("昨收"),
2215
- }
2216
- )
2387
+ with socket_timeout(25):
2388
+ quotes = _failover(
2389
+ [("eastmoney", _cn_indices_eastmoney), ("tencent", _cn_indices_tencent)], label="market_indices_cn"
2217
2390
  )
2218
- 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
+ ]
2219
2397
  elif region in _REGION_INDEX_SYMBOLS:
2220
2398
  from concurrent.futures import ThreadPoolExecutor
2221
2399
 
@@ -2226,6 +2404,8 @@ def cmd_market_indices(args):
2226
2404
  def fetch_index(item):
2227
2405
  sym, name = item
2228
2406
  try:
2407
+ if sym in _TENCENT_HK_INDEX:
2408
+ return _clean_row({"code": sym, "name": name, **_quote_tencent_hk_index(sym)})
2229
2409
  info = yf.Ticker(sym).info
2230
2410
  return _clean_row(
2231
2411
  {
@@ -2252,7 +2432,7 @@ def cmd_market_indices(args):
2252
2432
  return list(pool.map(fetch_index, symbols.items()))
2253
2433
  return {"error": f"Unknown region: {region}"}
2254
2434
  except Exception as e:
2255
- return {"error": str(e)}
2435
+ return {"error": error_message(e)}
2256
2436
 
2257
2437
 
2258
2438
  # --------------- sector_rankings ---------------
@@ -2383,24 +2563,72 @@ def _sector_rankings_sina(indicator: str) -> list:
2383
2563
  return [_clean_row(r) for r in df[keep].to_dict("records")]
2384
2564
 
2385
2565
 
2566
+ US_SECTOR_ETFS = {
2567
+ "XLK": "Technology",
2568
+ "XLF": "Financials",
2569
+ "XLV": "Health Care",
2570
+ "XLY": "Consumer Discretionary",
2571
+ "XLP": "Consumer Staples",
2572
+ "XLE": "Energy",
2573
+ "XLI": "Industrials",
2574
+ "XLB": "Materials",
2575
+ "XLU": "Utilities",
2576
+ "XLRE": "Real Estate",
2577
+ "XLC": "Communication Services",
2578
+ }
2579
+
2580
+
2581
+ def _sector_rankings_us() -> list:
2582
+ """S&P 500 sector proxies from the SPDR Select Sector ETFs' last two daily closes."""
2583
+ import yfinance as yf
2584
+
2585
+ closes = yf.download(list(US_SECTOR_ETFS), period="10d", auto_adjust=False, progress=False)["Close"]
2586
+ rows = []
2587
+ for symbol, name in US_SECTOR_ETFS.items():
2588
+ series = closes[symbol].dropna() if symbol in closes else []
2589
+ if len(series) < 2:
2590
+ continue
2591
+ rows.append(
2592
+ {
2593
+ "name": name,
2594
+ "etf": symbol,
2595
+ "close": round(float(series.iloc[-1]), 4),
2596
+ "change_pct": round(float(series.iloc[-1] / series.iloc[-2] - 1) * 100, 4),
2597
+ "as_of": str(series.index[-1])[:10],
2598
+ }
2599
+ )
2600
+ if not rows:
2601
+ raise ValueError("US sector ETF quotes unavailable")
2602
+ return rows
2603
+
2604
+
2386
2605
  def cmd_sector_rankings(args):
2387
2606
  board_type = getattr(args, "board_type", "industry")
2388
2607
  direction = getattr(args, "direction", "top")
2389
- sources = [_sourced("eastmoney", lambda: _sector_rankings_em(board_type))]
2390
- if board_type == "industry":
2391
- # efinance has no concept boards — it stays an industry-only leg
2392
- sources.append(_sourced("ths", _sector_rankings_ths))
2393
- sources.append(_sourced("efinance", _sector_rankings_efinance))
2608
+ market = getattr(args, "market", "A")
2609
+ if market == "US" and board_type != "industry":
2610
+ return {"error": "US sector rankings support board_type=industry only"}
2611
+ if market == "US":
2612
+ sources = [_sourced("yfinance", _sector_rankings_us)]
2394
2613
  else:
2395
- sources.append(_sourced("sina", lambda: _sector_rankings_sina("概念")))
2614
+ sources = [_sourced("eastmoney", lambda: _sector_rankings_em(board_type))]
2615
+ if board_type == "industry":
2616
+ # efinance has no concept boards — it stays an industry-only leg
2617
+ sources += [_sourced("ths", _sector_rankings_ths), _sourced("efinance", _sector_rankings_efinance)]
2618
+ else:
2619
+ sources.append(_sourced("sina", lambda: _sector_rankings_sina("概念")))
2396
2620
  try:
2397
- rows = _failover(sources, label=f"sector_rankings:{board_type}")
2621
+ rows = _failover(
2622
+ sources, label="sector_rankings_us:industry" if market == "US" else f"sector_rankings:{board_type}"
2623
+ )
2398
2624
  if not rows:
2399
2625
  raise ValueError(f"no {board_type} board rankings data")
2400
2626
  except Exception as e:
2401
- return {"error": str(e)}
2627
+ return {"error": error_message(e)}
2402
2628
  for r in rows:
2403
2629
  r["board_type"] = board_type
2630
+ if market == "US":
2631
+ r.update(market="US", proxy="SPDR Select Sector ETF")
2404
2632
 
2405
2633
  def _pct(row, default):
2406
2634
  v = row.get("change_pct")
@@ -2418,10 +2646,9 @@ def cmd_sector_rankings(args):
2418
2646
 
2419
2647
  # --------------- sector constituents / stock sectors (issue #18) ---------------
2420
2648
 
2421
- # Disk cache lives in tempdir — cross-platform (Windows has no ~/.cache). Stores
2422
- # sector constituents (24h TTL; membership changes slowly) and sticky failover
2423
- # winners (keys prefixed `sticky-`, see _failover).
2424
- _DATA_CACHE_DIR = Path(tempfile.gettempdir()) / "pi-stock-analysis"
2649
+ # Small routing state (24h TTL) beside the user-private slow-data cache: sticky failover
2650
+ # winners (`sticky-`, see _failover) and per-leg outage markers (`resolve-down-`).
2651
+ _DATA_CACHE_DIR = CACHE_DIR / "state"
2425
2652
  _DATA_CACHE_TTL = 24 * 3600
2426
2653
 
2427
2654
 
@@ -2475,17 +2702,20 @@ def _constituents_em(sector: str, board_kind: str) -> dict:
2475
2702
  """Eastmoney industry/concept board constituents via akshare, fuzzy-resolved."""
2476
2703
  import akshare as ak
2477
2704
 
2478
- if board_kind == "industry":
2479
- name_fn, cons_fn = ak.stock_board_industry_name_em, ak.stock_board_industry_cons_em
2480
- else:
2481
- name_fn, cons_fn = ak.stock_board_concept_name_em, ak.stock_board_concept_cons_em
2482
- names_df = _akshare_retry(name_fn)
2483
- if names_df is None or names_df.empty or "板块名称" not in names_df.columns:
2484
- raise ValueError(f"eastmoney {board_kind} board list unavailable")
2485
- 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))
2486
2715
  if not exact:
2487
2716
  raise ValueError(f"no eastmoney {board_kind} board matching '{sector}'")
2488
- 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])
2489
2719
  if df is None or df.empty:
2490
2720
  raise ValueError(f"eastmoney {board_kind} board '{exact}' returned no constituents")
2491
2721
  constituents = _constituent_rows(df)
@@ -2564,16 +2794,10 @@ def _sector_constituents(args):
2564
2794
  sector = args.sector.strip()
2565
2795
  if not sector:
2566
2796
  return {"error": "sector name required"}
2567
- board_type = args.board_type
2568
- # Sector names are user input — hash them instead of sanitizing, so names that
2569
- # differ only in whitespace/punctuation (创新药 vs 创新 药) can't collide.
2570
- cache_key = hashlib.sha256(f"{sector}|{board_type}".encode()).hexdigest()[:16]
2571
2797
  try:
2572
- result = sector_constituents_a(sector, board_type)
2798
+ return sector_constituents_a(sector, args.board_type)
2573
2799
  except Exception as e:
2574
- return {"error": str(e)}
2575
- _disk_cache_set(cache_key, result)
2576
- return result
2800
+ return {"error": error_message(e)}
2577
2801
 
2578
2802
 
2579
2803
  def _board_entry(name, source: str, board_type: str) -> dict:
@@ -2671,17 +2895,11 @@ def _stock_boards_cninfo(symbol: str) -> list:
2671
2895
  def _stock_boards_from_cache(symbol: str) -> list:
2672
2896
  """Reverse lookup against the get_sector_constituents disk cache — the honest
2673
2897
  fallback when eastmoney blocks the caller's IP and xueqiu has no token (the two
2674
- live A-share sources are both eastmoney-flavored). Reads week-lived user-directory entries plus legacy temporary payloads,
2675
- and returns one entry per cached sector
2676
- containing the symbol. Cold cache → []; malformed cache files are skipped
2677
- (_disk_cache_get returns None on expiry, bad JSON, or read errors)."""
2678
- from itertools import chain
2679
-
2680
- from _cache import cached_values
2681
-
2898
+ live A-share sources are both eastmoney-flavored). Reads the week-lived get_sector_constituents
2899
+ entries and returns one entry per cached sector containing the symbol. Cold cache → [];
2900
+ expired or malformed entries are skipped."""
2682
2901
  boards = []
2683
- legacy = (_disk_cache_get(path.stem) for path in _DATA_CACHE_DIR.glob("*.json"))
2684
- for data in chain(legacy, cached_values("sector-constituents", WEEK)):
2902
+ for data in cached_values("sector-constituents", WEEK):
2685
2903
  if not isinstance(data, dict):
2686
2904
  continue
2687
2905
  sector = data.get("sector")
@@ -2730,14 +2948,13 @@ _RESOLVE_DOWN_TTL = 3600
2730
2948
  def _resolve_leg_down(name: str) -> bool:
2731
2949
  """True while leg `name`'s failure marker is fresh. The marker stores a plain
2732
2950
  timestamp — the effective TTL above is shorter than the 24h file lifetime that
2733
- _disk_cache_get enforces on the shared cache dir."""
2951
+ _disk_cache_get enforces on the private state cache dir."""
2734
2952
  ts = _disk_cache_get(f"resolve-down-{name}")
2735
2953
  return isinstance(ts, (int, float)) and time.time() - ts < _RESOLVE_DOWN_TTL
2736
2954
 
2737
2955
 
2738
2956
  def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
2739
- if info_map is not None:
2740
- 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.
2741
2958
  return cached(f"stock-sectors:{symbol}", WEEK, lambda: _resolve_stock_sectors(symbol, info_map))
2742
2959
 
2743
2960
 
@@ -2870,12 +3087,13 @@ def cmd_stock_info(args):
2870
3087
  if is_etf:
2871
3088
  try:
2872
3089
  # ETF 最新净值(天天基金-场内基金历史净值明细)。窄日期窗口 → 单页返回
2873
- end = datetime.now()
3090
+ # akshare 1.20.41 改名为 fund_exchange_nav_hist_em;py3.10 只能装到 <1.20.40,仍是旧名
3091
+ end = _cn_now()
2874
3092
  start = end - timedelta(days=30)
2875
3093
  nav_df = _cached_frame(
2876
3094
  f"etf-basic-nav:{args.symbol}",
2877
3095
  lambda: _akshare_retry(
2878
- ak.fund_etf_fund_info_em,
3096
+ getattr(ak, "fund_exchange_nav_hist_em", None) or ak.fund_etf_fund_info_em,
2879
3097
  fund=args.symbol,
2880
3098
  start_date=start.strftime("%Y%m%d"),
2881
3099
  end_date=end.strftime("%Y%m%d"),
@@ -2914,7 +3132,7 @@ def cmd_stock_info(args):
2914
3132
  }
2915
3133
  )
2916
3134
  except Exception as e:
2917
- return {"error": str(e)}
3135
+ return {"error": error_message(e)}
2918
3136
 
2919
3137
 
2920
3138
  # --------------- chip_distribution ---------------
@@ -2957,93 +3175,140 @@ def cmd_chip_distribution(args):
2957
3175
  result["concentration_70"] = round((h70 - l70) / ((h70 + l70) / 2) * 100, 2)
2958
3176
  return result
2959
3177
  except Exception as e:
2960
- return {"error": str(e)}
3178
+ return {"error": error_message(e)}
2961
3179
 
2962
3180
 
2963
3181
  # --------------- market_stats ---------------
2964
3182
 
2965
3183
 
2966
- def compute_market_stats(data: list) -> dict:
2967
- """Compute breadth/limit/turnover statistics from a market snapshot list."""
2968
- 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
2969
3187
 
2970
- changes = []
2971
- turnovers = []
2972
- limit_ups = []
2973
- limit_downs = []
2974
- up_count = down_count = flat_count = 0
2975
-
2976
- for s in data:
2977
- chg = s.get("change_pct")
2978
- if chg is not None:
2979
- changes.append(float(chg))
2980
- if chg > 0:
2981
- up_count += 1
2982
- elif chg < 0:
2983
- down_count += 1
2984
- else:
2985
- flat_count += 1
2986
- t = s.get("turnover")
2987
- if t is not None:
2988
- turnovers.append(float(t))
2989
-
2990
- price = s.get("price")
2991
- prev = s.get("prev_close")
2992
- sym = s.get("symbol", "")
2993
- if price and prev and prev > 0:
2994
- code_info = normalize_stock_code(sym)
2995
- lp = code_info.get("limit_pct")
2996
- if lp:
2997
- name = s.get("name", "")
2998
- if name and "ST" in name.upper() and code_info["board"] == "main":
2999
- lp = 0.05
3000
- limit_up = calc_limit_price(float(prev), lp, "up")
3001
- limit_down = calc_limit_price(float(prev), lp, "down")
3002
- if abs(float(price) - limit_up) < 0.01:
3003
- limit_ups.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
3004
- elif abs(float(price) - limit_down) < 0.01:
3005
- limit_downs.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
3006
-
3007
- changes_arr = np.array(changes) if changes else np.array([0])
3008
- sorted_data = sorted(data, key=lambda x: x.get("change_pct") or 0, reverse=True)
3009
3188
 
3010
- 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 [
3011
3233
  {
3012
- "total_stocks": len(data),
3013
- "up_count": up_count,
3014
- "down_count": down_count,
3015
- "flat_count": flat_count,
3016
- "limit_up_count": len(limit_ups),
3017
- "limit_down_count": len(limit_downs),
3018
- "avg_change_pct": round(float(changes_arr.mean()), 2),
3019
- "median_change_pct": round(float(np.median(changes_arr)), 2),
3020
- "total_turnover": round(sum(turnovers), 0) if turnovers else None,
3021
- "top5_gainers": [
3022
- {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
3023
- for s in sorted_data[:5]
3024
- ],
3025
- "top5_losers": [
3026
- {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
3027
- for s in sorted_data[-5:]
3028
- ],
3029
- "limit_up_samples": limit_ups[:5],
3030
- "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),
3031
3237
  }
3032
- )
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
+ }
3033
3301
 
3034
3302
 
3035
3303
  def cmd_market_stats(args):
3304
+ market = getattr(args, "market", "A")
3036
3305
  try:
3037
- data = snapshot_a()
3038
- if isinstance(data, dict) and "error" in data:
3039
- return data
3040
- if isinstance(data, list) and len(data) == 1 and isinstance(data[0], dict) and "error" in data[0]:
3041
- return data[0]
3042
- if not data:
3043
- return {"error": "No market data"}
3044
- return compute_market_stats(data)
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})
3045
3310
  except Exception as e:
3046
- return {"error": str(e)}
3311
+ return {"error": error_message(e)}
3047
3312
 
3048
3313
 
3049
3314
  # --------------- fundamental_context ---------------
@@ -3110,7 +3375,7 @@ def cmd_fundamental_context(args):
3110
3375
 
3111
3376
  return result
3112
3377
  except Exception as e:
3113
- return {"error": str(e)}
3378
+ return {"error": error_message(e)}
3114
3379
 
3115
3380
 
3116
3381
  # --------------- short-term sentiment: limit_up_pool / dragon_tiger / hot_stocks (A-share only) ---------------
@@ -3128,7 +3393,7 @@ def _resolve_cn_data_date(raw: str) -> tuple[datetime, datetime]:
3128
3393
  from trading_calendar import cn_trade_dates # flat sibling import
3129
3394
 
3130
3395
  req = datetime.strptime(raw, "%Y%m%d")
3131
- d = min(req, datetime.now().replace(hour=0, minute=0, second=0, microsecond=0))
3396
+ d = min(req, _cn_now().replace(hour=0, minute=0, second=0, microsecond=0))
3132
3397
  trade_dates = cn_trade_dates(d.year)
3133
3398
  if d.month == 1: # the walk-back can cross into December of the previous year
3134
3399
  trade_dates |= cn_trade_dates(d.year - 1)
@@ -3150,7 +3415,7 @@ def cmd_limit_up_pool(args):
3150
3415
  request resolves to itself and no stale label is emitted (the zt-pool payload
3151
3416
  carries no date column to cross-check against)."""
3152
3417
  try:
3153
- req, data_day = _resolve_cn_data_date((args.date or datetime.now().strftime("%Y%m%d")).replace("-", ""))
3418
+ req, data_day = _resolve_cn_data_date((args.date or _cn_now().strftime("%Y%m%d")).replace("-", ""))
3154
3419
  except ValueError:
3155
3420
  return {"error": f"limit_up_pool: invalid --date {args.date!r} (want YYYYMMDD)"}
3156
3421
  date = data_day.strftime("%Y%m%d")
@@ -3202,7 +3467,7 @@ def cmd_limit_up_pool(args):
3202
3467
  result["pool"] = pool
3203
3468
  return result
3204
3469
  except Exception as e:
3205
- return {"error": f"limit_up_pool date={requested}: {e}"}
3470
+ return {"error": f"limit_up_pool date={requested}: {error_message(e)}"}
3206
3471
 
3207
3472
 
3208
3473
  def _dragon_tiger_em(raw: str) -> list:
@@ -3267,7 +3532,7 @@ def cmd_dragon_tiger(args):
3267
3532
  date is served the last trading day's list, labeled with requested_date and
3268
3533
  stale=True."""
3269
3534
  try:
3270
- req, data_day = _resolve_cn_data_date((args.date or datetime.now().strftime("%Y-%m-%d")).replace("-", ""))
3535
+ req, data_day = _resolve_cn_data_date((args.date or _cn_now().strftime("%Y-%m-%d")).replace("-", ""))
3271
3536
  except ValueError:
3272
3537
  return {"error": f"dragon_tiger: invalid --date {args.date!r} (want YYYY-MM-DD)"}
3273
3538
  raw = data_day.strftime("%Y%m%d")
@@ -3280,7 +3545,7 @@ def cmd_dragon_tiger(args):
3280
3545
  try:
3281
3546
  rows = _failover(sources, label=f"dragon_tiger:{raw}")
3282
3547
  except Exception as e:
3283
- return {"error": str(e)}
3548
+ return {"error": error_message(e)}
3284
3549
  stale = req != data_day
3285
3550
  if not rows:
3286
3551
  result = {"date": date, "count": 0, "items": []}
@@ -3351,7 +3616,7 @@ def _hot_stocks_baidu() -> list:
3351
3616
  df = _akshare_retry(
3352
3617
  ak.stock_hot_search_baidu,
3353
3618
  symbol="A股",
3354
- date=datetime.now().strftime("%Y%m%d"),
3619
+ date=_cn_now().strftime("%Y%m%d"),
3355
3620
  time="今日",
3356
3621
  )
3357
3622
  if df is None or df.empty:
@@ -3376,7 +3641,7 @@ def cmd_hot_stocks(args):
3376
3641
  try:
3377
3642
  rows = _failover(sources, label="hot_stocks")
3378
3643
  except Exception as e:
3379
- return {"error": str(e)}
3644
+ return {"error": error_message(e)}
3380
3645
  if not rows:
3381
3646
  return {"count": 0, "items": [], "note": "no hot-rank data available"}
3382
3647
  items = [{k: v for k, v in r.items() if k != "source"} for r in rows[: args.top]]
@@ -3385,122 +3650,43 @@ def cmd_hot_stocks(args):
3385
3650
 
3386
3651
  # --------------- margin_trading / northbound_flow (A-share only) ---------------
3387
3652
 
3388
- # Exchange margin-detail column layouts differ: SSE carries 融资偿还额/融券偿还量,
3389
- # SZSE carries 融券余额/融资融券余额 — the output keys below follow the source.
3390
- _MARGIN_DETAIL_SSE_COLS = {
3391
- "标的证券代码": "symbol",
3392
- "标的证券简称": "name",
3393
- "融资余额": "margin_balance",
3394
- "融资买入额": "margin_buy",
3395
- "融资偿还额": "margin_repay",
3396
- "融券余量": "short_balance_shares",
3397
- "融券卖出量": "short_sell_shares",
3398
- "融券偿还量": "short_repay_shares",
3399
- }
3400
-
3401
- _MARGIN_DETAIL_SZSE_COLS = {
3402
- "证券代码": "symbol",
3403
- "证券简称": "name",
3404
- "融资买入额": "margin_buy",
3405
- "融资余额": "margin_balance",
3406
- "融券卖出量": "short_sell_shares",
3407
- "融券余量": "short_balance_shares",
3408
- "融券余额": "short_balance",
3409
- "融资融券余额": "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",
3410
3665
  }
3411
3666
 
3412
3667
 
3413
3668
  def cmd_margin_trading(args):
3414
- """Per-stock margin trading detail (融资融券) for the last N trading days.
3415
-
3416
- akshare has no per-stock margin history endpoint — the exchange detail lists
3417
- (stock_margin_detail_sse / stock_margin_detail_szse) are per-day, all-stock
3418
- tables, so each trading day is fetched and filtered to the symbol. Daily fetches
3419
- are independent and run concurrently (5 workers; a serial 60-day run would
3420
- overrun the host's 120s subprocess budget), then re-sorted newest-first — the
3421
- output never depends on completion order. A day that fails or has no data is
3422
- skipped (两融明细 T 日晚间才发布 — a morning run always misses the latest day);
3423
- only when every day failed is an error returned. Days where the symbol is
3424
- absent (non-margin underlying that day) are skipped; if no day yields a row the
3425
- 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)."""
3426
3673
  if detect_market(args.symbol) != "A":
3427
3674
  return {"error": "margin_trading only available for A-shares"}
3428
3675
  days = getattr(args, "days", 10)
3429
3676
  if days < 1:
3430
3677
  return {"error": f"days must be a positive integer, got {days}"}
3431
- # One akshare call per day — cap the fan-out so a slow source can't overrun
3432
- # the host's subprocess budget (rows carry their dates; truncation is evident).
3433
3678
  days = min(days, 60)
3434
- code = _cn_code(args.symbol)
3435
- exchange, bare = code[:2], code[2:]
3679
+ bare = _cn_code(args.symbol)[2:]
3436
3680
  try:
3437
- import concurrent.futures
3438
-
3439
- from trading_calendar import prev_trading_days
3440
-
3441
- if exchange == "sh":
3442
- col_map = _MARGIN_DETAIL_SSE_COLS
3443
- elif exchange == "sz":
3444
- col_map = _MARGIN_DETAIL_SZSE_COLS
3445
- else:
3446
- return {"error": f"margin_trading: {args.symbol} is a BSE code; only SSE/SZSE margin detail is available"}
3447
- trade_days = prev_trading_days("CN", days)
3448
- if not trade_days:
3449
- return {"error": "margin_trading: trading calendar unavailable"}
3450
-
3451
- def fetch_day(date_iso):
3452
- """Cache the published exchange-day table, then select this stock."""
3453
-
3454
- def fetch_table():
3455
- import akshare as ak
3456
-
3457
- detail_fn = ak.stock_margin_detail_sse if exchange == "sh" else ak.stock_margin_detail_szse
3458
- df = _akshare_retry(detail_fn, date=date_iso.replace("-", ""))
3459
- if df is None or df.empty:
3460
- return []
3461
- df = df.rename(columns=col_map)
3462
- required = {"symbol", "margin_buy", "margin_balance"}
3463
- if not required.issubset(df.columns):
3464
- raise ValueError("margin detail table missing financial columns")
3465
- df = df[df["symbol"].astype(str).str.fullmatch(r"\d{6}")]
3466
- if "信用交易日期" in df.columns:
3467
- source_days = df["信用交易日期"].map(lambda v: re.sub(r"\D", "", str(v))[:8])
3468
- if not (source_days == date_iso.replace("-", "")).all():
3469
- raise ValueError("margin detail table date does not match requested day")
3470
- keep = [c for c in col_map.values() if c in df.columns]
3471
- table = [{"date": date_iso, **_clean_row(r)} for r in df[keep].to_dict("records")]
3472
- for row in table:
3473
- values = [_parse_float(row.get(field)) for field in ("margin_buy", "margin_balance")]
3474
- if any(value is None or not math.isfinite(value) for value in values):
3475
- # Keep usable stocks without persisting an incomplete exchange snapshot.
3476
- return {"rows": table, "warnings": ["incomplete financial values"]}
3477
- return table
3478
-
3479
- try:
3480
- today = datetime.now(ZoneInfo("Asia/Shanghai")).date().isoformat()
3481
- table = (
3482
- cached(f"margin-table:{exchange}:{date_iso}", DAY, fetch_table)
3483
- if date_iso < today
3484
- else fetch_table()
3485
- )
3486
- if isinstance(table, dict):
3487
- table = table["rows"]
3488
- return date_iso, [r for r in table if str(r.get("symbol")) == bare], None
3489
- except Exception as e:
3490
- return date_iso, [], f"{date_iso}: {type(e).__name__}: {scrub_secrets(str(e))}"
3491
-
3492
- rows = []
3493
- failures = []
3494
- with socket_timeout(25), concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool:
3495
- for _date_iso, day_rows, failure in pool.map(fetch_day, trade_days):
3496
- if failure:
3497
- failures.append(failure)
3498
- rows.extend(day_rows)
3499
- 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
+ ]
3500
3689
  if not rows:
3501
- if failures:
3502
- msg = f"margin detail fetch failed for all {len(trade_days)} day(s): {'; '.join(failures)}"
3503
- return {"symbol": args.symbol, "error": msg}
3504
3690
  return {"symbol": args.symbol, "error": f"no margin trading data for {args.symbol} (可能不是两融标的)"}
3505
3691
  if getattr(args, "include_ratio", False):
3506
3692
  try:
@@ -3530,7 +3716,7 @@ def cmd_margin_trading(args):
3530
3716
  row["margin_buy_ratio_note"] = "matching-date CNY turnover unavailable"
3531
3717
  return rows
3532
3718
  except Exception as e:
3533
- return {"error": str(e)}
3719
+ return {"error": error_message(e)}
3534
3720
 
3535
3721
 
3536
3722
  def _northbound_row_is_shell(row: dict) -> bool:
@@ -3587,7 +3773,7 @@ def cmd_northbound_flow(args):
3587
3773
  rows.reverse()
3588
3774
  return rows
3589
3775
  except Exception as e:
3590
- return {"error": str(e)}
3776
+ return {"error": error_message(e)}
3591
3777
 
3592
3778
 
3593
3779
  # --------------- CLI ---------------
@@ -3609,6 +3795,13 @@ def main():
3609
3795
  p_quote = sub.add_parser("quote")
3610
3796
  p_quote.add_argument("symbol")
3611
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
+
3612
3805
  p_fund_quote = sub.add_parser("fund_quote")
3613
3806
  p_fund_quote.add_argument("symbol")
3614
3807
 
@@ -3624,9 +3817,11 @@ def main():
3624
3817
  p_fin.add_argument("symbol")
3625
3818
  p_fin.add_argument("--periods", type=int, default=1)
3626
3819
  p_fin.add_argument("--annual", action="store_true", help="Select annual reports; periods=10 requests ten years")
3820
+ p_fin.add_argument("--include-dcf-inputs", action="store_true", help="Add annual A/HK/US DCF input fields")
3627
3821
 
3628
3822
  p_snap = sub.add_parser("market_snapshot")
3629
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")
3630
3825
 
3631
3826
  p_idx = sub.add_parser("market_indices")
3632
3827
  p_idx.add_argument("--region", default="cn", choices=["cn", "hk", "us", "jp", "kr", "tw"])
@@ -3635,6 +3830,7 @@ def main():
3635
3830
  p_sec.add_argument("--top", type=int, default=10)
3636
3831
  p_sec.add_argument("--direction", default="top", choices=["top", "bottom", "both"])
3637
3832
  p_sec.add_argument("--board-type", default="industry", choices=["industry", "concept"])
3833
+ p_sec.add_argument("--market", default="A", choices=["A", "US"])
3638
3834
 
3639
3835
  p_cons = sub.add_parser("sector_constituents")
3640
3836
  p_cons.add_argument("sector")
@@ -3649,7 +3845,8 @@ def main():
3649
3845
  p_chip = sub.add_parser("chip_distribution")
3650
3846
  p_chip.add_argument("symbol")
3651
3847
 
3652
- sub.add_parser("market_stats")
3848
+ p_stats = sub.add_parser("market_stats")
3849
+ p_stats.add_argument("--market", default="A", choices=["A", "HK", "US"])
3653
3850
 
3654
3851
  p_fund = sub.add_parser("fundamental_context")
3655
3852
  p_fund.add_argument("symbol")
@@ -3681,6 +3878,8 @@ def main():
3681
3878
  dispatch = {
3682
3879
  "kline": cmd_kline,
3683
3880
  "quote": cmd_quote,
3881
+ "quotes": cmd_quotes,
3882
+ "klines": cmd_klines,
3684
3883
  "fund_quote": cmd_fund_quote,
3685
3884
  "capital_flow": cmd_capital_flow,
3686
3885
  "news": cmd_news,
@@ -3707,6 +3906,10 @@ def main():
3707
3906
  output(result)
3708
3907
  if isinstance(result, dict) and "error" in result:
3709
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)
3710
3913
 
3711
3914
 
3712
3915
  if __name__ == "__main__":