stock-analysis-plugin 0.2.3 → 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.
@@ -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 _cache import DAY, WEEK, cached # noqa: E402
16
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:
@@ -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,8 +430,8 @@ 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,)):
@@ -450,24 +451,22 @@ 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
472
  with socket_timeout(25):
@@ -515,7 +514,7 @@ def _kline_akshare(symbol: str, period: str, count: int) -> list:
515
514
 
516
515
 
517
516
  def _kline_tushare(symbol: str, period: str, count: int) -> list:
518
- """Tushare kline via HTTP API. Requires TUSHARE_TOKEN."""
517
+ """Tushare qfq kline via daily factors or the adjusted week/month endpoint."""
519
518
  import os
520
519
 
521
520
  import pandas as pd
@@ -525,23 +524,29 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
525
524
  if not token:
526
525
  raise ValueError("TUSHARE_TOKEN not set")
527
526
 
528
- 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")
529
529
  end = datetime.now()
530
530
  start = end - timedelta(days=count * 7 if period == "weekly" else count * 31 if period == "monthly" else count * 2)
531
531
 
532
- 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]
533
542
 
534
543
  resp = requests.post(
535
544
  "http://api.tushare.pro",
536
545
  json={
537
- "api_name": api_name_map.get(period, "daily"),
546
+ "api_name": "daily" if period == "daily" else "stk_week_month_adj",
538
547
  "token": token,
539
- "params": {
540
- "ts_code": ts_code,
541
- "start_date": start.strftime("%Y%m%d"),
542
- "end_date": end.strftime("%Y%m%d"),
543
- },
544
- "fields": "trade_date,open,high,low,close,vol,amount,pct_chg",
548
+ "params": params,
549
+ "fields": ",".join(["trade_date", *fields, "vol", "amount", "pct_chg"]),
545
550
  },
546
551
  timeout=15,
547
552
  )
@@ -550,12 +555,36 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
550
555
  raise ValueError(f"tushare returned no data: {data.get('msg', '')}")
551
556
 
552
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")
553
583
  col_map = {"trade_date": "date", "vol": "volume", "amount": "turnover", "pct_chg": "change_pct"}
554
584
  df = df.rename(columns=col_map)
555
585
  df = df.sort_values("date")
556
- for c in ["open", "high", "low", "close", "volume"]:
557
- if c in df.columns:
558
- 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")
559
588
  keep = [c for c in ["date", "open", "high", "low", "close", "volume", "turnover", "change_pct"] if c in df.columns]
560
589
  df = df[keep].tail(count)
561
590
  return [_clean_row(r) for r in df.to_dict("records")]
@@ -632,41 +661,6 @@ def _pytdx_connect(api) -> None:
632
661
  raise ConnectionError(f"pytdx failed to connect to any TDX server ({'; '.join(errors)})")
633
662
 
634
663
 
635
- def _kline_pytdx(symbol: str, period: str, count: int) -> list:
636
- """pytdx kline from TDX market servers. No credentials needed."""
637
- from pytdx.hq import TdxHq_API
638
-
639
- market = 1 if symbol.startswith(("6", "9", "5")) else 0
640
- freq_map = {"daily": 9, "weekly": 5, "monthly": 6}
641
-
642
- api = TdxHq_API()
643
- try:
644
- _pytdx_connect(api)
645
- data = api.get_security_bars(freq_map.get(period, 9), market, symbol, 0, count)
646
- finally:
647
- api.disconnect()
648
-
649
- if not data:
650
- raise ValueError("pytdx returned empty data")
651
-
652
- rows = []
653
- for bar in data:
654
- rows.append(
655
- _clean_row(
656
- {
657
- "date": bar["datetime"][:10],
658
- "open": bar["open"],
659
- "high": bar["high"],
660
- "low": bar["low"],
661
- "close": bar["close"],
662
- "volume": bar["vol"],
663
- "turnover": bar.get("amount"),
664
- }
665
- )
666
- )
667
- return rows[-count:]
668
-
669
-
670
664
  def _quote_pytdx(symbol: str) -> dict:
671
665
  """pytdx realtime quote from TDX market servers."""
672
666
  from pytdx.hq import TdxHq_API
@@ -966,6 +960,9 @@ def cmd_kline(args):
966
960
  try:
967
961
  if market == "A":
968
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]
969
966
  else:
970
967
  data = kline_yf(args.symbol, args.period, args.count)
971
968
  return data
@@ -1363,17 +1360,46 @@ def _financials_a_row(r) -> dict:
1363
1360
  )
1364
1361
 
1365
1362
 
1366
- def financials_a(symbol: str, periods: int = 1) -> dict:
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):
1367
1383
  import akshare as ak
1368
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:
1369
1392
  try:
1370
- 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")]
1371
1396
  if df is None or df.empty:
1372
1397
  # error + note coexist (additive): consumers check either key
1373
1398
  return {"symbol": symbol, "error": "No financial data", "note": "No financial data"}
1374
1399
  result = {"symbol": symbol, **_financials_a_row(df.iloc[0])}
1375
1400
  if periods > 1:
1376
1401
  result["periods"] = [_financials_a_row(r) for r in df.head(periods).to_dict("records")]
1402
+ result["available_periods"] = len(result["periods"])
1377
1403
  return result
1378
1404
  except Exception:
1379
1405
  return {"symbol": symbol, "error": "Financial data unavailable", "note": "Financial data unavailable"}
@@ -1386,25 +1412,41 @@ def _yf_financial_periods(t, periods: int) -> list:
1386
1412
  fin = t.financials
1387
1413
  if fin is None or fin.empty:
1388
1414
  return []
1389
- return [
1390
- _clean_row(
1391
- {
1392
- "period_end": col,
1393
- "total_revenue": fin[col].get("Total Revenue"),
1394
- "net_income": fin[col].get("Net Income"),
1395
- }
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
+ )
1396
1434
  )
1397
- for col in list(fin.columns)[:periods]
1398
- ]
1435
+ return rows
1399
1436
  except Exception:
1400
1437
  return []
1401
1438
 
1402
1439
 
1403
- def financials_yf(symbol: str, periods: int = 1) -> dict:
1440
+ def financials_yf(symbol: str, periods: int = 1, annual: bool = False) -> dict:
1404
1441
  import yfinance as yf
1405
1442
 
1406
1443
  t = yf.Ticker(_yf_hk_symbol(symbol))
1407
- info = t.info
1444
+ warnings = []
1445
+ try:
1446
+ info = _yf_info(symbol, t)
1447
+ except Exception:
1448
+ info = {}
1449
+ warnings.append("Yahoo snapshot unavailable")
1408
1450
  result = _clean_row(
1409
1451
  {
1410
1452
  "symbol": symbol,
@@ -1421,20 +1463,63 @@ def financials_yf(symbol: str, periods: int = 1) -> dict:
1421
1463
  "dividend_yield": info.get("dividendYield"),
1422
1464
  }
1423
1465
  )
1424
- if periods > 1:
1425
- history = _yf_financial_periods(t, periods)
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
+ )
1426
1503
  if history:
1427
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"
1428
1510
  return result
1429
1511
 
1430
1512
 
1431
1513
  def cmd_financials(args):
1432
1514
  periods = getattr(args, "periods", 1)
1433
- if periods < 1:
1434
- return {"error": f"periods must be a positive integer, got {periods}"}
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)
1435
1518
  market = detect_market(args.symbol)
1436
1519
  try:
1437
- return financials_a(args.symbol, periods) if market == "A" else financials_yf(args.symbol, periods)
1520
+ return (
1521
+ financials_a(args.symbol, periods, annual) if market == "A" else financials_yf(args.symbol, periods, annual)
1522
+ )
1438
1523
  except Exception as e:
