@weaxs/openclaw-stock-analysis 0.2.10 → 0.2.12

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 (41) hide show
  1. package/README.md +11 -51
  2. package/dist/index.js +3693 -1971
  3. package/index.ts +74 -1636
  4. package/openclaw.plugin.json +3 -0
  5. package/package.json +3 -3
  6. package/scripts/cli-json.mjs +23 -0
  7. package/scripts/exec-process.mjs +89 -0
  8. package/scripts/setup-python.mjs +37 -4
  9. package/scripts/tool-calls.mjs +489 -0
  10. package/skills/event-driven/SKILL.md +2 -2
  11. package/skills/growth-quality/SKILL.md +1 -1
  12. package/skills/stock-analysis/SKILL.md +12 -7
  13. package/skills/stock-analysis/references/stateless-analysis.md +9 -1
  14. package/tools/_cache.py +13 -0
  15. package/tools/_subproc.py +46 -0
  16. package/tools/alert_rules.py +65 -16
  17. package/tools/backtest.py +3 -3
  18. package/tools/capabilities.py +32 -9
  19. package/tools/cli_budgets.json +2 -0
  20. package/tools/diagnostics.py +3 -4
  21. package/tools/gather.py +2 -6
  22. package/tools/import_parser.py +3 -4
  23. package/tools/lixinger.py +119 -27
  24. package/tools/market_regime.py +5 -2
  25. package/tools/market_review.py +5 -6
  26. package/tools/name_resolver.py +82 -21
  27. package/tools/report_renderer.py +3 -3
  28. package/tools/requirements.txt +6 -5
  29. package/tools/risk_screening.py +3 -2
  30. package/tools/screener.py +6 -4
  31. package/tools/search_intel.py +81 -12
  32. package/tools/sec_edgar.py +266 -0
  33. package/tools/sec_holders.py +6 -0
  34. package/tools/signal_tracker.py +11 -10
  35. package/tools/stateless_analysis.py +115 -0
  36. package/tools/stock_data.py +707 -222
  37. package/tools/tiaa_disclosures.py +267 -0
  38. package/tools/tool_schemas.json +2704 -0
  39. package/tools/trading_calendar.py +65 -30
  40. package/tools/value_data.py +366 -134
  41. package/tools/watchlist.py +43 -5
@@ -3,27 +3,28 @@
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
13
11
  import time
14
12
  from datetime import date, datetime, timedelta, timezone
15
13
  from pathlib import Path
16
14
  from zoneinfo import ZoneInfo
17
15
 
18
16
  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
17
+ from _cache import CACHE_DIR, DAY, WEEK, atomic_write, cached, cached_values # noqa: E402
18
+ from _subproc import error_message, run_tool, scrub_secrets, socket_timeout, utf8_stdio # noqa: E402
19
+ from trading_calendar import market_now # noqa: E402
21
20
 
22
21
  # Explicitly exchange-prefixed A-share codes (sh600519 / sz000858 / bj920001).
23
22
  # Bare 6-digit codes are always stocks; the prefix is the disambiguation escape
24
23
  # hatch for index codes (sh000001 = 上证指数, not 平安银行).
25
24
  _A_PREFIXED_RE = re.compile(r"(?:sh|sz|bj)\d{6}", re.IGNORECASE)
26
25
  _A_INDEX_RE = re.compile(r"(?:sh000\d{3}|sz399\d{3})", re.IGNORECASE)
26
+ # Yahoo has no Hang Seng Tech index; Tencent serves its live quote and daily history.
27
+ _TENCENT_HK_INDEX = {"^HSTECH": "hkHSTECH"}
27
28
 
28
29
 
29
30
  def detect_market(symbol: str) -> str:
