@weaxs/openclaw-stock-analysis 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 +11 -52
- package/dist/index.js +3423 -1546
- package/index.ts +74 -1648
- package/openclaw.plugin.json +3 -0
- package/package.json +1 -1
- 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/openclaw.plugin.json
CHANGED
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"screen_risk",
|
|
43
43
|
"detect_market_regime",
|
|
44
44
|
"get_market_review",
|
|
45
|
+
"get_watchlist",
|
|
45
46
|
"run_watchlist_analysis",
|
|
46
47
|
"detect_anomaly",
|
|
47
48
|
"diagnose_data_sources",
|
|
@@ -58,6 +59,7 @@
|
|
|
58
59
|
"parse_stock_list",
|
|
59
60
|
"get_valuation_history",
|
|
60
61
|
"get_index_valuation",
|
|
62
|
+
"get_cds_index",
|
|
61
63
|
"get_earnings_calendar",
|
|
62
64
|
"get_shareholder_structure",
|
|
63
65
|
"get_etf_details",
|
|
@@ -70,6 +72,7 @@
|
|
|
70
72
|
"analyze_portfolio_risk",
|
|
71
73
|
"plan_portfolio_rebalance",
|
|
72
74
|
"calculate_position_size",
|
|
75
|
+
"calculate_dcf_valuation",
|
|
73
76
|
"analyze_event_study",
|
|
74
77
|
"validate_backtest",
|
|
75
78
|
"compare_analysis_snapshots",
|
package/package.json
CHANGED
|
@@ -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,493 @@
|
|
|
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_cds_index: {
|
|
32
|
+
script: "credit_data.py",
|
|
33
|
+
argv: (p) => ["cds_index", "--source", p.source ?? "cn", "--days", String(p.days ?? 30), "--index", p.index ?? "", "--tenor", p.tenor ?? ""],
|
|
34
|
+
},
|
|
35
|
+
get_earnings_calendar: {
|
|
36
|
+
script: "value_data.py",
|
|
37
|
+
argv: (p) => ["earnings_calendar", "--report-date", p.report_date ?? "", "--symbol", p.symbol ?? "", "--top", String(p.top ?? 50)],
|
|
38
|
+
},
|
|
39
|
+
get_shareholder_structure: {
|
|
40
|
+
script: "value_data.py",
|
|
41
|
+
argv: (p) => ["shareholder_structure", p.symbol, "--report-date", p.report_date ?? ""],
|
|
42
|
+
},
|
|
43
|
+
get_etf_details: {
|
|
44
|
+
script: "value_data.py",
|
|
45
|
+
argv: (p) => ["etf_details", p.symbol, "--benchmark", p.benchmark ?? "", "--days", String(p.days ?? 60), ...(p.include_full_holdings ? ["--include-full-holdings"] : [])],
|
|
46
|
+
},
|
|
47
|
+
get_dividends: {
|
|
48
|
+
script: "value_data.py",
|
|
49
|
+
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)],
|
|
50
|
+
},
|
|
51
|
+
get_kline: {
|
|
52
|
+
script: "stock_data.py",
|
|
53
|
+
argv: (p) => [
|
|
54
|
+
"kline",
|
|
55
|
+
p.symbol,
|
|
56
|
+
"--period",
|
|
57
|
+
p.period ?? "daily",
|
|
58
|
+
"--count",
|
|
59
|
+
String(p.count ?? 60),
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
get_quote: {
|
|
63
|
+
script: "stock_data.py",
|
|
64
|
+
argv: (p) => ["quote", p.symbol],
|
|
65
|
+
},
|
|
66
|
+
get_fund_quote: {
|
|
67
|
+
script: "stock_data.py",
|
|
68
|
+
argv: (p) => ["fund_quote", p.symbol],
|
|
69
|
+
},
|
|
70
|
+
get_capital_flow: {
|
|
71
|
+
script: "stock_data.py",
|
|
72
|
+
argv: (p) => {
|
|
73
|
+
const args = ["capital_flow"];
|
|
74
|
+
if (p.symbol) args.push(p.symbol);
|
|
75
|
+
args.push("--mode", p.mode ?? "detail");
|
|
76
|
+
return args;
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
get_news: {
|
|
80
|
+
script: "stock_data.py",
|
|
81
|
+
argv: (p) => ["news", p.symbol, "--days", String(p.days ?? 3)],
|
|
82
|
+
},
|
|
83
|
+
get_financials: {
|
|
84
|
+
script: "stock_data.py",
|
|
85
|
+
argv: (p) => {
|
|
86
|
+
const args = ["financials", p.symbol];
|
|
87
|
+
if (p.periods !== undefined && p.periods !== 1) args.push("--periods", String(p.periods));
|
|
88
|
+
if (p.annual) args.push("--annual");
|
|
89
|
+
if (p.include_dcf_inputs) args.push("--include-dcf-inputs");
|
|
90
|
+
return args;
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
get_technical_analysis: {
|
|
94
|
+
script: "technical.py",
|
|
95
|
+
argv: (p) => {
|
|
96
|
+
const args = [
|
|
97
|
+
"analyze",
|
|
98
|
+
p.symbol,
|
|
99
|
+
"--period",
|
|
100
|
+
p.period ?? "daily",
|
|
101
|
+
"--count",
|
|
102
|
+
String(p.count ?? 120),
|
|
103
|
+
];
|
|
104
|
+
if (p.periods) args.push("--periods", p.periods);
|
|
105
|
+
return args;
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
analyze_pattern: {
|
|
109
|
+
script: "pattern.py",
|
|
110
|
+
argv: (p) => [
|
|
111
|
+
"analyze",
|
|
112
|
+
p.symbol,
|
|
113
|
+
"--period",
|
|
114
|
+
p.period ?? "daily",
|
|
115
|
+
"--days",
|
|
116
|
+
String(p.days ?? 60),
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
get_market_indices: {
|
|
120
|
+
script: "stock_data.py",
|
|
121
|
+
argv: (p) => ["market_indices", "--region", p.region ?? "cn"],
|
|
122
|
+
},
|
|
123
|
+
get_sector_rankings: {
|
|
124
|
+
script: "stock_data.py",
|
|
125
|
+
argv: (p) => [
|
|
126
|
+
"sector_rankings",
|
|
127
|
+
"--top",
|
|
128
|
+
String(p.top ?? 10),
|
|
129
|
+
"--direction",
|
|
130
|
+
p.direction ?? "top",
|
|
131
|
+
"--board-type",
|
|
132
|
+
p.board_type ?? "industry",
|
|
133
|
+
"--market",
|
|
134
|
+
p.market ?? "A",
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
get_sector_constituents: {
|
|
138
|
+
script: "stock_data.py",
|
|
139
|
+
argv: (p) => [
|
|
140
|
+
"sector_constituents",
|
|
141
|
+
p.sector,
|
|
142
|
+
"--board-type",
|
|
143
|
+
p.board_type ?? "auto",
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
resolve_stock_sectors: {
|
|
147
|
+
script: "stock_data.py",
|
|
148
|
+
argv: (p) => ["resolve_stock_sectors", p.symbol],
|
|
149
|
+
},
|
|
150
|
+
get_stock_info: {
|
|
151
|
+
script: "stock_data.py",
|
|
152
|
+
argv: (p) => ["stock_info", p.symbol],
|
|
153
|
+
},
|
|
154
|
+
get_chip_distribution: {
|
|
155
|
+
script: "stock_data.py",
|
|
156
|
+
argv: (p) => ["chip_distribution", p.symbol],
|
|
157
|
+
},
|
|
158
|
+
get_market_stats: {
|
|
159
|
+
script: "stock_data.py",
|
|
160
|
+
argv: (p) => ["market_stats", "--market", p.market ?? "A"],
|
|
161
|
+
},
|
|
162
|
+
get_limit_up_pool: {
|
|
163
|
+
script: "stock_data.py",
|
|
164
|
+
argv: (p) => {
|
|
165
|
+
const args = ["limit_up_pool"];
|
|
166
|
+
if (p.date) args.push("--date", p.date);
|
|
167
|
+
return args;
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
get_dragon_tiger: {
|
|
171
|
+
script: "stock_data.py",
|
|
172
|
+
argv: (p) => {
|
|
173
|
+
const args = ["dragon_tiger"];
|
|
174
|
+
if (p.date) args.push("--date", p.date);
|
|
175
|
+
if (p.symbol) args.push("--symbol", p.symbol);
|
|
176
|
+
args.push("--top", String(p.top ?? 20));
|
|
177
|
+
return args;
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
get_hot_stocks: {
|
|
181
|
+
script: "stock_data.py",
|
|
182
|
+
argv: (p) => ["hot_stocks", "--top", String(p.top ?? 20)],
|
|
183
|
+
},
|
|
184
|
+
get_margin_trading: {
|
|
185
|
+
script: "stock_data.py",
|
|
186
|
+
argv: (p) => ["margin_trading", p.symbol, "--days", String(p.days ?? 10), ...(p.include_ratio ? ["--include-ratio"] : [])],
|
|
187
|
+
},
|
|
188
|
+
get_northbound_flow: {
|
|
189
|
+
script: "stock_data.py",
|
|
190
|
+
argv: (p) => ["northbound_flow", "--days", String(p.days ?? 10)],
|
|
191
|
+
},
|
|
192
|
+
get_fundamental_context: {
|
|
193
|
+
script: "stock_data.py",
|
|
194
|
+
argv: (p) => ["fundamental_context", p.symbol],
|
|
195
|
+
},
|
|
196
|
+
screen_stocks: {
|
|
197
|
+
script: "screener.py",
|
|
198
|
+
argv: (p) => {
|
|
199
|
+
const args = ["screen", "--market", p.market ?? "A", "--top", String(p.top ?? 20)];
|
|
200
|
+
if (p.config) args.push("--config", p.config);
|
|
201
|
+
if (p.l2) args.push("--l2");
|
|
202
|
+
return args;
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
calculate_rotation_metrics: {
|
|
206
|
+
script: "sector_rotation.py",
|
|
207
|
+
input: (p) => p,
|
|
208
|
+
argv: (p, path) => ["analyze", "--input", path],
|
|
209
|
+
},
|
|
210
|
+
backtest_rotation_allocation: {
|
|
211
|
+
script: "sector_rotation.py",
|
|
212
|
+
input: (p) => p,
|
|
213
|
+
argv: (p, path) => ["backtest", "--input", path],
|
|
214
|
+
},
|
|
215
|
+
analyze_trend_strategy: {
|
|
216
|
+
script: "trend_strategy.py",
|
|
217
|
+
input: (p) => p,
|
|
218
|
+
argv: (p, path) => ["fetch", "--input", path],
|
|
219
|
+
},
|
|
220
|
+
run_backtest: {
|
|
221
|
+
script: "backtest.py",
|
|
222
|
+
argv: (p) => {
|
|
223
|
+
const args = ["run", p.strategy, p.symbol];
|
|
224
|
+
if (p.start) args.push("--start", p.start);
|
|
225
|
+
if (p.end) args.push("--end", p.end);
|
|
226
|
+
args.push("--capital", String(p.capital ?? 1000000));
|
|
227
|
+
return args;
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
evaluate_signal: {
|
|
231
|
+
script: "backtest.py",
|
|
232
|
+
argv: (p) => [
|
|
233
|
+
"evaluate_signal",
|
|
234
|
+
p.symbol,
|
|
235
|
+
p.signal,
|
|
236
|
+
"--forward",
|
|
237
|
+
p.forward_days ?? "3,5,10",
|
|
238
|
+
"--lookback",
|
|
239
|
+
String(p.lookback ?? 250),
|
|
240
|
+
],
|
|
241
|
+
},
|
|
242
|
+
resolve_stock_name: {
|
|
243
|
+
script: "name_resolver.py",
|
|
244
|
+
argv: (p) => ["resolve", p.query, "--top", String(p.top ?? 5)],
|
|
245
|
+
},
|
|
246
|
+
check_trading_day: {
|
|
247
|
+
script: "trading_calendar.py",
|
|
248
|
+
argv: (p) => {
|
|
249
|
+
const args = ["check", p.market];
|
|
250
|
+
if (p.date) args.push("--date", p.date);
|
|
251
|
+
return args;
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
get_trading_days: {
|
|
255
|
+
script: "trading_calendar.py",
|
|
256
|
+
argv: (p) => {
|
|
257
|
+
const args = [p.direction ?? "next", p.market, "--count", String(p.count ?? 5)];
|
|
258
|
+
if (p.date) args.push("--date", p.date);
|
|
259
|
+
return args;
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
get_trading_phase: {
|
|
263
|
+
script: "trading_calendar.py",
|
|
264
|
+
argv: (p) => ["phase", p.market],
|
|
265
|
+
},
|
|
266
|
+
calculate_ma: {
|
|
267
|
+
script: "technical.py",
|
|
268
|
+
argv: (p) => [
|
|
269
|
+
"calculate_ma",
|
|
270
|
+
p.symbol,
|
|
271
|
+
"--periods",
|
|
272
|
+
p.periods ?? "5,10,20,30,60,120,250",
|
|
273
|
+
"--period",
|
|
274
|
+
p.kline_period ?? "daily",
|
|
275
|
+
"--count",
|
|
276
|
+
"300",
|
|
277
|
+
],
|
|
278
|
+
},
|
|
279
|
+
get_volume_analysis: {
|
|
280
|
+
script: "volume_analysis.py",
|
|
281
|
+
argv: (p) => [
|
|
282
|
+
"analyze",
|
|
283
|
+
p.symbol,
|
|
284
|
+
"--period",
|
|
285
|
+
p.period ?? "daily",
|
|
286
|
+
"--count",
|
|
287
|
+
String(p.count ?? 60),
|
|
288
|
+
],
|
|
289
|
+
},
|
|
290
|
+
search_stock_news: {
|
|
291
|
+
script: "search_intel.py",
|
|
292
|
+
argv: (p) => ["search", p.query, "--count", String(p.count ?? 10)],
|
|
293
|
+
},
|
|
294
|
+
search_comprehensive_intel: {
|
|
295
|
+
script: "search_intel.py",
|
|
296
|
+
argv: (p) => {
|
|
297
|
+
const args = ["comprehensive", p.symbol];
|
|
298
|
+
if (p.name) args.push("--name", p.name);
|
|
299
|
+
return args;
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
get_social_sentiment: {
|
|
303
|
+
script: "search_intel.py",
|
|
304
|
+
argv: (p) => ["sentiment", p.symbol],
|
|
305
|
+
},
|
|
306
|
+
get_trending_sentiment: {
|
|
307
|
+
script: "search_intel.py",
|
|
308
|
+
argv: () => ["trending"],
|
|
309
|
+
},
|
|
310
|
+
extract_article: {
|
|
311
|
+
script: "search_intel.py",
|
|
312
|
+
argv: (p) => ["extract", p.url, "--max-chars", String(p.max_chars ?? 3000)],
|
|
313
|
+
},
|
|
314
|
+
screen_risk: {
|
|
315
|
+
script: "risk_screening.py",
|
|
316
|
+
argv: (p) => {
|
|
317
|
+
const args = ["screen", p.symbol];
|
|
318
|
+
if (p.name) args.push("--name", p.name);
|
|
319
|
+
return args;
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
detect_market_regime: {
|
|
323
|
+
script: "market_regime.py",
|
|
324
|
+
argv: (p) => ["detect", p.market ?? "A"],
|
|
325
|
+
},
|
|
326
|
+
get_market_review: {
|
|
327
|
+
script: "market_review.py",
|
|
328
|
+
argv: (p) => ["review", "--market", p.market ?? "A"],
|
|
329
|
+
},
|
|
330
|
+
get_watchlist: {
|
|
331
|
+
script: "watchlist.py",
|
|
332
|
+
argv: () => ["list"],
|
|
333
|
+
},
|
|
334
|
+
run_watchlist_analysis: {
|
|
335
|
+
script: "watchlist.py",
|
|
336
|
+
argv: (p) => ["analyze", p.symbols, "--workers", String(p.workers ?? 3)],
|
|
337
|
+
},
|
|
338
|
+
detect_anomaly: {
|
|
339
|
+
script: "anomaly_detect.py",
|
|
340
|
+
argv: (p) => ["detect", p.symbol],
|
|
341
|
+
},
|
|
342
|
+
diagnose_data_sources: {
|
|
343
|
+
script: "diagnostics.py",
|
|
344
|
+
argv: (p) => ["check", "--market", p.market ?? "all"],
|
|
345
|
+
},
|
|
346
|
+
get_market_capabilities: {
|
|
347
|
+
script: "capabilities.py",
|
|
348
|
+
argv: (p) =>
|
|
349
|
+
p.symbol
|
|
350
|
+
? ["get", "--symbol", p.symbol]
|
|
351
|
+
: ["get", "--market", p.market ?? "A"],
|
|
352
|
+
},
|
|
353
|
+
render_stock_report: {
|
|
354
|
+
script: "report_renderer.py",
|
|
355
|
+
input: (p) => p.report,
|
|
356
|
+
argv: (p, path) => ["stock", "--template", p.template ?? "full", "--input", path, ...(p.final ? ["--final"] : [])],
|
|
357
|
+
},
|
|
358
|
+
render_market_report: {
|
|
359
|
+
script: "report_renderer.py",
|
|
360
|
+
input: (p) => p.report,
|
|
361
|
+
argv: (p, path) => ["market", "--input", path, ...(p.final ? ["--final"] : []), ...(p.save ? ["--save"] : [])],
|
|
362
|
+
},
|
|
363
|
+
get_review_history: {
|
|
364
|
+
script: "market_review.py",
|
|
365
|
+
argv: (p) => ["history", "--limit", String(p.limit ?? 10)],
|
|
366
|
+
},
|
|
367
|
+
build_watchlist_context: {
|
|
368
|
+
script: "watchlist_context.py",
|
|
369
|
+
argv: (p) => {
|
|
370
|
+
const args = ["build", p.symbols, "--workers", String(p.workers ?? 3)];
|
|
371
|
+
if (p.include_market_review) args.push("--include-market-review");
|
|
372
|
+
return args;
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
analyze_position_context: {
|
|
376
|
+
script: "position_context.py",
|
|
377
|
+
argv: (p) => {
|
|
378
|
+
const args = [
|
|
379
|
+
"analyze",
|
|
380
|
+
p.symbol,
|
|
381
|
+
"--cost",
|
|
382
|
+
String(p.cost),
|
|
383
|
+
"--quantity",
|
|
384
|
+
String(p.quantity),
|
|
385
|
+
];
|
|
386
|
+
if (p.stop_loss !== undefined) args.push("--stop-loss", String(p.stop_loss));
|
|
387
|
+
if (p.take_profit !== undefined) args.push("--take-profit", String(p.take_profit));
|
|
388
|
+
return args;
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
check_alert_rules: {
|
|
392
|
+
script: "alert_rules.py",
|
|
393
|
+
argv: (p) => {
|
|
394
|
+
const b64 = Buffer.from(JSON.stringify(p.rules), "utf-8").toString("base64");
|
|
395
|
+
return ["check", p.symbol, "--rules-b64", b64];
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
record_signal: {
|
|
399
|
+
script: "signal_tracker.py",
|
|
400
|
+
argv: (p) => {
|
|
401
|
+
const args = [
|
|
402
|
+
"record",
|
|
403
|
+
p.symbol,
|
|
404
|
+
"--direction",
|
|
405
|
+
p.direction,
|
|
406
|
+
"--horizon-days",
|
|
407
|
+
String(p.horizon_days ?? 10),
|
|
408
|
+
];
|
|
409
|
+
if (p.entry_price !== undefined) args.push("--entry-price", String(p.entry_price));
|
|
410
|
+
if (p.target_price !== undefined) args.push("--target-price", String(p.target_price));
|
|
411
|
+
if (p.stop_price !== undefined) args.push("--stop-price", String(p.stop_price));
|
|
412
|
+
if (p.source) args.push("--source", p.source);
|
|
413
|
+
if (p.note) args.push("--note", p.note);
|
|
414
|
+
return args;
|
|
415
|
+
},
|
|
416
|
+
},
|
|
417
|
+
evaluate_signals: {
|
|
418
|
+
script: "signal_tracker.py",
|
|
419
|
+
argv: (p) => {
|
|
420
|
+
const args = ["evaluate"];
|
|
421
|
+
if (p.symbol) args.push("--symbol", p.symbol);
|
|
422
|
+
return args;
|
|
423
|
+
},
|
|
424
|
+
},
|
|
425
|
+
get_signal_summary: {
|
|
426
|
+
script: "signal_tracker.py",
|
|
427
|
+
argv: (p) => {
|
|
428
|
+
const args = ["summary"];
|
|
429
|
+
if (p.source) args.push("--source", p.source);
|
|
430
|
+
if (p.symbol) args.push("--symbol", p.symbol);
|
|
431
|
+
args.push("--status", p.status ?? "all");
|
|
432
|
+
return args;
|
|
433
|
+
},
|
|
434
|
+
},
|
|
435
|
+
parse_stock_list: {
|
|
436
|
+
script: "import_parser.py",
|
|
437
|
+
argv: (p) => {
|
|
438
|
+
const b64 = Buffer.from(String(p.text), "utf-8").toString("base64");
|
|
439
|
+
return ["parse", "--text-b64", b64];
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
compare_stocks: {
|
|
443
|
+
script: "stateless_analysis.py",
|
|
444
|
+
input: (p) => p,
|
|
445
|
+
argv: (p, path) => ["peers", "--input", path],
|
|
446
|
+
},
|
|
447
|
+
analyze_disclosure_changes: {
|
|
448
|
+
script: "stateless_analysis.py",
|
|
449
|
+
input: (p) => p,
|
|
450
|
+
argv: (p, path) => ["disclosure", "--input", path],
|
|
451
|
+
},
|
|
452
|
+
analyze_portfolio_risk: {
|
|
453
|
+
script: "stateless_analysis.py",
|
|
454
|
+
input: (p) => p,
|
|
455
|
+
argv: (p, path) => ["portfolio", "--input", path],
|
|
456
|
+
},
|
|
457
|
+
plan_portfolio_rebalance: {
|
|
458
|
+
script: "stateless_analysis.py",
|
|
459
|
+
input: (p) => p,
|
|
460
|
+
argv: (p, path) => ["rebalance", "--input", path],
|
|
461
|
+
},
|
|
462
|
+
calculate_position_size: {
|
|
463
|
+
script: "stateless_analysis.py",
|
|
464
|
+
input: (p) => p,
|
|
465
|
+
argv: (p, path) => ["position_size", "--input", path],
|
|
466
|
+
},
|
|
467
|
+
calculate_dcf_valuation: {
|
|
468
|
+
script: "stateless_analysis.py",
|
|
469
|
+
input: (p) => p,
|
|
470
|
+
argv: (p, path) => ["dcf", "--input", path],
|
|
471
|
+
},
|
|
472
|
+
analyze_event_study: {
|
|
473
|
+
script: "stateless_analysis.py",
|
|
474
|
+
input: (p) => p,
|
|
475
|
+
argv: (p, path) => ["event_study", "--input", path],
|
|
476
|
+
},
|
|
477
|
+
validate_backtest: {
|
|
478
|
+
script: "stateless_analysis.py",
|
|
479
|
+
input: (p) => p,
|
|
480
|
+
argv: (p, path) => ["robustness", "--input", path],
|
|
481
|
+
},
|
|
482
|
+
compare_analysis_snapshots: {
|
|
483
|
+
script: "stateless_analysis.py",
|
|
484
|
+
input: (p) => p,
|
|
485
|
+
argv: (p, path) => ["snapshot", "--input", path],
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
/** Run one tool call; `run(script, argv)` resolves to the CLI's JSON text. */
|
|
490
|
+
export function callTool(name, args, run) {
|
|
491
|
+
const { script, argv, input } = TOOL_CALLS[name];
|
|
492
|
+
return input ? withJsonInput(input(args), path => run(script, argv(args, path))) : run(script, argv(args));
|
|
493
|
+
}
|