stock-analysis-plugin 0.2.11 → 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.
- package/README.md +231 -381
- package/docs/mcp.md +60 -0
- package/hermes/__init__.py +35 -0
- package/hermes/plugin.yaml +131 -0
- package/hermes/schemas.py +6 -0
- package/hermes/tools.py +630 -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 +489 -0
- package/skills/event-driven/SKILL.md +2 -2
- package/skills/growth-quality/SKILL.md +1 -1
- package/skills/stock-analysis/SKILL.md +8 -6
- package/skills/stock-analysis/references/stateless-analysis.md +9 -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 +32 -9
- package/tools/cli_budgets.json +2 -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 +66 -7
- package/tools/report_renderer.py +3 -3
- package/tools/requirements.txt +5 -4
- package/tools/risk_screening.py +3 -2
- package/tools/screener.py +6 -4
- package/tools/search_intel.py +79 -10
- package/tools/sec_edgar.py +266 -0
- package/tools/sec_holders.py +6 -0
- package/tools/signal_tracker.py +5 -5
- package/tools/stateless_analysis.py +115 -0
- package/tools/stock_data.py +455 -201
- package/tools/tiaa_disclosures.py +4 -0
- package/tools/tool_schemas.json +2704 -0
- package/tools/trading_calendar.py +65 -30
- package/tools/value_data.py +366 -134
- package/tools/watchlist.py +43 -5
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Shared host-side CLI result rules; every adapter must surface failures the same way.
|
|
2
|
+
export const failureMessage = (error) =>
|
|
3
|
+
error.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER" ? "Python command output exceeded buffer limit"
|
|
4
|
+
: error.killed ? "Python command timed out"
|
|
5
|
+
: typeof error.code === "number" ? `Python command failed (exit ${error.code}); check interpreter/dependencies`
|
|
6
|
+
: "Python command failed; check interpreter/dependencies";
|
|
7
|
+
|
|
8
|
+
// Failed, empty, or non-JSON CLI output becomes a JSON error; a CLI-provided JSON error is kept.
|
|
9
|
+
export function cliJson(stdout, failure) {
|
|
10
|
+
try {
|
|
11
|
+
const data = JSON.parse(stdout);
|
|
12
|
+
if (!failure || (data && typeof data.error === "string")) return stdout;
|
|
13
|
+
} catch {
|
|
14
|
+
failure ??= stdout.trim() ? "Python command returned non-JSON output" : "Python command returned no JSON";
|
|
15
|
+
}
|
|
16
|
+
return JSON.stringify({ error: failure });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Explicit null optional args would otherwise reach argv as "null"; dropping them applies defaults.
|
|
20
|
+
export const withoutNulls = (params) =>
|
|
21
|
+
params && typeof params === "object" && !Array.isArray(params)
|
|
22
|
+
? Object.fromEntries(Object.entries(params).filter(([, value]) => value !== null))
|
|
23
|
+
: params;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import * as cp from "node:child_process";
|
|
2
|
+
|
|
3
|
+
function terminateTree(child) {
|
|
4
|
+
if (!child.pid) return;
|
|
5
|
+
if (process.platform === "win32") {
|
|
6
|
+
cp.execFile("taskkill", ["/F", "/T", "/PID", String(child.pid)], { timeout: 10_000 }, () => {});
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
process.kill(-child.pid, "SIGKILL");
|
|
11
|
+
} catch {
|
|
12
|
+
try { child.kill("SIGKILL"); } catch { /* already exited */ }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function failure(stdout, code, extra = {}) {
|
|
17
|
+
return Object.assign(new Error("Python command failed"), { stdout, code, ...extra });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// POSIX children run in their own session, so the host's terminal hangup no longer reaches them.
|
|
21
|
+
const live = new Set();
|
|
22
|
+
process.on("exit", () => { for (const child of live) terminateTree(child); });
|
|
23
|
+
|
|
24
|
+
/** execFile-compatible stdout collection that also terminates nested CLI children.
|
|
25
|
+
* @param {string} bin
|
|
26
|
+
* @param {string[]} argv
|
|
27
|
+
* @param {{timeoutMs?: number, maxBuffer?: number, signal?: AbortSignal}} [options]
|
|
28
|
+
*/
|
|
29
|
+
export function execFileTree(bin, argv, { timeoutMs, maxBuffer = 32 * 1024 * 1024, signal } = {}) {
|
|
30
|
+
if (signal?.aborted) {
|
|
31
|
+
return Promise.reject(Object.assign(new Error("The operation was aborted"), { name: "AbortError", stdout: "" }));
|
|
32
|
+
}
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const child = cp.spawn(bin, argv, {
|
|
35
|
+
detached: process.platform !== "win32",
|
|
36
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
37
|
+
windowsHide: true,
|
|
38
|
+
});
|
|
39
|
+
live.add(child);
|
|
40
|
+
const chunks = [];
|
|
41
|
+
const stdout = () => Buffer.concat(chunks).toString("utf8");
|
|
42
|
+
let bytes = 0;
|
|
43
|
+
let settled = false;
|
|
44
|
+
let timedOut = false;
|
|
45
|
+
let aborted = false;
|
|
46
|
+
let overflow = false;
|
|
47
|
+
|
|
48
|
+
const finish = (error) => {
|
|
49
|
+
if (settled) return;
|
|
50
|
+
settled = true;
|
|
51
|
+
if (timer) clearTimeout(timer);
|
|
52
|
+
signal?.removeEventListener("abort", onAbort);
|
|
53
|
+
if (error) reject(error);
|
|
54
|
+
else resolve(stdout());
|
|
55
|
+
};
|
|
56
|
+
const onAbort = () => {
|
|
57
|
+
aborted = true;
|
|
58
|
+
terminateTree(child);
|
|
59
|
+
};
|
|
60
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
61
|
+
const timer = timeoutMs > 0 ? setTimeout(() => {
|
|
62
|
+
timedOut = true;
|
|
63
|
+
terminateTree(child);
|
|
64
|
+
}, timeoutMs) : null;
|
|
65
|
+
timer?.unref();
|
|
66
|
+
|
|
67
|
+
child.stdout.on("data", chunk => {
|
|
68
|
+
if (bytes + chunk.length > maxBuffer) {
|
|
69
|
+
overflow = true;
|
|
70
|
+
terminateTree(child);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
bytes += chunk.length;
|
|
74
|
+
chunks.push(chunk);
|
|
75
|
+
});
|
|
76
|
+
child.on("error", error => {
|
|
77
|
+
error.stdout = stdout();
|
|
78
|
+
finish(error);
|
|
79
|
+
});
|
|
80
|
+
child.on("close", code => {
|
|
81
|
+
live.delete(child);
|
|
82
|
+
if (overflow) finish(failure(stdout(), "ERR_CHILD_PROCESS_STDIO_MAXBUFFER", { killed: true }));
|
|
83
|
+
else if (timedOut) finish(failure(stdout(), null, { killed: true }));
|
|
84
|
+
else if (aborted) finish(Object.assign(failure(stdout(), null), { name: "AbortError" }));
|
|
85
|
+
else if (code !== 0) finish(failure(stdout(), code));
|
|
86
|
+
else finish();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
package/scripts/setup-python.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { execFileSync } from "node:child_process";
|
|
3
|
-
import { existsSync, mkdirSync, renameSync } from "node:fs";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { pythonEnvironmentRoot, venvPythonPath } from "./venv-python.mjs";
|
|
@@ -19,6 +19,15 @@ function run(bin, args) {
|
|
|
19
19
|
execFileSync(bin, args, { stdio: "inherit", timeout: 1_800_000 });
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
function pipWorks() {
|
|
23
|
+
try {
|
|
24
|
+
execFileSync(venvPython, ["-m", "pip", "--version"], { stdio: "pipe" });
|
|
25
|
+
return true;
|
|
26
|
+
} catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
22
31
|
function findPython() {
|
|
23
32
|
for (const name of ["python3", "python"]) {
|
|
24
33
|
try {
|
|
@@ -48,11 +57,35 @@ try {
|
|
|
48
57
|
mkdirSync(envRoot, { recursive: true });
|
|
49
58
|
renameSync(join(pkgDir, ".venv"), venvDir);
|
|
50
59
|
}
|
|
51
|
-
|
|
52
|
-
|
|
60
|
+
// A half-built venv (e.g. Debian without python3-venv) leaves python but no pip; rebuild it.
|
|
61
|
+
if (!existsSync(venvDir) || !pipWorks()) {
|
|
62
|
+
try {
|
|
63
|
+
run(python, ["-m", "venv", "--clear", venvDir]);
|
|
64
|
+
} catch (err) {
|
|
65
|
+
rmSync(venvDir, { recursive: true, force: true });
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
53
68
|
}
|
|
54
69
|
|
|
55
|
-
|
|
70
|
+
try {
|
|
71
|
+
run(venvPython, ["-m", "pip", "install", "-q", "-r", requirements]);
|
|
72
|
+
} catch {
|
|
73
|
+
// A failover provider without a wheel for this platform must not block the core tools.
|
|
74
|
+
const lines = readFileSync(requirements, "utf8").split(/\r?\n/).map(line => line.trim());
|
|
75
|
+
const marker = lines.findIndex(line => line.startsWith("# optional providers"));
|
|
76
|
+
const specs = list => list.filter(line => line && !line.startsWith("#"));
|
|
77
|
+
const core = specs(marker < 0 ? lines : lines.slice(0, marker));
|
|
78
|
+
if (core.length) run(venvPython, ["-m", "pip", "install", "-q", ...core]);
|
|
79
|
+
const skipped = specs(marker < 0 ? [] : lines.slice(marker + 1)).filter(spec => {
|
|
80
|
+
try {
|
|
81
|
+
run(venvPython, ["-m", "pip", "install", "-q", spec]);
|
|
82
|
+
return false;
|
|
83
|
+
} catch {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (skipped.length) console.warn(`[stock-analysis-plugin] optional providers skipped: ${skipped.join(", ")}`);
|
|
88
|
+
}
|
|
56
89
|
} catch (err) {
|
|
57
90
|
console.warn(
|
|
58
91
|
`\n[pi-stock-analysis] Python setup failed: ${err.message}\n` +
|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import TOOL_SCHEMAS from "../tools/tool_schemas.json" with { type: "json" };
|
|
2
|
+
import { withJsonInput } from "./json-input.mjs";
|
|
3
|
+
|
|
4
|
+
export { TOOL_SCHEMAS };
|
|
5
|
+
|
|
6
|
+
// Shared host -> CLI mapping for the Pi, OpenClaw and dsh adapters; parameter
|
|
7
|
+
// schemas live in tools/tool_schemas.json and Hermes keeps its Python twin
|
|
8
|
+
// (hermes/tools.py). tests/test_adapter_parity.ts pins all of them together.
|
|
9
|
+
// `input` tools ship their JSON through a temp file (argv limits on Windows).
|
|
10
|
+
export const TOOL_CALLS = {
|
|
11
|
+
get_announcements: {
|
|
12
|
+
script: "value_data.py",
|
|
13
|
+
argv: (p) => ["announcements", p.symbol, "--category", p.category ?? "all", "--start-date", p.start_date ?? "", "--end-date", p.end_date ?? "", "--top", String(p.top ?? 50)],
|
|
14
|
+
},
|
|
15
|
+
get_us_holdings: {
|
|
16
|
+
script: "sec_edgar.py",
|
|
17
|
+
argv: (p) => ["holdings", p.cik, ...(p.mode ? ["--mode", p.mode] : [])],
|
|
18
|
+
},
|
|
19
|
+
get_insider_trades: {
|
|
20
|
+
script: "sec_edgar.py",
|
|
21
|
+
argv: (p) => ["insider_trades", p.symbol, "--days", String(p.days ?? 90), "--top", String(p.top ?? 10)],
|
|
22
|
+
},
|
|
23
|
+
get_valuation_history: {
|
|
24
|
+
script: "value_data.py",
|
|
25
|
+
argv: (p) => ["valuation_history", p.symbol, "--years", String(p.years ?? 5)],
|
|
26
|
+
},
|
|
27
|
+
get_index_valuation: {
|
|
28
|
+
script: "value_data.py",
|
|
29
|
+
argv: (p) => ["index_valuation", p.symbol, "--years", String(p.years ?? 5)],
|
|
30
|
+
},
|
|
31
|
+
get_earnings_calendar: {
|
|
32
|
+
script: "value_data.py",
|
|
33
|
+
argv: (p) => ["earnings_calendar", "--report-date", p.report_date ?? "", "--symbol", p.symbol ?? "", "--top", String(p.top ?? 50)],
|
|
34
|
+
},
|
|
35
|
+
get_shareholder_structure: {
|
|
36
|
+
script: "value_data.py",
|
|
37
|
+
argv: (p) => ["shareholder_structure", p.symbol, "--report-date", p.report_date ?? ""],
|
|
38
|
+
},
|
|
39
|
+
get_etf_details: {
|
|
40
|
+
script: "value_data.py",
|
|
41
|
+
argv: (p) => ["etf_details", p.symbol, "--benchmark", p.benchmark ?? "", "--days", String(p.days ?? 60), ...(p.include_full_holdings ? ["--include-full-holdings"] : [])],
|
|
42
|
+
},
|
|
43
|
+
get_dividends: {
|
|
44
|
+
script: "value_data.py",
|
|
45
|
+
argv: (p) => ["dividends", "--market", p.market ?? "A", "--symbol", p.symbol ?? "", "--mode", p.mode ?? "history", "--report-date", p.report_date ?? "", "--years", String(p.years ?? 10), "--top", String(p.top ?? 20)],
|
|
46
|
+
},
|
|
47
|
+
get_kline: {
|
|
48
|
+
script: "stock_data.py",
|
|
49
|
+
argv: (p) => [
|
|
50
|
+
"kline",
|
|
51
|
+
p.symbol,
|
|
52
|
+
"--period",
|
|
53
|
+
p.period ?? "daily",
|
|
54
|
+
"--count",
|
|
55
|
+
String(p.count ?? 60),
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
get_quote: {
|
|
59
|
+
script: "stock_data.py",
|
|
60
|
+
argv: (p) => ["quote", p.symbol],
|
|
61
|
+
},
|
|
62
|
+
get_fund_quote: {
|
|
63
|
+
script: "stock_data.py",
|
|
64
|
+
argv: (p) => ["fund_quote", p.symbol],
|
|
65
|
+
},
|
|
66
|
+
get_capital_flow: {
|
|
67
|
+
script: "stock_data.py",
|
|
68
|
+
argv: (p) => {
|
|
69
|
+
const args = ["capital_flow"];
|
|
70
|
+
if (p.symbol) args.push(p.symbol);
|
|
71
|
+
args.push("--mode", p.mode ?? "detail");
|
|
72
|
+
return args;
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
get_news: {
|
|
76
|
+
script: "stock_data.py",
|
|
77
|
+
argv: (p) => ["news", p.symbol, "--days", String(p.days ?? 3)],
|
|
78
|
+
},
|
|
79
|
+
get_financials: {
|
|
80
|
+
script: "stock_data.py",
|
|
81
|
+
argv: (p) => {
|
|
82
|
+
const args = ["financials", p.symbol];
|
|
83
|
+
if (p.periods !== undefined && p.periods !== 1) args.push("--periods", String(p.periods));
|
|
84
|
+
if (p.annual) args.push("--annual");
|
|
85
|
+
if (p.include_dcf_inputs) args.push("--include-dcf-inputs");
|
|
86
|
+
return args;
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
get_technical_analysis: {
|
|
90
|
+
script: "technical.py",
|
|
91
|
+
argv: (p) => {
|
|
92
|
+
const args = [
|
|
93
|
+
"analyze",
|
|
94
|
+
p.symbol,
|
|
95
|
+
"--period",
|
|
96
|
+
p.period ?? "daily",
|
|
97
|
+
"--count",
|
|
98
|
+
String(p.count ?? 120),
|
|
99
|
+
];
|
|
100
|
+
if (p.periods) args.push("--periods", p.periods);
|
|
101
|
+
return args;
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
analyze_pattern: {
|
|
105
|
+
script: "pattern.py",
|
|
106
|
+
argv: (p) => [
|
|
107
|
+
"analyze",
|
|
108
|
+
p.symbol,
|
|
109
|
+
"--period",
|
|
110
|
+
p.period ?? "daily",
|
|
111
|
+
"--days",
|
|
112
|
+
String(p.days ?? 60),
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
get_market_indices: {
|
|
116
|
+
script: "stock_data.py",
|
|
117
|
+
argv: (p) => ["market_indices", "--region", p.region ?? "cn"],
|
|
118
|
+
},
|
|
119
|
+
get_sector_rankings: {
|
|
120
|
+
script: "stock_data.py",
|
|
121
|
+
argv: (p) => [
|
|
122
|
+
"sector_rankings",
|
|
123
|
+
"--top",
|
|
124
|
+
String(p.top ?? 10),
|
|
125
|
+
"--direction",
|
|
126
|
+
p.direction ?? "top",
|
|
127
|
+
"--board-type",
|
|
128
|
+
p.board_type ?? "industry",
|
|
129
|
+
"--market",
|
|
130
|
+
p.market ?? "A",
|
|
131
|
+
],
|
|
132
|
+
},
|
|
133
|
+
get_sector_constituents: {
|
|
134
|
+
script: "stock_data.py",
|
|
135
|
+
argv: (p) => [
|
|
136
|
+
"sector_constituents",
|
|
137
|
+
p.sector,
|
|
138
|
+
"--board-type",
|
|
139
|
+
p.board_type ?? "auto",
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
resolve_stock_sectors: {
|
|
143
|
+
script: "stock_data.py",
|
|
144
|
+
argv: (p) => ["resolve_stock_sectors", p.symbol],
|
|
145
|
+
},
|
|
146
|
+
get_stock_info: {
|
|
147
|
+
script: "stock_data.py",
|
|
148
|
+
argv: (p) => ["stock_info", p.symbol],
|
|
149
|
+
},
|
|
150
|
+
get_chip_distribution: {
|
|
151
|
+
script: "stock_data.py",
|
|
152
|
+
argv: (p) => ["chip_distribution", p.symbol],
|
|
153
|
+
},
|
|
154
|
+
get_market_stats: {
|
|
155
|
+
script: "stock_data.py",
|
|
156
|
+
argv: (p) => ["market_stats", "--market", p.market ?? "A"],
|
|
157
|
+
},
|
|
158
|
+
get_limit_up_pool: {
|
|
159
|
+
script: "stock_data.py",
|
|
160
|
+
argv: (p) => {
|
|
161
|
+
const args = ["limit_up_pool"];
|
|
162
|
+
if (p.date) args.push("--date", p.date);
|
|
163
|
+
return args;
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
get_dragon_tiger: {
|
|
167
|
+
script: "stock_data.py",
|
|
168
|
+
argv: (p) => {
|
|
169
|
+
const args = ["dragon_tiger"];
|
|
170
|
+
if (p.date) args.push("--date", p.date);
|
|
171
|
+
if (p.symbol) args.push("--symbol", p.symbol);
|
|
172
|
+
args.push("--top", String(p.top ?? 20));
|
|
173
|
+
return args;
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
get_hot_stocks: {
|
|
177
|
+
script: "stock_data.py",
|
|
178
|
+
argv: (p) => ["hot_stocks", "--top", String(p.top ?? 20)],
|
|
179
|
+
},
|
|
180
|
+
get_margin_trading: {
|
|
181
|
+
script: "stock_data.py",
|
|
182
|
+
argv: (p) => ["margin_trading", p.symbol, "--days", String(p.days ?? 10), ...(p.include_ratio ? ["--include-ratio"] : [])],
|
|
183
|
+
},
|
|
184
|
+
get_northbound_flow: {
|
|
185
|
+
script: "stock_data.py",
|
|
186
|
+
argv: (p) => ["northbound_flow", "--days", String(p.days ?? 10)],
|
|
187
|
+
},
|
|
188
|
+
get_fundamental_context: {
|
|
189
|
+
script: "stock_data.py",
|
|
190
|
+
argv: (p) => ["fundamental_context", p.symbol],
|
|
191
|
+
},
|
|
192
|
+
screen_stocks: {
|
|
193
|
+
script: "screener.py",
|
|
194
|
+
argv: (p) => {
|
|
195
|
+
const args = ["screen", "--market", p.market ?? "A", "--top", String(p.top ?? 20)];
|
|
196
|
+
if (p.config) args.push("--config", p.config);
|
|
197
|
+
if (p.l2) args.push("--l2");
|
|
198
|
+
return args;
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
calculate_rotation_metrics: {
|
|
202
|
+
script: "sector_rotation.py",
|
|
203
|
+
input: (p) => p,
|
|
204
|
+
argv: (p, path) => ["analyze", "--input", path],
|
|
205
|
+
},
|
|
206
|
+
backtest_rotation_allocation: {
|
|
207
|
+
script: "sector_rotation.py",
|
|
208
|
+
input: (p) => p,
|
|
209
|
+
argv: (p, path) => ["backtest", "--input", path],
|
|
210
|
+
},
|
|
211
|
+
analyze_trend_strategy: {
|
|
212
|
+
script: "trend_strategy.py",
|
|
213
|
+
input: (p) => p,
|
|
214
|
+
argv: (p, path) => ["fetch", "--input", path],
|
|
215
|
+
},
|
|
216
|
+
run_backtest: {
|
|
217
|
+
script: "backtest.py",
|
|
218
|
+
argv: (p) => {
|
|
219
|
+
const args = ["run", p.strategy, p.symbol];
|
|
220
|
+
if (p.start) args.push("--start", p.start);
|
|
221
|
+
if (p.end) args.push("--end", p.end);
|
|
222
|
+
args.push("--capital", String(p.capital ?? 1000000));
|
|
223
|
+
return args;
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
evaluate_signal: {
|
|
227
|
+
script: "backtest.py",
|
|
228
|
+
argv: (p) => [
|
|
229
|
+
"evaluate_signal",
|
|
230
|
+
p.symbol,
|
|
231
|
+
p.signal,
|
|
232
|
+
"--forward",
|
|
233
|
+
p.forward_days ?? "3,5,10",
|
|
234
|
+
"--lookback",
|
|
235
|
+
String(p.lookback ?? 250),
|
|
236
|
+
],
|
|
237
|
+
},
|
|
238
|
+
resolve_stock_name: {
|
|
239
|
+
script: "name_resolver.py",
|
|
240
|
+
argv: (p) => ["resolve", p.query, "--top", String(p.top ?? 5)],
|
|
241
|
+
},
|
|
242
|
+
check_trading_day: {
|
|
243
|
+
script: "trading_calendar.py",
|
|
244
|
+
argv: (p) => {
|
|
245
|
+
const args = ["check", p.market];
|
|
246
|
+
if (p.date) args.push("--date", p.date);
|
|
247
|
+
return args;
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
get_trading_days: {
|
|
251
|
+
script: "trading_calendar.py",
|
|
252
|
+
argv: (p) => {
|
|
253
|
+
const args = [p.direction ?? "next", p.market, "--count", String(p.count ?? 5)];
|
|
254
|
+
if (p.date) args.push("--date", p.date);
|
|
255
|
+
return args;
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
get_trading_phase: {
|
|
259
|
+
script: "trading_calendar.py",
|
|
260
|
+
argv: (p) => ["phase", p.market],
|
|
261
|
+
},
|
|
262
|
+
calculate_ma: {
|
|
263
|
+
script: "technical.py",
|
|
264
|
+
argv: (p) => [
|
|
265
|
+
"calculate_ma",
|
|
266
|
+
p.symbol,
|
|
267
|
+
"--periods",
|
|
268
|
+
p.periods ?? "5,10,20,30,60,120,250",
|
|
269
|
+
"--period",
|
|
270
|
+
p.kline_period ?? "daily",
|
|
271
|
+
"--count",
|
|
272
|
+
"300",
|
|
273
|
+
],
|
|
274
|
+
},
|
|
275
|
+
get_volume_analysis: {
|
|
276
|
+
script: "volume_analysis.py",
|
|
277
|
+
argv: (p) => [
|
|
278
|
+
"analyze",
|
|
279
|
+
p.symbol,
|
|
280
|
+
"--period",
|
|
281
|
+
p.period ?? "daily",
|
|
282
|
+
"--count",
|
|
283
|
+
String(p.count ?? 60),
|
|
284
|
+
],
|
|
285
|
+
},
|
|
286
|
+
search_stock_news: {
|
|
287
|
+
script: "search_intel.py",
|
|
288
|
+
argv: (p) => ["search", p.query, "--count", String(p.count ?? 10)],
|
|
289
|
+
},
|
|
290
|
+
search_comprehensive_intel: {
|
|
291
|
+
script: "search_intel.py",
|
|
292
|
+
argv: (p) => {
|
|
293
|
+
const args = ["comprehensive", p.symbol];
|
|
294
|
+
if (p.name) args.push("--name", p.name);
|
|
295
|
+
return args;
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
get_social_sentiment: {
|
|
299
|
+
script: "search_intel.py",
|
|
300
|
+
argv: (p) => ["sentiment", p.symbol],
|
|
301
|
+
},
|
|
302
|
+
get_trending_sentiment: {
|
|
303
|
+
script: "search_intel.py",
|
|
304
|
+
argv: () => ["trending"],
|
|
305
|
+
},
|
|
306
|
+
extract_article: {
|
|
307
|
+
script: "search_intel.py",
|
|
308
|
+
argv: (p) => ["extract", p.url, "--max-chars", String(p.max_chars ?? 3000)],
|
|
309
|
+
},
|
|
310
|
+
screen_risk: {
|
|
311
|
+
script: "risk_screening.py",
|
|
312
|
+
argv: (p) => {
|
|
313
|
+
const args = ["screen", p.symbol];
|
|
314
|
+
if (p.name) args.push("--name", p.name);
|
|
315
|
+
return args;
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
detect_market_regime: {
|
|
319
|
+
script: "market_regime.py",
|
|
320
|
+
argv: (p) => ["detect", p.market ?? "A"],
|
|
321
|
+
},
|
|
322
|
+
get_market_review: {
|
|
323
|
+
script: "market_review.py",
|
|
324
|
+
argv: (p) => ["review", "--market", p.market ?? "A"],
|
|
325
|
+
},
|
|
326
|
+
get_watchlist: {
|
|
327
|
+
script: "watchlist.py",
|
|
328
|
+
argv: () => ["list"],
|
|
329
|
+
},
|
|
330
|
+
run_watchlist_analysis: {
|
|
331
|
+
script: "watchlist.py",
|
|
332
|
+
argv: (p) => ["analyze", p.symbols, "--workers", String(p.workers ?? 3)],
|
|
333
|
+
},
|
|
334
|
+
detect_anomaly: {
|
|
335
|
+
script: "anomaly_detect.py",
|
|
336
|
+
argv: (p) => ["detect", p.symbol],
|
|
337
|
+
},
|
|
338
|
+
diagnose_data_sources: {
|
|
339
|
+
script: "diagnostics.py",
|
|
340
|
+
argv: (p) => ["check", "--market", p.market ?? "all"],
|
|
341
|
+
},
|
|
342
|
+
get_market_capabilities: {
|
|
343
|
+
script: "capabilities.py",
|
|
344
|
+
argv: (p) =>
|
|
345
|
+
p.symbol
|
|
346
|
+
? ["get", "--symbol", p.symbol]
|
|
347
|
+
: ["get", "--market", p.market ?? "A"],
|
|
348
|
+
},
|
|
349
|
+
render_stock_report: {
|
|
350
|
+
script: "report_renderer.py",
|
|
351
|
+
input: (p) => p.report,
|
|
352
|
+
argv: (p, path) => ["stock", "--template", p.template ?? "full", "--input", path, ...(p.final ? ["--final"] : [])],
|
|
353
|
+
},
|
|
354
|
+
render_market_report: {
|
|
355
|
+
script: "report_renderer.py",
|
|
356
|
+
input: (p) => p.report,
|
|
357
|
+
argv: (p, path) => ["market", "--input", path, ...(p.final ? ["--final"] : []), ...(p.save ? ["--save"] : [])],
|
|
358
|
+
},
|
|
359
|
+
get_review_history: {
|
|
360
|
+
script: "market_review.py",
|
|
361
|
+
argv: (p) => ["history", "--limit", String(p.limit ?? 10)],
|
|
362
|
+
},
|
|
363
|
+
build_watchlist_context: {
|
|
364
|
+
script: "watchlist_context.py",
|
|
365
|
+
argv: (p) => {
|
|
366
|
+
const args = ["build", p.symbols, "--workers", String(p.workers ?? 3)];
|
|
367
|
+
if (p.include_market_review) args.push("--include-market-review");
|
|
368
|
+
return args;
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
analyze_position_context: {
|
|
372
|
+
script: "position_context.py",
|
|
373
|
+
argv: (p) => {
|
|
374
|
+
const args = [
|
|
375
|
+
"analyze",
|
|
376
|
+
p.symbol,
|
|
377
|
+
"--cost",
|
|
378
|
+
String(p.cost),
|
|
379
|
+
"--quantity",
|
|
380
|
+
String(p.quantity),
|
|
381
|
+
];
|
|
382
|
+
if (p.stop_loss !== undefined) args.push("--stop-loss", String(p.stop_loss));
|
|
383
|
+
if (p.take_profit !== undefined) args.push("--take-profit", String(p.take_profit));
|
|
384
|
+
return args;
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
check_alert_rules: {
|
|
388
|
+
script: "alert_rules.py",
|
|
389
|
+
argv: (p) => {
|
|
390
|
+
const b64 = Buffer.from(JSON.stringify(p.rules), "utf-8").toString("base64");
|
|
391
|
+
return ["check", p.symbol, "--rules-b64", b64];
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
record_signal: {
|
|
395
|
+
script: "signal_tracker.py",
|
|
396
|
+
argv: (p) => {
|
|
397
|
+
const args = [
|
|
398
|
+
"record",
|
|
399
|
+
p.symbol,
|
|
400
|
+
"--direction",
|
|
401
|
+
p.direction,
|
|
402
|
+
"--horizon-days",
|
|
403
|
+
String(p.horizon_days ?? 10),
|
|
404
|
+
];
|
|
405
|
+
if (p.entry_price !== undefined) args.push("--entry-price", String(p.entry_price));
|
|
406
|
+
if (p.target_price !== undefined) args.push("--target-price", String(p.target_price));
|
|
407
|
+
if (p.stop_price !== undefined) args.push("--stop-price", String(p.stop_price));
|
|
408
|
+
if (p.source) args.push("--source", p.source);
|
|
409
|
+
if (p.note) args.push("--note", p.note);
|
|
410
|
+
return args;
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
evaluate_signals: {
|
|
414
|
+
script: "signal_tracker.py",
|
|
415
|
+
argv: (p) => {
|
|
416
|
+
const args = ["evaluate"];
|
|
417
|
+
if (p.symbol) args.push("--symbol", p.symbol);
|
|
418
|
+
return args;
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
get_signal_summary: {
|
|
422
|
+
script: "signal_tracker.py",
|
|
423
|
+
argv: (p) => {
|
|
424
|
+
const args = ["summary"];
|
|
425
|
+
if (p.source) args.push("--source", p.source);
|
|
426
|
+
if (p.symbol) args.push("--symbol", p.symbol);
|
|
427
|
+
args.push("--status", p.status ?? "all");
|
|
428
|
+
return args;
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
parse_stock_list: {
|
|
432
|
+
script: "import_parser.py",
|
|
433
|
+
argv: (p) => {
|
|
434
|
+
const b64 = Buffer.from(String(p.text), "utf-8").toString("base64");
|
|
435
|
+
return ["parse", "--text-b64", b64];
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
compare_stocks: {
|
|
439
|
+
script: "stateless_analysis.py",
|
|
440
|
+
input: (p) => p,
|
|
441
|
+
argv: (p, path) => ["peers", "--input", path],
|
|
442
|
+
},
|
|
443
|
+
analyze_disclosure_changes: {
|
|
444
|
+
script: "stateless_analysis.py",
|
|
445
|
+
input: (p) => p,
|
|
446
|
+
argv: (p, path) => ["disclosure", "--input", path],
|
|
447
|
+
},
|
|
448
|
+
analyze_portfolio_risk: {
|
|
449
|
+
script: "stateless_analysis.py",
|
|
450
|
+
input: (p) => p,
|
|
451
|
+
argv: (p, path) => ["portfolio", "--input", path],
|
|
452
|
+
},
|
|
453
|
+
plan_portfolio_rebalance: {
|
|
454
|
+
script: "stateless_analysis.py",
|
|
455
|
+
input: (p) => p,
|
|
456
|
+
argv: (p, path) => ["rebalance", "--input", path],
|
|
457
|
+
},
|
|
458
|
+
calculate_position_size: {
|
|
459
|
+
script: "stateless_analysis.py",
|
|
460
|
+
input: (p) => p,
|
|
461
|
+
argv: (p, path) => ["position_size", "--input", path],
|
|
462
|
+
},
|
|
463
|
+
calculate_dcf_valuation: {
|
|
464
|
+
script: "stateless_analysis.py",
|
|
465
|
+
input: (p) => p,
|
|
466
|
+
argv: (p, path) => ["dcf", "--input", path],
|
|
467
|
+
},
|
|
468
|
+
analyze_event_study: {
|
|
469
|
+
script: "stateless_analysis.py",
|
|
470
|
+
input: (p) => p,
|
|
471
|
+
argv: (p, path) => ["event_study", "--input", path],
|
|
472
|
+
},
|
|
473
|
+
validate_backtest: {
|
|
474
|
+
script: "stateless_analysis.py",
|
|
475
|
+
input: (p) => p,
|
|
476
|
+
argv: (p, path) => ["robustness", "--input", path],
|
|
477
|
+
},
|
|
478
|
+
compare_analysis_snapshots: {
|
|
479
|
+
script: "stateless_analysis.py",
|
|
480
|
+
input: (p) => p,
|
|
481
|
+
argv: (p, path) => ["snapshot", "--input", path],
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
/** Run one tool call; `run(script, argv)` resolves to the CLI's JSON text. */
|
|
486
|
+
export function callTool(name, args, run) {
|
|
487
|
+
const { script, argv, input } = TOOL_CALLS[name];
|
|
488
|
+
return input ? withJsonInput(input(args), path => run(script, argv(args, path))) : run(script, argv(args));
|
|
489
|
+
}
|
|
@@ -28,7 +28,7 @@ gather 工具返回:
|
|
|
28
28
|
- `stock_info` — 公司基本信息
|
|
29
29
|
- `sector_rankings` — 板块排名
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
事件原文先调用 `get_announcements`:A 股业绩预告用 `forecast`、快报用 `express`、减持用 `reduction`、增发用 `issuance`、问询及回复用 `inquiry`、分红用 `dividend`;港股用 HKEXnews,支持 `all/forecast/annual/interim/quarterly/issuance/dividend`;美股用 SEC 申报(`all` 含 8-K/6-K/DEF 14A,需 SEC_USER_AGENT),不支持的类别会报错。指定 `start_date/end_date`;PDF/HTML 原文用 `extract_article` 提取并检查 `truncated`,读取后再量化,列表截断时缩小日期范围。
|
|
32
32
|
|
|
33
33
|
新闻摘要只用于发现事件。金额、期限、合同对手、业绩变化或政策原文必须由公告/原文确认;只有摘要时不做精确量化。
|
|
34
34
|
|
|
@@ -100,4 +100,4 @@ A 股事件原文先调用 `get_announcements`:业绩预告用 `forecast`、
|
|
|
100
100
|
|
|
101
101
|
### 补充数据覆盖
|
|
102
102
|
|
|
103
|
-
get_earnings_calendar 查询 A 股仍需 report_date;HK 提供 symbol,可选财政期末 report_date;优先 HKEX 已公告业绩审批董事会排期 confirmation=official_board_meeting_notice、date_kind=scheduled_board_meeting
|
|
103
|
+
get_earnings_calendar 查询 A 股仍需 report_date;HK 提供 symbol,可选财政期末 report_date;优先 HKEX 已公告业绩审批董事会排期 confirmation=official_board_meeting_notice、date_kind=scheduled_board_meeting,不能称实际披露日。US/JP 提供 symbol,日期来自 yfinance、confirmation=provider_only,非官方确认;KR/TW 不可用。未知期次在请求特定报告期时另列 unverified_schedules,不能强行匹配。
|
|
@@ -26,7 +26,7 @@ gather 工具返回:
|
|
|
26
26
|
- `stock_info` — 公司基本信息(行业、板块)
|
|
27
27
|
- `sector_rankings` — 板块排名
|
|
28
28
|
|
|
29
|
-
需要财务趋势时调用 `get_financials(symbol, annual=true, periods=10)`。港股配置 `LIXINGER_TOKEN` 后优先理杏仁非金融公司财报,按真实 `report_type` 判断年报,保留实际财年末、披露日期、币种;历史 ROE/毛利率/净利率为百分数,顶层 `roe/profit_margin` 延续小数比例。读取 `available_periods/warnings
|
|
29
|
+
需要财务趋势时调用 `get_financials(symbol, annual=true, periods=10)`。港股配置 `LIXINGER_TOKEN` 后优先理杏仁非金融公司财报,按真实 `report_type` 判断年报,保留实际财年末、披露日期、币种;历史 ROE/毛利率/净利率为百分数,顶层 `roe/profit_margin` 延续小数比例。读取 `available_periods/warnings`,缺期不补造。A/港/美股需要 DCF 输入时追加 `include_dcf_inputs=true`,读取 `dcf_inputs.source/periods/warnings`,每行 `field_sources` 标明字段来源、`missing_fields` 列出仍缺字段;`total_debt` 不含租赁,`lease_liabilities` 单列,按估值口径决定是否计入债务。有效 Yahoo 顶层快照与历史行分开读:`currency` 为快照财报币种、`market_cap_currency` 为市值币种,历史币种在 `periods` 每行,`snapshot_source` 标记快照来源;null 不用历史值补成跨期或跨币种数字;顶层 `report_date` 未知时读取 `periods` 的历史日期,不当作当前快照报告期。
|
|
30
30
|
|
|
31
31
|
Yahoo 顶层快照单位以 `snapshot_units` 为准:`roe/profit_margin/dividend_yield=ratio`,`debt_to_equity=percent`,例如 150 表示 150%,不是 150 倍;`pe=trailing`,不与 dynamic PE 直接比较。
|
|
32
32
|
|