@@ -88,7 +89,6 @@ def calc_limit_price(pre_close: float, ratio: float, direction: str = "up") -> f
88
89
  _STICKY_CHAINS = {
89
90
  "kline_a",
90
91
  "quote_a",
91
- "quote_a_etf",
92
92
  "snapshot_a",
93
93
  "sector_constituents",
94
94
  "sina",
@@ -108,16 +108,19 @@ def _failover(sources: list, label: str, evidence: bool = False):
108
108
  entry costs one failed attempt, then the new winner replaces it (self-healing).
109
109
  """
110
110
  chain = label.split(":", 1)[0]
111
+ sticky = None
111
112
  if chain in _STICKY_CHAINS:
112
113
  sticky = _disk_cache_get(f"sticky-{chain}")
113
114
  if sticky:
114
- sources = sorted(sources, key=lambda s: s[0] != sticky)
115
+ # Single-symbol A quotes always try Tencent first; sticky only reorders the fallbacks behind it.
116
+ first = "tencent" if chain == "quote_a" else sticky
117
+ sources = sorted(sources, key=lambda s: (s[0] != first, s[0] != sticky))
115
118
  errors = []
116
119
  for name, fn in sources:
117
120
  try:
118
121
  result = fn()
119
122
  if result:
120
- if chain in _STICKY_CHAINS:
123
+ if chain in _STICKY_CHAINS and name != sticky:
121
124
  _disk_cache_set(f"sticky-{chain}", name)
122
125
  if evidence:
123
126
  fetched_at = datetime.now(timezone.utc).isoformat()
@@ -225,7 +228,7 @@ def _kline_baostock(symbol: str, period: str, count: int) -> list:
225
228
  import pandas as pd
226
229
 
227
230
  freq_map = {"daily": "d", "weekly": "w", "monthly": "m"}
228
- end = datetime.now()
231
+ end = _cn_now()
229
232
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
230
233
  # baostock login/logout print status lines to stdout — silence them so the
231
234
  # tool's JSON output on stdout stays clean for the calling host.
@@ -331,9 +334,9 @@ def _unix_quote_time(value) -> str | None:
331
334
  return None
332
335
 
333
336
 
334
- def _quote_time(value: str, fmt: str) -> str | None:
337
+ def _quote_time(value: str, fmt: str, tz=timezone(timedelta(hours=8))) -> str | None:
335
338
  try:
336
- return datetime.strptime(value, fmt).replace(tzinfo=timezone(timedelta(hours=8))).isoformat()
339
+ return datetime.strptime(value, fmt).replace(tzinfo=tz).isoformat()
337
340
  except ValueError:
338
341
  return None
339
342
 
@@ -342,9 +345,14 @@ def _tencent_payload_row(f: list, symbol: str) -> dict:
342
345
  """Build the quote row from one Tencent `~`-separated payload (already split).
343
346
  Shared by _quote_tencent (single) and _tencent_batch_quotes (snapshot leg)."""
344
347
  f += [""] * (50 - len(f)) # pad optional tail fields (pe/pb/market_cap/volume_ratio…) on short payloads
345
- hk = detect_market(symbol) == "HK"
348
+ market = detect_market(symbol)
349
+ foreign = market in ("HK", "US") # HK and US share currency-unit turnover and lack the A-share ratio fields
346
350
  turnover = _parse_float(f[37])
347
351
  market_cap = _parse_float(f[45])
352
+ if market == "US":
353
+ as_of = _quote_time(f[30], "%Y-%m-%d %H:%M:%S", ZoneInfo("America/New_York"))
354
+ else:
355
+ as_of = _quote_time(f[30], "%Y/%m/%d %H:%M:%S" if foreign else "%Y%m%d%H%M%S")
348
356
  return {
349
357
  "symbol": symbol,
350
358
  "name": f[1],
@@ -352,7 +360,7 @@ def _tencent_payload_row(f: list, symbol: str) -> dict:
352
360
  "change": _parse_float(f[31]),
353
361
  "change_pct": _parse_float(f[32]),
354
362
  "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,
363
+ "turnover": turnover * (1 if foreign else 1e4) if turnover is not None else None,
356
364
  "high": _parse_float(f[33]),
357
365
  "low": _parse_float(f[34]),
358
366
  "open": _parse_float(f[5]),
@@ -360,50 +368,94 @@ def _tencent_payload_row(f: list, symbol: str) -> dict:
360
368
  "market_cap": market_cap * 1e8 if market_cap is not None else None,
361
369
  "pe": _parse_float(f[39]),
362
370
  "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]),
371
+ # Tencent's HK map labels f[47] DY and f[49] WK52_Low, and US f[46] is the English name.
372
+ "pb": None if foreign else _parse_float(f[46]),
373
+ "turnover_rate": None if foreign else _parse_float(f[38]),
374
+ "volume_ratio": None if foreign else _parse_float(f[49]),
367
375
  "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"),
376
+ "data_as_of": as_of,
369
377
  }
370
378
 
371
379
 
372
- def _quote_tencent(symbol: str) -> dict:
373
- """Tencent A/HK quote via qt.gtimg.cn, with a GBK-encoded `~` payload.
380
+ def _tencent_hk_code(symbol: str) -> str:
381
+ if not re.fullmatch(r"[0-9]{1,5}\.HK", symbol, re.IGNORECASE):
382
+ raise ValueError(f"invalid HK symbol: {symbol}")
383
+ return f"hk{symbol[:-3].zfill(5)}"
384
+
385
+
386
+ def _tencent_us_code(symbol: str) -> str:
387
+ """Tencent spells class shares with a dot (BRK.B) where Yahoo uses a dash (BRK-B)."""
388
+ if not re.fullmatch(r"[A-Za-z]{1,6}(?:[.-][A-Za-z])?", symbol):
389
+ raise ValueError(f"invalid US symbol: {symbol}")
390
+ return "us" + symbol.upper().replace("-", ".")
374
391
 
375
- A-share turnover is 万元; HK turnover is already HKD. Market cap is 亿元
376
- (CNY/HKD respectively); normalize monetary fields to currency units."""
392
+
393
+ def _tencent_fields(code: str) -> list:
377
394
  import requests
378
395
 
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
396
  resp = requests.get(f"https://qt.gtimg.cn/q={code}", timeout=10)
385
397
  resp.raise_for_status()
386
- f = _gbk_var_payload(resp.content).split("~")
398
+ return _gbk_var_payload(resp.content).split("~")
399
+
400
+
401
+ def _quote_tencent(symbol: str) -> dict:
402
+ """Tencent A/HK/US quote via qt.gtimg.cn, with a GBK-encoded `~` payload.
403
+
404
+ A-share turnover is 万元; HK/US turnover is already HKD/USD. Market cap is 亿
405
+ in the quote currency; normalize monetary fields to currency units."""
406
+ market = detect_market(symbol)
407
+ if market == "HK":
408
+ # Tencent's hk feed is delayed 15 minutes; r_hk uses the same fields for live quotes.
409
+ code = f"r_{_tencent_hk_code(symbol)}"
410
+ elif market == "US":
411
+ code = _tencent_us_code(symbol)
412
+ else:
413
+ code = _cn_code(symbol)
414
+ f = _tencent_fields(code)
387
415
  if len(f) < 35 or not f[1]:
388
416
  raise ValueError(f"tencent returned no quote for {symbol}")
389
417
  row = _clean_row(_tencent_payload_row(f, symbol))
390
- if hk and (row["price"] is None or row["price"] <= 0):
418
+ if market != "A" and (row["price"] is None or row["price"] <= 0):
391
419
  raise ValueError(f"tencent returned no quote for {symbol}")
392
420
  if normalize_stock_code(symbol)["is_etf"]:
393
- row["is_etf"] = True
421
+ # f[73] is total fund shares and f[78] the intraday IOPV (blank for money/bond ETFs);
422
+ # premium matches _quote_akshare_etf.
423
+ iopv = _parse_float(f[78]) if len(f) > 78 else None
424
+ price = row.get("price")
425
+ row.update(
426
+ is_etf=True,
427
+ fund_shares=_parse_float(f[73]) if len(f) > 73 else None,
428
+ iopv=iopv,
429
+ premium_discount_rate=round((price - iopv) / iopv * 100, 4) if price is not None and iopv else None,
430
+ )
394
431
  return row
395
432
 
396
433
 
397
434
  def _kline_tencent(symbol: str, period: str, count: int) -> list:
398
435
  """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."""
436
+ volume] ascending (A: 手, HK/US: shares); raw series are accepted only for explicit indices.
437
+ HK/US stocks need the hk/us endpoints: the generic one ignores qfq for them."""
400
438
  import requests
401
439
 
402
440
  p_map = {"daily": "day", "weekly": "week", "monthly": "month"}
403
441
  p = p_map.get(period, "day")
404
- code = _cn_code(symbol)
442
+ market = detect_market(symbol)
443
+ hk_index = symbol.upper() in _TENCENT_HK_INDEX
444
+ endpoint = "fqkline"
445
+ if hk_index:
446
+ code = _TENCENT_HK_INDEX[symbol.upper()]
447
+ elif market == "HK":
448
+ endpoint, code = "hkfqkline", _tencent_hk_code(symbol)
449
+ elif market == "US":
450
+ # usfqkline only adjusts the exchange-suffixed code (AAPL.OQ) that the quote API reports.
451
+ f = _tencent_fields(_tencent_us_code(symbol))
452
+ if len(f) < 3 or not f[2]:
453
+ raise ValueError(f"tencent returned no quote for {symbol}")
454
+ endpoint, code = "usfqkline", f"us{f[2]}"
455
+ else:
456
+ code = _cn_code(symbol)
405
457
  resp = requests.get(
406
- "https://web.ifzq.gtimg.cn/appstock/app/fqkline/get",
458
+ f"https://web.ifzq.gtimg.cn/appstock/app/{endpoint}/get",
407
459
  params={"param": f"{code},{p},,,{count},qfq"},
408
460
  timeout=10,
409
461
  )
@@ -411,9 +463,11 @@ def _kline_tencent(symbol: str, period: str, count: int) -> list:
411
463
  if data.get("code") != 0:
412
464
  raise ValueError(f"tencent kline failed: {data.get('msg', '')}")
413
465
  node = data.get("data", {}).get(code, {})
414
- rows = node.get(p) if _A_INDEX_RE.fullmatch(symbol) else node.get(f"qfq{p}")
466
+ raw = _A_INDEX_RE.fullmatch(symbol) or hk_index
467
+ rows = node.get(p) if raw else node.get(f"qfq{p}")
415
468
  if not rows:
416
469
  raise ValueError("tencent kline returned empty data for required adjustment")
470
+ extra = {} if market == "A" else {"adjusted": "none" if raw else "qfq"}
417
471
  return [
418
472
  _clean_row(
419
473
  {
@@ -423,6 +477,7 @@ def _kline_tencent(symbol: str, period: str, count: int) -> list:
423
477
  "low": _parse_float(r[4]),
424
478
  "close": _parse_float(r[2]),
425
479
  "volume": _parse_float(r[5]),
480
+ **extra,
426
481
  }
427
482
  )
428
483
  for r in rows[-count:]
@@ -520,6 +575,10 @@ def _sanitize(obj):
520
575
  return obj
521
576
 
522
577
 
578
+ def _cn_now() -> datetime:
579
+ return market_now("CN")
580
+
581
+
523
582
  def _clean_row(d: dict) -> dict:
524
583
  return {k: _sanitize(v) for k, v in d.items()}
525
584
 
@@ -554,11 +613,16 @@ def _kline_akshare(symbol: str, period: str, count: int) -> list:
554
613
  import akshare as ak
555
614
 
556
615
  period_map = {"daily": "daily", "weekly": "weekly", "monthly": "monthly"}
557
- end = datetime.now()
616
+ end = _cn_now()
558
617
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
559
618
 
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
619
+ # ETFs (51/52/56/58/15/16/18xxxx) are not covered by stock_zh_a_hist.
620
+ # akshare 1.20.44-1.20.46 renamed several APIs; py3.10 stays below 1.20.40 with the old names.
621
+ hist_fn = (
622
+ ak.fund_etf_hist_em
623
+ if normalize_stock_code(symbol)["is_etf"]
624
+ else getattr(ak, "stock_zh_a_hist_em", None) or ak.stock_zh_a_hist
625
+ )
562
626
  df = _akshare_retry(
563
627
  hist_fn,
564
628
  symbol=symbol,
@@ -603,7 +667,7 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
603
667
 
604
668
  if normalize_stock_code(symbol)["is_etf"]:
605
669
  raise ValueError("tushare stock adjustment does not cover ETFs")
606
- end = datetime.now()
670
+ end = _cn_now()
607
671
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
608
672
 
609
673
  ts_code = f"{symbol}.{_cn_code(symbol)[:2].upper()}"
@@ -718,69 +782,6 @@ def _quote_tushare(symbol: str) -> dict:
718
782
  )
719
783
 
720
784
 
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
785
  def _kline_yfinance(symbol: str, period: str, count: int) -> list:
785
786
  import yfinance as yf
786
787
 
@@ -1028,25 +1029,38 @@ def _quote_alphavantage(symbol: str) -> dict:
1028
1029
  )
1029
1030
 
1030
1031
 
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).
1032
+ def _yf_failover(symbol: str, yfinance, tencent, finnhub, longbridge, alphavantage, label: str, evidence: bool = False):
1033
+ """Shared HK/US/JP/KR/TW failover chain. HK: yfinance → tencent → finnhub → longbridge;
1034
+ US: yfinance → finnhub → longbridge → tencent → alphavantage; JP/KR/TW: yfinance only.
1033
1035
 
1036
+ Keyless Tencent backs up HK first (finnhub's free tier has no HK). For US, configured
1037
+ keyed sources go first and Tencent precedes alphavantage to spare its 25 calls/day.
1034
1038
  Each provider is a zero-arg callable; callers bind their own symbol/period/count.
1035
1039
  """
1036
1040
  market = detect_market(symbol)
1037
1041
  sources = [("yfinance", yfinance)]
1038
- if market in ("HK", "US"):
1039
- sources += [("finnhub", finnhub), ("longbridge", longbridge)]
1040
- if market == "US":
1041
- sources.append(("alphavantage", alphavantage))
1042
+ if market == "HK":
1043
+ sources += [("tencent", tencent), ("finnhub", finnhub), ("longbridge", longbridge)]
1044
+ elif market == "US":
1045
+ sources += [
1046
+ ("finnhub", finnhub),
1047
+ ("longbridge", longbridge),
1048
+ ("tencent", tencent),
1049
+ ("alphavantage", alphavantage),
1050
+ ]
1042
1051
  return _failover(sources, label=label, evidence=evidence)
1043
1052
 
1044
1053
 
