openclaw-stock-analysis 0.2.2 → 0.2.5

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 (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -2
  3. package/dist/index.js +170 -23
  4. package/index.ts +184 -26
  5. package/openclaw.plugin.json +10 -1
  6. package/package.json +1 -1
  7. package/scripts/ci-resolve-llm-host.mjs +29 -0
  8. package/scripts/setup-python.mjs +7 -9
  9. package/skills/event-driven/SKILL.md +6 -0
  10. package/skills/growth-quality/SKILL.md +6 -0
  11. package/skills/hot-theme/SKILL.md +3 -2
  12. package/skills/stock-analysis/SKILL.md +22 -0
  13. package/skills/stock-screener/SKILL.md +19 -1
  14. package/skills/strategy-backtest/SKILL.md +11 -5
  15. package/strategies/examples/bottom_volume.yaml +5 -0
  16. package/strategies/examples/box_oscillation.yaml +5 -0
  17. package/strategies/examples/bull_trend.yaml +11 -3
  18. package/strategies/examples/dragon_head.yaml +4 -2
  19. package/strategies/examples/emotion_cycle.yaml +5 -0
  20. package/strategies/examples/ma_crossover.yaml +10 -3
  21. package/strategies/examples/rsi_oversold.yaml +5 -0
  22. package/strategies/examples/shrink_pullback.yaml +7 -1
  23. package/strategies/examples/volume_breakout.yaml +7 -1
  24. package/tools/_cache.py +61 -0
  25. package/tools/_subproc.py +49 -0
  26. package/tools/alert_rules.py +19 -9
  27. package/tools/anomaly_detect.py +24 -35
  28. package/tools/backtest.py +180 -100
  29. package/tools/capabilities.py +13 -5
  30. package/tools/diagnostics.py +12 -6
  31. package/tools/gather.py +13 -9
  32. package/tools/import_parser.py +2 -5
  33. package/tools/market_regime.py +27 -35
  34. package/tools/market_review.py +56 -9
  35. package/tools/name_resolver.py +2 -5
  36. package/tools/pattern.py +2 -5
  37. package/tools/position_context.py +2 -5
  38. package/tools/report_renderer.py +10 -6
  39. package/tools/requirements.txt +2 -1
  40. package/tools/risk_screening.py +29 -23
  41. package/tools/screener.py +80 -18
  42. package/tools/search_intel.py +5 -13
  43. package/tools/signal_tracker.py +2 -6
  44. package/tools/stock_data.py +520 -169
  45. package/tools/technical.py +136 -40
  46. package/tools/trading_calendar.py +78 -18
  47. package/tools/value_data.py +1368 -0
  48. package/tools/volume_analysis.py +2 -5
  49. package/tools/watchlist.py +2 -5
  50. package/tools/watchlist_context.py +2 -5
@@ -9,16 +9,18 @@ import re
9
9
  import sys
10
10
  import tempfile
11
11
  import time
12
- from datetime import datetime, timedelta
12
+ from datetime import date, datetime, timedelta
13
13
  from pathlib import Path
14
14
 
15
15
  sys.path.insert(0, str(Path(__file__).resolve().parent))
16
- from _subproc import run_tool # noqa: E402
16
+ from _cache import DAY, WEEK, cached # noqa: E402
17
+ from _subproc import run_tool, scrub_secrets, socket_timeout, utf8_stdio # noqa: E402
17
18
 
18
19
  # Explicitly exchange-prefixed A-share codes (sh600519 / sz000858 / bj920001).
19
20
  # Bare 6-digit codes are always stocks; the prefix is the disambiguation escape
20
21
  # hatch for index codes (sh000001 = 上证指数, not 平安银行).
21
22
  _A_PREFIXED_RE = re.compile(r"(?:sh|sz|bj)\d{6}", re.IGNORECASE)
23
+ _A_INDEX_RE = re.compile(r"(?:sh000\d{3}|sz399\d{3})", re.IGNORECASE)
22
24
 
23
25
 
24
26
  def detect_market(symbol: str) -> str:
@@ -40,7 +42,7 @@ def detect_market(symbol: str) -> str:
40
42
 
41
43
  def normalize_stock_code(symbol: str) -> dict:
42
44
  """Classify A-share stock code into board/type with limit-up/down ratio."""
43
- info = {"market": detect_market(symbol), "board": "main", "is_st": False, "is_etf": False, "limit_pct": 0.10}
45
+ info = {"market": detect_market(symbol), "board": "main", "is_etf": False, "limit_pct": 0.10}
44
46
  if info["market"] != "A":
45
47
  info["limit_pct"] = None
46
48
  return info
@@ -66,7 +68,7 @@ def normalize_stock_code(symbol: str) -> dict:
66
68
 
67
69
 
68
70
  def calc_limit_price(pre_close: float, ratio: float, direction: str = "up") -> float:
69
- """Calculate limit-up or limit-down price with banker's rounding."""
71
+ """Calculate limit-up or limit-down price with exchange rounding (round-half-up: floor(x*100+0.5)/100)."""
70
72
  import numpy as np
71
73
 
72
74
  sign = 1 if direction == "up" else -1
@@ -115,7 +117,7 @@ def _failover(sources: list, label: str):
115
117
  _disk_cache_set(f"sticky-{chain}", name)
116
118
  return result
117
119
  except Exception as e:
118
- errors.append(f"{name}: {type(e).__name__}: {e}")
120
+ errors.append(f"{name}: {type(e).__name__}: {scrub_secrets(str(e))}")
119
121
  if errors:
120
122
  raise RuntimeError(f"{label}: {'; '.join(errors)}")
121
123
  return None
@@ -140,7 +142,7 @@ def _kline_efinance(symbol: str, period: str, count: int) -> list:
140
142
  import efinance as ef
141
143
 
142
144
  freq_map = {"daily": 101, "weekly": 102, "monthly": 103}
143
- df = ef.stock.get_quote_history(symbol, klt=freq_map.get(period, 101))
145
+ df = ef.stock.get_quote_history(symbol, klt=freq_map.get(period, 101), fqt=1)
144
146
  if df is None or df.empty:
145
147
  raise ValueError("efinance returned empty data")
146
148
  col_map = {
@@ -318,8 +320,7 @@ def _quote_tencent(symbol: str) -> dict:
318
320
 
319
321
  def _kline_tencent(symbol: str, period: str, count: int) -> list:
320
322
  """Tencent kline via web.ifzq.gtimg.cn. Rows are [date, open, close, high, low,
321
- volume(手)] ascending; qfq-adjusted like akshare. When a symbol has no qfq series
322
- (e.g. BSE codes) the endpoint returns the raw `day/week/month` key instead."""
323
+ volume(手)] ascending; raw series are accepted only for explicit indices."""
323
324
  import requests
324
325
 
325
326
  p_map = {"daily": "day", "weekly": "week", "monthly": "month"}
@@ -334,9 +335,9 @@ def _kline_tencent(symbol: str, period: str, count: int) -> list:
334
335
  if data.get("code") != 0:
335
336
  raise ValueError(f"tencent kline failed: {data.get('msg', '')}")
336
337
  node = data.get("data", {}).get(code, {})
337
- rows = node.get(f"qfq{p}") or node.get(p)
338
+ rows = node.get(p) if _A_INDEX_RE.fullmatch(symbol) else node.get(f"qfq{p}")
338
339
  if not rows:
339
- raise ValueError("tencent kline returned empty data")
340
+ raise ValueError("tencent kline returned empty data for required adjustment")
340
341
  return [
341
342
  _clean_row(
342
343
  {
@@ -429,16 +430,16 @@ def _sanitize(obj):
429
430
  import numpy as np
430
431
  import pandas as pd
431
432
 
432
- if isinstance(obj, (pd.Timestamp, datetime)):
433
- return obj.strftime("%Y-%m-%d")
433
+ if isinstance(obj, (pd.Timestamp, datetime, date)):
434
+ return None if pd.isna(obj) else obj.strftime("%Y-%m-%d")
434
435
  if isinstance(obj, (np.integer,)):
435
436
  return int(obj)
436
437
  if isinstance(obj, (np.floating,)):
437
- return None if np.isnan(obj) else round(float(obj), 4)
438
+ return round(float(obj), 4) if np.isfinite(obj) else None
438
439
  if isinstance(obj, (np.bool_,)):
439
440
  return bool(obj)
440
441
  if isinstance(obj, float):
441
- return None if obj != obj else round(obj, 4)
442
+ return round(obj, 4) if np.isfinite(obj) else None
442
443
  return obj
443
444
 
444
445
 
@@ -450,27 +451,26 @@ def _clean_row(d: dict) -> dict:
450
451
 
451
452
 
452
453
  def kline_a(symbol: str, period: str, count: int) -> list:
453
- """A-share kline with failover: akshare → tushare → efinance → tencent → sina → pytdx → baostock.
454
+ """A-share qfq kline: akshare → tushare → efinance → tencent → baostock.
454
455
 
455
- Explicitly-prefixed symbols (sh000001, typically indices) skip the eastmoney
456
- sources — they take bare 6-digit codes only and would fail slowly first."""
457
- if _A_PREFIXED_RE.fullmatch(symbol):
456
+ Explicit indices keep raw points; unadjusted stock sources never enter the chain."""
457
+ if _A_INDEX_RE.fullmatch(symbol):
458
458
  sources = [
459
459
  ("tencent", lambda: _kline_tencent(symbol, period, count)),
460
460
  ("sina", lambda: _kline_sina(symbol, period, count)),
461
461
  ("baostock", lambda: _kline_baostock(symbol, period, count)),
462
462
  ]
463
463
  else:
464
+ symbol = symbol[2:] if _A_PREFIXED_RE.fullmatch(symbol) else symbol
464
465
  sources = [
465
466
  ("akshare", lambda: _kline_akshare(symbol, period, count)),
466
467
  ("tushare", lambda: _kline_tushare(symbol, period, count)),
467
468
  ("efinance", lambda: _kline_efinance(symbol, period, count)),
468
469
  ("tencent", lambda: _kline_tencent(symbol, period, count)),
469
- ("sina", lambda: _kline_sina(symbol, period, count)),
470
- ("pytdx", lambda: _kline_pytdx(symbol, period, count)),
471
470
  ("baostock", lambda: _kline_baostock(symbol, period, count)),
472
471
  ]
473
- return _failover(sources, label=f"kline_a:{symbol}")
472
+ with socket_timeout(25):
473
+ return _failover(sources, label=f"kline_a:{symbol}")
474
474
 
475
475
 
476
476
  def _kline_akshare(symbol: str, period: str, count: int) -> list:
@@ -514,7 +514,7 @@ def _kline_akshare(symbol: str, period: str, count: int) -> list:
514
514
 
515
515
 
516
516
  def _kline_tushare(symbol: str, period: str, count: int) -> list:
517
- """Tushare kline via HTTP API. Requires TUSHARE_TOKEN."""
517
+ """Tushare qfq kline via daily factors or the adjusted week/month endpoint."""
518
518
  import os
519
519
 
520
520
  import pandas as pd
@@ -524,23 +524,29 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
524
524
  if not token:
525
525
  raise ValueError("TUSHARE_TOKEN not set")
526
526
 
527
- api_name_map = {"daily": "daily", "weekly": "weekly", "monthly": "monthly"}
527
+ if normalize_stock_code(symbol)["is_etf"]:
528
+ raise ValueError("tushare stock adjustment does not cover ETFs")
528
529
  end = datetime.now()
529
530
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
530
531
 
531
- ts_code = f"{symbol}.SH" if symbol.startswith(("6", "9", "5")) else f"{symbol}.SZ"
532
+ ts_code = f"{symbol}.{_cn_code(symbol)[:2].upper()}"
533
+ params = {
534
+ "ts_code": ts_code,
535
+ "start_date": start.strftime("%Y%m%d"),
536
+ "end_date": end.strftime("%Y%m%d"),
537
+ }
538
+ prices = ["open", "high", "low", "close"]
539
+ if period != "daily":
540
+ params["freq"] = "week" if period == "weekly" else "month"
541
+ fields = prices if period == "daily" else [f"{c}_qfq" for c in prices]
532
542
 
533
543
  resp = requests.post(
534
544
  "http://api.tushare.pro",
535
545
  json={
536
- "api_name": api_name_map.get(period, "daily"),
546
+ "api_name": "daily" if period == "daily" else "stk_week_month_adj",
537
547
  "token": token,
538
- "params": {
539
- "ts_code": ts_code,
540
- "start_date": start.strftime("%Y%m%d"),
541
- "end_date": end.strftime("%Y%m%d"),
542
- },
543
- "fields": "trade_date,open,high,low,close,vol,amount,pct_chg",
548
+ "params": params,
549
+ "fields": ",".join(["trade_date", *fields, "vol", "amount", "pct_chg"]),
544
550
  },
545
551
  timeout=15,
546
552
  )
@@ -549,12 +555,36 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
549
555
  raise ValueError(f"tushare returned no data: {data.get('msg', '')}")
550
556
 
551
557
  df = pd.DataFrame(data["data"]["items"], columns=data["data"]["fields"])
558
+ if not set(fields).issubset(df.columns):
559
+ raise ValueError("tushare returned incomplete adjusted prices")
560
+ df[fields] = df[fields].apply(pd.to_numeric, errors="coerce")
561
+ if period == "daily":
562
+ factor_resp = requests.post(
563
+ "http://api.tushare.pro",
564
+ json={"api_name": "adj_factor", "token": token, "params": params, "fields": "trade_date,adj_factor"},
565
+ timeout=15,
566
+ ).json()
567
+ if factor_resp.get("code") != 0 or not factor_resp.get("data", {}).get("items"):
568
+ raise ValueError("tushare returned no adjustment factors")
569
+ factors = pd.DataFrame(factor_resp["data"]["items"], columns=factor_resp["data"]["fields"])
570
+ factors["adj_factor"] = pd.to_numeric(factors["adj_factor"], errors="coerce")
571
+ if not (factors["adj_factor"].gt(0) & factors["adj_factor"].lt(float("inf"))).all():
572
+ raise ValueError("tushare returned invalid adjustment factors")
573
+ latest_factor = factors.sort_values("trade_date").iloc[-1]["adj_factor"]
574
+ df = df.merge(factors, on="trade_date", how="left", validate="one_to_one")
575
+ if df["adj_factor"].isna().any():
576
+ raise ValueError("tushare adjustment factors missing for price dates")
577
+ df[prices] = df[prices].mul(df["adj_factor"] / latest_factor, axis=0)
578
+ else:
579
+ df = df.drop(columns=prices, errors="ignore")
580
+ df = df.rename(columns={f"{c}_qfq": c for c in prices})
581
+ if not df[prices].abs().lt(float("inf")).all().all():
582
+ raise ValueError("tushare returned invalid adjusted prices")
552
583
  col_map = {"trade_date": "date", "vol": "volume", "amount": "turnover", "pct_chg": "change_pct"}
553
584
  df = df.rename(columns=col_map)
554
585
  df = df.sort_values("date")
555
- for c in ["open", "high", "low", "close", "volume"]:
556
- if c in df.columns:
557
- df[c] = pd.to_numeric(df[c], errors="coerce")
586
+ if "volume" in df.columns:
587
+ df["volume"] = pd.to_numeric(df["volume"], errors="coerce")
558
588
  keep = [c for c in ["date", "open", "high", "low", "close", "volume", "turnover", "change_pct"] if c in df.columns]
559
589
  df = df[keep].tail(count)
560
590
  return [_clean_row(r) for r in df.to_dict("records")]
@@ -631,41 +661,6 @@ def _pytdx_connect(api) -> None:
631
661
  raise ConnectionError(f"pytdx failed to connect to any TDX server ({'; '.join(errors)})")
632
662
 
633
663
 
634
- def _kline_pytdx(symbol: str, period: str, count: int) -> list:
635
- """pytdx kline from TDX market servers. No credentials needed."""
636
- from pytdx.hq import TdxHq_API
637
-
638
- market = 1 if symbol.startswith(("6", "9", "5")) else 0
639
- freq_map = {"daily": 9, "weekly": 5, "monthly": 6}
640
-
641
- api = TdxHq_API()
642
- try:
643
- _pytdx_connect(api)
644
- data = api.get_security_bars(freq_map.get(period, 9), market, symbol, 0, count)
645
- finally:
646
- api.disconnect()
647
-
648
- if not data:
649
- raise ValueError("pytdx returned empty data")
650
-
651
- rows = []
652
- for bar in data:
653
- rows.append(
654
- _clean_row(
655
- {
656
- "date": bar["datetime"][:10],
657
- "open": bar["open"],
658
- "high": bar["high"],
659
- "low": bar["low"],
660
- "close": bar["close"],
661
- "volume": bar["vol"],
662
- "turnover": bar.get("amount"),
663
- }
664
- )
665
- )
666
- return rows[-count:]
667
-
668
-
669
664
  def _quote_pytdx(symbol: str) -> dict:
670
665
  """pytdx realtime quote from TDX market servers."""
671
666
  from pytdx.hq import TdxHq_API
@@ -965,6 +960,9 @@ def cmd_kline(args):
965
960
  try:
966
961
  if market == "A":
967
962
  data = kline_a(args.symbol, args.period, args.count)
963
+ if data:
964
+ adjusted = "none" if _A_INDEX_RE.fullmatch(args.symbol) else "qfq"
965
+ data = [dict(row, adjusted=adjusted) for row in data]
968
966
  else:
969
967
  data = kline_yf(args.symbol, args.period, args.count)
970
968
  return data
@@ -984,7 +982,8 @@ def quote_a(symbol: str) -> dict:
984
982
  (quote_a_etf) so a stock-side winner never demotes the fund-spot source."""
985
983
  if _A_PREFIXED_RE.fullmatch(symbol):
986
984
  sources = [("tencent", lambda: _quote_tencent(symbol)), ("sina", lambda: _quote_sina(symbol))]
987
- return _failover(sources, label=f"quote_a:{symbol}")
985
+ with socket_timeout(25):
986
+ return _failover(sources, label=f"quote_a:{symbol}")
988
987
  is_etf = normalize_stock_code(symbol)["is_etf"]
989
988
  sources = []
990
989
  if is_etf:
@@ -998,11 +997,18 @@ def quote_a(symbol: str) -> dict:
998
997
  ("pytdx", lambda: _quote_pytdx(symbol)),
999
998
  ]