1439
1524
  return {"error": str(e)}
1440
1525
 
@@ -2050,6 +2135,12 @@ def sector_constituents_a(sector: str, board_type: str = "auto") -> dict:
2050
2135
 
2051
2136
 
2052
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):
2053
2144
  # argparse 保证属性存在;空串仍可传入
2054
2145
  sector = args.sector.strip()
2055
2146
  if not sector:
@@ -2058,9 +2149,6 @@ def cmd_sector_constituents(args):
2058
2149
  # Sector names are user input — hash them instead of sanitizing, so names that
2059
2150
  # differ only in whitespace/punctuation (创新药 vs 创新 药) can't collide.
2060
2151
  cache_key = hashlib.sha256(f"{sector}|{board_type}".encode()).hexdigest()[:16]
2061
- cached = _disk_cache_get(cache_key)
2062
- if cached is not None:
2063
- return cached
2064
2152
  try:
2065
2153
  result = sector_constituents_a(sector, board_type)
2066
2154
  except Exception as e:
@@ -2164,13 +2252,17 @@ def _stock_boards_cninfo(symbol: str) -> list:
2164
2252
  def _stock_boards_from_cache(symbol: str) -> list:
2165
2253
  """Reverse lookup against the get_sector_constituents disk cache — the honest
2166
2254
  fallback when eastmoney blocks the caller's IP and xueqiu has no token (the two
2167
- live A-share sources are both eastmoney-flavored). Reads every unexpired
2168
- 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
2169
2257
  containing the symbol. Cold cache → []; malformed cache files are skipped
2170
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
+
2171
2263
  boards = []
2172
- for path in _DATA_CACHE_DIR.glob("*.json"):
2173
- 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)):
2174
2266
  if not isinstance(data, dict):
2175
2267
  continue
2176
2268
  sector = data.get("sector")
@@ -2178,7 +2270,9 @@ def _stock_boards_from_cache(symbol: str) -> list:
2178
2270
  continue
2179
2271
  for row in data.get("constituents") or []:
2180
2272
  if isinstance(row, dict) and str(row.get("code")) == symbol:
2181
- 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)
2182
2276
  break
2183
2277
  return boards
2184
2278
 
@@ -2223,6 +2317,12 @@ def _resolve_leg_down(name: str) -> bool:
2223
2317
 
2224
2318
 
2225
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:
2226
2326
  """Reverse map: stock → boards it belongs to (A/HK). Sources are merged (a single
2227
2327
  source failing is not fatal — the others' boards are still returned) rather than
2228
2328
  first-hit failover: each source covers a different flavor (industry vs concept).
@@ -2299,6 +2399,8 @@ def resolve_stock_sectors(symbol: str, info_map=None) -> dict:
2299
2399
  result["error"] = error
2300
2400
  return result
2301
2401
  result["sectors"] = deduped
2402
+ if errors:
2403
+ result["warnings"] = errors
2302
2404
  return result
2303
2405
 
2304
2406
 
@@ -2317,7 +2419,10 @@ def cmd_stock_info(args):
2317
2419
  result["is_etf"] = True
2318
2420
  info_map = None
2319
2421
  try:
2320
- 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
+ )
2321
2426
  if df is not None and not df.empty:
2322
2427
  info_map = {}
2323
2428
  for _, row in df.iterrows():
@@ -2329,26 +2434,33 @@ def cmd_stock_info(args):
2329
2434
  result["float_shares"] = info_map.get("流通股")
2330
2435
  result["total_market_cap"] = info_map.get("总市值")
2331
2436
  result["float_market_cap"] = info_map.get("流通市值")
2437
+ else:
2438
+ result.setdefault("warnings", []).append("basic information unavailable")
2332
2439
  except Exception:
2333
- pass
2440
+ result.setdefault("warnings", []).append("basic information unavailable")
2334
2441
  try:
2335
2442
  # boards 复用 resolve_stock_sectors;info_map 透传避免东财二次拉取
2336
2443
  resolved = resolve_stock_sectors(args.symbol, info_map=info_map)
2337
2444
  boards = [s["name"] for s in resolved.get("sectors", [])]
2338
2445
  if boards:
2339
2446
  result["boards"] = boards[:10]
2447
+ if resolved.get("error") or resolved.get("warnings"):
2448
+ result.setdefault("warnings", []).append("sector mapping incomplete")
2340
2449
  except Exception:
2341
- pass
2450
+ result.setdefault("warnings", []).append("sector mapping unavailable")
2342
2451
  if is_etf:
2343
2452
  try:
2344
2453
  # ETF 最新净值(天天基金-场内基金历史净值明细)。窄日期窗口 → 单页返回
2345
2454
  end = datetime.now()
2346
2455
  start = end - timedelta(days=30)
2347
- nav_df = _akshare_retry(
2348
- ak.fund_etf_fund_info_em,
2349
- fund=args.symbol,
2350
- start_date=start.strftime("%Y%m%d"),
2351
- end_date=end.strftime("%Y%m%d"),
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
+ ),
2352
2464
  )
2353
2465
  if nav_df is not None and not nav_df.empty:
2354
2466
  latest = nav_df.iloc[-1]
@@ -2357,14 +2469,16 @@ def cmd_stock_info(args):
2357
2469
  nav_date = latest.get("净值日期")
2358
2470
  if nav_date is not None:
2359
2471
  result["nav_date"] = str(nav_date)[:10] # datetime.date/Timestamp → ISO date
2472
+ else:
2473
+ result.setdefault("warnings", []).append("ETF NAV unavailable")
2360
2474
  except Exception:
2361
- pass
2475
+ result.setdefault("warnings", []).append("ETF NAV unavailable")
2362
2476
  return _clean_row(result)
2363
2477
  else:
2364
2478
  import yfinance as yf
2365
2479
 
2366
2480
  t = yf.Ticker(_yf_hk_symbol(args.symbol))
2367
- info = t.info
2481
+ info = _yf_info(args.symbol, t)
2368
2482
  return _clean_row(
2369
2483
  {
2370
2484
  "symbol": args.symbol,
@@ -2532,7 +2646,7 @@ def cmd_fundamental_context(args):
2532
2646
  result["valuation"] = {}
2533
2647
 
2534
2648
  try:
2535
- df = _akshare_retry(ak.stock_financial_analysis_indicator, symbol=args.symbol)
2649
+ df = _financials_a_frame(args.symbol)
2536
2650
  if df is not None and len(df) >= 1:
2537
2651
  r0 = df.iloc[0]
2538
2652
  profitability = {
@@ -2940,6 +3054,32 @@ def cmd_margin_trading(args):
2940
3054
  msg = f"margin detail fetch failed for all {len(trade_days)} day(s): {'; '.join(failures)}"
2941
3055
  return {"symbol": args.symbol, "error": msg}
2942
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"
2943
3083
  return rows
2944
3084
  except Exception as e:
2945
3085
  return {"error": str(e)}
@@ -3032,6 +3172,7 @@ def main():
3032
3172
  p_fin = sub.add_parser("financials")
3033
3173
  p_fin.add_argument("symbol")
3034
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")
3035
3176
 
3036
3177
  p_snap = sub.add_parser("market_snapshot")
3037
3178
  p_snap.add_argument("--market", default="A", choices=["A", "HK", "US"])
@@ -3076,6 +3217,7 @@ def main():
3076
3217
  p_margin = sub.add_parser("margin_trading")
3077
3218
  p_margin.add_argument("symbol")
3078
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")
3079
3221
 
3080
3222
  p_nb = sub.add_parser("northbound_flow")
3081
3223
  p_nb.add_argument("--days", type=int, default=10)