stock-analysis-plugin 0.2.5 → 0.2.7

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 (45) hide show
  1. package/README.md +126 -15
  2. package/package.json +2 -1
  3. package/pi/index.ts +918 -72
  4. package/schemas/market_review_schema.json +12 -5
  5. package/schemas/report_schema.json +68 -4
  6. package/scripts/cli-timeout.mjs +27 -0
  7. package/scripts/json-input.mjs +26 -0
  8. package/scripts/setup-python.mjs +6 -6
  9. package/skills/bull-trend/SKILL.md +34 -10
  10. package/skills/bull-trend/references/fat-tails.md +91 -0
  11. package/skills/bull-trend/scripts/tail_analysis.py +132 -0
  12. package/skills/event-driven/SKILL.md +6 -0
  13. package/skills/growth-quality/SKILL.md +6 -0
  14. package/skills/market-review/SKILL.md +2 -0
  15. package/skills/stock-analysis/SKILL.md +25 -5
  16. package/skills/stock-analysis/references/stateless-analysis.md +66 -0
  17. package/skills/stock-screener/SKILL.md +7 -1
  18. package/skills/strategy-backtest/SKILL.md +13 -0
  19. package/skills/wisburg-research/SKILL.md +2 -0
  20. package/templates/market_review/full.md.j2 +7 -0
  21. package/templates/stock_analysis/_evidence.md.j2 +11 -0
  22. package/templates/stock_analysis/brief.md.j2 +6 -1
  23. package/templates/stock_analysis/full.md.j2 +8 -3
  24. package/tools/_subproc.py +66 -11
  25. package/tools/alert_rules.py +32 -4
  26. package/tools/backtest.py +95 -18
  27. package/tools/capabilities.py +11 -0
  28. package/tools/cli_budgets.json +26 -0
  29. package/tools/gather.py +24 -16
  30. package/tools/lixinger.py +100 -0
  31. package/tools/market_regime.py +7 -3
  32. package/tools/market_review.py +37 -15
  33. package/tools/position_context.py +20 -2
  34. package/tools/report_renderer.py +2 -0
  35. package/tools/risk_screening.py +45 -17
  36. package/tools/screener.py +74 -24
  37. package/tools/sec_edgar.py +346 -0
  38. package/tools/sec_holders.py +263 -0
  39. package/tools/signal_tracker.py +218 -24
  40. package/tools/stateless_analysis.py +630 -0
  41. package/tools/stock_data.py +170 -32
  42. package/tools/technical.py +16 -2
  43. package/tools/value_data.py +159 -0
  44. package/tools/watchlist.py +24 -1
  45. package/tools/watchlist_context.py +25 -10
@@ -7,6 +7,7 @@ import os
7
7
  import re
8
8
  import sys
9
9
  from datetime import date, timedelta
10
+ from html import unescape
10
11
  from io import StringIO
11
12
  from pathlib import Path
12
13
  from urllib.parse import unquote
@@ -86,6 +87,36 @@ def valuation_history(args):
86
87
  market, symbol = stock_identity(args.symbol)
87
88
  if not 1 <= args.years <= 10:
88
89
  raise ValueError("valuation history requires an A/HK symbol and years in 1..10")
90
+ warning = None
91
+ if market == "HK":
92
+ if os.environ.get("LIXINGER_TOKEN"):
93
+ try:
94
+ from lixinger import hk_data
95
+
96
+ rows = hk_data(args.symbol, "fundamental", ["pe_ttm", "pb"])
97
+ cutoff = (date.today() - pd.DateOffset(years=args.years)).date().isoformat()
98
+ history = sorted(
99
+ [
100
+ {"date": iso(r.get("date")), "pe_ttm": number(r.get("pe_ttm")), "pb": number(r.get("pb"))}
101
+ for r in rows
102
+ if iso(r.get("date")) and iso(r.get("date")) >= cutoff
103
+ ],
104
+ key=lambda r: r["date"],
105
+ )
106
+ if not history or not all(any(r[field] is not None for r in history) for field in ("pe_ttm", "pb")):
107
+ raise ValueError("Lixinger HK valuation history incomplete")
108
+ return {
109
+ "symbol": args.symbol,
110
+ "market": market,
111
+ "source": "lixinger",
112
+ "requested_years": args.years,
113
+ "history": history,
114
+ "metrics": {field: valuation_summary(history, field) for field in ("pe_ttm", "pb")},
115
+ }
116
+ except Exception:
117
+ warning = "Lixinger HK unavailable; Baidu history retained without mixing sources"
118
+ else:
119
+ warning = "LIXINGER_TOKEN not configured; HK history limited to available Baidu data"
89
120
  fetch = ak.stock_zh_valuation_baidu if market == "A" else ak.stock_hk_valuation_baidu