1000
999
  chain = "quote_a_etf" if is_etf else "quote_a"
1001
- return _failover(sources, label=f"{chain}:{symbol}")
1000
+ with socket_timeout(25):
1001
+ return _failover(sources, label=f"{chain}:{symbol}")
1002
1002
 
1003
1003
 
1004
1004
  def _quote_akshare_etf(symbol: str) -> dict:
1005
- """A-share ETF realtime quote via akshare fund_etf_spot_em."""
1005
+ """A-share ETF realtime quote via akshare fund_etf_spot_em.
1006
+
1007
+ On top of the stock-shaped fields, the same spot row carries fund-native
1008
+ columns: iopv (盘中实时估值) and fund_shares (最新份额). premium_discount_rate
1009
+ is derived here as (price - iopv) / iopv * 100 — positive = 溢价 (price above
1010
+ IOPV), negative = 折价; None when the fund publishes no intraday IOPV (money/
1011
+ bond ETFs)."""
1006
1012
  import akshare as ak
1007
1013
 
1008
1014
  df = _akshare_retry(ak.fund_etf_spot_em)
@@ -1010,7 +1016,7 @@ def _quote_akshare_etf(symbol: str) -> dict:
1010
1016
  if row.empty:
1011
1017
  raise ValueError(f"ETF {symbol} not found in akshare fund spot")