1045
1054
  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)."""
1055
+ """HK/US/JP/KR/TW kline; see _yf_failover for the chain."""
1056
+ if symbol.upper() in _TENCENT_HK_INDEX:
1057
+ with socket_timeout(25):
1058
+ sources = [("tencent", lambda: _kline_tencent(symbol, period, count))]
1059
+ return _failover(sources, label=f"kline_hk_index:{symbol}", evidence=evidence)
1047
1060
  return _yf_failover(
1048
1061
  symbol,
1049
1062
  lambda: _kline_yfinance(symbol, period, count),
1063
+ lambda: _kline_tencent(symbol, period, count),
1050
1064
  lambda: _kline_finnhub(symbol, period, count),
1051
1065
  lambda: _kline_longbridge(symbol, period, count),
1052
1066
  lambda: _kline_alphavantage(symbol, period, count),
@@ -1067,34 +1081,34 @@ def cmd_kline(args):
1067
1081
  data = kline_yf(args.symbol, args.period, args.count, evidence=True)
1068
1082
  return data
1069
1083
  except Exception as e:
1070
- return {"error": str(e)}
1084
+ return {"error": error_message(e)}
1071
1085
 
1072
1086
 
1073
1087
  # --------------- quote ---------------
1074
1088
 
1075
1089
 
1076
1090
  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.
1091
+ """A-share quote with failover: tencent → akshare → tushare → efinance → sina; ETFs add fund spot after tencent.
1092
+
1093
+ Tencent answers one symbol per request; the eastmoney spot sources page the whole
1094
+ market (~55 requests) to return one row, so they only serve as fallbacks.
1078
1095
 
1079
1096
  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
