stock-analysis-plugin 0.2.10 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +237 -381
  2. package/docs/mcp.md +60 -0
  3. package/hermes/__init__.py +35 -0
  4. package/hermes/plugin.yaml +131 -0
  5. package/hermes/schemas.py +6 -0
  6. package/hermes/tools.py +630 -0
  7. package/mcp_host/__init__.py +1 -0
  8. package/mcp_host/__main__.py +3 -0
  9. package/mcp_host/server.py +222 -0
  10. package/package.json +17 -10
  11. package/pi/index.ts +53 -2887
  12. package/scripts/cli-json.mjs +23 -0
  13. package/scripts/exec-process.mjs +89 -0
  14. package/scripts/setup-python.mjs +37 -4
  15. package/scripts/tool-calls.mjs +489 -0
  16. package/skills/event-driven/SKILL.md +2 -2
  17. package/skills/growth-quality/SKILL.md +1 -1
  18. package/skills/stock-analysis/SKILL.md +12 -7
  19. package/skills/stock-analysis/references/stateless-analysis.md +9 -1
  20. package/tools/_cache.py +13 -0
  21. package/tools/_subproc.py +46 -0
  22. package/tools/alert_rules.py +65 -16
  23. package/tools/backtest.py +3 -3
  24. package/tools/capabilities.py +32 -9
  25. package/tools/cli_budgets.json +2 -0
  26. package/tools/diagnostics.py +3 -4
  27. package/tools/gather.py +2 -6
  28. package/tools/import_parser.py +3 -4
  29. package/tools/lixinger.py +119 -27
  30. package/tools/market_regime.py +5 -2
  31. package/tools/market_review.py +5 -6
  32. package/tools/name_resolver.py +82 -21
  33. package/tools/report_renderer.py +3 -3
  34. package/tools/requirements.txt +6 -5
  35. package/tools/risk_screening.py +3 -2
  36. package/tools/screener.py +6 -4
  37. package/tools/search_intel.py +81 -12
  38. package/tools/sec_edgar.py +266 -0
  39. package/tools/sec_holders.py +6 -0
  40. package/tools/signal_tracker.py +11 -10
  41. package/tools/stateless_analysis.py +115 -0
  42. package/tools/stock_data.py +707 -222
  43. package/tools/tiaa_disclosures.py +267 -0
  44. package/tools/tool_schemas.json +2704 -0
  45. package/tools/trading_calendar.py +65 -30
  46. package/tools/value_data.py +366 -134
  47. package/tools/watchlist.py +43 -5
