stock-analysis-plugin 0.2.11 → 0.2.13
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.
- package/README.md +234 -382
- package/docs/mcp.md +60 -0
- package/hermes/__init__.py +35 -0
- package/hermes/plugin.yaml +132 -0
- package/hermes/schemas.py +6 -0
- package/hermes/tools.py +647 -0
- package/mcp_host/__init__.py +1 -0
- package/mcp_host/__main__.py +3 -0
- package/mcp_host/server.py +222 -0
- package/package.json +7 -1
- package/pi/index.ts +52 -2906
- package/scripts/cli-json.mjs +23 -0
- package/scripts/exec-process.mjs +89 -0
- package/scripts/setup-python.mjs +37 -4
- package/scripts/tool-calls.mjs +493 -0
- package/skills/bull-trend/SKILL.md +2 -2
- package/skills/bull-trend/references/fat-tails.md +76 -3
- package/skills/bull-trend/scripts/tail_analysis.py +92 -3
- package/skills/event-driven/SKILL.md +2 -2
- package/skills/growth-quality/SKILL.md +1 -1
- package/skills/market-review/SKILL.md +1 -0
- package/skills/stock-analysis/SKILL.md +12 -7
- package/skills/stock-analysis/references/stateless-analysis.md +9 -1
- package/skills/stock-screener/SKILL.md +1 -1
- package/skills/strategy-backtest/SKILL.md +1 -1
- package/tools/_cache.py +13 -0
- package/tools/_subproc.py +46 -0
- package/tools/alert_rules.py +2 -2
- package/tools/backtest.py +3 -3
- package/tools/capabilities.py +33 -9
- package/tools/cli_budgets.json +1 -0
- package/tools/credit_data.py +293 -0
- package/tools/diagnostics.py +3 -4
- package/tools/gather.py +2 -6
- package/tools/import_parser.py +3 -4
- package/tools/lixinger.py +119 -27
- package/tools/market_regime.py +5 -2
- package/tools/market_review.py +5 -6
- package/tools/name_resolver.py +45 -33
- package/tools/report_renderer.py +3 -3
- package/tools/requirements.txt +5 -4
- package/tools/risk_screening.py +3 -2
- package/tools/screener.py +41 -30
- package/tools/search_intel.py +79 -10
- package/tools/sec_edgar.py +268 -1
- package/tools/sec_holders.py +6 -0
- package/tools/signal_tracker.py +5 -5
- package/tools/stateless_analysis.py +132 -19
- package/tools/stock_data.py +1020 -817
- package/tools/tiaa_disclosures.py +4 -0
- package/tools/tool_schemas.json +2734 -0
- package/tools/trading_calendar.py +65 -30
- package/tools/value_data.py +423 -175
- package/tools/watchlist.py +43 -5
package/hermes/tools.py
ADDED
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import contextvars
|
|
3
|
+
import json
|
|
4
|
+
import runpy
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
import tempfile
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
_PACKAGE_DIR = Path(__file__).resolve().parent
|
|
11
|
+
# Installed wheels carry the payload in hermes/_payload; a dev checkout keeps it at the repo root.
|
|
12
|
+
PROJECT_ROOT = _PACKAGE_DIR / "_payload" if (_PACKAGE_DIR / "_payload").is_dir() else _PACKAGE_DIR.parent
|
|
13
|
+
TOOLS_DIR = PROJECT_ROOT / "tools"
|
|
14
|
+
# Hermes owns the top-level tools package; load our shared helper without shadowing it.
|
|
15
|
+
_subproc = runpy.run_path(str(TOOLS_DIR / "_subproc.py"))
|
|
16
|
+
command_timeout = _subproc["command_timeout"]
|
|
17
|
+
run_tree = _subproc["run_tree"]
|
|
18
|
+
terminate_tree = _subproc["terminate_tree"]
|
|
19
|
+
TREE_POPEN = _subproc["TREE_POPEN"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _find_python() -> str:
|
|
23
|
+
# Windows venvs ship Scripts/python.exe (there is no python3); POSIX venvs ship bin/python3.
|
|
24
|
+
if sys.platform == "win32":
|
|
25
|
+
venv_python = PROJECT_ROOT / ".venv" / "Scripts" / "python.exe"
|
|
26
|
+
else:
|
|
27
|
+
venv_python = PROJECT_ROOT / ".venv" / "bin" / "python3"
|
|
28
|
+
if venv_python.exists():
|
|
29
|
+
return str(venv_python)
|
|
30
|
+
# Installed wheels share the host environment, even when its venv is not activated.
|
|
31
|
+
return sys.executable
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _cli_json(stdout: str, failure: str | None = None) -> str:
|
|
35
|
+
# Failed, empty, or non-JSON CLI output becomes a JSON error; a CLI-provided JSON error is kept.
|
|
36
|
+
try:
|
|
37
|
+
payload = json.loads(stdout)
|
|
38
|
+
except ValueError:
|
|
39
|
+
failure = failure or (
|
|
40
|
+
"Python command returned non-JSON output" if stdout.strip() else "Python command returned no JSON"
|
|
41
|
+
)
|
|
42
|
+
else:
|
|
43
|
+
if failure is None or (isinstance(payload, dict) and isinstance(payload.get("error"), str)):
|
|
44
|
+
return stdout
|
|
45
|
+
return json.dumps({"error": failure})
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
_RUN_OVERRIDE = contextvars.ContextVar("stock_analysis_hermes_run_override", default=None)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _run_default(script: str, argv: list) -> str:
|
|
52
|
+
# argv list: 参数原样进子进程,不经 shell(Windows 同理)
|
|
53
|
+
python = _find_python()
|
|
54
|
+
timeout = command_timeout(script, argv) + 30
|
|
55
|
+
try:
|
|
56
|
+
result = run_tree([python, TOOLS_DIR / script, *map(str, argv)], timeout=timeout)
|
|
57
|
+
except subprocess.TimeoutExpired:
|
|
58
|
+
return json.dumps({"error": "Python command timed out"})
|
|
59
|
+
except Exception:
|
|
60
|
+
return json.dumps({"error": "Python command failed; check interpreter/dependencies"})
|
|
61
|
+
failure = None
|
|
62
|
+
if result.returncode != 0:
|
|
63
|
+
failure = f"Python command failed (exit {result.returncode}); check interpreter/dependencies"
|
|
64
|
+
return _cli_json(result.stdout or "", failure)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _run(script: str, argv: list) -> str:
|
|
68
|
+
override = _RUN_OVERRIDE.get()
|
|
69
|
+
return (override or _run_default)(script, argv)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def get_kline(args: dict, **kwargs) -> str:
|
|
73
|
+
"""Relay the CLI array, including each A-share row's adjusted=qfq/none marker."""
|
|
74
|
+
symbol = args["symbol"]
|
|
75
|
+
period = args.get("period", "daily")
|
|
76
|
+
count = args.get("count", 60)
|
|
77
|
+
return _run("stock_data.py", ["kline", symbol, "--period", period, "--count", count])
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def get_quote(args: dict, **kwargs) -> str:
|
|
81
|
+
symbol = args["symbol"]
|
|
82
|
+
return _run("stock_data.py", ["quote", symbol])
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def get_fund_quote(args: dict, **kwargs) -> str:
|
|
86
|
+
symbol = args["symbol"]
|
|
87
|
+
return _run("stock_data.py", ["fund_quote", symbol])
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def get_capital_flow(args: dict, **kwargs) -> str:
|
|
91
|
+
symbol = args.get("symbol", "")
|
|
92
|
+
mode = args.get("mode", "detail")
|
|
93
|
+
argv = ["capital_flow"]
|
|
94
|
+
if symbol:
|
|
95
|
+
argv.append(symbol)
|
|
96
|
+
argv += ["--mode", mode]
|
|
97
|
+
return _run("stock_data.py", argv)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_news(args: dict, **kwargs) -> str:
|
|
101
|
+
symbol = args["symbol"]
|
|
102
|
+
days = args.get("days", 3)
|
|
103
|
+
return _run("stock_data.py", ["news", symbol, "--days", days])
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def get_financials(args: dict, **kwargs) -> str:
|
|
107
|
+
symbol = args["symbol"]
|
|
108
|
+
periods = args.get("periods", 1)
|
|
109
|
+
argv = ["financials", symbol]
|
|
110
|
+
if periods != 1:
|
|
111
|
+
argv += ["--periods", periods]
|
|
112
|
+
if args.get("annual"):
|
|
113
|
+
argv += ["--annual"]
|
|
114
|
+
if args.get("include_dcf_inputs"):
|
|
115
|
+
argv += ["--include-dcf-inputs"]
|
|
116
|
+
return _run("stock_data.py", argv)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def get_technical_analysis(args: dict, **kwargs) -> str:
|
|
120
|
+
symbol = args["symbol"]
|
|
121
|
+
period = args.get("period", "daily")
|
|
122
|
+
count = args.get("count", 120)
|
|
123
|
+
argv = ["analyze", symbol, "--period", period, "--count", count]
|
|
124
|
+
periods = args.get("periods")
|
|
125
|
+
if periods:
|
|
126
|
+
argv += ["--periods", periods]
|
|
127
|
+
return _run("technical.py", argv)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def analyze_pattern(args: dict, **kwargs) -> str:
|
|
131
|
+
symbol = args["symbol"]
|
|
132
|
+
period = args.get("period", "daily")
|
|
133
|
+
days = args.get("days", 60)
|
|
134
|
+
return _run("pattern.py", ["analyze", symbol, "--period", period, "--days", days])
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def get_market_indices(args: dict, **kwargs) -> str:
|
|
138
|
+
region = args.get("region", "cn")
|
|
139
|
+
return _run("stock_data.py", ["market_indices", "--region", region])
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def get_sector_rankings(args: dict, **kwargs) -> str:
|
|
143
|
+
top = args.get("top", 10)
|
|
144
|
+
direction = args.get("direction", "top")
|
|
145
|
+
board_type = args.get("board_type", "industry")
|
|
146
|
+
return _run(
|
|
147
|
+
"stock_data.py",
|
|
148
|
+
[
|
|
149
|
+
"sector_rankings",
|
|
150
|
+
"--top",
|
|
151
|
+
top,
|
|
152
|
+
"--direction",
|
|
153
|
+
direction,
|
|
154
|
+
"--board-type",
|
|
155
|
+
board_type,
|
|
156
|
+
"--market",
|
|
157
|
+
args.get("market", "A"),
|
|
158
|
+
],
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def get_sector_constituents(args: dict, **kwargs) -> str:
|
|
163
|
+
sector = args["sector"]
|
|
164
|
+
board_type = args.get("board_type", "auto")
|
|
165
|
+
return _run("stock_data.py", ["sector_constituents", sector, "--board-type", board_type])
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def resolve_stock_sectors(args: dict, **kwargs) -> str:
|
|
169
|
+
symbol = args["symbol"]
|
|
170
|
+
return _run("stock_data.py", ["resolve_stock_sectors", symbol])
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def get_stock_info(args: dict, **kwargs) -> str:
|
|
174
|
+
symbol = args["symbol"]
|
|
175
|
+
return _run("stock_data.py", ["stock_info", symbol])
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def get_chip_distribution(args: dict, **kwargs) -> str:
|
|
179
|
+
symbol = args["symbol"]
|
|
180
|
+
return _run("stock_data.py", ["chip_distribution", symbol])
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def get_market_stats(args: dict, **kwargs) -> str:
|
|
184
|
+
return _run("stock_data.py", ["market_stats", "--market", args.get("market", "A")])
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def get_limit_up_pool(args: dict, **kwargs) -> str:
|
|
188
|
+
date = args.get("date", "")
|
|
189
|
+
argv = ["limit_up_pool"]
|
|
190
|
+
if date:
|
|
191
|
+
argv += ["--date", date]
|
|
192
|
+
return _run("stock_data.py", argv)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def get_dragon_tiger(args: dict, **kwargs) -> str:
|
|
196
|
+
date = args.get("date", "")
|
|
197
|
+
symbol = args.get("symbol", "")
|
|
198
|
+
top = args.get("top", 20)
|
|
199
|
+
argv = ["dragon_tiger"]
|
|
200
|
+
if date:
|
|
201
|
+
argv += ["--date", date]
|
|
202
|
+
if symbol:
|
|
203
|
+
argv += ["--symbol", symbol]
|
|
204
|
+
argv += ["--top", top]
|
|
205
|
+
return _run("stock_data.py", argv)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def get_hot_stocks(args: dict, **kwargs) -> str:
|
|
209
|
+
top = args.get("top", 20)
|
|
210
|
+
return _run("stock_data.py", ["hot_stocks", "--top", top])
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def get_margin_trading(args: dict, **kwargs) -> str:
|
|
214
|
+
symbol = args["symbol"]
|
|
215
|
+
days = args.get("days", 10)
|
|
216
|
+
argv = ["margin_trading", symbol, "--days", days]
|
|
217
|
+
if args.get("include_ratio"):
|
|
218
|
+
argv += ["--include-ratio"]
|
|
219
|
+
return _run("stock_data.py", argv)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def get_northbound_flow(args: dict, **kwargs) -> str:
|
|
223
|
+
days = args.get("days", 10)
|
|
224
|
+
return _run("stock_data.py", ["northbound_flow", "--days", days])
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def get_fundamental_context(args: dict, **kwargs) -> str:
|
|
228
|
+
symbol = args["symbol"]
|
|
229
|
+
return _run("stock_data.py", ["fundamental_context", symbol])
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def screen_stocks(args: dict, **kwargs) -> str:
|
|
233
|
+
market = args.get("market", "A")
|
|
234
|
+
top = args.get("top", 20)
|
|
235
|
+
config = args.get("config", "")
|
|
236
|
+
argv = ["screen", "--market", market, "--top", top]
|
|
237
|
+
if config:
|
|
238
|
+
argv += ["--config", config]
|
|
239
|
+
if args.get("l2"):
|
|
240
|
+
argv.append("--l2")
|
|
241
|
+
return _run("screener.py", argv)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def analyze_trend_strategy(args: dict, **kwargs) -> str:
|
|
245
|
+
return _run_analysis("fetch", args, script="trend_strategy.py")
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def run_backtest(args: dict, **kwargs) -> str:
|
|
249
|
+
strategy = args["strategy"]
|
|
250
|
+
symbol = args["symbol"]
|
|
251
|
+
start = args.get("start", "")
|
|
252
|
+
end = args.get("end", "")
|
|
253
|
+
capital = args.get("capital", 1000000)
|
|
254
|
+
argv = ["run", strategy, symbol]
|
|
255
|
+
if start:
|
|
256
|
+
argv += ["--start", start]
|
|
257
|
+
if end:
|
|
258
|
+
argv += ["--end", end]
|
|
259
|
+
argv += ["--capital", capital]
|
|
260
|
+
return _run("backtest.py", argv)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def evaluate_signal(args: dict, **kwargs) -> str:
|
|
264
|
+
symbol = args["symbol"]
|
|
265
|
+
signal = args["signal"]
|
|
266
|
+
forward_days = args.get("forward_days", "3,5,10")
|
|
267
|
+
lookback = args.get("lookback", 250)
|
|
268
|
+
return _run(
|
|
269
|
+
"backtest.py",
|
|
270
|
+
["evaluate_signal", symbol, signal, "--forward", forward_days, "--lookback", lookback],
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def resolve_stock_name(args: dict, **kwargs) -> str:
|
|
275
|
+
query = args["query"]
|
|
276
|
+
top = args.get("top", 5)
|
|
277
|
+
return _run("name_resolver.py", ["resolve", query, "--top", top])
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def check_trading_day(args: dict, **kwargs) -> str:
|
|
281
|
+
market = args["market"]
|
|
282
|
+
date = args.get("date", "")
|
|
283
|
+
argv = ["check", market]
|
|
284
|
+
if date:
|
|
285
|
+
argv += ["--date", date]
|
|
286
|
+
return _run("trading_calendar.py", argv)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def get_trading_days(args: dict, **kwargs) -> str:
|
|
290
|
+
market = args["market"]
|
|
291
|
+
direction = args.get("direction", "next")
|
|
292
|
+
count = args.get("count", 5)
|
|
293
|
+
date = args.get("date", "")
|
|
294
|
+
argv = [direction, market, "--count", count]
|
|
295
|
+
if date:
|
|
296
|
+
argv += ["--date", date]
|
|
297
|
+
return _run("trading_calendar.py", argv)
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def get_trading_phase(args: dict, **kwargs) -> str:
|
|
301
|
+
market = args["market"]
|
|
302
|
+
return _run("trading_calendar.py", ["phase", market])
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def calculate_ma(args: dict, **kwargs) -> str:
|
|
306
|
+
symbol = args["symbol"]
|
|
307
|
+
periods = args.get("periods", "5,10,20,30,60,120,250")
|
|
308
|
+
kline_period = args.get("kline_period", "daily")
|
|
309
|
+
return _run(
|
|
310
|
+
"technical.py",
|
|
311
|
+
["calculate_ma", symbol, "--periods", periods, "--period", kline_period, "--count", 300],
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def get_volume_analysis(args: dict, **kwargs) -> str:
|
|
316
|
+
symbol = args["symbol"]
|
|
317
|
+
period = args.get("period", "daily")
|
|
318
|
+
count = args.get("count", 60)
|
|
319
|
+
return _run("volume_analysis.py", ["analyze", symbol, "--period", period, "--count", count])
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def search_stock_news(args: dict, **kwargs) -> str:
|
|
323
|
+
query = args["query"]
|
|
324
|
+
count = args.get("count", 10)
|
|
325
|
+
return _run("search_intel.py", ["search", query, "--count", count])
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def search_comprehensive_intel(args: dict, **kwargs) -> str:
|
|
329
|
+
symbol = args["symbol"]
|
|
330
|
+
name = args.get("name", "")
|
|
331
|
+
argv = ["comprehensive", symbol]
|
|
332
|
+
if name:
|
|
333
|
+
argv += ["--name", name]
|
|
334
|
+
return _run("search_intel.py", argv)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def get_social_sentiment(args: dict, **kwargs) -> str:
|
|
338
|
+
symbol = args["symbol"]
|
|
339
|
+
return _run("search_intel.py", ["sentiment", symbol])
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def get_trending_sentiment(args: dict, **kwargs) -> str:
|
|
343
|
+
return _run("search_intel.py", ["trending"])
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def extract_article(args: dict, **kwargs) -> str:
|
|
347
|
+
url = args["url"]
|
|
348
|
+
max_chars = args.get("max_chars", 3000)
|
|
349
|
+
return _run("search_intel.py", ["extract", url, "--max-chars", max_chars])
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def screen_risk(args: dict, **kwargs) -> str:
|
|
353
|
+
symbol = args["symbol"]
|
|
354
|
+
name = args.get("name", "")
|
|
355
|
+
argv = ["screen", symbol]
|
|
356
|
+
if name:
|
|
357
|
+
argv += ["--name", name]
|
|
358
|
+
return _run("risk_screening.py", argv)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def detect_market_regime(args: dict, **kwargs) -> str:
|
|
362
|
+
market = args.get("market", "A")
|
|
363
|
+
return _run("market_regime.py", ["detect", market])
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def get_market_review(args: dict, **kwargs) -> str:
|
|
367
|
+
market = args.get("market", "A")
|
|
368
|
+
return _run("market_review.py", ["review", "--market", market])
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def get_watchlist(args: dict, **kwargs) -> str:
|
|
372
|
+
return _run("watchlist.py", ["list"])
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def run_watchlist_analysis(args: dict, **kwargs) -> str:
|
|
376
|
+
symbols = args["symbols"]
|
|
377
|
+
workers = args.get("workers", 3)
|
|
378
|
+
return _run("watchlist.py", ["analyze", symbols, "--workers", workers])
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def detect_anomaly(args: dict, **kwargs) -> str:
|
|
382
|
+
symbol = args["symbol"]
|
|
383
|
+
return _run("anomaly_detect.py", ["detect", symbol])
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def diagnose_data_sources(args: dict, **kwargs) -> str:
|
|
387
|
+
market = args.get("market", "all")
|
|
388
|
+
return _run("diagnostics.py", ["check", "--market", market])
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def get_market_capabilities(args: dict, **kwargs) -> str:
|
|
392
|
+
market = args.get("market")
|
|
393
|
+
symbol = args.get("symbol")
|
|
394
|
+
argv = ["get", "--symbol", symbol] if symbol else ["get", "--market", market or "A"]
|
|
395
|
+
return _run("capabilities.py", argv)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def render_stock_report(args: dict, **kwargs) -> str:
|
|
399
|
+
template = args.get("template", "full")
|
|
400
|
+
extra = ["--template", template, *(["--final"] if args.get("final") else [])]
|
|
401
|
+
return _run_analysis("stock", args["report"], script="report_renderer.py", extra=extra)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def render_market_report(args: dict, **kwargs) -> str:
|
|
405
|
+
extra = [*(["--final"] if args.get("final") else []), *(["--save"] if args.get("save") else [])]
|
|
406
|
+
return _run_analysis("market", args["report"], script="report_renderer.py", extra=extra)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def get_review_history(args: dict, **kwargs) -> str:
|
|
410
|
+
limit = args.get("limit", 10)
|
|
411
|
+
return _run("market_review.py", ["history", "--limit", str(limit)])
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def build_watchlist_context(args: dict, **kwargs) -> str:
|
|
415
|
+
symbols = args["symbols"]
|
|
416
|
+
workers = args.get("workers", 3)
|
|
417
|
+
argv = ["build", symbols, "--workers", workers]
|
|
418
|
+
if args.get("include_market_review"):
|
|
419
|
+
argv.append("--include-market-review")
|
|
420
|
+
return _run("watchlist_context.py", argv)
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def analyze_position_context(args: dict, **kwargs) -> str:
|
|
424
|
+
symbol = args["symbol"]
|
|
425
|
+
cost = args["cost"]
|
|
426
|
+
quantity = args["quantity"]
|
|
427
|
+
stop_loss = args.get("stop_loss")
|
|
428
|
+
take_profit = args.get("take_profit")
|
|
429
|
+
argv = ["analyze", symbol, "--cost", cost, "--quantity", quantity]
|
|
430
|
+
if stop_loss is not None:
|
|
431
|
+
argv += ["--stop-loss", stop_loss]
|
|
432
|
+
if take_profit is not None:
|
|
433
|
+
argv += ["--take-profit", take_profit]
|
|
434
|
+
return _run("position_context.py", argv)
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def check_alert_rules(args: dict, **kwargs) -> str:
|
|
438
|
+
symbol = args["symbol"]
|
|
439
|
+
rules = args["rules"]
|
|
440
|
+
payload = base64.b64encode(json.dumps(rules, ensure_ascii=False).encode("utf-8")).decode("ascii")
|
|
441
|
+
return _run("alert_rules.py", ["check", symbol, "--rules-b64", payload])
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def record_signal(args: dict, **kwargs) -> str:
|
|
445
|
+
symbol = args["symbol"]
|
|
446
|
+
direction = args["direction"]
|
|
447
|
+
horizon_days = args.get("horizon_days", 10)
|
|
448
|
+
argv = ["record", symbol, "--direction", direction, "--horizon-days", horizon_days]
|
|
449
|
+
for key, flag in (
|
|
450
|
+
("entry_price", "--entry-price"),
|
|
451
|
+
("target_price", "--target-price"),
|
|
452
|
+
("stop_price", "--stop-price"),
|
|
453
|
+
):
|
|
454
|
+
if args.get(key) is not None:
|
|
455
|
+
argv += [flag, args[key]]
|
|
456
|
+
if args.get("source"):
|
|
457
|
+
argv += ["--source", args["source"]]
|
|
458
|
+
if args.get("note"):
|
|
459
|
+
argv += ["--note", args["note"]]
|
|
460
|
+
return _run("signal_tracker.py", argv)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def evaluate_signals(args: dict, **kwargs) -> str:
|
|
464
|
+
symbol = args.get("symbol", "")
|
|
465
|
+
argv = ["evaluate"]
|
|
466
|
+
if symbol:
|
|
467
|
+
argv += ["--symbol", symbol]
|
|
468
|
+
return _run("signal_tracker.py", argv)
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def get_signal_summary(args: dict, **kwargs) -> str:
|
|
472
|
+
source = args.get("source", "")
|
|
473
|
+
symbol = args.get("symbol", "")
|
|
474
|
+
status = args.get("status", "all")
|
|
475
|
+
argv = ["summary"]
|
|
476
|
+
if source:
|
|
477
|
+
argv += ["--source", source]
|
|
478
|
+
if symbol:
|
|
479
|
+
argv += ["--symbol", symbol]
|
|
480
|
+
argv += ["--status", status]
|
|
481
|
+
return _run("signal_tracker.py", argv)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
def parse_stock_list(args: dict, **kwargs) -> str:
|
|
485
|
+
text = args["text"]
|
|
486
|
+
payload = base64.b64encode(str(text).encode("utf-8")).decode("ascii")
|
|
487
|
+
return _run("import_parser.py", ["parse", "--text-b64", payload])
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def get_valuation_history(args: dict, **kwargs) -> str:
|
|
491
|
+
return _run("value_data.py", ["valuation_history", args["symbol"], "--years", args.get("years", 5)])
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
def get_index_valuation(args: dict, **kwargs) -> str:
|
|
495
|
+
return _run("value_data.py", ["index_valuation", args["symbol"], "--years", args.get("years", 5)])
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def get_cds_index(args: dict, **kwargs) -> str:
|
|
499
|
+
return _run(
|
|
500
|
+
"credit_data.py",
|
|
501
|
+
[
|
|
502
|
+
"cds_index",
|
|
503
|
+
"--source",
|
|
504
|
+
args.get("source", "cn"),
|
|
505
|
+
"--days",
|
|
506
|
+
args.get("days", 30),
|
|
507
|
+
"--index",
|
|
508
|
+
args.get("index", ""),
|
|
509
|
+
"--tenor",
|
|
510
|
+
args.get("tenor", ""),
|
|
511
|
+
],
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def get_earnings_calendar(args: dict, **kwargs) -> str:
|
|
516
|
+
return _run(
|
|
517
|
+
"value_data.py",
|
|
518
|
+
[
|
|
519
|
+
"earnings_calendar",
|
|
520
|
+
"--report-date",
|
|
521
|
+
args.get("report_date", ""),
|
|
522
|
+
"--symbol",
|
|
523
|
+
args.get("symbol", ""),
|
|
524
|
+
"--top",
|
|
525
|
+
args.get("top", 50),
|
|
526
|
+
],
|
|
527
|
+
)
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def get_shareholder_structure(args: dict, **kwargs) -> str:
|
|
531
|
+
return _run(
|
|
532
|
+
"value_data.py", ["shareholder_structure", args["symbol"], "--report-date", args.get("report_date", "")]
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def get_etf_details(args: dict, **kwargs) -> str:
|
|
537
|
+
argv = ["etf_details", args["symbol"], "--benchmark", args.get("benchmark", ""), "--days", args.get("days", 60)]
|
|
538
|
+
if args.get("include_full_holdings"):
|
|
539
|
+
argv.append("--include-full-holdings")
|
|
540
|
+
return _run("value_data.py", argv)
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
def get_dividends(args: dict, **kwargs) -> str:
|
|
544
|
+
return _run(
|
|
545
|
+
"value_data.py",
|
|
546
|
+
[
|
|
547
|
+
"dividends",
|
|
548
|
+
"--market",
|
|
549
|
+
args.get("market", "A"),
|
|
550
|
+
"--symbol",
|
|
551
|
+
args.get("symbol", ""),
|
|
552
|
+
"--mode",
|
|
553
|
+
args.get("mode", "history"),
|
|
554
|
+
"--report-date",
|
|
555
|
+
args.get("report_date", ""),
|
|
556
|
+
"--years",
|
|
557
|
+
args.get("years", 10),
|
|
558
|
+
"--top",
|
|
559
|
+
args.get("top", 20),
|
|
560
|
+
],
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def get_announcements(args: dict, **kwargs) -> str:
|
|
565
|
+
return _run(
|
|
566
|
+
"value_data.py",
|
|
567
|
+
[
|
|
568
|
+
"announcements",
|
|
569
|
+
args["symbol"],
|
|
570
|
+
"--category",
|
|
571
|
+
args.get("category", "all"),
|
|
572
|
+
"--start-date",
|
|
573
|
+
args.get("start_date", ""),
|
|
574
|
+
"--end-date",
|
|
575
|
+
args.get("end_date", ""),
|
|
576
|
+
"--top",
|
|
577
|
+
args.get("top", 50),
|
|
578
|
+
],
|
|
579
|
+
)
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
def get_us_holdings(args: dict, **kwargs) -> str:
|
|
583
|
+
argv = ["holdings", args["cik"]]
|
|
584
|
+
if args.get("mode"):
|
|
585
|
+
argv += ["--mode", args["mode"]]
|
|
586
|
+
return _run("sec_edgar.py", argv)
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def get_insider_trades(args: dict, **kwargs) -> str:
|
|
590
|
+
return _run(
|
|
591
|
+
"sec_edgar.py", ["insider_trades", args["symbol"], "--days", args.get("days", 90), "--top", args.get("top", 10)]
|
|
592
|
+
)
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
def _run_analysis(command: str, args: dict, *, script="stateless_analysis.py", extra=()) -> str:
|
|
596
|
+
try:
|
|
597
|
+
with tempfile.TemporaryDirectory(prefix="stock-analysis-") as directory:
|
|
598
|
+
path = Path(directory) / "input.json"
|
|
599
|
+
path.write_text(json.dumps(args, ensure_ascii=False, allow_nan=False), encoding="utf-8")
|
|
600
|
+
path.chmod(0o600)
|
|
601
|
+
return _run(script, [command, "--input", str(path), *extra])
|
|
602
|
+
except Exception:
|
|
603
|
+
return json.dumps({"error": "Analysis input transport failed"})
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
def compare_stocks(args: dict, **kwargs) -> str:
|
|
607
|
+
return _run_analysis("peers", args)
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
def analyze_disclosure_changes(args: dict, **kwargs) -> str:
|
|
611
|
+
return _run_analysis("disclosure", args)
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
def analyze_portfolio_risk(args: dict, **kwargs) -> str:
|
|
615
|
+
return _run_analysis("portfolio", args)
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
def plan_portfolio_rebalance(args: dict, **kwargs) -> str:
|
|
619
|
+
return _run_analysis("rebalance", args)
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
def calculate_position_size(args: dict, **kwargs) -> str:
|
|
623
|
+
return _run_analysis("position_size", args)
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
def calculate_dcf_valuation(args: dict, **kwargs) -> str:
|
|
627
|
+
return _run_analysis("dcf", args)
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
def analyze_event_study(args: dict, **kwargs) -> str:
|
|
631
|
+
return _run_analysis("event_study", args)
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
def validate_backtest(args: dict, **kwargs) -> str:
|
|
635
|
+
return _run_analysis("robustness", args)
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
def compare_analysis_snapshots(args: dict, **kwargs) -> str:
|
|
639
|
+
return _run_analysis("snapshot", args)
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
def calculate_rotation_metrics(args: dict, **kwargs) -> str:
|
|
643
|
+
return _run_analysis("analyze", args, script="sector_rotation.py")
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
def backtest_rotation_allocation(args: dict, **kwargs) -> str:
|
|
647
|
+
return _run_analysis("backtest", args, script="sector_rotation.py")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Stock analysis MCP stdio host."""
|