1097
+ tencent/sina — the eastmoney sources take bare 6-digit codes only. ETF quotes use their own sticky chain key
1082
1098
  (quote_a_etf) so a stock-side winner never demotes the fund-spot source."""
1083
1099
  if _A_PREFIXED_RE.fullmatch(symbol):
1084
1100
  sources = [("tencent", lambda: _quote_tencent(symbol)), ("sina", lambda: _quote_sina(symbol))]
1085
1101
  with socket_timeout(25):
1086
1102
  return _failover(sources, label=f"quote_a:{symbol}", evidence=evidence)
1087
1103
  is_etf = normalize_stock_code(symbol)["is_etf"]
1088
- sources = []
1104
+ sources = [("tencent", lambda: _quote_tencent(symbol))]
1089
1105
  if is_etf:
1090
1106
  sources.append(("akshare_etf", lambda: _quote_akshare_etf(symbol)))
1091
1107
  sources += [
1092
1108
  ("akshare", lambda: _quote_akshare(symbol)),
1093
1109
  ("tushare", lambda: _quote_tushare(symbol)),
1094
1110
  ("efinance", lambda: _quote_efinance(symbol)),
1095
- ("tencent", lambda: _quote_tencent(symbol)),
1096
1111
  ("sina", lambda: _quote_sina(symbol)),
1097
- ("pytdx", lambda: _quote_pytdx(symbol)),
1098
1112
  ]
1099
1113
  chain = "quote_a_etf" if is_etf else "quote_a"
1100
1114
  with socket_timeout(25):
@@ -1231,21 +1245,14 @@ def _quote_finnhub(symbol: str) -> dict:
1231
1245
 
1232
1246
 
1233
1247
  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
- )
1248
+ """HK/US/JP/KR/TW quote; see _yf_failover for the chain."""
1249
+ if symbol.upper() in _TENCENT_HK_INDEX:
1250
+ sources = [("tencent", lambda: {"symbol": symbol, **_quote_tencent_hk_index(symbol.upper())})]
1251
+ return _failover(sources, label=f"quote_hk_index:{symbol}", evidence=evidence)
1246
1252
  return _yf_failover(
1247
1253
  symbol,
1248
1254
  lambda: _quote_yfinance(symbol),
1255
+ lambda: _quote_tencent(symbol),
1249
1256
  lambda: _quote_finnhub(symbol),
1250
1257
  lambda: _quote_longbridge(symbol),
1251
1258
  lambda: _quote_alphavantage(symbol),
@@ -1286,12 +1293,132 @@ def _quote_stale_marker(market: str):
1286
1293
  }
1287
1294
 
1288
1295
 
1296
+ _YOUZHIYOUXING_TIAA = {
1297
+ "TIAA001001": "长钱账户",
1298
+ "TIAA001002": "海外长钱",
1299
+ "TIAA001004": "稳钱账户",
1300
+ }
1301
+
1302
+
1303
+ def _quote_tiaa(symbol: str) -> dict:
1304
+ import requests
1305
+
1306
+ disclosure_name = _YOUZHIYOUXING_TIAA.get(symbol)
1307
+ result = {
1308
+ "symbol": symbol,
1309
+ "name": None,
1310
+ "asset_type": "investment_advisory_portfolio",
1311
+ "type_description": "雪球投顾组合,无统一单位净值",
1312
+ "manager": None,
1313
+ "advisor_code": f"A{symbol[4:7]}",
1314
+ "advisor_org": None,
1315
+ "advisor_mapping_basis": "tiaa_prefix_best_effort",
1316
+ "advisor_source_url": None,
1317
+ "advisor_retrieved_at": None,
1318
+ "nav": None,
1319
+ "example_account_performance": None,
1320
+ "quote_type": None,
1321
+ "exchange": None,
1322
+ "source": None,
1323
+ "source_url": f"https://xueqiu.com/S/{symbol}",
1324
+ "data_as_of": None,
1325
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
1326
+ "official_disclosures": [],
1327
+ "public_search_results": [],
1328
+ "disclosure_provider": "youzhiyouxing" if disclosure_name else None,
1329
+ "disclosure_status": "unavailable" if disclosure_name else "unsupported_provider",
1330
+ "disclosures_partial": True,
1331
+ "disclosures_as_of": None,
1332
+ "note": (
1333
+ "投顾组合无统一净值;主理人及完整业绩/持仓需在对应投顾平台登录后查询;"
1334
+ "官方公开披露仅为部分历史信息,持有人收益随买入时点和调仓跟进不同"
1335
+ ),
1336
+ }
1337
+ try:
1338
+ with requests.Session() as session:
1339
+ session.get(
1340
+ "https://xueqiu.com/",
1341
+ headers={"User-Agent": "Mozilla/5.0"},
1342
+ timeout=10,
1343
+ allow_redirects=True,
1344
+ ).raise_for_status()
1345
+ response = session.get(
1346
+ "https://stock.xueqiu.com/v5/stock/quote.json",
1347
+ params={"symbol": symbol, "extend": "detail"},
1348
+ headers={"User-Agent": "Mozilla/5.0", "Referer": "https://xueqiu.com/"},
1349
+ timeout=10,
1350
+ )
1351
+ response.raise_for_status()
1352
+ payload = response.json()
1353
+ if payload.get("error_code") not in (None, 0):
1354
+ raise ValueError("xueqiu portfolio metadata unavailable")
1355
+ quote = (payload.get("data") or {}).get("quote") or {}
1356
+ if quote.get("symbol") != symbol:
1357
+ raise ValueError("xueqiu returned different portfolio metadata")
1358
+ result.update(
1359
+ name=quote.get("name"),
1360
+ quote_type=quote.get("type"),
1361
+ exchange=quote.get("exchange"),
1362
+ source="xueqiu",
1363
+ data_as_of=_unix_quote_time(quote.get("timestamp")),
1364
+ )
1365
+ except Exception:
1366
+ result["warnings"] = ["xueqiu portfolio name unavailable; type guidance remains available"]
1367
+ if disclosure_name and result["name"] in (None, disclosure_name):
1368
+ try:
1369
+ from tiaa_disclosures import public_disclosures
1370
+
1371
+ disclosure = public_disclosures(symbol, disclosure_name)
1372
+ result["official_disclosures"] = disclosure["items"]
1373
+ result["disclosures_as_of"] = disclosure["as_of"]
1374
+ if disclosure["items"]:
1375
+ result["disclosure_status"] = "partial_public"
1376
+ except Exception:
1377
+ result.setdefault("warnings", []).append(
1378
+ "official public disclosures unavailable; type guidance remains available"
1379
+ )
1380
+ elif disclosure_name and result["name"]:
1381
+ result["disclosure_status"] = "unsupported_provider"
1382
+ result.setdefault("warnings", []).append("portfolio name does not match the registered disclosure source")
1383
+ else:
1384
+ from tiaa_disclosures import search_public
1385
+
1386
+ result["public_search_results"] = search_public(symbol, result["name"])
1387
+ if result["public_search_results"]:
1388
+ result["disclosure_provider"] = "web_search"
1389
+ result["disclosure_status"] = "search_partial"
1390
+ try:
1391
+ from tiaa_disclosures import advisor_metadata
1392
+
1393
+ result.update(advisor_metadata(symbol))
1394
+ except Exception:
1395
+ result.setdefault("warnings", []).append("advisor organization metadata unavailable")
1396
+ return result
1397
+
1398
+
1289
1399
  def cmd_quote(args):
1290
- market = detect_market(args.symbol)
1400
+ if re.fullmatch(r"TIAA\d{6}", args.symbol, re.IGNORECASE):
1401
+ return _quote_tiaa(args.symbol.upper())
1402
+ # detect_market reads every ^index as US; known region indices keep their own calendar for freshness.
1403
+ market = next(
1404
+ (region.upper() for region, symbols in _REGION_INDEX_SYMBOLS.items() if args.symbol.upper() in symbols),
1405
+ None,
1406
+ ) or detect_market(args.symbol)
1291
1407
  try:
1292
1408
  result = quote_a(args.symbol, evidence=True) if market == "A" else quote_yf(args.symbol, evidence=True)
1293
1409
  except Exception as e:
1294
- return {"error": str(e)}
1410
+ result = {"error": error_message(e)}
1411
+ # Exchange quotes cannot see open-end funds. Stock-only prefixes (SH 60/68, ChiNext 30, BSE) keep the bare error.
1412
+ if (
1413
+ isinstance(result, dict)
1414
+ and "error" in result
1415
+ and re.fullmatch(r"\d{6}", args.symbol)
1416
+ and not re.match(r"(?:60|68|30|43|83|87|92)", args.symbol)
1417
+ ):
1418
+ result["hint"] = (
1419
+ f"{args.symbol} 若为场外公募基金,请改用 get_fund_quote(stock_data.py fund_quote {args.symbol})"
1420
+ )
1421
+ return result
1295
1422
  if isinstance(result, dict) and "error" not in result:
1296
1423
  marker = _quote_stale_marker(market)
1297
1424
  if marker:
@@ -1322,6 +1449,109 @@ def cmd_quote(args):
1322
1449
  return result
1323
1450
 
1324
1451
 
1452
+ # --------------- open-end fund quote ---------------
1453
+
1454
+
1455
+ def _fund_js_value(text: str, name: str):
1456
+ match = re.search(rf"\b{re.escape(name)}\s*=\s*", text)
1457
+ if not match:
1458
+ raise ValueError(f"eastmoney fund data missing {name}")
1459
+ try:
1460
+ return json.JSONDecoder().raw_decode(text[match.end() :])[0]
1461
+ except json.JSONDecodeError as exc:
1462
+ raise ValueError(f"eastmoney fund data has invalid {name}") from exc
1463
+
1464
+
1465
+ def _fund_nav_date(stamp) -> str:
1466
+ return datetime.fromtimestamp(stamp / 1000, timezone.utc).astimezone(ZoneInfo("Asia/Shanghai")).date().isoformat()
1467
+
1468
+
1469
+ def _fund_estimate(symbol: str) -> dict:
1470
+ import akshare as ak
1471
+
1472
+ with socket_timeout(25):
1473
+ df = ak.fund_value_estimation_em()
1474
+ if df is None or df.empty or "基金代码" not in df.columns:
1475
+ return {}
1476
+ rows = df[df["基金代码"] == symbol]
1477
+ value_col = next((c for c in df.columns if c.endswith("-估算数据-估算值")), None)
1478
+ change_col = next((c for c in df.columns if c.endswith("-估算数据-估算增长率")), None)
1479
+ if rows.empty or not value_col:
1480
+ return {}
1481
+ row = rows.iloc[0]
1482
+ estimated_nav = _parse_float(row.get(value_col))
1483
+ if estimated_nav is None:
1484
+ return {}
1485
+ return {
1486
+ "estimated_nav": estimated_nav,
1487
+ "estimated_change_pct": _parse_float(str(row.get(change_col)).rstrip("%")) if change_col else None,
1488
+ "estimated_at": value_col.split("-估算数据-", 1)[0],
1489
+ "estimate_status": "available",
1490
+ }
1491
+
1492
+
1493
+ def cmd_fund_quote(args):
1494
+ symbol = args.symbol
1495
+ if not re.fullmatch(r"\d{6}", symbol):
1496
+ return {"error": "open-end fund symbol must be exactly 6 digits"}
1497
+ try:
1498
+ import requests
1499
+
1500
+ response = requests.get(f"https://fund.eastmoney.com/pingzhongdata/{symbol}.js", timeout=15)
1501
+ response.raise_for_status()
1502
+ text = response.text
1503
+ if _fund_js_value(text, "fS_code") != symbol:
1504
+ raise ValueError(f"eastmoney returned a different fund for {symbol}")
1505
+ name = _fund_js_value(text, "fS_name")
1506
+ nav_rows = _fund_js_value(text, "Data_netWorthTrend")
1507
+ accumulated_rows = _fund_js_value(text, "Data_ACWorthTrend")
1508
+ if not nav_rows:
1509
+ raise ValueError(f"eastmoney returned no NAV for {symbol}")
1510
+ latest = nav_rows[-1]
1511
+ nav_date = _fund_nav_date(latest["x"])
1512
+ accumulated_by_date = {_fund_nav_date(row[0]): row[1] for row in accumulated_rows}
1513
+ today = _cn_now().date().isoformat()
1514
+ estimate = {
1515
+ "estimated_nav": None,
1516
+ "estimated_change_pct": None,
1517
+ "estimated_at": None,
1518
+ "estimate_status": "unavailable",
1519
+ }
1520
+ warnings = []
1521
+ try:
1522
+ estimate.update(_fund_estimate(symbol))
1523
+ except Exception:
1524
+ warnings.append("estimated NAV provider request failed; published NAV remains available")
1525
+ result = _clean_row(
1526
+ {
1527
+ "symbol": symbol,
1528
+ "name": name,
1529
+ "asset_type": "open_end_fund",
1530
+ "nav": latest.get("y"),
1531
+ "accumulated_nav": accumulated_by_date.get(nav_date),
1532
+ "change_pct": latest.get("equityReturn"),
1533
+ "nav_date": nav_date,
1534
+ "data_as_of": nav_date,
1535
+ "as_of": nav_date,
1536
+ "as_of_basis": "provider",
1537
+ "stale": nav_date < today,
1538
+ "source": "eastmoney",
1539
+ "source_url": f"https://fund.eastmoney.com/{symbol}.html",
1540
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
1541
+ "note": (
1542
+ "nav is published end-of-day NAV, not a real-time price; QDII disclosure may lag "
1543
+ "1-2 trading days; estimated_nav is separate and may be unavailable"
1544
+ ),
1545
+ **estimate,
1546
+ }
1547
+ )
1548
+ if warnings:
1549
+ result["warnings"] = warnings
1550
+ return result
1551
+ except Exception as e:
1552
+ return {"error": error_message(e)}
1553
+
1554
+
1325
1555
  # --------------- capital_flow ---------------
1326
1556
 
1327
1557
 
@@ -1357,7 +1587,7 @@ def cmd_capital_flow(args):
1357
1587
  try:
1358
1588
  import akshare as ak
1359
1589
 
1360
- df = _akshare_retry(ak.stock_sector_fund_flow_rank)
1590
+ df = _akshare_retry(getattr(ak, "stock_fund_flow_sector_rank_em", None) or ak.stock_sector_fund_flow_rank)
1361
1591
  if df is None or df.empty:
1362
1592
  return {"error": "Sector fund flow data unavailable"}
1363
1593
  col_map = {
@@ -1372,7 +1602,7 @@ def cmd_capital_flow(args):
1372
1602
  keep = [c for c in col_map.values() if c in df.columns]
1373
1603
  return [_clean_row(r) for r in df[keep].head(20).to_dict("records")]
1374
1604
  except Exception as e:
1375
- return {"error": str(e)}
1605
+ return {"error": error_message(e)}
1376
1606
 
1377
1607
  if detect_market(args.symbol) != "A":
1378
1608
  return {"error": "capital_flow only available for A-shares"}
@@ -1384,7 +1614,9 @@ def cmd_capital_flow(args):
1384
1614
  # akshare wants the bare 6-digit stock plus market ∈ {sh, sz, bj}.
1385
1615
  code = _cn_code(args.symbol)
1386
1616
  market, stock = code[:2], code[2:]
1387
- df = _akshare_retry(ak.stock_individual_fund_flow, stock=stock, market=market)
1617
+ # Positional: the 1.20.45 rename also renamed `stock=` to `symbol=`.
1618
+ flow = getattr(ak, "stock_fund_flow_individual_hist_em", None) or ak.stock_individual_fund_flow
1619
+ df = _akshare_retry(flow, stock, market)
1388
1620
  col_map = {
1389
1621
  "日期": "date",
1390
1622
  "主力净流入-净额": "main_net_inflow",
@@ -1423,7 +1655,7 @@ def cmd_capital_flow(args):
1423
1655
  try:
1424
1656
  return _capital_flow_efinance(args.symbol)
1425
1657
  except Exception as e2:
1426
- return {"error": str(e2)}
1658
+ return {"error": error_message(e2)}
1427
1659
 
1428
1660
 
1429
1661
  # --------------- news ---------------
@@ -1431,7 +1663,9 @@ def cmd_capital_flow(args):
1431
1663
 
1432
1664
  def _news_search_intel_fallback(symbol: str) -> list:
1433
1665
  """Fallback: use search_intel to find news when primary sources fail."""
1434
- data = run_tool("search_intel.py", ["search", f"{symbol} 最新消息"], timeout=30)
1666
+ data = run_tool("search_intel.py", ["search", f"{symbol} 最新消息"])
1667
+ if isinstance(data, dict):
1668
+ data = data.get("results", [])
1435
1669
  if isinstance(data, list):
1436
1670
  return [{"title": item.get("title", ""), "url": item.get("url", ""), "source": "search"} for item in data[:10]]
1437
1671
  return []
@@ -1455,7 +1689,7 @@ def cmd_news(args):
1455
1689
  }
1456
1690
  df = df.rename(columns=col_map)
1457
1691
  keep = [c for c in col_map.values() if c in df.columns]
1458
- cutoff = datetime.now() - timedelta(days=args.days)
1692
+ cutoff = _cn_now() - timedelta(days=args.days)
1459
1693
  df = df[keep]
1460
1694
  if "datetime" in df.columns:
1461
1695
  import pandas as pd
@@ -1472,15 +1706,18 @@ def cmd_news(args):
1472
1706
  raise ValueError("yfinance news empty")
1473
1707
  results = []
1474
1708
  for item in news[:20]:
1709
+ content = item.get("content") or item # yfinance >= 0.2.50 nests fields under `content`
1475
1710
  results.append(
1476
1711
  {
1477
- "title": item.get("title", ""),
1478
- "publisher": item.get("publisher", ""),
1479
- "url": item.get("link", ""),
1480
- "published": item.get("providerPublishTime", ""),
1481
- "type": item.get("type", ""),
1712
+ "title": content.get("title") or item.get("title", ""),
1713
+ "publisher": (content.get("provider") or {}).get("displayName") or item.get("publisher", ""),
1714
+ "url": (content.get("canonicalUrl") or {}).get("url") or item.get("link", ""),
1715
+ "published": content.get("pubDate") or item.get("providerPublishTime", ""),
1716
+ "type": content.get("contentType") or item.get("type", ""),
1482
1717
  }
1483
1718
  )
1719
+ if not any(r["title"] for r in results):
1720
+ raise ValueError("yfinance news has no titles")
1484
1721
  return results
1485
1722
  except Exception:
1486
1723
  return _news_search_intel_fallback(args.symbol)
@@ -1661,19 +1898,175 @@ def financials_yf(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1661
1898
  return result
1662
1899
 
1663
1900
 
1901
+ def _yf_dcf_inputs(symbol: str, periods: int) -> dict:
1902
+ import yfinance as yf
1903
+
1904
+ if detect_market(symbol) == "A":
1905
+ code = _cn_code(symbol)
1906
+ if code.startswith("bj"):
1907
+ raise ValueError("Yahoo has no Beijing Stock Exchange statements")
1908
+ ticker = yf.Ticker(f"{code[2:]}.{'SS' if code.startswith('sh') else 'SZ'}")
1909
+ else:
1910
+ ticker = yf.Ticker(_yf_hk_symbol(symbol))
1911
+ tables = [ticker.income_stmt, ticker.cashflow, ticker.balance_sheet]
1912
+ warnings = ["Yahoo standardized annual statements; verify against filings before valuation."]
1913
+ try:
1914
+ currency, reason = _yf_info(symbol, ticker).get("financialCurrency"), "not reported"
1915
+ except Exception as e:
1916
+ currency, reason = None, error_message(e)
1917
+ if not currency:
1918
+ warnings.append(f"Yahoo financial currency unavailable ({reason}); rows cannot fill other sources")
1919
+ fields = {
1920
+ "revenue": (0, "Total Revenue"),
1921
+ "operating_income": (0, "Operating Income"),
1922
+ "pretax_income": (0, "Pretax Income"),
1923
+ "income_tax": (0, "Tax Provision"),
1924
+ "diluted_shares": (0, "Diluted Average Shares"),
1925
+ "operating_cash_flow": (1, "Operating Cash Flow"),
1926
+ "capital_expenditure": (1, "Capital Expenditure"),
1927
+ "depreciation_amortization": (1, "Depreciation And Amortization"),
1928
+ "cash": (2, "Cash And Cash Equivalents"),
1929
+ "lease_liabilities": (2, "Capital Lease Obligations"),
1930
+ "minority_interest": (2, "Minority Interest"),
1931
+ "shares_outstanding": (2, "Ordinary Shares Number"),
1932
+ }
1933
+ ends = sorted(
1934
+ {str(col)[:10] for table in tables if table is not None and not table.empty for col in table.columns},
1935
+ reverse=True,
1936
+ )[:periods]
1937
+ rows = []
1938
+ for end in ends:
1939
+ entry = {"period_end": end, "currency": currency}
1940
+
1941
+ def cell(index, label, end=end):
1942
+ table = tables[index]
1943
+ column = next((col for col in getattr(table, "columns", []) if str(col)[:10] == end), None)
1944
+ return (
1945
+ _sanitize(_parse_float(table.loc[label, column]))
1946
+ if column is not None and label in table.index
1947
+ else None
1948
+ )
1949
+
1950
+ for key, (index, label) in fields.items():
1951
+ value = cell(index, label)
1952
+ entry[key] = abs(value) if key == "capital_expenditure" and value is not None else value
1953
+ # Yahoo "Total Debt" adds lease liabilities; SEC/Lixinger total_debt excludes them.
1954
+ debt = [value for value in (cell(2, "Current Debt"), cell(2, "Long Term Debt")) if value is not None]
1955
+ entry["total_debt"] = sum(debt) if debt else None
1956
+ rows.append(entry)
1957
+ if not rows:
1958
+ raise ValueError("Yahoo annual statements unavailable")
1959
+ return {
1960
+ "source": "yfinance",
1961
+ "periods": rows,
1962
+ "warnings": warnings + ["lease_liabilities is Yahoo Capital Lease Obligations (may omit operating leases)."],
1963
+ }
1964
+
1965
+
1966
+ DCF_FIELDS = (
1967
+ "revenue",
1968
+ "operating_income",
1969
+ "pretax_income",
1970
+ "income_tax",
1971
+ "operating_cash_flow",
1972
+ "capital_expenditure",
1973
+ "depreciation_amortization",
1974
+ "cash",
1975
+ "total_debt",
1976
+ "lease_liabilities",
1977
+ "minority_interest",
1978
+ "diluted_shares",
1979
+ "shares_outstanding",
1980
+ )
1981
+
1982
+
1983
+ def _fill_dcf_rows(result: dict, fallback: dict | None) -> dict:
1984
+ """Fill null primary fields from Yahoo for the same fiscal period and currency, recording each field's source."""
1985
+ for row in result["periods"]:
1986
+ match = None
1987
+ if fallback:
1988
+ end = datetime.fromisoformat(row["period_end"]).date()
1989
+ match = next(
1990
+ (
1991
+ other
1992
+ for other in fallback["periods"]
1993
+ if abs((datetime.fromisoformat(other["period_end"]).date() - end).days) <= 10
1994
+ and other.get("currency")
1995
+ and other.get("currency") == row.get("currency")
1996
+ ),
1997
+ None,
1998
+ )
1999
+ sources = {}
2000
+ for key in DCF_FIELDS:
2001
+ # Yahoo leases are finance-lease-only "Capital Lease Obligations"; never mix them into another source.
2002
+ if row.get(key) is None and match and match.get(key) is not None and key != "lease_liabilities":
2003
+ row[key], sources[key] = match[key], fallback["source"]
2004
+ elif row.get(key) is not None:
2005
+ sources[key] = result["source"]
2006
+ else:
2007
+ row[key] = None
2008
+ pretax, tax = row["pretax_income"], row["income_tax"]
2009
+ row["effective_tax_rate"] = tax / pretax if tax is not None and pretax and pretax > 0 else None
2010
+ row["field_sources"] = sources
2011
+ row["missing_fields"] = [key for key in DCF_FIELDS if row[key] is None]
2012
+ result["units"] = {"amounts": "reported currency units", "shares": "shares", "effective_tax_rate": "ratio"}
2013
+ return result
2014
+
2015
+
2016
+ def _dcf_inputs(symbol: str, periods: int) -> dict:
2017
+ market = detect_market(symbol)
2018
+ if market not in ("A", "HK", "US"):
2019
+ return {"error": "DCF inputs support A, HK and US stocks only"}
2020
+ warnings, primary = [], None
2021
+ if market == "US" or os.environ.get("LIXINGER_TOKEN"):
2022
+ label = "SEC companyfacts" if market == "US" else f"Lixinger {market}"
2023
+ try:
2024
+ if market == "US":
2025
+ import sec_edgar
2026
+
2027
+ primary = sec_edgar.dcf_inputs(symbol, periods)
2028
+ else:
2029
+ import lixinger
2030
+
2031
+ code = _cn_code(symbol)[2:] if market == "A" else symbol.split(".")[0].zfill(5)
2032
+ primary = lixinger.dcf_inputs("cn" if market == "A" else "hk", code, periods)
2033
+ except Exception as e:
2034
+ warnings.append(f"{label} unavailable: {error_message(e)}; Yahoo fallback used")
2035
+ try:
2036
+ yahoo = _yf_dcf_inputs(symbol, periods)
2037
+ except Exception as e:
2038
+ if primary is None:
2039
+ return {"error": f"DCF inputs unavailable: {error_message(e)}", "warnings": warnings}
2040
+ yahoo = None
2041
+ warnings.append(f"Yahoo fill unavailable: {error_message(e)}")
2042
+ result = _fill_dcf_rows(primary or yahoo, yahoo if primary else None)
2043
+ result["warnings"] = warnings + result["warnings"]
2044
+ return result
2045
+
2046
+
1664
2047
  def cmd_financials(args):