@@ -0,0 +1,267 @@
1
+ """Public, login-free TIAA disclosures from the official YouZhiYouXing site."""
2
+
3
+ import re
4
+ import sys
5
+ import threading
6
+ import time
7
+ import xml.etree.ElementTree as ET
8
+ from concurrent.futures import TimeoutError as FutureTimeoutError
9
+ from datetime import datetime, timezone
10
+ from html import unescape
11
+ from html.parser import HTMLParser
12
+ from pathlib import Path
13
+ from urllib.parse import urlparse
14
+
15
+ import requests
16
+
17
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
18
+ from _cache import cached # noqa: E402
19
+
20
+ _SITEMAP = "https://youzhiyouxing.cn/sitemap.xml"
21
+ _ADVISOR_URL = "https://www.csisc.cn/zbscbzw/c100264/list_jjbm.shtml"
22
+ _ARTICLE_URL = re.compile(r"https://youzhiyouxing\.cn/(?:n/)?lite-posts/(\d+)(?:[/?#].*)?")
23
+ _DISCLOSURE_TERMS = ("示例账户", "成分基金", "配置", "业绩", "收益", "调仓", "持仓")
24
+ _MAX_ARTICLES = 40
25
+ _DISCLOSURE_DEADLINE = 30
26
+
27
+
28
+ class _DisclosureTimeout(Exception):
29
+ """A bounded disclosure attempt failed and must not be cached as an empty result."""
30
+
31
+
32
+ def _until(deadline, fn, *args):
33
+ """Run a blocking provider call in a daemon thread and stop waiting at the deadline.
34
+
35
+ A provider that ignores its own timeout can then neither stretch the request nor keep
36
+ the CLI process alive after it has answered.
37
+ """
38
+ box = {}
39
+
40
+ def run():
41
+ try:
42
+ box["value"] = fn(*args)
43
+ except BaseException as exc: # re-raised in the caller's thread
44
+ box["error"] = exc
45
+
46
+ worker = threading.Thread(target=run, daemon=True)
47
+ worker.start()
48
+ worker.join(max(0.0, deadline - time.monotonic()))
49
+ if worker.is_alive():
50
+ raise _DisclosureTimeout
51
+ if "error" in box:
52
+ raise box["error"]
53
+ return box.get("value")
54
+
55
+
56
+ class _ArticleParser(HTMLParser):
57
+ def __init__(self):
58
+ super().__init__(convert_charrefs=True)
59
+ self.meta = {}
60
+ self.parts = []
61
+ self.ignored = 0
62
+
63
+ def handle_starttag(self, tag, attrs):
64
+ if tag in ("script", "style"):
65
+ self.ignored += 1
66
+ if tag == "meta":
67
+ values = dict(attrs)
68
+ if values.get("property") in ("og:title", "og:description"):
69
+ self.meta[values["property"]] = values.get("content", "")
70
+ if tag in ("p", "div", "h1", "h2", "h3", "li", "br"):
71
+ self.parts.append("\n")
72
+
73
+ def handle_endtag(self, tag):
74
+ if tag in ("script", "style") and self.ignored:
75
+ self.ignored -= 1
76
+ if tag in ("p", "div", "h1", "h2", "h3", "li"):
77
+ self.parts.append("\n")
78
+
79
+ def handle_data(self, data):
80
+ if not self.ignored:
81
+ self.parts.append(data)
82
+
83
+ def text(self):
84
+ lines = []
85
+ for line in "".join(self.parts).splitlines():
86
+ clean = re.sub(r"\s+", " ", line).strip()
87
+ if clean:
88
+ lines.append(clean)
89
+ title = self.meta.get("og:title")
90
+ title_lines = [index for index, line in enumerate(lines) if line == title]
91
+ if title_lines:
92
+ lines = lines[title_lines[-1] + 1 :]
93
+ comments = next((index for index, line in enumerate(lines) if line.endswith("条回应")), len(lines))
94
+ return "\n".join(lines[:comments])
95
+
96
+
97
+ def _sitemap_urls():
98
+ response = requests.get(_SITEMAP, headers={"User-Agent": "Mozilla/5.0"}, timeout=(2, 8))
99
+ response.raise_for_status()
100
+ root = ET.fromstring(response.content)
101
+ namespace = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
102
+ entries = []
103
+ for node in root.findall("s:url", namespace):
104
+ location = node.findtext("s:loc", default="", namespaces=namespace)
105
+ match = _ARTICLE_URL.fullmatch(location)
106
+ if match:
107
+ entries.append((node.findtext("s:lastmod", default="", namespaces=namespace), match.group(1)))
108
+ return [
109
+ f"https://youzhiyouxing.cn/n/lite-posts/{article_id}" for _, article_id in sorted(entries, reverse=True)[:40]
110
+ ]
111
+
112
+
113
+ def _search_urls(portfolio_name, budget_seconds=45):
114
+ try:
115
+ from search_intel import search_news
116
+
117
+ query = f"site:youzhiyouxing.cn/n/lite-posts/ {portfolio_name} 示例账户 成分基金"
118
+ result = search_news(query, max_results=10, budget_seconds=budget_seconds)
119
+ if any("time budget exhausted" in str(error).lower() for error in result.get("engines_errors", {}).values()):
120
+ raise _DisclosureTimeout
121
+ urls = []
122
+ for row in result.get("results", []):
123
+ match = _ARTICLE_URL.fullmatch(row.get("url", ""))
124
+ if match:
125
+ urls.append(f"https://youzhiyouxing.cn/n/lite-posts/{match.group(1)}")
126
+ return urls
127
+ except _DisclosureTimeout:
128
+ raise
129
+ except Exception:
130
+ return []
131
+
132
+
133
+ def _article(url, portfolio_name):
134
+ try:
135
+ response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=(1, 3))
136
+ response.raise_for_status()
137
+ parser = _ArticleParser()
138
+ parser.feed(response.text)
139
+ text = parser.text()
140
+ if portfolio_name not in text or not any(term in text for term in _DISCLOSURE_TERMS):
141
+ return None
142
+ date_match = re.search(r"(20\d{2})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*日", response.text)
143
+ published_at = None
144
+ if date_match:
145
+ published_at = "-".join([date_match.group(1), *(part.zfill(2) for part in date_match.groups()[1:])])
146
+ return {
147
+ "title": parser.meta.get("og:title", ""),
148
+ "url": url,
149
+ "published_at": published_at,
150
+ "summary": parser.meta.get("og:description", ""),
151
+ "text": text[:3000],
152
+ "source": "youzhiyouxing",
153
+ "partial": True,
154
+ }
155
+ except Exception:
156
+ return None
157
+
158
+
159
+ def advisor_metadata(symbol):
160
+ advisor_code = f"A{symbol[4:7]}"
161
+
162
+ def fetch():
163
+ response = requests.get(_ADVISOR_URL, headers={"User-Agent": "Mozilla/5.0"}, timeout=15)
164
+ response.raise_for_status()
165
+ text = response.content.decode("utf-8-sig")
166
+ rows = re.findall(
167
+ r"<tr\b[^>]*>\s*<td\b[^>]*>\s*\d+\s*</td>\s*"
168
+ r"<td\b[^>]*>\s*(.*?)\s*</td>\s*<td\b[^>]*>\s*(A\d{3})\s*</td>",
169
+ text,
170
+ re.S,
171
+ )
172
+ if not rows:
173
+ raise ValueError("CSISC advisor organization table not found")
174
+ return {
175
+ "organizations": {code: unescape(re.sub(r"<[^>]+>", "", name)).strip() for name, code in rows},
176
+ "retrieved_at": datetime.now(timezone.utc).isoformat(),
177
+ }
178
+
179
+ data = cached("csisc-fund-advisor-organizations", 24 * 60 * 60, fetch)
180
+ return {
181
+ "advisor_code": advisor_code,
182
+ "advisor_org": data["organizations"].get(advisor_code),
183
+ "advisor_mapping_basis": "tiaa_prefix_best_effort",
184
+ "advisor_source_url": _ADVISOR_URL,
185
+ "advisor_retrieved_at": data["retrieved_at"],
186
+ }
187
+
188
+
189
+ def public_disclosures(symbol, portfolio_name):
190
+ def fetch():
191
+ deadline = time.monotonic() + _DISCLOSURE_DEADLINE
192
+ urls = _sitemap_urls()
193
+ search_results = []
194
+
195
+ timeout_state = {"search": False, "articles": False}
196
+ if time.monotonic() < deadline:
197
+ try:
198
+ search_results = _until(
199
+ deadline, _search_urls, portfolio_name, max(1, int(deadline - time.monotonic()))
200
+ )
201
+ # concurrent.futures.TimeoutError only aliases the builtin from Python 3.11.
202
+ except (TimeoutError, FutureTimeoutError, _DisclosureTimeout):
203
+ timeout_state["search"] = True
204
+ if time.monotonic() >= deadline:
205
+ timeout_state["search"] = True
206
+ else:
207
+ timeout_state["search"] = True
208
+ urls = list(dict.fromkeys([*search_results, *urls]))[:_MAX_ARTICLES]
209
+ items = []
210
+ for url in urls:
211
+ if time.monotonic() >= deadline:
212
+ timeout_state["articles"] = True
213
+ break
214
+ try:
215
+ item = _until(deadline, _article, url, portfolio_name)
216
+ if item:
217
+ items.append(item)
218
+ except _DisclosureTimeout:
219
+ timeout_state["articles"] = True
220
+ break
221
+ except (TimeoutError, FutureTimeoutError):
222
+ timeout_state["articles"] = True
223
+ if time.monotonic() >= deadline and urls:
224
+ timeout_state["articles"] = True
225
+ items.sort(key=lambda item: item["published_at"] or "", reverse=True)
226
+ items = items[:3]
227
+ result = {
228
+ "items": items,
229
+ "as_of": max((item["published_at"] for item in items if item["published_at"]), default=None),
230
+ }
231
+ if any(timeout_state.values()):
232
+ if not items:
233
+ raise _DisclosureTimeout
234
+ result["error"] = "disclosure_timeout"
235
+ return result
236
+
237
+ return cached(f"tiaa-public-disclosures:{symbol}", 6 * 60 * 60, fetch)
238
+
239
+
240
+ def search_public(symbol, portfolio_name):
241
+ def fetch():
242
+ try:
243
+ from search_intel import search_news
244
+
245
+ label = portfolio_name or symbol
246
+ result = search_news(f'"{symbol}" "{label}" 示例账户 成分基金 业绩', max_results=3)
247
+ rows = []
248
+ for row in result.get("results", []):
249
+ parsed = urlparse(row.get("url", ""))
250
+ if parsed.scheme not in ("http", "https") or not parsed.netloc:
251
+ continue
252
+ rows.append(
253
+ {
254
+ "title": row.get("title", ""),
255
+ "url": row["url"],
256
+ "snippet": row.get("snippet", ""),
257
+ "source": row.get("source", ""),
258
+ "publisher": parsed.netloc,
259
+ "verification": "unverified_search_result",
260
+ "partial": True,
261
+ }
262
+ )
263
+ return rows
264
+ except Exception:
265
+ return []
266
+
267
+ return cached(f"tiaa-public-search:{symbol}", 6 * 60 * 60, fetch)