1012
1018
  r = row.iloc[0]
1013
- return _clean_row(
1019
+ result = _clean_row(
1014
1020
  {
1015
1021
  "symbol": symbol,
1016
1022
  "name": r.get("名称"),
@@ -1027,8 +1033,13 @@ def _quote_akshare_etf(symbol: str) -> dict:
1027
1033
  "turnover_rate": r.get("换手率"),
1028
1034
  "volume_ratio": r.get("量比"),
1029
1035
  "is_etf": True,
1036
+ "iopv": r.get("IOPV实时估值"),
1037
+ "fund_shares": r.get("最新份额"),
1030
1038
  }
1031
1039
  )
1040
+ price, iopv = result.get("price"), result.get("iopv")
1041
+ result["premium_discount_rate"] = round((price - iopv) / iopv * 100, 4) if price is not None and iopv else None
1042
+ return result
1032
1043
 
1033
1044
 
1034
1045
  def _quote_akshare(symbol: str) -> dict:
@@ -1228,8 +1239,12 @@ def cmd_capital_flow(args):
1228
1239
  try:
1229
1240
  import akshare as ak
1230
1241
 
1231
- market = "sh" if args.symbol.startswith("6") else "sz"
1232
- df = _akshare_retry(ak.stock_individual_fund_flow, stock=args.symbol, market=market)
1242
+ # Exchange via the shared prefix helper: bare 9/5-prefix SH codes, BSE
1243
+ # (43/81-83/87/88/92) and explicitly-prefixed input (sh600519) all resolve;
1244
+ # akshare wants the bare 6-digit stock plus market ∈ {sh, sz, bj}.
1245
+ code = _cn_code(args.symbol)
1246
+ market, stock = code[:2], code[2:]
1247
+ df = _akshare_retry(ak.stock_individual_fund_flow, stock=stock, market=market)
1233
1248
  col_map = {
1234
1249
  "日期": "date",
1235
1250
  "主力净流入-净额": "main_net_inflow",
@@ -1332,35 +1347,107 @@ def cmd_news(args):
1332
1347
  # --------------- financials ---------------
1333
1348
 
1334
1349
 
1335
- def financials_a(symbol: str) -> dict:
1350
+ def _financials_a_row(r) -> dict:
1351
+ return _clean_row(
1352
+ {
1353
+ "report_date": r.get("日期"),
1354
+ "roe": r.get("净资产收益率(%)"),
1355
+ "net_profit_margin": r.get("销售净利率(%)"),
1356
+ "gross_margin": r.get("销售毛利率(%)"),
1357
+ "debt_ratio": r.get("资产负债率(%)"),
1358
+ "current_ratio": r.get("流动比率"),
1359
+ }
1360
+ )
1361
+
1362
+
1363
+ def _cached_frame(key, fetch):
1364
+ import pandas as pd
1365
+
1366
+ rows = cached(key, DAY, lambda: [_clean_row(r) for r in fetch().to_dict("records")])
1367
+ return pd.DataFrame(rows)
1368
+
1369
+
1370
+ def _yf_info(symbol, ticker):
1371
+ def fetch():
1372
+ info = ticker.info
1373
+ if not isinstance(info, dict) or not any(
1374
+ info.get(k) for k in ("shortName", "longName", "currency", "exchange")
1375
+ ):
1376
+ raise ValueError("Yahoo basic information unavailable")
1377
+ return info
1378
+
1379
+ return cached(f"yf-basic-info:{_yf_hk_symbol(symbol)}", DAY, fetch)
1380
+
1381
+
1382
+ def _financials_a_frame(symbol):
1336
1383
  import akshare as ak
1337
1384
 
1385
+ df = _cached_frame(
1386
+ f"financial-source:{symbol}", lambda: _akshare_retry(ak.stock_financial_analysis_indicator, symbol=symbol)
1387
+ )
1388
+ return df.sort_values("日期", ascending=False) if "日期" in df.columns else df
1389
+
1390
+
1391
+ def financials_a(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1338
1392
  try:
1339
- df = _akshare_retry(ak.stock_financial_analysis_indicator, symbol=symbol)
1393
+ df = _financials_a_frame(symbol)
1394
+ if annual and not df.empty:
1395
+ df = df[df["日期"].astype(str).str.endswith("12-31")]
1340
1396
  if df is None or df.empty:
1341
- return {"symbol": symbol, "error": "No financial data"}
1342
- r = df.iloc[0]
1343
- return _clean_row(
1344
- {
1345
- "symbol": symbol,
1346
- "report_date": r.get("日期"),
1347
- "roe": r.get("净资产收益率(%)"),
1348
- "net_profit_margin": r.get("销售净利率(%)"),
1349
- "gross_margin": r.get("销售毛利率(%)"),
1350
- "debt_ratio": r.get("资产负债率(%)"),
1351
- "current_ratio": r.get("流动比率"),
1352
- }
1353
- )
1397
+ # error + note coexist (additive): consumers check either key
1398
+ return {"symbol": symbol, "error": "No financial data", "note": "No financial data"}
1399
+ result = {"symbol": symbol, **_financials_a_row(df.iloc[0])}
1400
+ if periods > 1:
1401
+ result["periods"] = [_financials_a_row(r) for r in df.head(periods).to_dict("records")]
1402
+ result["available_periods"] = len(result["periods"])
1403
+ return result
1404
+ except Exception:
1405
+ return {"symbol": symbol, "error": "Financial data unavailable", "note": "Financial data unavailable"}
1406
+
1407
+
1408
+ def _yf_financial_periods(t, periods: int) -> list:
1409
+ """Best-effort annual history from yfinance's income statement (t.info is a
1410
+ snapshot only). Silent [] on any failure — multi-period is a bonus, never a blocker."""
1411
+ try:
1412
+ fin = t.financials
1413
+ if fin is None or fin.empty:
1414
+ return []
1415
+ rows = []
1416
+ for col in list(fin.columns)[:periods]:
1417
+ revenue = _sanitize(_parse_float(fin[col].get("Total Revenue")))
1418
+ profit = _sanitize(_parse_float(fin[col].get("Net Income")))
1419
+ gross = _sanitize(_parse_float(fin[col].get("Gross Profit")))
1420
+ rows.append(
1421
+ _clean_row(
1422
+ {
1423
+ "period_end": col,
1424
+ "total_revenue": revenue,
1425
+ "net_income": profit,
1426
+ "net_profit_margin": profit / revenue * 100
1427
+ if profit is not None and revenue and revenue > 0
1428
+ else None,
1429
+ "gross_margin": gross / revenue * 100
1430
+ if gross is not None and revenue and revenue > 0
1431
+ else None,
1432
+ }
1433
+ )
1434
+ )
1435
+ return rows
1354
1436
  except Exception:
1355
- return {"symbol": symbol, "note": "Financial data unavailable"}
1437
+ return []
1356
1438
 
1357
1439
 
1358
- def financials_yf(symbol: str) -> dict:
1440
+ def financials_yf(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1359
1441
  import yfinance as yf
1360
1442
 
1361
1443
  t = yf.Ticker(_yf_hk_symbol(symbol))
1362
- info = t.info
1363
- return _clean_row(
1444
+ warnings = []
1445
+ try:
1446
+ info = _yf_info(symbol, t)
1447
+ except Exception:
1448
+ info = {}
1449
+ warnings.append("Yahoo snapshot unavailable")
1450
+ result = _clean_row(
1364
1451
  {
1365
1452
  "symbol": symbol,
1366
1453
  "name": info.get("shortName"),
@@ -1376,12 +1463,63 @@ def financials_yf(symbol: str) -> dict:
1376
1463
  "dividend_yield": info.get("dividendYield"),
1377
1464
  }
1378
1465
  )
1466
+ if warnings:
1467
+ result["warnings"] = warnings
1468
+ if periods > 1 or (annual and detect_market(symbol) == "HK"):
1469
+ history = cached(f"yf-income-history:{_yf_hk_symbol(symbol)}", DAY, lambda: _yf_financial_periods(t, 40))[
1470
+ :periods
1471
+ ]
1472
+ if detect_market(symbol) == "HK" and annual:
1473
+ try:
1474
+ import akshare as ak
1475
+
1476
+ hk_symbol = _yf_hk_symbol(symbol).split(".")[0].zfill(5)
1477
+ df = _cached_frame(
1478
+ f"hk-annual-indicators:{hk_symbol}",
1479
+ lambda: _akshare_retry(
1480
+ ak.stock_financial_hk_analysis_indicator_em, symbol=hk_symbol, indicator="年度"
1481
+ ),
1482
+ )
1483
+ if df.empty:
1484
+ raise ValueError("HK annual indicators unavailable")
1485
+ if df is not None and not df.empty:
1486
+ df = df.sort_values("REPORT_DATE", ascending=False)
1487
+ history = [
1488
+ _clean_row(
1489
+ {
1490
+ "report_date": str(r.get("REPORT_DATE"))[:10],
1491
+ "roe": r.get("ROE_AVG"),
1492
+ "gross_margin": r.get("GROSS_PROFIT_RATIO"),
1493
+ "net_profit_margin": r.get("NET_PROFIT_RATIO"),
1494
+ "currency": r.get("CURRENCY"),
1495
+ }
1496
+ )
1497
+ for r in df.head(periods).to_dict("records")
1498
+ ]
1499
+ except Exception:
1500
+ result.setdefault("warnings", []).append(
1501
+ "HK annual indicators unavailable; available Yahoo history retained"
1502
+ )
1503
+ if history:
1504
+ result["periods"] = history
1505
+ result["available_periods"] = len(history)
1506
+ result["requested_periods"] = periods
1507
+ result["history_units"] = {"roe": "percent", "gross_margin": "percent", "net_profit_margin": "percent"}
1508
+ if not info and not result.get("periods"):
1509
+ result["error"] = "Financial data unavailable"
1510
+ return result
1379
1511
 
1380
1512
 
1381
1513
  def cmd_financials(args):
1514
+ periods = getattr(args, "periods", 1)
1515
+ if not 1 <= periods <= 40:
1516
+ return {"error": f"periods must be between 1 and 40, got {periods}"}
1517
+ annual = getattr(args, "annual", False)
1382
1518
  market = detect_market(args.symbol)
1383
1519
  try:
1384
- return financials_a(args.symbol) if market == "A" else financials_yf(args.symbol)
1520
+ return (
1521
+ financials_a(args.symbol, periods, annual) if market == "A" else financials_yf(args.symbol, periods, annual)
1522
+ )
1385
1523
  except Exception as e:
1386
1524
  return {"error": str(e)}
1387
1525
 
@@ -1390,29 +1528,21 @@ def cmd_financials(args):
1390
1528
 
1391
1529
 
1392
1530
  def snapshot_a() -> list:
1393
- import socket
1394
-
1395
- # akshare/efinance leave requests timeout-less — on a blackholed network a leg
1396
- # hangs until the kernel TCP timeout (~2min), and the chain would overrun the
1397
- # caller's subprocess budget before ever reaching the tencent leg. Bound each
1398
- # blocking socket op for the chain's duration (explicit per-request timeouts,
1399
- # like the tencent leg's, still win).
1400
- old_timeout = socket.getdefaulttimeout()
1401
- socket.setdefaulttimeout(25)
1531
+ # akshare/efinance leave requests timeout-less — the chain-wide socket bound
1532
+ # (socket_timeout) is what lets the tencent leg get its turn on a dead network.
1402
1533
  try:
1403
- return _failover(
1404
- [
1405
- ("akshare", _snapshot_akshare),
1406
- ("efinance", _snapshot_efinance),
1407
- ("sina", _snapshot_sina),
1408
- ("tencent", _snapshot_tencent),
1409
- ],
1410
- label="snapshot_a",
1411
- )
1534
+ with socket_timeout(25):
1535
+ return _failover(
1536
+ [
1537
+ ("akshare", _snapshot_akshare),
1538
+ ("efinance", _snapshot_efinance),
1539
+ ("sina", _snapshot_sina),
1540
+ ("tencent", _snapshot_tencent),
1541
+ ],
1542
+ label="snapshot_a",
1543
+ )
1412
1544
  except Exception as e:
1413
1545
  return [{"error": f"A-share snapshot unavailable from all sources: {e}"}]
1414
- finally:
1415
- socket.setdefaulttimeout(old_timeout)
1416
1546
 
1417
1547
 
1418
1548
  def _snapshot_efinance() -> list:
@@ -1573,8 +1703,9 @@ def snapshot_hk() -> list:
1573
1703
  df = df.rename(columns=col_map)
1574
1704
  keep = [c for c in col_map.values() if c in df.columns]
1575
1705
  return [_clean_row(r) for r in df[keep].to_dict("records")]
1576
- except Exception:
1577
- return [{"error": "HK snapshot unavailable"}]
1706
+ except Exception as e:
1707
+ # summary only (type + short message) — never a stack trace or response body
1708
+ return [{"error": f"HK snapshot unavailable: {type(e).__name__}: {scrub_secrets(str(e))}"[:200]}]
1578
1709
 
1579
1710
 
1580
1711
  def snapshot_us() -> list:
@@ -1595,8 +1726,8 @@ def snapshot_us() -> list:
1595
1726
  df = df.rename(columns=col_map)
1596
1727
  keep = [c for c in col_map.values() if c in df.columns]
1597
1728
  return [_clean_row(r) for r in df[keep].to_dict("records")]
1598
- except Exception:
1599
- return [{"error": "US snapshot unavailable"}]
1729
+ except Exception as e:
1730
+ return [{"error": f"US snapshot unavailable: {type(e).__name__}: {scrub_secrets(str(e))}"[:200]}]
1600
1731
 
1601
1732
 
1602
1733
  def cmd_market_snapshot(args):
@@ -2004,6 +2135,12 @@ def sector_constituents_a(sector: str, board_type: str = "auto") -> dict:
2004
2135
 
2005
2136
 
2006
2137
  def cmd_sector_constituents(args):
2138
+ return cached(
2139
+ f"sector-constituents:{args.sector.strip()}:{args.board_type}", WEEK, lambda: _sector_constituents(args)
2140
+ )
2141
+
2142
+
2143
+ def _sector_constituents(args):
2007
2144
  # argparse 保证属性存在;空串仍可传入
2008
2145
  sector = args.sector.strip()
2009
2146
  if not sector:
@@ -2012,9 +2149,6 @@ def cmd_sector_constituents(args):
2012
2149
  # Sector names are user input — hash them instead of sanitizing, so names that
2013
2150
  # differ only in whitespace/punctuation (创新药 vs 创新 药) can't collide.
2014
2151
  cache_key = hashlib.sha256(f"{sector}|{board_type}".encode()).hexdigest()[:16]
2015
- cached = _disk_cache_get(cache_key)
2016
- if cached is not None:
2017
- return cached
2018
2152
  try:
2019
2153
  result = sector_constituents_a(sector, board_type)
2020
2154
  except Exception as e:
@@ -2118,13 +2252,17 @@ def _stock_boards_cninfo(symbol: str) -> list:
2118
2252
  def _stock_boards_from_cache(symbol: str) -> list:
2119
2253
  """Reverse lookup against the get_sector_constituents disk cache — the honest
2120
2254
  fallback when eastmoney blocks the caller's IP and xueqiu has no token (the two
2121
- live A-share sources are both eastmoney-flavored). Reads every unexpired
2122
- constituents payload in the cache dir and returns one entry per cached sector
2255
+ live A-share sources are both eastmoney-flavored). Reads week-lived user-directory entries plus legacy temporary payloads,
2256
+ and returns one entry per cached sector
2123
2257
  containing the symbol. Cold cache → []; malformed cache files are skipped
2124
2258
  (_disk_cache_get returns None on expiry, bad JSON, or read errors)."""
2259
+ from itertools import chain
2260
+
2261
+ from _cache import cached_values
2262
+
2125
2263
  boards = []
2126
- for path in _DATA_CACHE_DIR.glob("*.json"):
2127
- data = _disk_cache_get(path.stem)
2264
+ legacy = (_disk_cache_get(path.stem) for path in _DATA_CACHE_DIR.glob("*.json"))
2265
+ for data in chain(legacy, cached_values("sector-constituents", WEEK)):
2128
2266
  if not isinstance(data, dict):
2129
2267
  continue
2130
2268
  sector = data.get("sector")
@@ -2132,7 +2270,9 @@ def _stock_boards_from_cache(symbol: str) -> list:
2132
2270
  continue
2133
2271
  for row in data.get("constituents") or []:
2134
2272
  if isinstance(row, dict) and str(row.get("code")) == symbol:
2135
- boards.append(_board_entry(sector, "cache", data.get("board_type", "")))
2273
+ board = _board_entry(sector, "cache", data.get("board_type", ""))
2274
+ if board not in boards:
2275
+ boards.append(board)
2136
2276
  break
2137
2277
  return boards
2138
2278
 
@@ -2177,6 +2317,12 @@ def _resolve_leg_down(name: str) -> bool:
2177
2317
 
2178
2318
 
2179
2319
  def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
2320
+ if info_map is not None:
2321
+ return _resolve_stock_sectors(symbol, info_map)
2322
+ return cached(f"stock-sectors:{symbol}", WEEK, lambda: _resolve_stock_sectors(symbol, info_map))
2323
+
2324
+
2325
+ def _resolve_stock_sectors(symbol: str, info_map=None) -> dict:
2180
2326
  """Reverse map: stock → boards it belongs to (A/HK). Sources are merged (a single
2181
2327
  source failing is not fatal — the others' boards are still returned) rather than
2182
2328
  first-hit failover: each source covers a different flavor (industry vs concept).
@@ -2220,29 +2366,22 @@ def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
2220
2366
  except _DataMiss as e:
2221
2367
  # per-symbol miss (provider answered but doesn't cover this code) —
2222
2368
  # not an outage: never poison the leg for every other symbol
2223
- errors.append(f"{name}: {type(e).__name__}: {e}")
2369
+ errors.append(f"{name}: {type(e).__name__}: {scrub_secrets(str(e))}")
2224
2370
  except Exception as e:
2225
- errors.append(f"{name}: {type(e).__name__}: {e}")
2371
+ errors.append(f"{name}: {type(e).__name__}: {scrub_secrets(str(e))}")
2226
2372
  if negative_cache and not offline_em:
2227
2373
  _disk_cache_set(f"resolve-down-{name}", time.time())
2228
2374
 
2229
2375
  if market == "A":
2230
2376
  # akshare/efinance leave requests timeout-less — on a blackholed network a leg
2231
- # hangs until the kernel TCP timeout (snapshot_a has the same guard). Bound
2232
- # each blocking socket op so every leg gets its turn within the caller's
2233
- # subprocess budget.
2234
- import socket
2235
-
2236
- old_timeout = socket.getdefaulttimeout()
2237
- socket.setdefaulttimeout(25)
2238
- try:
2377
+ # hangs until the kernel TCP timeout; the chain-wide socket bound
2378
+ # (socket_timeout) gives every leg its turn within the caller's budget.
2379
+ with socket_timeout(25):
2239
2380
  run_legs(sources, negative_cache=True)
2240
2381
  # cninfo last: the token-free non-eastmoney active fallback (issue #35),
2241
2382
  # tried only when everything above yielded nothing
2242
2383
  if not sectors:
2243
2384
  run_legs([("cninfo", lambda: _stock_boards_cninfo(symbol))], negative_cache=True)
2244
- finally:
2245
- socket.setdefaulttimeout(old_timeout)
2246
2385
  else:
2247
2386
  run_legs(sources, negative_cache=False)
2248
2387
  seen = set()
@@ -2260,6 +2399,8 @@ def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
2260
2399
  result["error"] = error
2261
2400
  return result
2262
2401
  result["sectors"] = deduped
2402
+ if errors:
2403
+ result["warnings"] = errors
2263
2404
  return result
2264
2405
 
2265
2406
 
@@ -2273,9 +2414,15 @@ def cmd_stock_info(args):
2273
2414
  import akshare as ak
2274
2415
 
2275
2416
  result = {"symbol": args.symbol, "market": "A"}
2417
+ is_etf = normalize_stock_code(args.symbol)["is_etf"]
2418
+ if is_etf:
2419
+ result["is_etf"] = True
2276
2420
  info_map = None
2277
2421
  try:
2278
- df = _akshare_retry(ak.stock_individual_info_em, symbol=args.symbol)
2422
+ df = _cached_frame(
2423
+ f"a-basic-info:{args.symbol}",
2424
+ lambda: _akshare_retry(ak.stock_individual_info_em, symbol=args.symbol),
2425
+ )
2279
2426
  if df is not None and not df.empty:
2280
2427
  info_map = {}
2281
2428
  for _, row in df.iterrows():
@@ -2287,22 +2434,51 @@ def cmd_stock_info(args):
2287
2434
  result["float_shares"] = info_map.get("流通股")
2288
2435
  result["total_market_cap"] = info_map.get("总市值")
2289
2436
  result["float_market_cap"] = info_map.get("流通市值")
2437
+ else:
2438
+ result.setdefault("warnings", []).append("basic information unavailable")
2290
2439
  except Exception:
2291
- pass
2440
+ result.setdefault("warnings", []).append("basic information unavailable")
2292
2441
  try:
2293
2442
  # boards 复用 resolve_stock_sectors;info_map 透传避免东财二次拉取
2294
2443
  resolved = resolve_stock_sectors(args.symbol, info_map=info_map)
2295
2444
  boards = [s["name"] for s in resolved.get("sectors", [])]
2296
2445
  if boards:
2297
2446
  result["boards"] = boards[:10]
2447
+ if resolved.get("error") or resolved.get("warnings"):
2448
+ result.setdefault("warnings", []).append("sector mapping incomplete")
2298
2449
  except Exception:
2299
- pass
2450
+ result.setdefault("warnings", []).append("sector mapping unavailable")
2451
+ if is_etf:
2452
+ try:
2453
+ # ETF 最新净值(天天基金-场内基金历史净值明细)。窄日期窗口 → 单页返回
2454
+ end = datetime.now()
2455
+ start = end - timedelta(days=30)
2456
+ nav_df = _cached_frame(
2457
+ f"etf-basic-nav:{args.symbol}",
2458
+ lambda: _akshare_retry(
2459
+ ak.fund_etf_fund_info_em,
2460
+ fund=args.symbol,
2461
+ start_date=start.strftime("%Y%m%d"),
2462
+ end_date=end.strftime("%Y%m%d"),
2463
+ ),
2464
+ )
2465
+ if nav_df is not None and not nav_df.empty:
2466
+ latest = nav_df.iloc[-1]
2467
+ result["nav"] = latest.get("单位净值")
2468
+ result["accumulated_nav"] = latest.get("累计净值")
2469
+ nav_date = latest.get("净值日期")
2470
+ if nav_date is not None:
2471
+ result["nav_date"] = str(nav_date)[:10] # datetime.date/Timestamp → ISO date
2472
+ else:
2473
+ result.setdefault("warnings", []).append("ETF NAV unavailable")
2474
+ except Exception:
2475
+ result.setdefault("warnings", []).append("ETF NAV unavailable")
2300
2476
  return _clean_row(result)
2301
2477
  else:
2302
2478
  import yfinance as yf
2303
2479
 
2304
2480
  t = yf.Ticker(_yf_hk_symbol(args.symbol))
2305
- info = t.info
2481
+ info = _yf_info(args.symbol, t)
2306
2482
  return _clean_row(
2307
2483
  {
2308
2484
  "symbol": args.symbol,
@@ -2470,7 +2646,7 @@ def cmd_fundamental_context(args):
2470
2646
  result["valuation"] = {}
2471
2647
 
2472
2648
  try:
2473
- df = _akshare_retry(ak.stock_financial_analysis_indicator, symbol=args.symbol)
2649
+ df = _financials_a_frame(args.symbol)
2474
2650
  if df is not None and len(df) >= 1:
2475
2651
  r0 = df.iloc[0]
2476
2652
  profitability = {
@@ -2744,24 +2920,6 @@ def _hot_stocks_em() -> list:
2744
2920
  return [_strip_exchange_prefix(_clean_row(r)) for r in df[keep].to_dict("records")]
2745
2921
 
2746
2922
 
2747
- def _hot_stocks_xq() -> list:
2748
- """Xueqiu follow ranking via akshare stock_hot_follow_xq. No 涨跌幅 column;
2749
- rank is the row position. Codes arrive SH/SZ-prefixed like eastmoney's."""
2750
- import akshare as ak
2751
-
2752
- df = _akshare_retry(ak.stock_hot_follow_xq, symbol="最热门")
2753
- if df is None or df.empty:
2754
- return []
2755
- col_map = {
2756
- "股票代码": "code",
2757
- "股票简称": "name",
2758
- "最新价": "price",
2759
- }
2760
- df = df.rename(columns=col_map)
2761
- keep = [c for c in col_map.values() if c in df.columns]
2762
- return [{"rank": i, **_strip_exchange_prefix(_clean_row(r))} for i, r in enumerate(df[keep].to_dict("records"), 1)]
2763
-
2764
-
2765
2923
  def _hot_stocks_baidu() -> list:
2766
2924
  """Baidu 股市通热搜 via akshare stock_hot_search_baidu. No code/price columns —
2767
2925
  only the stock name — and 涨跌幅 strings carry a % suffix."""
@@ -2783,10 +2941,13 @@ def _hot_stocks_baidu() -> list:
2783
2941
 
2784
2942
 
2785
2943
  def cmd_hot_stocks(args):
2786
- """A-share popularity ranking (人气榜): eastmoney → xueqiu → baidu."""
2944
+ """A-share popularity ranking (人气榜): eastmoney → baidu.
2945
+
2946
+ The xueqiu leg was dropped: akshare's stock_hot_follow_xq accepts no user token
2947
+ and its built-in xq_a_token is stale (xueqiu answers 400016), so the leg could
2948
+ never win — failover errors now name only the sources that actually ran."""
2787
2949
  sources = [
2788
2950
  _sourced("eastmoney", _hot_stocks_em),
2789
- _sourced("xueqiu", _hot_stocks_xq),
2790
2951
  _sourced("baidu", _hot_stocks_baidu),
2791
2952
  ]
2792
2953
  try:
@@ -2799,6 +2960,188 @@ def cmd_hot_stocks(args):
2799
2960
  return {"count": len(items), "items": items, "source": rows[0]["source"]}
2800
2961
 
2801
2962
 
2963
+ # --------------- margin_trading / northbound_flow (A-share only) ---------------
2964
+
2965
+ # Exchange margin-detail column layouts differ: SSE carries 融资偿还额/融券偿还量,
2966
+ # SZSE carries 融券余额/融资融券余额 — the output keys below follow the source.
2967
+ _MARGIN_DETAIL_SSE_COLS = {
2968
+ "标的证券代码": "symbol",
2969
+ "标的证券简称": "name",
2970
+ "融资余额": "margin_balance",
2971
+ "融资买入额": "margin_buy",
2972
+ "融资偿还额": "margin_repay",
2973
+ "融券余量": "short_balance_shares",
2974
+ "融券卖出量": "short_sell_shares",
2975
+ "融券偿还量": "short_repay_shares",
2976
+ }
2977
+
2978
+ _MARGIN_DETAIL_SZSE_COLS = {
2979
+ "证券代码": "symbol",
2980
+ "证券简称": "name",
2981
+ "融资买入额": "margin_buy",
2982
+ "融资余额": "margin_balance",
2983
+ "融券卖出量": "short_sell_shares",
2984
+ "融券余量": "short_balance_shares",
2985
+ "融券余额": "short_balance",
2986
+ "融资融券余额": "total_balance",
2987
+ }
2988
+
2989
+
2990
+ def cmd_margin_trading(args):
2991
+ """Per-stock margin trading detail (融资融券) for the last N trading days.
2992
+
2993
+ akshare has no per-stock margin history endpoint — the exchange detail lists
2994
+ (stock_margin_detail_sse / stock_margin_detail_szse) are per-day, all-stock
2995
+ tables, so each trading day is fetched and filtered to the symbol. Daily fetches
2996
+ are independent and run concurrently (5 workers; a serial 60-day run would
2997
+ overrun the host's 120s subprocess budget), then re-sorted newest-first — the
2998
+ output never depends on completion order. A day that fails or has no data is
2999
+ skipped (两融明细 T 日晚间才发布 — a morning run always misses the latest day);
3000
+ only when every day failed is an error returned. Days where the symbol is
3001
+ absent (non-margin underlying that day) are skipped; if no day yields a row the
3002
+ symbol is reported as not covered."""
3003
+ if detect_market(args.symbol) != "A":
3004
+ return {"error": "margin_trading only available for A-shares"}
3005
+ days = getattr(args, "days", 10)
3006
+ if days < 1:
3007
+ return {"error": f"days must be a positive integer, got {days}"}
3008
+ # One akshare call per day — cap the fan-out so a slow source can't overrun
3009
+ # the host's subprocess budget (rows carry their dates; truncation is evident).
3010
+ days = min(days, 60)
3011
+ code = _cn_code(args.symbol)
3012
+ exchange, bare = code[:2], code[2:]
3013
+ try:
3014
+ import concurrent.futures
3015
+
3016
+ import akshare as ak
3017
+ from trading_calendar import prev_trading_days
3018
+
3019
+ if exchange == "sh":
3020
+ col_map = _MARGIN_DETAIL_SSE_COLS
3021
+ detail_fn = ak.stock_margin_detail_sse
3022
+ elif exchange == "sz":
3023
+ col_map = _MARGIN_DETAIL_SZSE_COLS
3024
+ detail_fn = ak.stock_margin_detail_szse
3025
+ else:
3026
+ return {"error": f"margin_trading: {args.symbol} is a BSE code; only SSE/SZSE margin detail is available"}
3027
+ trade_days = prev_trading_days("CN", days)
3028
+ if not trade_days:
3029
+ return {"error": "margin_trading: trading calendar unavailable"}
3030
+
3031
+ def fetch_day(date_iso):
3032
+ """Fetch + filter one day's all-stock table, fault-isolated per day."""
3033
+ try:
3034
+ df = _akshare_retry(detail_fn, date=date_iso.replace("-", ""))
3035
+ except Exception as e:
3036
+ return date_iso, [], f"{date_iso}: {type(e).__name__}: {scrub_secrets(str(e))}"
3037
+ if df is None or df.empty:
3038
+ return date_iso, [], None
3039
+ df = df.rename(columns=col_map)
3040
+ keep = [c for c in col_map.values() if c in df.columns]
3041
+ hit = df[df["symbol"].astype(str) == bare] if "symbol" in df.columns else df.iloc[0:0]
3042
+ return date_iso, [{"date": date_iso, **_clean_row(r)} for r in hit[keep].to_dict("records")], None
3043
+
3044
+ rows = []
3045
+ failures = []
3046
+ with socket_timeout(25), concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool:
3047
+ for _date_iso, day_rows, failure in pool.map(fetch_day, trade_days):
3048
+ if failure:
3049
+ failures.append(failure)
3050
+ rows.extend(day_rows)
3051
+ rows.sort(key=lambda r: r["date"], reverse=True)
3052
+ if not rows:
3053
+ if failures:
3054
+ msg = f"margin detail fetch failed for all {len(trade_days)} day(s): {'; '.join(failures)}"
3055
+ return {"symbol": args.symbol, "error": msg}
3056
+ return {"symbol": args.symbol, "error": f"no margin trading data for {args.symbol} (可能不是两融标的)"}
3057
+ if getattr(args, "include_ratio", False):
3058
+ try:
3059
+ with socket_timeout(25):
3060
+ history = (
3061
+ _failover(
3062
+ [
3063
+ ("akshare", lambda: _kline_akshare(bare, "daily", days + 10)),
3064
+ ("efinance", lambda: _kline_efinance(bare, "daily", days + 10)),
3065
+ ("baostock", lambda: _kline_baostock(bare, "daily", days + 10)),
3066
+ ],
3067
+ label=f"margin_turnover:{bare}",
3068
+ )
3069
+ or []
3070
+ )
3071
+ turnovers = {str(r.get("date"))[:10]: _sanitize(_parse_float(r.get("turnover"))) for r in history}
3072
+ except Exception:
3073
+ turnovers = {}
3074
+ for row in rows:
3075
+ turnover = turnovers.get(row["date"])
3076
+ buy = _sanitize(_parse_float(row.get("margin_buy")))
3077
+ row["turnover"] = turnover
3078
+ row["margin_buy_ratio_pct"] = (
3079
+ round(buy / turnover * 100, 4) if buy is not None and turnover and turnover > 0 else None
3080
+ )
3081
+ if row["margin_buy_ratio_pct"] is None:
3082
+ row["margin_buy_ratio_note"] = "matching-date CNY turnover unavailable"
3083
+ return rows
3084
+ except Exception as e:
3085
+ return {"error": str(e)}
3086
+
3087
+
3088
+ def _northbound_row_is_shell(row: dict) -> bool:
3089
+ """停披期空壳行:date 之外每个数据字段都是 None。持股市值在停披后被回填 0.0
3090
+ (北向持仓市值约 1.9 万亿,0 是缺失哨兵而非真实值),视同 None。"""
3091
+ for k, v in row.items():
3092
+ if k == "date" or v is None:
3093
+ continue
3094
+ if k == "hold_market_cap" and not v:
3095
+ continue
3096
+ return False
3097
+ return True
3098
+
3099
+
3100
+ def cmd_northbound_flow(args):
3101
+ """Market-level northbound (北向资金) flow history via eastmoney hsgt history.
3102
+
3103
+ Data caliber: the exchanges stopped publishing daily northbound net-buy after
3104
+ 2024-08-16 — later rows are shells (date only, flow fields all None,
3105
+ 持股市值 backfilled 0.0) except quarter-end rows, which still disclose
3106
+ 持股市值. Shell rows are filtered out of the tail(days) window; when nothing
3107
+ real remains, a clean error explains the disclosure stop (increase days to
3108
+ reach pre-2024-08 history). The series comes back ascending — returned
3109
+ newest-first."""
3110
+ days = getattr(args, "days", 10)
3111
+ if days < 1:
3112
+ return {"error": f"days must be a positive integer, got {days}"}
3113
+ try:
3114
+ import akshare as ak
3115
+
3116
+ with socket_timeout(25):
3117
+ df = _akshare_retry(ak.stock_hsgt_hist_em, symbol="北向资金")
3118
+ if df is None or df.empty:
3119
+ return {"error": "northbound flow data unavailable"}
3120
+ col_map = {
3121
+ "日期": "date",
3122
+ "当日成交净买额": "net_buy",
3123
+ "买入成交额": "buy_turnover",
3124
+ "卖出成交额": "sell_turnover",
3125
+ "历史累计净买额": "accum_net_buy",
3126
+ "持股市值": "hold_market_cap",
3127
+ }
3128
+ df = df.rename(columns=col_map)
3129
+ keep = [c for c in col_map.values() if c in df.columns]
3130
+ rows = [_clean_row(r) for r in df[keep].tail(days).to_dict("records")]
3131
+ rows = [r for r in rows if not _northbound_row_is_shell(r)]
3132
+ if not rows:
3133
+ return {
3134
+ "error": (
3135
+ "北向资金日度净买额自 2024-08-16 起交易所停止披露,仅此前历史数据可用"
3136
+ f"(当前 days={days} 窗口内无有效数据,加大 days 可取历史)"
3137
+ )
3138
+ }
3139
+ rows.reverse()
3140
+ return rows
3141
+ except Exception as e:
3142
+ return {"error": str(e)}
3143
+
3144
+
2802
3145
  # --------------- CLI ---------------
2803
3146
 
2804
3147
 
@@ -2828,6 +3171,8 @@ def main():
2828
3171
 
2829
3172
  p_fin = sub.add_parser("financials")
2830
3173
  p_fin.add_argument("symbol")
3174
+ p_fin.add_argument("--periods", type=int, default=1)
3175
+ p_fin.add_argument("--annual", action="store_true", help="Select annual reports; periods=10 requests ten years")
2831
3176
 
2832
3177
  p_snap = sub.add_parser("market_snapshot")
2833
3178
  p_snap.add_argument("--market", default="A", choices=["A", "HK", "US"])
@@ -2869,6 +3214,14 @@ def main():
2869
3214
  p_hot = sub.add_parser("hot_stocks")
2870
3215
  p_hot.add_argument("--top", type=int, default=20)
2871
3216
 
3217
+ p_margin = sub.add_parser("margin_trading")
3218
+ p_margin.add_argument("symbol")
3219
+ p_margin.add_argument("--days", type=int, default=10)
3220
+ p_margin.add_argument("--include-ratio", action="store_true", help="Add same-day margin buys / CNY turnover")
3221
+
3222
+ p_nb = sub.add_parser("northbound_flow")
3223
+ p_nb.add_argument("--days", type=int, default=10)
3224
+
2872
3225
  args = parser.parse_args()
2873
3226
  if not args.command:
2874
3227
  parser.print_help()
@@ -2892,6 +3245,8 @@ def main():
2892
3245
  "limit_up_pool": cmd_limit_up_pool,
2893
3246
  "dragon_tiger": cmd_dragon_tiger,
2894
3247
  "hot_stocks": cmd_hot_stocks,
3248
+ "margin_trading": cmd_margin_trading,
3249
+ "northbound_flow": cmd_northbound_flow,
2895
3250
  }
2896
3251
  # Provider libraries (baostock etc.) print prose to stdout on failure,
2897
3252
  # which would corrupt the JSON-only contract — divert that noise to stderr.
@@ -2901,9 +3256,5 @@ def main():
2901
3256
 
2902
3257
 
2903
3258
  if __name__ == "__main__":
2904
- # Windows defaults stdio to a legacy code page (cp1252) that cannot encode the
2905
- # Chinese text these tools emit — force UTF-8 so stdout never crashes there.
2906
- for _s in (sys.stdout, sys.stderr):
2907
- if hasattr(_s, "reconfigure"):
2908
- _s.reconfigure(encoding="utf-8")
3259
+ utf8_stdio()
2909
3260
  main()