1665
2048
  periods = getattr(args, "periods", 1)
1666
2049
  if not 1 <= periods <= 40:
1667
2050
  return {"error": f"periods must be between 1 and 40, got {periods}"}
1668
2051
  annual = getattr(args, "annual", False)
1669
2052
  market = detect_market(args.symbol)
2053
+ result = _financials_without_dcf(args, periods, annual, market)
2054
+ if getattr(args, "include_dcf_inputs", False) and "error" not in result:
2055
+ result["dcf_inputs"] = _dcf_inputs(args.symbol, periods)
2056
+ return result
2057
+
2058
+
2059
+ def _financials_without_dcf(args, periods, annual, market):
1670
2060
  try:
1671
2061
  if market == "HK":
1672
2062
  if os.environ.get("LIXINGER_TOKEN"):
1673
2063
  try:
1674
2064
  from lixinger import financials_hk
1675
2065
 
1676
- result = financials_hk(args.symbol, periods, annual)
2066
+ result = {
2067
+ "symbol": args.symbol,
2068
+ **financials_hk(args.symbol.split(".")[0].zfill(5), periods, annual),
2069
+ }
1677
2070
  snapshot = financials_yf(args.symbol, 1, False)
1678
2071
  result.update(
1679
2072
  {
@@ -1703,7 +2096,7 @@ def cmd_financials(args):
1703
2096
  financials_a(args.symbol, periods, annual) if market == "A" else financials_yf(args.symbol, periods, annual)
1704
2097
  )
1705
2098
  except Exception as e:
1706
- return {"error": str(e)}
2099
+ return {"error": error_message(e)}
1707
2100
 
1708
2101
 
1709
2102
  # --------------- market_snapshot ---------------
@@ -1724,7 +2117,7 @@ def snapshot_a() -> list:
1724
2117
  label="snapshot_a",
1725
2118
  )
