@weaxs/dsh-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 +37 -83
- package/dist/client.js +14 -1
- package/dist/index.js +3897 -2694
- package/dist/web/app.js +84 -39
- package/dist/web/index.html +6 -2
- package/dist/web/style.css +10 -4
- 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
|
@@ -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
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: bull-trend
|
|
3
|
-
description:
|
|
3
|
+
description: 趋势交易:识别中长期趋势,制定入场、持仓和退出规则。用于趋势温度计算、趋势股持有、规则化趋势策略、肥尾收益或非对称风险(赔率、凸性、脆弱性)分析。
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# 趋势追踪分析
|
|
@@ -9,7 +9,7 @@ description: 趋势交易:识别中长期趋势,制定入场、持仓和退
|
|
|
9
9
|
|
|
10
10
|
趋势追踪用可执行的规则参与趋势,通过控制试错损失、保留盈利空间捕获少数大行情。肥尾描述收益分布,趋势描述价格路径;肥尾指标不是趋势启动或延续信号。
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
用户询问塔勒布、肥尾计算、极端行情、非对称风险(赔率、盈亏比、凸性、脆弱性)或策略是否依赖少数大盈利时,先读取 [肥尾、非对称风险与趋势收益诊断](references/fat-tails.md)。概念问题直接解释方法;需要具体统计结果时,按参考中的数据口径准备输入并调用 `scripts/tail_analysis.py`。普通趋势判断无需加载这份参考。
|
|
13
13
|
|
|
14
14
|
仅解释肥尾概念时,到此完成;下述流程用于具体标的的趋势分析。
|
|
15
15
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 肥尾、非对称风险与趋势收益诊断
|
|
2
2
|
|
|
3
3
|
本参考供趋势 skill 和趋势策略回测按需读取。数值由本 skill 的 [计算脚本](../scripts/tail_analysis.py) 执行,无需独立工具注册。保留输入、运行参数和输出;没有执行环境或数据时只解释方法,数值标为未计算。
|
|
4
4
|
|
|
@@ -9,10 +9,12 @@
|
|
|
9
9
|
```bash
|
|
10
10
|
python skills/bull-trend/scripts/tail_analysis.py returns historical-returns.json --n 2 10 30 --draws 20000 --seed 42
|
|
11
11
|
python skills/bull-trend/scripts/tail_analysis.py returns historical-returns.json --current 0.05
|
|
12
|
+
python skills/bull-trend/scripts/tail_analysis.py returns historical-returns.json --tail-prob 0.05 --threshold 0
|
|
12
13
|
python skills/bull-trend/scripts/tail_analysis.py trades backtest-result.json
|
|
14
|
+
python skills/bull-trend/scripts/tail_analysis.py fragility scenarios.json --center 0
|
|
13
15
|
```
|
|
14
16
|
|
|
15
|
-
`returns` 输入是同周期历史对数收益的 JSON 数组,如 `[0.01, -0.02, 0.005]`,以小数表示;`--current` 的当前收益另传,必须从历史输入中排除。脚本不转换价格、不抓行情、不识别日期,因此先按下文校验并记录数据口径。`trades` 输入是含完整 `trades` 数组的回测 JSON
|
|
17
|
+
`returns` 输入是同周期历史对数收益的 JSON 数组,如 `[0.01, -0.02, 0.005]`,以小数表示;`--current` 的当前收益另传,必须从历史输入中排除。脚本不转换价格、不抓行情、不识别日期,因此先按下文校验并记录数据口径。`trades` 输入是含完整 `trades` 数组的回测 JSON。`fragility` 输入是情景损益表,格式见下文。脚本 stdout 输出 JSON,失败返回 `error` 并以非零状态退出。
|
|
16
18
|
|
|
17
19
|
κ 默认 n0 = 1,以固定种子做 iid 经验重抽样,输出每个 n 的 κ 和平均绝对偏差;换种子重复运行检查 Monte Carlo 波动,换历史窗口检查数据敏感性。输出没有总体置信区间。`sample_count` 是历史收益数量,`draws` 是模拟轮数,两者不能混称样本量。`current_event.percentile` 为 0..1,`upper_exceedances` / `lower_exceedances` 是历史严格超过 / 低于当前收益的次数。
|
|
18
20
|
|
|
@@ -23,9 +25,13 @@ python skills/bull-trend/scripts/tail_analysis.py trades backtest-result.json
|
|
|
23
25
|
| 收益分布有多肥尾 | 固定周期的历史收益 | 样本尾部特征、κ 的条件性估计 |
|
|
24
26
|
| 当前涨跌是否极端 | 当前收益与此前同周期收益 | 历史分位或阈值超越,不能推导后续方向 |
|
|
25
27
|
| 趋势系统是否靠大赢家 | 策略平仓盈亏 | 盈利集中度及移除大赢家后的敏感性 |
|
|
28
|
+
| 涨跌空间是否对称、赔率是否划算 | 历史收益或交易计划 | 尾部盈亏比、Omega、赔率与盈亏平衡胜率 |
|
|
29
|
+
| 头寸是否怕波动(脆弱性) | 冲击情景下的头寸损益 | 凸性 H、上下行盈亏比 |
|
|
26
30
|
|
|
27
31
|
市场收益的厚尾、策略收益的正偏和趋势延续是不同性质。塔勒布的度量回答统计稳定性问题;下文的分位和盈利贡献是本项目的诊断方法,不称为塔勒布交易信号。
|
|
28
32
|
|
|
33
|
+
肥尾和非对称风险是两层问题:肥尾描述**分布**(极端值多大、多常见,用 κ 和分位),非对称描述**暴露**(同样幅度的上下波动带来的损益是否对称,用凸性 H)。同一只股票,裸多头是线性暴露,买认购期权是凸的,卖认沽期权是凹的;分布相同,风险完全不同。
|
|
34
|
+
|
|
29
35
|
## 数据口径
|
|
30
36
|
|
|
31
37
|
使用 `get_kline` 或现有 `stock_data.py kline`,核对实际返回的日期范围、周期、数量和复权标记。股票历史价格保持一致复权口径,指数保持原始点位;先处理乱序、重复日期和无效价格,说明停牌或缺失导致的时间间隔,保留真实极端收益。
|
|
@@ -86,6 +92,73 @@ T_without_k = T - 最大 k 笔正盈利之和
|
|
|
86
92
|
|
|
87
93
|
现有回测有固定止盈默认值;省略 `take_profit` 不会关闭止盈。验证保留右尾的退出规则前,检查实际引擎和 DSL 的支持情况,将无法表达的规则明确列为未验证。
|
|
88
94
|
|
|
95
|
+
## 非对称风险:公式模板
|
|
96
|
+
|
|
97
|
+
以下全部用经验数据或用户给定的情景,不拟合分布。不要用偏度、峰度做核心指标:肥尾下高阶矩样本估计极不稳定,一个极端值就能改写结果。
|
|
98
|
+
|
|
99
|
+
### 收益分布的尾部不对称(`returns` 的 `asymmetry`)
|
|
100
|
+
|
|
101
|
+
```text
|
|
102
|
+
q_lo = 分位(r, p),q_hi = 分位(r, 1-p),p 由 --tail-prob 设定,默认 0.05
|
|
103
|
+
ETL = mean(r_i | r_i <= q_lo) 左尾平均亏损(负数)
|
|
104
|
+
ETG = mean(r_i | r_i >= q_hi) 右尾平均盈利
|
|
105
|
+
尾部盈亏比 = ETG / |ETL| ETG <= 0 或 ETL >= 0 时为 null
|
|
106
|
+
Omega(τ) = sum(max(r_i-τ, 0)) / sum(max(τ-r_i, 0)) τ 由 --threshold 设定
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
尾部盈亏比 > 1 说明样本里大涨的幅度大于大跌;Omega > 1 说明高于 τ 的收益总量超过低于 τ 的总量。没有低于 τ 的收益时 Omega 为 null。尾部样本数 `lower_tail_count` / `upper_tail_count` 很少时只作描述;N = 500、p = 0.05 时每侧约 25 个点。
|
|
110
|
+
|
|
111
|
+
### 交易赔率(`trades` 与交易计划)
|
|
112
|
+
|
|
113
|
+
```text
|
|
114
|
+
胜率 p = 盈利笔数 / 平仓笔数
|
|
115
|
+
赔率 b = 平均盈利 / |平均亏损| 脚本字段 payoff_ratio
|
|
116
|
+
盈亏平衡胜率 p* = 1 / (1 + b) 脚本字段 breakeven_win_rate
|
|
117
|
+
单笔期望(以 1 单位风险计)E = p*b - (1-p)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
单笔计划用 R 倍数,无需脚本:
|
|
121
|
+
|
|
122
|
+
```text
|
|
123
|
+
R = (目标价 - 入场价) / (入场价 - 止损价)
|
|
124
|
+
p* = 1 / (1 + R)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
例:入场 10、止损 9、目标 13,R = 3,p* = 25%;胜率估计高于 25% 才有正期望。止损可能跳空击穿,实际亏损按历史跳空幅度或最坏情景补充说明。
|
|
128
|
+
|
|
129
|
+
### 暴露的凸性与脆弱性(`fragility`)
|
|
130
|
+
|
|
131
|
+
来源:[Taleb, Canetti et al., A New Heuristic Measure of Fragility and Tail Risks, IMF WP/12/216](https://www.imf.org/en/publications/wp/issues/2016/12/31/a-new-heuristic-measure-of-fragility-and-tail-risks-application-to-stress-testing-26222)([arXiv 1208.1189](https://arxiv.org/abs/1208.1189))。
|
|
132
|
+
|
|
133
|
+
设 f(x) 是冲击幅度 x(如标的涨跌幅、利率变动)下的头寸损益,c 为中心情景:
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
H(Δ) = (f(c-Δ) + f(c+Δ)) / 2 - f(c)
|
|
137
|
+
H < 0 凹,脆弱:波动越大越亏,伤害随冲击加速
|
|
138
|
+
H > 0 凸,反脆弱:波动越大越有利
|
|
139
|
+
H ≈ 0 线性
|
|
140
|
+
上下行盈亏比 = (f(c+Δ) - f(c)) / (f(c) - f(c-Δ))
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
输入模板(`capital` 可选,用于输出 `h_over_capital`):
|
|
144
|
+
|
|
145
|
+
```json
|
|
146
|
+
{
|
|
147
|
+
"capital": 100000,
|
|
148
|
+
"scenarios": [
|
|
149
|
+
{"shock": -0.2, "pnl": -2000},
|
|
150
|
+
{"shock": -0.1, "pnl": -2000},
|
|
151
|
+
{"shock": 0, "pnl": -1500},
|
|
152
|
+
{"shock": 0.1, "pnl": 3500},
|
|
153
|
+
{"shock": 0.2, "pnl": 8500}
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
这是买入认购期权的形状,H 为正。卖出认沽、加杠杆被强平、高负债公司的股权在下跌端加速亏损,H 为负。必须包含中心情景和对称的 ±Δ;多组 Δ 时看 H 是否随 Δ 增大而加速变负,这是脆弱性的核心信号。
|
|
159
|
+
|
|
160
|
+
`analyze_portfolio_risk` 的压力测试按 beta 线性推算,其结果作为 f(x) 时 H 恒为 0;非线性只来自期权、止损、杠杆与保证金、可转债,或公司层面的负债与经营杠杆。公司层面可用不同营收或利率冲击下的净利润或权益价值作为 f(x),情景须由用户或财报数据给出并注明假设。
|
|
161
|
+
|
|
89
162
|
## 完成标准
|
|
90
163
|
|
|
91
|
-
报告指出分析对象、数据来源及区间、周期、复权口径、样本量、公式与估计方法、实际数值或未计算原因。κ 结果还需注明 n0、n
|
|
164
|
+
报告指出分析对象、数据来源及区间、周期、复权口径、样本量、公式与估计方法、实际数值或未计算原因。κ 结果还需注明 n0、n、抽样假设和敏感性;盈利集中度需注明平仓与未平仓口径;非对称结果需注明 tail-prob、阈值或情景来源。交易建议依据趋势和风险规则,统计结果仅提供其能支持的证据。
|