90
121
  period = "近一年" if args.years == 1 else "近三年" if args.years <= 3 else "近五年" if args.years <= 5 else "近十年"
91
122
  if market == "HK" and args.years > 3:
@@ -114,6 +145,8 @@ def valuation_history(args):
114
145
  result.update(history=history, metrics={field: valuation_summary(history, field) for field in ("pe_ttm", "pb")})
115
146
  if series.get("warnings"):
116
147
  result["warnings"] = series["warnings"]
148
+ if warning:
149
+ result.setdefault("warnings", []).append(warning)
117
150
  return result
118
151
 
119
152
 
@@ -1311,6 +1344,125 @@ def etf_details(args):
1311
1344
  return result
1312
1345
 
1313
1346
 
1347
+ ANNOUNCEMENT_CATEGORIES = {
1348
+ "all": ("", ""),
1349
+ "forecast": ("category_yjygjxz_szsh", "业绩预告"),
1350
+ "express": ("", "业绩快报"),
1351
+ "annual": ("category_ndbg_szsh", ""),
1352
+ "interim": ("category_bndbg_szsh", ""),
1353
+ "quarterly": ("category_yjdbg_szsh;category_sjdbg_szsh", ""),
1354
+ "reduction": ("category_gqbd_szsh", "减持"),
1355
+ "issuance": ("category_zf_szsh", ""),
1356
+ "inquiry": ("", "问询"),
1357
+ "dividend": ("category_qyfpxzcs_szsh", ""),
1358
+ }
1359
+
1360
+
1361
+ def announcements(args):
1362
+ import requests
1363
+
1364
+ market, symbol = stock_identity(args.symbol)
1365
+ if market != "A" or not 1 <= args.top <= 500 or args.category not in ANNOUNCEMENT_CATEGORIES:
1366
+ raise ValueError("announcements require an A-share stock, valid category and top in 1..500")
1367
+ end = date.fromisoformat(args.end_date) if args.end_date else date.today()
1368
+ start = date.fromisoformat(args.start_date) if args.start_date else end - timedelta(days=90)
1369
+ if start > end:
1370
+ raise ValueError("start_date must not exceed end_date")
1371
+
1372
+ def stock_list():
1373
+ response = requests.get("https://www.cninfo.com.cn/new/data/szse_stock.json", timeout=20)
1374
+ response.raise_for_status()
1375
+ return response.json()["stockList"]
1376
+
1377
+ stocks = cached("cninfo-stocks:A", DAY, stock_list)
1378
+ stock = next((r for r in stocks if r.get("code") == symbol), None)
1379
+ if not stock or not stock.get("orgId"):
1380
+ raise ValueError("stock not found in cninfo A-share directory")
1381
+ category, keyword = ANNOUNCEMENT_CATEGORIES[args.category]
1382
+ payload = {
1383
+ "pageNum": 1,
1384
+ "pageSize": 30,
1385
+ "column": "szse",
1386
+ "tabName": "fulltext",
1387
+ "plate": "",
1388
+ "stock": f"{symbol},{stock['orgId']}",
1389
+ "searchkey": keyword,
1390
+ "category": category,
1391
+ "seDate": f"{start.isoformat()}~{end.isoformat()}",
1392
+ "sortName": "time",
1393
+ "sortType": "desc",
1394
+ "isHLtitle": "false",
1395
+ }
1396
+ rows, seen = [], set()
1397
+ total = 0
1398
+ # shortcut: bounded 17-page query, narrow the date range for more than 500 disclosures.
1399
+ for page in range(1, (args.top + 29) // 30 + 1):
1400
+ payload["pageNum"] = page
1401
+ response = requests.post(
1402
+ "https://www.cninfo.com.cn/new/hisAnnouncement/query",
1403
+ data=payload,
1404
+ headers={"Referer": "https://www.cninfo.com.cn/"},
1405
+ timeout=20,
1406
+ )
1407
+ response.raise_for_status()
1408
+ data = response.json()
1409
+ if "totalAnnouncement" not in data:
1410
+ raise ValueError("cninfo announcement response unavailable")
1411
+ total = int(data["totalAnnouncement"])
1412
+ items = data.get("announcements") or []
1413
+ for row in items:
1414
+ if row.get("secCode") != symbol:
1415
+ continue
1416
+ stamp = iso(pd.to_datetime(row.get("announcementTime"), unit="ms", utc=True))
1417
+ path = row.get("adjunctUrl") or ""
1418
+ if not stamp or not start.isoformat() <= stamp <= end.isoformat() or path in seen:
1419
+ continue
1420
+ if not re.fullmatch(r"finalpage/[\d-]+/[\w.-]+\.pdf", path, re.I):
1421
+ continue
1422
+ seen.add(path)
1423
+ title = unescape(re.sub(r"<[^>]+>", "", row.get("announcementTitle") or ""))
1424
+ inferred = next(
1425
+ (
1426
+ key
1427
+ for key, pattern in [
1428
+ ("forecast", "业绩预告"),
1429
+ ("express", "业绩快报"),
1430
+ ("inquiry", "问询"),
1431
+ ("reduction", "减持"),
1432
+ ("interim", "半年度报告"),
1433
+ ("annual", "年度报告"),
1434
+ ("quarterly", "季度报告"),
1435
+ ("issuance", "发行|增发|定增"),
1436
+ ("dividend", "分红|权益分派|利润分配"),
1437
+ ]
1438
+ if re.search(pattern, title)
1439
+ ),
1440
+ "other",
1441
+ )
1442
+ rows.append(
1443
+ {
1444
+ "title": title,
1445
+ "date": stamp,
1446
+ "category": inferred,
1447
+ "pdf_url": f"https://static.cninfo.com.cn/{path}",
1448
+ }
1449
+ )
1450
+ if not items or page * 30 >= total or len(rows) >= args.top:
1451
+ break
1452
+ return {
1453
+ "symbol": args.symbol,
1454
+ "market": "A",
1455
+ "source": "cninfo",
1456
+ "start_date": start.isoformat(),
1457
+ "end_date": end.isoformat(),
1458
+ "total": total,
1459
+ "announcements": rows[: args.top],
1460
+ "truncated": total > len(rows[: args.top]),
1461
+ "body_extracted": False,
1462
+ "requested_category": args.category,
1463
+ }
1464
+
1465
+
1314
1466
  def main():
1315
1467
  parser = argparse.ArgumentParser(description="Value-investing datasets and dated historical context")
1316
1468
  sub = parser.add_subparsers(dest="command")
@@ -1318,6 +1470,12 @@ def main():
1318
1470
  p = sub.add_parser(command)
1319
1471
  p.add_argument("symbol")
1320
1472
  p.add_argument("--years", type=int, default=5)
1473
+ p = sub.add_parser("announcements")
1474
+ p.add_argument("symbol")
1475
+ p.add_argument("--category", choices=list(ANNOUNCEMENT_CATEGORIES), default="all")
1476
+ p.add_argument("--start-date", default="")
1477
+ p.add_argument("--end-date", default="")
1478
+ p.add_argument("--top", type=int, default=50)
1321
1479
  p = sub.add_parser("earnings_calendar")
1322
1480
  p.add_argument("--symbol", default="")
1323
1481
  p.add_argument("--report-date", default="")
@@ -1342,6 +1500,7 @@ def main():
1342
1500
  try:
1343
1501
  args = parser.parse_args()
1344
1502
  handlers = {
1503
+ "announcements": announcements,
1345
1504
  "valuation_history": valuation_history,
1346
1505
  "index_valuation": index_valuation,
1347
1506
  "earnings_calendar": earnings_calendar,
@@ -3,6 +3,7 @@
3
3
 
4
4
  import argparse
5
5
  import json
6
+ import math
6
7
  import sys
7
8
  from concurrent.futures import ThreadPoolExecutor, as_completed
8
9
  from pathlib import Path
@@ -12,7 +13,24 @@ from _subproc import utf8_stdio
12
13
  from gather import gather_analysis
13
14
 
14
15
 
16
+ def has_analysis_data(data) -> bool:
17
+ if not isinstance(data, dict):
18
+ return False
19
+ for key, field in (("quote", "price"), ("kline", "close"), ("technical", "signal_score")):
20
+ payload = data.get(key)
21
+ rows = payload if isinstance(payload, list) else [payload]
22
+ for row in rows:
23
+ if not isinstance(row, dict) or "error" in row:
24
+ continue
25
+ value = row.get(field)
26
+ if isinstance(value, (int, float)) and math.isfinite(value) and (key == "technical" or value > 0):
27
+ return True
28
+ return False
29
+
30
+
15
31
  def analyze_watchlist(symbols: list[str], workers: int = 3) -> dict:
32
+ if isinstance(workers, bool) or not isinstance(workers, int) or workers <= 0:
33
+ return {"error": "workers must be a positive integer"}
16
34
  results = {}
17
35
  success = 0
18
36
  failed = 0
@@ -24,7 +42,10 @@ def analyze_watchlist(symbols: list[str], workers: int = 3) -> dict:
24
42
  try:
25
43
  data = future.result()
26
44
  results[sym] = data
27
- success += 1
45
+ if has_analysis_data(data):
46
+ success += 1
47
+ else:
48
+ failed += 1
28
49
  except Exception:
29
50
  results[sym] = None
30
51
  failed += 1
@@ -53,6 +74,8 @@ def main():
53
74
  result = analyze_watchlist(codes, workers=args.workers)
54
75
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2)
55
76
  print()
77
+ if "error" in result:
78
+ sys.exit(1)
56
79
  else:
57
80
  parser.print_help()
58
81
  sys.exit(1)
@@ -14,9 +14,9 @@ from pathlib import Path
14
14
 
15
15
  sys.path.insert(0, str(Path(__file__).resolve().parent))
16
16
  from _subproc import run_tool as _run_json # noqa: E402
17
- from _subproc import utf8_stdio
17
+ from _subproc import scrub_secrets, utf8_stdio
18
18
  from stock_data import detect_market # noqa: E402
19
- from watchlist import analyze_watchlist # noqa: E402
19
+ from watchlist import analyze_watchlist, has_analysis_data # noqa: E402
20
20
 
21
21
 
22
22
  def _suggest_next_tools(market: str, trend: str, anomalies: list, risk_level: str) -> list:
@@ -39,20 +39,27 @@ def _suggest_next_tools(market: str, trend: str, anomalies: list, risk_level: st
39
39
 
40
40
 
41
41
  def _build_item(symbol: str, data: dict | None) -> dict:
42
- if not data:
42
+ if not has_analysis_data(data):
43
43
  return {"symbol": symbol, "error": "no data"}
44
44
 
45
45
  tech = data.get("technical") or {}
46
46
  risk = data.get("risk") or {}
47
47
 
48
48
  # anomalies — separate call (watchlist gather doesn't include them)
49
- anom_result = _run_json("anomaly_detect.py", ["detect", symbol]) or {}
50
- anomalies = anom_result.get("anomalies", []) if isinstance(anom_result, dict) else []
49
+ anom_result = _run_json("anomaly_detect.py", ["detect", symbol], preserve_errors=True)
50
+ anomaly_ok = (
51
+ isinstance(anom_result, dict) and "error" not in anom_result and isinstance(anom_result.get("anomalies"), list)
52
+ )
53
+ anomalies = anom_result["anomalies"] if anomaly_ok else []
54
+ anomaly_error = None
55
+ if not anomaly_ok:
56
+ error = anom_result.get("error") if isinstance(anom_result, dict) else None
57
+ anomaly_error = scrub_secrets(error) if isinstance(error, str) else "Anomaly data unavailable"
51
58
 
52
59
  score = tech.get("signal_score")
53
60
  trend = (tech.get("trend") or {}).get("overall") or "unknown"
54
61
  risk_level = risk.get("risk_level", "unknown")
55
- veto = bool(risk.get("veto_buy", False))
62
+ veto = risk.get("veto_buy")
56
63
  buy_signal = tech.get("buy_signal")
57
64
  market = detect_market(symbol)
58
65
 
@@ -72,13 +79,18 @@ def _build_item(symbol: str, data: dict | None) -> dict:
72
79
  "anomalies": [
73
80
  {"type": a.get("type"), "severity": a.get("severity"), "direction": a.get("direction")} for a in anomalies
74
81
  ],
75
- "anomaly_count": len(anomalies),
82
+ "anomaly_count": len(anomalies) if anomaly_ok else None,
83
+ "anomaly_status": "ok" if anomaly_ok else "unavailable",
84
+ "anomaly_error": anomaly_error,
85
+ "warnings": [f"Anomaly detection unavailable: {anomaly_error}"] if anomaly_error else [],
76
86
  "next_tools": _suggest_next_tools(market, trend, anomalies, risk_level),
77
87
  }
78
88
 
79
89
 
80
90
  def build_context(symbols: list[str], include_market_review: bool = False, workers: int = 3) -> dict:
81
91
  raw = analyze_watchlist(symbols, workers=workers)
92
+ if "error" in raw:
93
+ return raw
82
94
  results = raw.get("results", {})
83
95
 
84
96
  items = []
@@ -98,11 +110,12 @@ def build_context(symbols: list[str], include_market_review: bool = False, worke
98
110
  "fetched_at": datetime.now(timezone.utc).isoformat(),
99
111
  "freshness": "realtime_or_latest_trade_day",
100
112
  "fallback_used": False,
101
- "warnings": [],
113
+ "warnings": [f"{i['symbol']}: {warning}" for i in items for warning in i.get("warnings", [])],
102
114
  },
103
115
  "summary": {
104
116
  "total": len(symbols),
105
- "success": sum(1 for i in items if "error" not in i),
117
+ "success": sum(1 for i in items if "error" not in i and i.get("anomaly_status") == "ok"),
118
+ "partial": sum(1 for i in items if "error" not in i and i.get("anomaly_status") == "unavailable"),
106
119
  "failed": sum(1 for i in items if "error" in i),
107
120
  "high_risk_count": sum(1 for i in items if i.get("risk_level") == "high"),
108
121
  "veto_count": sum(1 for i in items if i.get("veto_buy")),
@@ -114,7 +127,7 @@ def build_context(symbols: list[str], include_market_review: bool = False, worke
114
127
  markets = sorted({i.get("market") for i in items if i.get("market")})
115
128
  review = {}
116
129
  for m in markets:
117
- r = _run_json("market_review.py", ["review", "--market", m], timeout=60)
130
+ r = _run_json("market_review.py", ["review", "--market", m])
118
131
  if r:
119
132
  review[m] = r
120
133
  output["market_review"] = review
@@ -148,6 +161,8 @@ def main():
148
161
  result = build_context(codes, include_market_review=args.include_market_review, workers=args.workers)
149
162
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
150
163
  print()
164
+ if "error" in result:
165
+ sys.exit(1)
151
166
 
152
167
 
153
168
  if __name__ == "__main__":