1726
2119
  except Exception as e:
1727
- return [{"error": f"A-share snapshot unavailable from all sources: {e}"}]
2120
+ return [{"error": f"A-share snapshot unavailable from all sources: {error_message(e)}"}]
1728
2121
 
1729
2122
 
1730
2123
  def _snapshot_efinance() -> list:
@@ -1758,7 +2151,7 @@ def _snapshot_sina() -> list:
1758
2151
  import akshare as ak
1759
2152
  import pandas as pd
1760
2153
 
1761
- df = _akshare_retry(ak.stock_zh_a_spot)
2154
+ df = _akshare_retry(getattr(ak, "stock_zh_a_spot_sina", None) or ak.stock_zh_a_spot)
1762
2155
  if df is None or df.empty:
1763
2156
  raise ValueError("sina snapshot empty")
1764
2157
  df["代码"] = df["代码"].astype(str).str.replace(r"^(?:sh|sz|bj)", "", regex=True)
@@ -1872,7 +2265,8 @@ def snapshot_hk() -> list:
1872
2265
  try:
1873
2266
  import akshare as ak
1874
2267
 
1875
- df = _akshare_retry(ak.stock_hk_spot_em)
2268
+ with socket_timeout(25):
2269
+ df = _akshare_retry(ak.stock_hk_spot_em)
1876
2270
  col_map = {
1877
2271
  "代码": "symbol",
1878
2272
  "名称": "name",
@@ -1896,11 +2290,19 @@ def snapshot_hk() -> list:
1896
2290
  return [{"error": f"HK snapshot unavailable: {type(e).__name__}: {scrub_secrets(str(e))}"[:200]}]
1897
2291
 
1898
2292
 
2293
+ def normalize_us_symbol(value) -> str:
2294
+ symbol = str(value).strip().upper()
2295
+ symbol = re.sub(r"^(?:105|106|107)[.]", "", symbol)
2296
+ symbol = re.sub(r"[.](?:OQ|N|A)$", "", symbol)
2297
+ return re.sub(r"[._]", "-", symbol) # Eastmoney writes class shares as BRK_B; yfinance wants BRK-B
2298
+
2299
+
1899
2300
  def snapshot_us() -> list:
1900
2301
  try:
1901
2302
  import akshare as ak
1902
2303
 
1903
- df = _akshare_retry(ak.stock_us_spot_em)
2304
+ with socket_timeout(25):
2305
+ df = _akshare_retry(ak.stock_us_spot_em)
1904
2306
  col_map = {
1905
2307
  "代码": "symbol",
1906
2308
  "名称": "name",
@@ -1913,8 +2315,7 @@ def snapshot_us() -> list:
1913
2315
  }
1914
2316
  df = df.rename(columns=col_map)
1915
2317
  if "symbol" in df.columns:
1916
- symbols = df["symbol"].astype(str).str.upper().str.replace(r"\.(?:OQ|N|A)$", "", regex=True)
1917
- df["symbol"] = symbols.str.replace(".", "-", regex=False)
2318
+ df["symbol"] = df["symbol"].map(normalize_us_symbol)
1918
2319
  df["pe_basis"] = "unknown"
1919
2320
  keep = [c for c in col_map.values() if c in df.columns] + ["pe_basis"]
1920
2321
  return [_clean_row(r) for r in df[keep].to_dict("records")]
@@ -1933,7 +2334,7 @@ def cmd_market_snapshot(args):
1933
2334
  return snapshot_us()
1934
2335
  return {"error": f"Unknown market: {m}"}
1935
2336
  except Exception as e:
1936
- return {"error": str(e)}
2337
+ return {"error": error_message(e)}
1937
2338
 
1938
2339
 
1939
2340
  # --------------- market_indices ---------------
@@ -1948,6 +2349,31 @@ _REGION_INDEX_SYMBOLS = {
1948
2349
  }
1949
2350
 
1950
2351
 
2352
+ def _quote_tencent_hk_index(symbol: str) -> dict:
2353
+ """Tencent r_hk index quote, using the same `~` field positions as _tencent_payload_row.
2354
+ Volume is omitted: the index feed's volume field has no confirmed unit."""
2355
+ import requests
2356
+
2357
+ resp = requests.get(f"https://qt.gtimg.cn/q=r_{_TENCENT_HK_INDEX[symbol]}", timeout=10)
2358
+ resp.raise_for_status()
2359
+ f = _gbk_var_payload(resp.content).split("~")
2360
+ price = _parse_float(f[3]) if len(f) > 34 else None
2361
+ if price is None or price <= 0:
2362
+ raise ValueError(f"tencent returned no quote for {symbol}")
2363
+ return {
2364
+ "price": price,
2365
+ "change": _parse_float(f[31]),
2366
+ "change_pct": _parse_float(f[32]),
2367
+ "high": _parse_float(f[33]),
2368
+ "low": _parse_float(f[34]),
2369
+ "open": _parse_float(f[5]),
2370
+ "prev_close": _parse_float(f[4]),
2371
+ "source": "tencent",
2372
+ "data_as_of": _quote_time(f[30], "%Y/%m/%d %H:%M:%S"),
2373
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
2374
+ }
2375
+
2376
+
1951
2377
  def cmd_market_indices(args):
1952
2378
  region = args.region.lower()
1953
2379
  try:
@@ -1964,10 +2390,10 @@ def cmd_market_indices(args):
1964
2390
  ]
1965
2391
  results = []
1966
2392
  try:
1967
- df = _akshare_retry(ak.stock_zh_index_spot_em)
2393
+ df = _akshare_retry(getattr(ak, "index_zh_spot_em", None) or ak.stock_zh_index_spot_em)
1968
2394
  except Exception:
1969
2395
  # eastmoney refuses datacenter/proxy IPs (RemoteDisconnected) — sina is the fallback
1970
- df = _akshare_retry(ak.stock_zh_index_spot_sina)
2396
+ df = _akshare_retry(getattr(ak, "index_zh_spot_sina", None) or ak.stock_zh_index_spot_sina)
1971
2397
  for code, name in indices:
1972
2398
  clean_code = code[2:] if code[:2] in ("sh", "sz") else code
1973
2399
  row = df[df["代码"] == clean_code]
@@ -1996,36 +2422,44 @@ def cmd_market_indices(args):
1996
2422
  )
1997
2423
  return results
1998
2424
  elif region in _REGION_INDEX_SYMBOLS:
2425
+ from concurrent.futures import ThreadPoolExecutor
2426
+
1999
2427
  import yfinance as yf
2000
2428
 
2001
2429
  symbols = _REGION_INDEX_SYMBOLS[region]
2002
- results = []
2003
- for sym, name in symbols.items():
2430
+
2431
+ def fetch_index(item):
2432
+ sym, name = item
2004
2433
  try:
