@weaxs/dsh-stock-analysis 0.2.5 → 0.2.6

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.
@@ -642,13 +642,25 @@ def analyze_multi(symbol: str, period: str, count: int, periods: list) -> dict:
642
642
 
643
643
 
644
644
  def analyze(symbol: str, period: str = "daily", count: int = 120) -> dict:
645
- records = fetch_kline(symbol, period, count)
645
+ return analyze_records(symbol, fetch_kline(symbol, period, count), period)
646
+
647
+
648
+ def analyze_records(symbol: str, records: list, period: str = "daily") -> dict:
646
649
  if isinstance(records, dict) and "error" in records:
647
650
  return records
648
- if not records:
651
+ if not isinstance(records, list) or not records:
649
652
  return {"error": "No kline data returned"}
653
+ if any(not isinstance(row, dict) for row in records):
654
+ return {"error": "Invalid kline rows"}
650
655
 
651
656
  df = to_dataframe(records)
657
+ required = ["date", "close", "high", "low", "volume"]
658
+ if any(field not in df for field in required):
659
+ return {"error": "Missing kline fields"}
660
+ if df["date"].isna().any() or not np.isfinite(df[required[1:]]).all().all():
661
+ return {"error": "Invalid kline values"}
662
+ if (df[["close", "high", "low"]] <= 0).any().any() or (df["volume"] < 0).any():
663
+ return {"error": "Kline prices must be positive and volume nonnegative"}
652
664
  if len(df) < 30:
653
665
  return {"error": f"Insufficient data: {len(df)} rows (need at least 30)"}
654
666
 
@@ -731,6 +743,8 @@ def main():
731
743
  else:
732
744
  result = {"error": f"Unknown command: {args.command}"}
733
745
  print(json.dumps(result, ensure_ascii=False, default=str))
746
+ if "error" in result:
747
+ sys.exit(1)
734
748
 
735
749
 
736
750
  if __name__ == "__main__":
@@ -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)
@@ -16,7 +16,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parent))
16
16
  from _subproc import run_tool as _run_json # noqa: E402
17
17
  from _subproc import 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,7 +39,7 @@ 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 {}
@@ -52,7 +52,7 @@ def _build_item(symbol: str, data: dict | None) -> dict:
52
52
  score = tech.get("signal_score")
53
53
  trend = (tech.get("trend") or {}).get("overall") or "unknown"
54
54
  risk_level = risk.get("risk_level", "unknown")
55
- veto = bool(risk.get("veto_buy", False))
55
+ veto = risk.get("veto_buy")
56
56
  buy_signal = tech.get("buy_signal")
57
57
  market = detect_market(symbol)
58
58
 
@@ -79,6 +79,8 @@ def _build_item(symbol: str, data: dict | None) -> dict:
79
79
 
80
80
  def build_context(symbols: list[str], include_market_review: bool = False, workers: int = 3) -> dict:
81
81
  raw = analyze_watchlist(symbols, workers=workers)
82
+ if "error" in raw:
83
+ return raw
82
84
  results = raw.get("results", {})
83
85
 
84
86
  items = []
@@ -148,6 +150,8 @@ def main():
148
150
  result = build_context(codes, include_market_review=args.include_market_review, workers=args.workers)
149
151
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
150
152
  print()
153
+ if "error" in result:
154
+ sys.exit(1)
151
155
 
152
156
 
153
157
  if __name__ == "__main__":