2005
- t = yf.Ticker(sym)
2006
- info = t.info
2007
- results.append(
2008
- _clean_row(
2009
- {
2010
- "code": sym,
2011
- "name": name,
2012
- "price": info.get("regularMarketPrice"),
2013
- "change": info.get("regularMarketChange"),
2014
- "change_pct": info.get("regularMarketChangePercent"),
2015
- "volume": info.get("regularMarketVolume"),
2016
- "high": info.get("regularMarketDayHigh"),
2017
- "low": info.get("regularMarketDayLow"),
2018
- "open": info.get("regularMarketOpen"),
2019
- "prev_close": info.get("regularMarketPreviousClose"),
2020
- }
2021
- )
2434
+ if sym in _TENCENT_HK_INDEX:
2435
+ return _clean_row({"code": sym, "name": name, **_quote_tencent_hk_index(sym)})
2436
+ info = yf.Ticker(sym).info
2437
+ return _clean_row(
2438
+ {
2439
+ "code": sym,
2440
+ "name": name,
2441
+ "price": info.get("regularMarketPrice"),
2442
+ "change": info.get("regularMarketChange"),
2443
+ "change_pct": info.get("regularMarketChangePercent"),
2444
+ "volume": info.get("regularMarketVolume"),
2445
+ "high": info.get("regularMarketDayHigh"),
2446
+ "low": info.get("regularMarketDayLow"),
2447
+ "open": info.get("regularMarketOpen"),
2448
+ "prev_close": info.get("regularMarketPreviousClose"),
2449
+ "source": "yfinance",
2450
+ "data_as_of": _unix_quote_time(info.get("regularMarketTime")),
2451
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
2452
+ }
2022
2453
  )
2023
2454
  except Exception:
2024
- results.append({"code": sym, "name": name, "note": "data unavailable"})
2025
- return results
2455
+ return {"code": sym, "name": name, "note": "data unavailable"}
2456
+
2457
+ # Preserve provider order while overlapping independent quote requests.
2458
+ with ThreadPoolExecutor(max_workers=min(4, len(symbols))) as pool:
2459
+ return list(pool.map(fetch_index, symbols.items()))
2026
2460
  return {"error": f"Unknown region: {region}"}
2027
2461
  except Exception as e:
2028
- return {"error": str(e)}
2462
+ return {"error": error_message(e)}
2029
2463
 
2030
2464
 
2031
2465
  # --------------- sector_rankings ---------------
@@ -2156,24 +2590,72 @@ def _sector_rankings_sina(indicator: str) -> list:
2156
2590
  return [_clean_row(r) for r in df[keep].to_dict("records")]
2157
2591
 
2158
2592
 
2593
+ US_SECTOR_ETFS = {
2594
+ "XLK": "Technology",
2595
+ "XLF": "Financials",
2596
+ "XLV": "Health Care",
2597
+ "XLY": "Consumer Discretionary",
2598
+ "XLP": "Consumer Staples",
2599
+ "XLE": "Energy",
2600
+ "XLI": "Industrials",
2601
+ "XLB": "Materials",
2602
+ "XLU": "Utilities",
2603
+ "XLRE": "Real Estate",
2604
+ "XLC": "Communication Services",
2605
+ }
2606
+
2607
+
2608
+ def _sector_rankings_us() -> list:
2609
+ """S&P 500 sector proxies from the SPDR Select Sector ETFs' last two daily closes."""
2610
+ import yfinance as yf
2611
+
2612
+ closes = yf.download(list(US_SECTOR_ETFS), period="10d", auto_adjust=False, progress=False)["Close"]
2613
+ rows = []
2614
+ for symbol, name in US_SECTOR_ETFS.items():
2615
+ series = closes[symbol].dropna() if symbol in closes else []
2616
+ if len(series) < 2:
2617
+ continue
2618
+ rows.append(
2619
+ {
2620
+ "name": name,
2621
+ "etf": symbol,
2622
+ "close": round(float(series.iloc[-1]), 4),
2623
+ "change_pct": round(float(series.iloc[-1] / series.iloc[-2] - 1) * 100, 4),
2624
+ "as_of": str(series.index[-1])[:10],
2625
+ }
2626
+ )
2627
+ if not rows:
2628
+ raise ValueError("US sector ETF quotes unavailable")
2629
+ return rows
2630
+
2631
+
2159
2632
  def cmd_sector_rankings(args):
2160
2633
  board_type = getattr(args, "board_type", "industry")
2161
2634
  direction = getattr(args, "direction", "top")
2162
- sources = [_sourced("eastmoney", lambda: _sector_rankings_em(board_type))]
2163
- if board_type == "industry":
2164
- # efinance has no concept boards — it stays an industry-only leg
2165
- sources.append(_sourced("ths", _sector_rankings_ths))
2166
- sources.append(_sourced("efinance", _sector_rankings_efinance))
2635
+ market = getattr(args, "market", "A")
2636
+ if market == "US" and board_type != "industry":
2637
+ return {"error": "US sector rankings support board_type=industry only"}
2638
+ if market == "US":
2639
+ sources = [_sourced("yfinance", _sector_rankings_us)]
2167
2640
  else:
2168
- sources.append(_sourced("sina", lambda: _sector_rankings_sina("概念")))
2641
+ sources = [_sourced("eastmoney", lambda: _sector_rankings_em(board_type))]
2642
+ if board_type == "industry":
2643
+ # efinance has no concept boards — it stays an industry-only leg
2644
+ sources += [_sourced("ths", _sector_rankings_ths), _sourced("efinance", _sector_rankings_efinance)]
2645
+ else:
2646
+ sources.append(_sourced("sina", lambda: _sector_rankings_sina("概念")))
2169
2647
  try:
2170
- rows = _failover(sources, label=f"sector_rankings:{board_type}")
2648
+ rows = _failover(
2649
+ sources, label="sector_rankings_us:industry" if market == "US" else f"sector_rankings:{board_type}"
2650
+ )
2171
2651
  if not rows:
2172
2652
  raise ValueError(f"no {board_type} board rankings data")
2173
2653
  except Exception as e:
2174
- return {"error": str(e)}
2654
+ return {"error": error_message(e)}
2175
2655
  for r in rows:
2176
2656
  r["board_type"] = board_type
2657
+ if market == "US":
2658
+ r.update(market="US", proxy="SPDR Select Sector ETF")
2177
2659
 
2178
2660
  def _pct(row, default):
2179
2661
  v = row.get("change_pct")
@@ -2191,10 +2673,9 @@ def cmd_sector_rankings(args):
2191
2673
 
2192
2674
  # --------------- sector constituents / stock sectors (issue #18) ---------------
2193
2675
 
2194
- # Disk cache lives in tempdir — cross-platform (Windows has no ~/.cache). Stores
2195
- # sector constituents (24h TTL; membership changes slowly) and sticky failover
2196
- # winners (keys prefixed `sticky-`, see _failover).
2197
- _DATA_CACHE_DIR = Path(tempfile.gettempdir()) / "pi-stock-analysis"
2676
+ # Small routing state (24h TTL) beside the user-private slow-data cache: sticky failover
2677
+ # winners (`sticky-`, see _failover) and per-leg outage markers (`resolve-down-`).
2678
+ _DATA_CACHE_DIR = CACHE_DIR / "state"
2198
2679
  _DATA_CACHE_TTL = 24 * 3600
2199
2680
 
2200
2681
 
@@ -2337,16 +2818,10 @@ def _sector_constituents(args):
2337
2818
  sector = args.sector.strip()
2338
2819
  if not sector:
2339
2820
  return {"error": "sector name required"}
2340
- board_type = args.board_type
2341
- # Sector names are user input — hash them instead of sanitizing, so names that
2342
- # differ only in whitespace/punctuation (创新药 vs 创新 药) can't collide.
2343
- cache_key = hashlib.sha256(f"{sector}|{board_type}".encode()).hexdigest()[:16]
2344
2821
  try:
2345
- result = sector_constituents_a(sector, board_type)
2822
+ return sector_constituents_a(sector, args.board_type)
2346
2823
  except Exception as e:
2347
- return {"error": str(e)}
2348
- _disk_cache_set(cache_key, result)
2349
- return result
2824
+ return {"error": error_message(e)}
2350
2825
 
2351
2826
 
2352
2827
  def _board_entry(name, source: str, board_type: str) -> dict:
@@ -2444,17 +2919,11 @@ def _stock_boards_cninfo(symbol: str) -> list:
2444
2919
  def _stock_boards_from_cache(symbol: str) -> list:
2445
2920
  """Reverse lookup against the get_sector_constituents disk cache — the honest
2446
2921
  fallback when eastmoney blocks the caller's IP and xueqiu has no token (the two
2447
- live A-share sources are both eastmoney-flavored). Reads week-lived user-directory entries plus legacy temporary payloads,
2448
- and returns one entry per cached sector
2449
- containing the symbol. Cold cache → []; malformed cache files are skipped
2450
- (_disk_cache_get returns None on expiry, bad JSON, or read errors)."""
2451
- from itertools import chain
2452
-
2453
- from _cache import cached_values
2454
-
2922
+ live A-share sources are both eastmoney-flavored). Reads the week-lived get_sector_constituents
2923
+ entries and returns one entry per cached sector containing the symbol. Cold cache → [];
2924
+ expired or malformed entries are skipped."""
2455
2925
  boards = []
2456
- legacy = (_disk_cache_get(path.stem) for path in _DATA_CACHE_DIR.glob("*.json"))
2457
- for data in chain(legacy, cached_values("sector-constituents", WEEK)):
2926
+ for data in cached_values("sector-constituents", WEEK):
2458
2927
  if not isinstance(data, dict):
2459
2928
  continue
2460
2929
  sector = data.get("sector")
@@ -2503,7 +2972,7 @@ _RESOLVE_DOWN_TTL = 3600
2503
2972
  def _resolve_leg_down(name: str) -> bool:
2504
2973
  """True while leg `name`'s failure marker is fresh. The marker stores a plain
2505
2974
  timestamp — the effective TTL above is shorter than the 24h file lifetime that
2506
- _disk_cache_get enforces on the shared cache dir."""
2975
+ _disk_cache_get enforces on the private state cache dir."""
2507
2976
  ts = _disk_cache_get(f"resolve-down-{name}")
2508
2977
  return isinstance(ts, (int, float)) and time.time() - ts < _RESOLVE_DOWN_TTL
2509
2978
 
@@ -2643,12 +3112,13 @@ def cmd_stock_info(args):
2643
3112
  if is_etf:
2644
3113
  try:
2645
3114
  # ETF 最新净值(天天基金-场内基金历史净值明细)。窄日期窗口 → 单页返回
2646
- end = datetime.now()
3115
+ # akshare 1.20.41 改名为 fund_exchange_nav_hist_em;py3.10 只能装到 <1.20.40,仍是旧名
3116
+ end = _cn_now()
2647
3117
  start = end - timedelta(days=30)
2648
3118
  nav_df = _cached_frame(
2649
3119
  f"etf-basic-nav:{args.symbol}",
2650
3120
  lambda: _akshare_retry(
2651
- ak.fund_etf_fund_info_em,
3121
+ getattr(ak, "fund_exchange_nav_hist_em", None) or ak.fund_etf_fund_info_em,
2652
3122
  fund=args.symbol,
2653
3123
  start_date=start.strftime("%Y%m%d"),
2654
3124
  end_date=end.strftime("%Y%m%d"),
@@ -2687,7 +3157,7 @@ def cmd_stock_info(args):
2687
3157
  }
2688
3158
  )
2689
3159
  except Exception as e:
2690
- return {"error": str(e)}
3160
+ return {"error": error_message(e)}
2691
3161
 
2692
3162
 
2693
3163
  # --------------- chip_distribution ---------------
@@ -2730,7 +3200,7 @@ def cmd_chip_distribution(args):
2730
3200
  result["concentration_70"] = round((h70 - l70) / ((h70 + l70) / 2) * 100, 2)
2731
3201
  return result
2732
3202
  except Exception as e:
2733
- return {"error": str(e)}
3203
+ return {"error": error_message(e)}
2734
3204
 
2735
3205
 
2736
3206
  # --------------- market_stats ---------------
@@ -2806,17 +3276,25 @@ def compute_market_stats(data: list) -> dict:
2806
3276
 
2807
3277
 
2808
3278
  def cmd_market_stats(args):
3279
+ market = getattr(args, "market", "A")
2809
3280
  try:
2810
- data = snapshot_a()
3281
+ data = {"A": snapshot_a, "HK": snapshot_hk, "US": snapshot_us}[market]()
2811
3282
  if isinstance(data, dict) and "error" in data:
2812
3283
  return data
2813
3284
  if isinstance(data, list) and len(data) == 1 and isinstance(data[0], dict) and "error" in data[0]:
2814
3285
  return data[0]
2815
3286
  if not data:
2816
3287
  return {"error": "No market data"}
2817
- return compute_market_stats(data)
3288
+ stats = compute_market_stats(data)
3289
+ if market != "A":
3290
+ # HK/US have no daily price limits; breadth covers the provider's listed-security universe.
3291
+ stats.update(
3292
+ dict.fromkeys(("limit_up_count", "limit_down_count", "limit_up_samples", "limit_down_samples"))
3293
+ )
3294
+ stats.update(market=market, source="eastmoney_spot", turnover_currency="HKD" if market == "HK" else "USD")
3295
+ return stats
2818
3296
  except Exception as e:
2819
- return {"error": str(e)}
3297
+ return {"error": error_message(e)}
2820
3298
 
2821
3299
 
2822
3300
  # --------------- fundamental_context ---------------
@@ -2883,7 +3361,7 @@ def cmd_fundamental_context(args):
2883
3361
 
2884
3362
  return result
2885
3363
  except Exception as e:
2886
- return {"error": str(e)}
3364
+ return {"error": error_message(e)}
2887
3365
 
2888
3366
 
2889
3367
  # --------------- short-term sentiment: limit_up_pool / dragon_tiger / hot_stocks (A-share only) ---------------
@@ -2901,7 +3379,7 @@ def _resolve_cn_data_date(raw: str) -> tuple[datetime, datetime]:
2901
3379
  from trading_calendar import cn_trade_dates # flat sibling import
2902
3380
 
2903
3381
  req = datetime.strptime(raw, "%Y%m%d")
2904
- d = min(req, datetime.now().replace(hour=0, minute=0, second=0, microsecond=0))
3382
+ d = min(req, _cn_now().replace(hour=0, minute=0, second=0, microsecond=0))
2905
3383
  trade_dates = cn_trade_dates(d.year)
2906
3384
  if d.month == 1: # the walk-back can cross into December of the previous year
2907
3385
  trade_dates |= cn_trade_dates(d.year - 1)
@@ -2923,7 +3401,7 @@ def cmd_limit_up_pool(args):
2923
3401
  request resolves to itself and no stale label is emitted (the zt-pool payload
2924
3402
  carries no date column to cross-check against)."""
2925
3403
  try:
2926
- req, data_day = _resolve_cn_data_date((args.date or datetime.now().strftime("%Y%m%d")).replace("-", ""))
3404
+ req, data_day = _resolve_cn_data_date((args.date or _cn_now().strftime("%Y%m%d")).replace("-", ""))
2927
3405
  except ValueError:
2928
3406
  return {"error": f"limit_up_pool: invalid --date {args.date!r} (want YYYYMMDD)"}
2929
3407
  date = data_day.strftime("%Y%m%d")
@@ -2975,7 +3453,7 @@ def cmd_limit_up_pool(args):
2975
3453
  result["pool"] = pool
2976
3454
  return result
2977
3455
  except Exception as e:
2978
- return {"error": f"limit_up_pool date={requested}: {e}"}
3456
+ return {"error": f"limit_up_pool date={requested}: {error_message(e)}"}
2979
3457
 
2980
3458
 
2981
3459
  def _dragon_tiger_em(raw: str) -> list:
@@ -3040,7 +3518,7 @@ def cmd_dragon_tiger(args):
3040
3518
  date is served the last trading day's list, labeled with requested_date and
3041
3519
  stale=True."""
3042
3520
  try:
3043
- req, data_day = _resolve_cn_data_date((args.date or datetime.now().strftime("%Y-%m-%d")).replace("-", ""))
3521
+ req, data_day = _resolve_cn_data_date((args.date or _cn_now().strftime("%Y-%m-%d")).replace("-", ""))
3044
3522
  except ValueError:
3045
3523
  return {"error": f"dragon_tiger: invalid --date {args.date!r} (want YYYY-MM-DD)"}
3046
3524
  raw = data_day.strftime("%Y%m%d")
@@ -3053,7 +3531,7 @@ def cmd_dragon_tiger(args):
3053
3531
  try:
3054
3532
  rows = _failover(sources, label=f"dragon_tiger:{raw}")
3055
3533
  except Exception as e:
3056
- return {"error": str(e)}
3534
+ return {"error": error_message(e)}
3057
3535
  stale = req != data_day
3058
3536
  if not rows:
3059
3537
  result = {"date": date, "count": 0, "items": []}
@@ -3124,7 +3602,7 @@ def _hot_stocks_baidu() -> list:
3124
3602
  df = _akshare_retry(
3125
3603
  ak.stock_hot_search_baidu,
3126
3604
  symbol="A股",
3127
- date=datetime.now().strftime("%Y%m%d"),
3605
+ date=_cn_now().strftime("%Y%m%d"),
3128
3606
  time="今日",
3129
3607
  )
3130
3608
  if df is None or df.empty:
@@ -3149,7 +3627,7 @@ def cmd_hot_stocks(args):
3149
3627
  try:
3150
3628
  rows = _failover(sources, label="hot_stocks")
3151
3629
  except Exception as e:
3152
- return {"error": str(e)}
3630
+ return {"error": error_message(e)}
3153
3631
  if not rows:
3154
3632
  return {"count": 0, "items": [], "note": "no hot-rank data available"}
3155
3633
  items = [{k: v for k, v in r.items() if k != "source"} for r in rows[: args.top]]
@@ -3250,7 +3728,7 @@ def cmd_margin_trading(args):
3250
3728
  return table
3251
3729
 
3252
3730
  try:
3253
- today = datetime.now(ZoneInfo("Asia/Shanghai")).date().isoformat()
3731
+ today = _cn_now().date().isoformat()
3254
3732
  table = (
3255
3733
  cached(f"margin-table:{exchange}:{date_iso}", DAY, fetch_table)
3256
3734
  if date_iso < today
@@ -3303,7 +3781,7 @@ def cmd_margin_trading(args):
3303
3781
  row["margin_buy_ratio_note"] = "matching-date CNY turnover unavailable"
3304
3782
  return rows
3305
3783
  except Exception as e:
3306
- return {"error": str(e)}
3784
+ return {"error": error_message(e)}
3307
3785
 
3308
3786
 
3309
3787
  def _northbound_row_is_shell(row: dict) -> bool:
@@ -3360,7 +3838,7 @@ def cmd_northbound_flow(args):
3360
3838
  rows.reverse()
3361
3839
  return rows
3362
3840
  except Exception as e:
3363
- return {"error": str(e)}
3841
+ return {"error": error_message(e)}
3364
3842
 
3365
3843
 
3366
3844
  # --------------- CLI ---------------
@@ -3382,6 +3860,9 @@ def main():
3382
3860
  p_quote = sub.add_parser("quote")
3383
3861
  p_quote.add_argument("symbol")
3384
3862
 
3863
+ p_fund_quote = sub.add_parser("fund_quote")
3864
+ p_fund_quote.add_argument("symbol")
3865
+
3385
3866
  p_cf = sub.add_parser("capital_flow")
3386
3867
  p_cf.add_argument("symbol", nargs="?", default="")
3387
3868
  p_cf.add_argument("--mode", default="detail", choices=["detail", "summary", "sector_flow"])
@@ -3394,6 +3875,7 @@ def main():
3394
3875
  p_fin.add_argument("symbol")
3395
3876
  p_fin.add_argument("--periods", type=int, default=1)
3396
3877
  p_fin.add_argument("--annual", action="store_true", help="Select annual reports; periods=10 requests ten years")
3878
+ p_fin.add_argument("--include-dcf-inputs", action="store_true", help="Add annual A/HK/US DCF input fields")
3397
3879
 
3398
3880
  p_snap = sub.add_parser("market_snapshot")
3399
3881
  p_snap.add_argument("--market", default="A", choices=["A", "HK", "US"])
@@ -3405,6 +3887,7 @@ def main():
3405
3887
  p_sec.add_argument("--top", type=int, default=10)
3406
3888
  p_sec.add_argument("--direction", default="top", choices=["top", "bottom", "both"])
3407
3889
  p_sec.add_argument("--board-type", default="industry", choices=["industry", "concept"])
3890
+ p_sec.add_argument("--market", default="A", choices=["A", "US"])
3408
3891
 
3409
3892
  p_cons = sub.add_parser("sector_constituents")
3410
3893
  p_cons.add_argument("sector")
@@ -3419,7 +3902,8 @@ def main():
3419
3902
  p_chip = sub.add_parser("chip_distribution")
3420
3903
  p_chip.add_argument("symbol")
3421
3904
 
3422
- sub.add_parser("market_stats")
3905
+ p_stats = sub.add_parser("market_stats")
3906
+ p_stats.add_argument("--market", default="A", choices=["A", "HK", "US"])
3423
3907
 
3424
3908
  p_fund = sub.add_parser("fundamental_context")
3425
3909
  p_fund.add_argument("symbol")
@@ -3451,6 +3935,7 @@ def main():
3451
3935
  dispatch = {
3452
3936
  "kline": cmd_kline,
3453
3937
  "quote": cmd_quote,
3938
+ "fund_quote": cmd_fund_quote,
3454
3939
  "capital_flow": cmd_capital_flow,
3455
3940
  "news": cmd_news,
3456
3941
  "financials": cmd_financials,