@weaxs/openclaw-stock-analysis 0.2.8 → 0.2.9

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Stock Analysis — OpenClaw Plugin
2
2
 
3
- A股 / 港股 / 美股 / 日股 / 韩股 / 台股 行情、技术分析、筛选与回测。OpenClaw Gateway 加载后即可在对话中调用 68 个工具。
3
+ A股 / 港股 / 美股 / 日股 / 韩股 / 台股 行情、技术分析、筛选与回测。OpenClaw Gateway 加载后即可在对话中调用 70 个工具。
4
4
 
5
5
  ## 安装
6
6
 
package/dist/index.js CHANGED
@@ -56,7 +56,8 @@ var cli_budgets_default = {
56
56
  watchlist_item: 480,
57
57
  "stateless_analysis.py:event_study": 300,
58
58
  "stateless_analysis.py:robustness": 660,
59
- "trend_strategy.py:fetch": 660
59
+ "trend_strategy.py:fetch": 660,
60
+ "sector_rotation.py:backtest": 300
60
61
  };
61
62
 
62
63
  // scripts/cli-timeout.mjs
@@ -85,11 +86,22 @@ function commandTimeout(script, args) {
85
86
  return budget * 1e3 + 3e4;
86
87
  }
87
88
 
88
- // openclaw/index.ts
89
+ // scripts/venv-python.mjs
89
90
  import { existsSync } from "node:fs";
91
+ import { join as join2 } from "node:path";
92
+ var isWin = process.platform === "win32";
93
+ function pythonEnvironmentRoot(rootDir) {
94
+ return existsSync(join2(rootDir, "openclaw.plugin.json")) ? join2(rootDir, "node_modules", ".stock-analysis-python") : rootDir;
95
+ }
96
+ function venvPythonPath(rootDir) {
97
+ return join2(rootDir, ".venv", isWin ? "Scripts" : "bin", isWin ? "python.exe" : "python3");
98
+ }
99
+
100
+ // openclaw/index.ts
101
+ import { existsSync as existsSync2 } from "node:fs";
90
102
  import * as cp from "node:child_process";
91
103
  import { promisify } from "node:util";
92
- import { dirname, join as join2 } from "node:path";
104
+ import { dirname, join as join3 } from "node:path";
93
105
  import { fileURLToPath } from "node:url";
94
106
 
95
107
  // node_modules/typebox/build/system/memory/memory.mjs
@@ -4526,28 +4538,39 @@ function __setExecutor(fn) {
4526
4538
  executor = fn;
4527
4539
  }
4528
4540
  var here = dirname(fileURLToPath(import.meta.url));
4529
- var toolsDir = existsSync(join2(here, "tools")) ? join2(here, "tools") : join2(here, "..", "tools");
4541
+ var toolsDir = existsSync2(join3(here, "tools")) ? join3(here, "tools") : join3(here, "..", "tools");
4530
4542
  var fallbackRoot = dirname(toolsDir);
4531
- var isWin = process.platform === "win32";
4532
- var venvBin = join2(".venv", isWin ? "Scripts" : "bin", isWin ? "python.exe" : "python3");
4543
+ var isWin2 = process.platform === "win32";
4544
+ var venvBin = join3(".venv", isWin2 ? "Scripts" : "bin", isWin2 ? "python.exe" : "python3");
4533
4545
  function asText(text) {
4534
4546
  return { content: [{ type: "text", text }] };
4535
4547
  }
4548
+ function asReport(text) {
4549
+ const result = asText(text);
4550
+ try {
4551
+ const report = JSON.parse(text);
4552
+ if (report?.final === true && !Object.hasOwn(report, "error") && report.format === "markdown" && typeof report.content === "string" && report.content.trim()) {
4553
+ return { ...result, details: { sourceReply: { text: report.content } } };
4554
+ }
4555
+ } catch {
4556
+ }
4557
+ return result;
4558
+ }
4536
4559
  var index_default = definePluginEntry({
4537
4560
  id: "stock-analysis",
4538
4561
  name: "Stock Analysis",
4539
4562
  description: "Stock analysis, screening, and strategy backtesting across A/HK/US/JP/KR/TW markets",
4540
4563
  register(api) {
4541
4564
  const repoRoot = api.rootDir || fallbackRoot;
4542
- const runtimeToolsDir = existsSync(join2(repoRoot, "tools")) ? join2(repoRoot, "tools") : toolsDir;
4565
+ const runtimeToolsDir = existsSync2(join3(repoRoot, "tools")) ? join3(repoRoot, "tools") : toolsDir;
4543
4566
  const pythonBin = () => {
4544
- const venvPython = join2(repoRoot, venvBin);
4545
- if (existsSync(venvPython)) return venvPython;
4546
- const parentVenvPython = join2(repoRoot, "..", venvBin);
4547
- if (existsSync(parentVenvPython)) return parentVenvPython;
4548
- return isWin ? "python" : "python3";
4567
+ const venvPython = venvPythonPath(pythonEnvironmentRoot(repoRoot));
4568
+ if (existsSync2(venvPython)) return venvPython;
4569
+ const parentVenvPython = join3(repoRoot, "..", venvBin);
4570
+ if (existsSync2(parentVenvPython)) return parentVenvPython;
4571
+ return isWin2 ? "python" : "python3";
4549
4572
  };
4550
- const runPy = (script, args) => executor(pythonBin(), [join2(runtimeToolsDir, script), ...args]);
4573
+ const runPy = (script, args) => executor(pythonBin(), [join3(runtimeToolsDir, script), ...args]);
4551
4574
  api.registerTool({
4552
4575
  name: "compare_stocks",
4553
4576
  description: "\u65E0\u72B6\u6001\u540C\u884C\u6307\u6807\u6BD4\u8F83\uFF1A\u5148\u7528\u73B0\u6709\u8D22\u52A1/\u884C\u60C5\u5DE5\u5177\u53D6\u6570\uFF0C\u518D\u4F20companies\uFF1B\u4EC5\u76F8\u540Cunit/basis/period\u53EF\u6392\u5E8F\uFF0C\u7F3A\u5931\u4E0D\u586B\u96F6\uFF0C\u4E0D\u751F\u6210\u4E70\u5356\u7EFC\u5408\u5206\u3002",
@@ -5263,6 +5286,56 @@ var index_default = definePluginEntry({
5263
5286
  return asText(out);
5264
5287
  }
5265
5288
  });
5289
+ api.registerTool({
5290
+ name: "calculate_rotation_metrics",
5291
+ description: "\u884C\u4E1A\u8F6E\u52A8\u6570\u503C\u590D\u6838\uFF1A\u8F93\u5165\u771F\u5B9EMC-dropout\u6837\u672C\u548C/\u6216\u5386\u53F2\u6210\u4EA4\u8BA1\u6570\uFF0C\u8BA1\u7B97\u96C6\u4E2D\u5EA6\u548C\u539F\u59CB\u914D\u6743\uFF1B\u4E0D\u8BAD\u7EC3\u6A21\u578B\u3001\u4E0D\u751F\u6210\u4E70\u5165\u4FE1\u53F7\uFF0C\u96F6\u5386\u53F2\u4E0D\u8865\u9ED8\u8BA4\u503C\u3002",
5292
+ parameters: typebox_exports.Object({
5293
+ prediction_samples: typebox_exports.Optional(typebox_exports.Array(typebox_exports.Object({
5294
+ symbol: typebox_exports.String({ "description": "\u552F\u4E00\u6807\u7684\u6807\u8BC6" }),
5295
+ samples: typebox_exports.Array(typebox_exports.Number({ "description": "\u540C\u4E00\u6A21\u578B/\u6807\u7684/\u65F6\u95F4\u7684\u6709\u9650\u7EBF\u6027\u8F93\u51FA\uFF0C\u4E0D\u662F\u4EF7\u683C\u6216\u6536\u76CA\u7387" }), { "description": "2..100000\u4E2A\u771F\u5B9EMC-dropout\u9884\u6D4B\uFF1B\u4E0D\u5F97\u7528\u968F\u673A\u6570\u6216\u52A8\u91CF\u4EE3\u66FF" })
5296
+ }, { "additionalProperties": false }), { "description": "1..100\u7EC4\uFF0C\u603B\u6837\u672C<=100000\uFF1B\u548Ctrade_counts\u81F3\u5C11\u63D0\u4F9B\u4E00\u9879" })),
5297
+ trade_counts: typebox_exports.Optional(typebox_exports.Array(typebox_exports.Object({
5298
+ symbol: typebox_exports.String({ "description": "\u552F\u4E00\u6807\u7684\u6807\u8BC6\uFF1B\u4EE3\u7801\u522B\u540D\u987B\u5148\u7EDF\u4E00" }),
5299
+ buys: typebox_exports.Number({ "description": "\u6B64\u524D\u5DF2\u5E73\u4ED3\u4E70\u5165\u5468\u6570\uFF0C\u975E\u8D1F\u6574\u6570" }),
5300
+ wins: typebox_exports.Number({ "description": "\u6B64\u524D\u76C8\u5229\u5E73\u4ED3\u6B21\u6570\uFF0C0..buys" }),
5301
+ streak: typebox_exports.Number({ "description": "\u6B64\u524D\u8FDE\u7EED\u76C8\u5229\u4EA4\u6613\u6B21\u6570\uFF0C0..wins" })
5302
+ }, { "additionalProperties": false }), { "description": "1..100\u6761\u5DF2\u5B8C\u6210\u5386\u53F2\u6210\u4EA4\u8BA1\u6570\uFF1B\u4E0D\u5F97\u4F7F\u7528\u51B3\u7B56\u540E\u7684\u4EA4\u6613" }))
5303
+ }, { "additionalProperties": false }),
5304
+ async execute(_id, params) {
5305
+ return asText(await withJsonInput(params, (path) => runPy("sector_rotation.py", ["analyze", "--input", path])));
5306
+ }
5307
+ });
5308
+ api.registerTool({
5309
+ name: "backtest_rotation_allocation",
5310
+ description: "\u884C\u4E1A\u8F6E\u52A8\u914D\u6743\u5206\u9879\u56DE\u6D4B\uFF1A\u4EE3\u7801\u81EA\u52A8\u53D6\u884C\u60C5\uFF0C\u6216\u4F20\u660E\u786E\u5E01\u79CD/\u590D\u6743OHLCV\uFF1B\u76F8\u540C\u6BCF\u5468\u5168\u6C60\u4E70\u5356\u4E0B\u6BD4\u8F83\u7B49\u6743\u4E0E\u5386\u53F2\u914D\u6743\u3002\u6309\u5E02\u573A\u548C\u62A5\u4EF7\u5E01\u79CD\u72EC\u7ACB\u8D44\u91D1\uFF0C\u652F\u6301A/HK/US/JP/KR/TW\uFF1B\u4E0D\u662F\u5B8C\u6574\u8BBA\u6587\u6DF1\u5EA6\u6A21\u578B\u6216\u8DE8\u5E01\u79CD\u5171\u4EAB\u8D44\u91D1\u56DE\u6D4B\u3002",
5311
+ parameters: typebox_exports.Object({
5312
+ symbols: typebox_exports.Optional(typebox_exports.Array(typebox_exports.String({ "description": "\u552F\u4E00\u80A1\u7968/ETF\u4EE3\u7801\uFF0C\u6309\u63D2\u4EF6\u4EE3\u7801\u683C\u5F0F" }), { "description": "1..20\u53EA\u81EA\u52A8\u53D6\u6570\uFF0C\u4E0Edatasets\u4E92\u65A5\uFF1B\u4EFB\u4E00\u5931\u8D25\u6574\u6C60\u505C\u6B62" })),
5313
+ datasets: typebox_exports.Optional(typebox_exports.Array(typebox_exports.Object({
5314
+ symbol: typebox_exports.String({ "description": "A\u516D\u4F4D\u4EE3\u7801/HK\u59822800.HK/US\u5982XLK/JP\u59821306.T/KR\u5982069500.KS/TW\u59820050.TW" }),
5315
+ currency: typebox_exports.String({ "description": "\u5DF2\u6838\u5B9E\u62A5\u4EF7\u5E01\u79CD\u4E09\u5B57\u6BCD\u4EE3\u7801\uFF0C\u4E0D\u6309\u4E0A\u5E02\u5730\u731C\u6D4B" }),
5316
+ price_basis: typebox_exports.Union([typebox_exports.Literal("qfq"), typebox_exports.Literal("total_return"), typebox_exports.Literal("split_adjusted")], { "description": "\u5DF2\u6838\u5B9E\u4E14\u4E00\u81F4\u7684\u590D\u6743\u53E3\u5F84" }),
5317
+ bars: typebox_exports.Array(typebox_exports.Object({
5318
+ date: typebox_exports.String({ "description": "YYYY-MM-DD" }),
5319
+ open: typebox_exports.Number({ "description": "\u6709\u9650\u6B63OHLC\u4EF7\u683C\uFF1Bvolume\u975E\u8D1F" }),
5320
+ high: typebox_exports.Number({ "description": "\u6709\u9650\u6B63OHLC\u4EF7\u683C\uFF1Bvolume\u975E\u8D1F" }),
5321
+ low: typebox_exports.Number({ "description": "\u6709\u9650\u6B63OHLC\u4EF7\u683C\uFF1Bvolume\u975E\u8D1F" }),
5322
+ close: typebox_exports.Number({ "description": "\u6709\u9650\u6B63OHLC\u4EF7\u683C\uFF1Bvolume\u975E\u8D1F" }),
5323
+ volume: typebox_exports.Number({ "description": "\u6709\u9650\u6B63OHLC\u4EF7\u683C\uFF1Bvolume\u975E\u8D1F" }),
5324
+ adjusted: typebox_exports.Optional(typebox_exports.String({ "description": "\u53EF\u9009\u9010bar\u590D\u6743\u6807\u8BB0\uFF0C\u5B58\u5728\u65F6\u5FC5\u987B\u4E0Eprice_basis\u4E00\u81F4" })),
5325
+ source: typebox_exports.Optional(typebox_exports.String({ "description": "\u53EF\u9009\u884C\u60C5\u6765\u6E90\u6807\u8BC6\uFF0C\u7528\u4E8E\u6570\u636E\u8986\u76D6\u62A5\u544A" }))
5326
+ }, { "additionalProperties": false }), { "description": "\u9012\u589E\u552F\u4E00daily OHLCV\uFF0C2..20000\u884C\uFF0C\u5305\u542B\u8BC4\u4F30\u524D\u9884\u70ED\uFF1B\u603B\u91CF<=100000" })
5327
+ }, { "additionalProperties": false }), { "description": "1..20\u4E2A\u79BB\u7EBF\u6570\u636E\u96C6\uFF0C\u4E0Esymbols\u4E92\u65A5\uFF1B8MiB JSON\u8F93\u5165\u4E0A\u9650" })),
5328
+ start_date: typebox_exports.String({ "description": "\u8BC4\u4F30\u8D77\u59CBYYYY-MM-DD\uFF1B\u8DE8\u8FB9\u754C\u5468\u4E0D\u4EA4\u6613\uFF0C\u4E0D\u80FD\u7528\u8BC4\u4F30\u671F\u6536\u76CA\u521D\u59CB\u5316\u914D\u6743" }),
5329
+ end_date: typebox_exports.String({ "description": "\u5DF2\u6536\u76D8\u622A\u6B62YYYY-MM-DD\uFF1B\u4E0D\u5B8C\u6574\u672B\u5468\u4E0D\u6210\u4EA4" }),
5330
+ history_start_date: typebox_exports.Optional(typebox_exports.String({ "description": "\u4EC5\u4EE3\u7801\u6A21\u5F0F\uFF1B\u9ED8\u8BA4\u8BC4\u4F30\u524D\u4E00\u5E74\uFF0C\u5FC5\u987B\u65E9\u4E8Estart_date" })),
5331
+ warmup_weeks: typebox_exports.Optional(typebox_exports.Number({ "description": "\u8BC4\u4F30\u524D\u5B8C\u6574\u5386\u53F2\u4EA4\u6613\u54681..104\u6574\u6570\uFF0C\u9ED8\u8BA426\uFF1B\u4E0D\u8DB3\u505C\u6B62" })),
5332
+ capital: typebox_exports.Optional(typebox_exports.Number({ "description": "\u6BCF\u5E02\u573A/\u62A5\u4EF7\u5E01\u79CD\u5206\u7EC4\u72EC\u7ACB\u521D\u59CB\u8D44\u91D1\uFF0C\u9ED8\u8BA41000000\uFF0C\u4E0D\u662F\u6DF7\u5408\u6C60\u603B\u989D" })),
5333
+ cost_per_side: typebox_exports.Optional(typebox_exports.Number({ "description": "\u6BCF\u8FB9\u6BD4\u4F8B\u6210\u672C0..0.1\uFF0C\u9ED8\u8BA40.001\uFF1B\u4E70\u5165\u9884\u7B97\u542B\u8D39\u7528" }))
5334
+ }, { "additionalProperties": false }),
5335
+ async execute(_id, params) {
5336
+ return asText(await withJsonInput(params, (path) => runPy("sector_rotation.py", ["backtest", "--input", path])));
5337
+ }
5338
+ });
5266
5339
  api.registerTool({
5267
5340
  name: "analyze_trend_strategy",
5268
5341
  description: "\u6708\u5EA6\u53F3\u4FA7\u8D8B\u52BF\u7B56\u7565\uFF1A\u76F4\u63A5\u4F20\u540C\u5E02\u573AA/HK/US\u80A1\u7968\u4EE3\u7801\uFF0C\u81EA\u52A8\u83B7\u53D6\u65E5K\uFF0C\u8F93\u51FA\u6708\u5EA6\u65B0\u5165/\u6301\u6709/\u9000\u51FA\u4E0E\u9006\u6CE2\u52A8\u9884\u7B97\u3002as_of\u9ED8\u8BA4\u6700\u8FD1\u5DF2\u6536\u76D8\u4EA4\u6613\u65E5\uFF0C\u6301\u4ED3\u53EF\u9009\u4E14\u4E0D\u4FDD\u5B58\uFF0C\u4E0D\u81EA\u52A8\u4E0B\u5355\uFF1B\u8DE8\u6837\u672C\u4ECDnot_validated\u3002",
@@ -5697,8 +5770,11 @@ var index_default = definePluginEntry({
5697
5770
  });
5698
5771
  api.registerTool({
5699
5772
  name: "render_stock_report",
5700
- description: "\u80A1\u7968\u5206\u6790\u62A5\u544A\u6E32\u67D3 \u2014 \u5C06\u7ED3\u6784\u5316\u62A5\u544A JSON \u901A\u8FC7 j2 \u6A21\u677F\u6E32\u67D3\u4E3A Markdown\u3002report \u5B57\u6BB5\u4EE5 skill \u63D0\u4F9B\u7684 report_schema \u4E3A\u51C6\uFF0C\u4E0D\u8981\u81EA\u9020\u5B57\u6BB5\u3002template: brief|full\u3002\u5168\u90E8\u5206\u6790\u5B8C\u6210\u540E\u7684\u6700\u540E\u4E00\u6B65\u8C03\u7528\u3002\u4EC5\u6E32\u67D3\uFF0C\u4E0D\u4FDD\u5B58\u4E0D\u63A8\u9001\u3002report\u53EF\u5305\u542Bevidence\uFF08path/label/value/source/as_of\uFF09\u4E0Edata_quality\uFF08flags/limitations\uFF09\uFF0Cbrief/full\u5747\u5C55\u793A\u8BC1\u636E\u548C\u9650\u5236\u3002",
5773
+ description: "\u80A1\u7968\u5206\u6790\u62A5\u544A\u6E32\u67D3 \u2014 \u5C06\u7ED3\u6784\u5316\u62A5\u544A JSON \u901A\u8FC7 j2 \u6A21\u677F\u6E32\u67D3\u4E3A Markdown\u3002report \u5B57\u6BB5\u4EE5 skill \u63D0\u4F9B\u7684 report_schema \u4E3A\u51C6\uFF0C\u4E0D\u8981\u81EA\u9020\u5B57\u6BB5\u3002template: brief|full\u3002\u5168\u90E8\u5206\u6790\u5B8C\u6210\u540E\u7684\u6700\u540E\u4E00\u6B65\u8C03\u7528\u3002\u9ED8\u8BA4\u4EC5\u6E32\u67D3\uFF1Bfinal=true\u8868\u793A\u6700\u7EC8\u56DE\u590D\uFF0C\u652F\u6301\u76F4\u51FA\u7684\u5BBF\u4E3B\u53EF\u539F\u6837\u4EA4\u4ED8\u5E76\u7ED3\u675F\u672C\u8F6E\u3002report\u53EF\u5305\u542Bevidence\uFF08path/label/value/source/as_of\uFF09\u4E0Edata_quality\uFF08flags/limitations\uFF09\uFF0Cbrief/full\u5747\u5C55\u793A\u8BC1\u636E\u548C\u9650\u5236\u3002",
5774
+ canDeliverSourceReply: true,
5775
+ catalogMode: "direct-only",
5701
5776
  parameters: typebox_exports.Object({
5777
+ final: typebox_exports.Optional(typebox_exports.Boolean({ description: "\u6700\u7EC8\u56DE\u590D\u6807\u8BB0\uFF0C\u9ED8\u8BA4false\uFF1B\u4FDD\u5B58\u9644\u4EF6\u6216\u7EE7\u7EED\u5206\u6790\u65F6\u4FDD\u6301false" })),
5702
5778
  report: typebox_exports.Object({}, { additionalProperties: true, description: "\u7ED3\u6784\u5316\u80A1\u7968\u62A5\u544A JSON" }),
5703
5779
  template: typebox_exports.Optional(
5704
5780
  typebox_exports.Union([typebox_exports.Literal("brief"), typebox_exports.Literal("full")], {
@@ -5713,15 +5789,19 @@ var index_default = definePluginEntry({
5713
5789
  "--template",
5714
5790
  params.template ?? "full",
5715
5791
  "--input-b64",
5716
- b64
5792
+ b64,
5793
+ ...params.final ? ["--final"] : []
5717
5794
  ]);
5718
- return asText(out);
5795
+ return asReport(out);
5719
5796
  }
5720
5797
  });
5721
5798
  api.registerTool({
5722
5799
  name: "render_market_report",
5723
- description: "\u5927\u76D8\u590D\u76D8\u62A5\u544A\u6E32\u67D3 \u2014 \u5C06\u7ED3\u6784\u5316\u62A5\u544A JSON \u901A\u8FC7 j2 \u6A21\u677F\u6E32\u67D3\u4E3A Markdown\u3002report \u5B57\u6BB5\u4EE5 schemas/market_review_schema.json \u4E3A\u51C6\u3002save=true \u65F6\u628A\u62A5\u544A\u5BF9\u8C61\u8FFD\u52A0\u5F52\u6863\u5230\u672C\u5730 JSONL\uFF08\u9ED8\u8BA4\u4E0D\u4FDD\u5B58\u4E0D\u63A8\u9001\uFF09\uFF0C\u7EB5\u5411\u5BF9\u6BD4\u7528 get_review_history",
5800
+ description: "\u5927\u76D8\u590D\u76D8\u62A5\u544A\u6E32\u67D3 \u2014 \u5C06\u7ED3\u6784\u5316\u62A5\u544A JSON \u901A\u8FC7 j2 \u6A21\u677F\u6E32\u67D3\u4E3A Markdown\u3002report \u5B57\u6BB5\u4EE5 schemas/market_review_schema.json \u4E3A\u51C6\u3002save=true \u65F6\u628A\u62A5\u544A\u5BF9\u8C61\u8FFD\u52A0\u5F52\u6863\u5230\u672C\u5730 JSONL\uFF08\u9ED8\u8BA4\u4E0D\u4FDD\u5B58\u4E0D\u63A8\u9001\uFF09\uFF0C\u7EB5\u5411\u5BF9\u6BD4\u7528 get_review_history\uFF1Bfinal=true\u4EC5\u7528\u4E8E\u6700\u7EC8\u56DE\u590D\uFF0C\u4FDD\u5B58\u9644\u4EF6\u6216\u7EE7\u7EED\u5206\u6790\u65F6\u4FDD\u6301false",
5801
+ canDeliverSourceReply: true,
5802
+ catalogMode: "direct-only",
5724
5803
  parameters: typebox_exports.Object({
5804
+ final: typebox_exports.Optional(typebox_exports.Boolean({ description: "\u6700\u7EC8\u56DE\u590D\u6807\u8BB0\uFF0C\u9ED8\u8BA4false\uFF1B\u4FDD\u5B58\u9644\u4EF6\u6216\u7EE7\u7EED\u5206\u6790\u65F6\u4FDD\u6301false" })),
5725
5805
  report: typebox_exports.Object({}, { additionalProperties: true, description: "\u7ED3\u6784\u5316\u5E02\u573A\u590D\u76D8" }),
5726
5806
  save: typebox_exports.Optional(typebox_exports.Boolean({ description: "\u662F\u5426\u5F52\u6863\u5230\u672C\u5730\u590D\u76D8 JSONL \u5B58\u50A8\uFF0C\u9ED8\u8BA4 false" }))
5727
5807
  }),
@@ -5731,9 +5811,10 @@ var index_default = definePluginEntry({
5731
5811
  "market",
5732
5812
  "--input-b64",
5733
5813
  b64,
5814
+ ...params.final ? ["--final"] : [],
5734
5815
  ...params.save ? ["--save"] : []
5735
5816
  ]);
5736
- return asText(out);
5817
+ return asReport(out);
5737
5818
  }
5738
5819
  });
5739
5820
  api.registerTool({
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { withJsonInput } from "../scripts/json-input.mjs";
2
2
  import { commandTimeout } from "../scripts/cli-timeout.mjs";
3
+ import { pythonEnvironmentRoot, venvPythonPath } from "../scripts/venv-python.mjs";
3
4
  import { existsSync } from "node:fs";
4
5
  import * as cp from "node:child_process";
5
6
  import { promisify } from "node:util";
@@ -47,6 +48,20 @@ function asText(text: string) {
47
48
  return { content: [{ type: "text" as const, text }] };
48
49
  }
49
50
 
51
+ function asReport(text: string) {
52
+ const result = asText(text);
53
+ try {
54
+ const report = JSON.parse(text);
55
+ if (report?.final === true && !Object.hasOwn(report, "error") && report.format === "markdown"
56
+ && typeof report.content === "string" && report.content.trim()) {
57
+ return { ...result, details: { sourceReply: { text: report.content } } };
58
+ }
59
+ } catch {
60
+ // Invalid output stays visible to the model and must never end the turn.
61
+ }
62
+ return result;
63
+ }
64
+
50
65
  export default definePluginEntry({
51
66
  id: "stock-analysis",
52
67
  name: "Stock Analysis",
@@ -57,7 +72,7 @@ export default definePluginEntry({
57
72
  const repoRoot = api.rootDir || fallbackRoot;
58
73
  const runtimeToolsDir = existsSync(join(repoRoot, "tools")) ? join(repoRoot, "tools") : toolsDir;
59
74
  const pythonBin = () => {
60
- const venvPython = join(repoRoot, venvBin);
75
+ const venvPython = venvPythonPath(pythonEnvironmentRoot(repoRoot));
61
76
  if (existsSync(venvPython)) return venvPython;
62
77
  // In a staged dev checkout, the venv still lives at the repo root.
63
78
  const parentVenvPython = join(repoRoot, "..", venvBin);
@@ -851,6 +866,48 @@ export default definePluginEntry({
851
866
  },
852
867
  });
853
868
 
869
+ api.registerTool({
870
+ name: "calculate_rotation_metrics",
871
+ description: "行业轮动数值复核:输入真实MC-dropout样本和/或历史成交计数,计算集中度和原始配权;不训练模型、不生成买入信号,零历史不补默认值。",
872
+ parameters: Type.Object({prediction_samples: Type.Optional(Type.Array(Type.Object({symbol: Type.String({"description": "唯一标的标识"}),
873
+ samples: Type.Array(Type.Number({"description": "同一模型/标的/时间的有限线性输出,不是价格或收益率"}), {"description": "2..100000个真实MC-dropout预测;不得用随机数或动量代替"})}, {"additionalProperties": false}), {"description": "1..100组,总样本<=100000;和trade_counts至少提供一项"})),
874
+ trade_counts: Type.Optional(Type.Array(Type.Object({symbol: Type.String({"description": "唯一标的标识;代码别名须先统一"}),
875
+ buys: Type.Number({"description": "此前已平仓买入周数,非负整数"}),
876
+ wins: Type.Number({"description": "此前盈利平仓次数,0..buys"}),
877
+ streak: Type.Number({"description": "此前连续盈利交易次数,0..wins"})}, {"additionalProperties": false}), {"description": "1..100条已完成历史成交计数;不得使用决策后的交易"}))}, {"additionalProperties": false}),
878
+ async execute(_id, params) {
879
+ return asText(await withJsonInput(params, (path: string) =>
880
+ runPy("sector_rotation.py", ["analyze", "--input", path])));
881
+ },
882
+ });
883
+
884
+ api.registerTool({
885
+ name: "backtest_rotation_allocation",
886
+ description: "行业轮动配权分项回测:代码自动取行情,或传明确币种/复权OHLCV;相同每周全池买卖下比较等权与历史配权。按市场和报价币种独立资金,支持A/HK/US/JP/KR/TW;不是完整论文深度模型或跨币种共享资金回测。",
887
+ parameters: Type.Object({symbols: Type.Optional(Type.Array(Type.String({"description": "唯一股票/ETF代码,按插件代码格式"}), {"description": "1..20只自动取数,与datasets互斥;任一失败整池停止"})),
888
+ datasets: Type.Optional(Type.Array(Type.Object({symbol: Type.String({"description": "A六位代码/HK如2800.HK/US如XLK/JP如1306.T/KR如069500.KS/TW如0050.TW"}),
889
+ currency: Type.String({"description": "已核实报价币种三字母代码,不按上市地猜测"}),
890
+ price_basis: Type.Union([Type.Literal("qfq"), Type.Literal("total_return"), Type.Literal("split_adjusted")], {"description": "已核实且一致的复权口径"}),
891
+ bars: Type.Array(Type.Object({date: Type.String({"description": "YYYY-MM-DD"}),
892
+ open: Type.Number({"description": "有限正OHLC价格;volume非负"}),
893
+ high: Type.Number({"description": "有限正OHLC价格;volume非负"}),
894
+ low: Type.Number({"description": "有限正OHLC价格;volume非负"}),
895
+ close: Type.Number({"description": "有限正OHLC价格;volume非负"}),
896
+ volume: Type.Number({"description": "有限正OHLC价格;volume非负"}),
897
+ adjusted: Type.Optional(Type.String({"description": "可选逐bar复权标记,存在时必须与price_basis一致"})),
898
+ source: Type.Optional(Type.String({"description": "可选行情来源标识,用于数据覆盖报告"}))}, {"additionalProperties": false}), {"description": "递增唯一daily OHLCV,2..20000行,包含评估前预热;总量<=100000"})}, {"additionalProperties": false}), {"description": "1..20个离线数据集,与symbols互斥;8MiB JSON输入上限"})),
899
+ start_date: Type.String({"description": "评估起始YYYY-MM-DD;跨边界周不交易,不能用评估期收益初始化配权"}),
900
+ end_date: Type.String({"description": "已收盘截止YYYY-MM-DD;不完整末周不成交"}),
901
+ history_start_date: Type.Optional(Type.String({"description": "仅代码模式;默认评估前一年,必须早于start_date"})),
902
+ warmup_weeks: Type.Optional(Type.Number({"description": "评估前完整历史交易周1..104整数,默认26;不足停止"})),
903
+ capital: Type.Optional(Type.Number({"description": "每市场/报价币种分组独立初始资金,默认1000000,不是混合池总额"})),
904
+ cost_per_side: Type.Optional(Type.Number({"description": "每边比例成本0..0.1,默认0.001;买入预算含费用"}))}, {"additionalProperties": false}),
905
+ async execute(_id, params) {
906
+ return asText(await withJsonInput(params, (path: string) =>
907
+ runPy("sector_rotation.py", ["backtest", "--input", path])));
908
+ },
909
+ });
910
+
854
911
  api.registerTool({
855
912
  name: "analyze_trend_strategy",
856
913
  description: "月度右侧趋势策略:直接传同市场A/HK/US股票代码,自动获取日K,输出月度新入/持有/退出与逆波动预算。as_of默认最近已收盘交易日,持仓可选且不保存,不自动下单;跨样本仍not_validated。",
@@ -1343,8 +1400,11 @@ export default definePluginEntry({
1343
1400
  api.registerTool({
1344
1401
  name: "render_stock_report",
1345
1402
  description:
1346
- "股票分析报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 skill 提供的 report_schema 为准,不要自造字段。template: brief|full。全部分析完成后的最后一步调用。仅渲染,不保存不推送。report可包含evidence(path/label/value/source/as_of)与data_quality(flags/limitations),brief/full均展示证据和限制。",
1403
+ "股票分析报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 skill 提供的 report_schema 为准,不要自造字段。template: brief|full。全部分析完成后的最后一步调用。默认仅渲染;final=true表示最终回复,支持直出的宿主可原样交付并结束本轮。report可包含evidence(path/label/value/source/as_of)与data_quality(flags/limitations),brief/full均展示证据和限制。",
1404
+ canDeliverSourceReply: true,
1405
+ catalogMode: "direct-only",
1347
1406
  parameters: Type.Object({
1407
+ final: Type.Optional(Type.Boolean({ description: "最终回复标记,默认false;保存附件或继续分析时保持false" })),
1348
1408
  report: Type.Object({}, { additionalProperties: true, description: "结构化股票报告 JSON" }),
1349
1409
  template: Type.Optional(
1350
1410
  Type.Union([Type.Literal("brief"), Type.Literal("full")], {
@@ -1360,16 +1420,20 @@ export default definePluginEntry({
1360
1420
  params.template ?? "full",
1361
1421
  "--input-b64",
1362
1422
  b64,
1423
+ ...(params.final ? ["--final"] : []),
1363
1424
  ]);
1364
- return asText(out);
1425
+ return asReport(out);
1365
1426
  },
1366
1427
  });
1367
1428
 
1368
1429
  api.registerTool({
1369
1430
  name: "render_market_report",
1370
1431
  description:
1371
- "大盘复盘报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 schemas/market_review_schema.json 为准。save=true 时把报告对象追加归档到本地 JSONL(默认不保存不推送),纵向对比用 get_review_history",
1432
+ "大盘复盘报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 schemas/market_review_schema.json 为准。save=true 时把报告对象追加归档到本地 JSONL(默认不保存不推送),纵向对比用 get_review_history;final=true仅用于最终回复,保存附件或继续分析时保持false",
1433
+ canDeliverSourceReply: true,
1434
+ catalogMode: "direct-only",
1372
1435
  parameters: Type.Object({
1436
+ final: Type.Optional(Type.Boolean({ description: "最终回复标记,默认false;保存附件或继续分析时保持false" })),
1373
1437
  report: Type.Object({}, { additionalProperties: true, description: "结构化市场复盘" }),
1374
1438
  save: Type.Optional(Type.Boolean({ description: "是否归档到本地复盘 JSONL 存储,默认 false" })),
1375
1439
  }),
@@ -1379,9 +1443,10 @@ export default definePluginEntry({
1379
1443
  "market",
1380
1444
  "--input-b64",
1381
1445
  b64,
1446
+ ...(params.final ? ["--final"] : []),
1382
1447
  ...(params.save ? ["--save"] : []),
1383
1448
  ]);
1384
- return asText(out);
1449
+ return asReport(out);
1385
1450
  },
1386
1451
  });
1387
1452
 
@@ -71,7 +71,9 @@
71
71
  "analyze_event_study",
72
72
  "validate_backtest",
73
73
  "compare_analysis_snapshots",
74
- "analyze_trend_strategy"
74
+ "analyze_trend_strategy",
75
+ "calculate_rotation_metrics",
76
+ "backtest_rotation_allocation"
75
77
  ]
76
78
  },
77
79
  "activation": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weaxs/openclaw-stock-analysis",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "OpenClaw plugin for stock analysis, screening, and strategy backtesting across A/HK/US/JP/KR/TW markets",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
@@ -13,8 +13,8 @@
13
13
  "minGatewayVersion": "2026.3.24-beta.2"
14
14
  },
15
15
  "build": {
16
- "openclawVersion": "2026.3.24-beta.2",
17
- "pluginSdkVersion": "2026.3.24-beta.2"
16
+ "openclawVersion": "2026.10.5",
17
+ "pluginSdkVersion": "2026.10.5"
18
18
  }
19
19
  },
20
20
  "scripts": {
@@ -27,6 +27,7 @@
27
27
  "README.md",
28
28
  "tools/",
29
29
  "skills/",
30
+ "!skills/**/__pycache__/**",
30
31
  "schemas/",
31
32
  "templates/",
32
33
  "strategies/",
@@ -1,17 +1,18 @@
1
1
  #!/usr/bin/env node
2
2
  import { execFileSync } from "node:child_process";
3
- import { existsSync } from "node:fs";
3
+ import { existsSync, mkdirSync, renameSync } from "node:fs";
4
4
  import { join } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
- import { venvPythonPath } from "./venv-python.mjs";
6
+ import { pythonEnvironmentRoot, venvPythonPath } from "./venv-python.mjs";
7
7
 
8
8
  // fileURLToPath (not URL.pathname) — the latter yields a bogus "/C:/..." path
9
9
  // on Windows, which would put the venv in the wrong place or fail outright.
10
10
  const pkgDir = fileURLToPath(new URL("..", import.meta.url));
11
- const venvDir = join(pkgDir, ".venv");
11
+ const envRoot = pythonEnvironmentRoot(pkgDir);
12
+ const venvDir = join(envRoot, ".venv");
12
13
  // Drive pip through the venv's own python (`-m pip`) instead of a pip/pip.exe
13
14
  // shim — resolves correctly on both POSIX (bin/python3) and Windows (Scripts/python.exe).
14
- const venvPython = venvPythonPath(pkgDir);
15
+ const venvPython = venvPythonPath(envRoot);
15
16
  const requirements = join(pkgDir, "tools", "requirements.txt");
16
17
 
17
18
  function run(bin, args) {
@@ -41,6 +42,12 @@ try {
41
42
  process.exit(1);
42
43
  }
43
44
 
45
+ if (envRoot !== pkgDir && existsSync(join(pkgDir, ".venv"))) {
46
+ // Preserve installed dependencies; never leave an old environment in captured source.
47
+ if (existsSync(venvDir)) throw new Error("Both legacy and current Python environments exist; move the legacy .venv outside the plugin package");
48
+ mkdirSync(envRoot, { recursive: true });
49
+ renameSync(join(pkgDir, ".venv"), venvDir);
50
+ }
44
51
  if (!existsSync(venvDir)) {
45
52
  run(python, ["-m", "venv", venvDir]);
46
53
  }
@@ -6,6 +6,13 @@ import { join } from "node:path";
6
6
 
7
7
  const isWin = process.platform === "win32";
8
8
 
9
+ // OpenClaw captures package source, including .venv symlinks; node_modules is excluded.
10
+ export function pythonEnvironmentRoot(rootDir) {
11
+ return existsSync(join(rootDir, "openclaw.plugin.json"))
12
+ ? join(rootDir, "node_modules", ".stock-analysis-python")
13
+ : rootDir;
14
+ }
15
+
9
16
  // Windows venvs ship Scripts/python.exe (there is no python3); POSIX venvs ship bin/python3.
10
17
  export function venvPythonPath(rootDir) {
11
18
  return join(rootDir, ".venv", isWin ? "Scripts" : "bin", isWin ? "python.exe" : "python3");
@@ -5,6 +5,8 @@ description: 事件驱动分析:评估公告、业绩、政策、订单或资
5
5
 
6
6
  # 事件驱动分析
7
7
 
8
+ 批量研究、子任务分工或报告文件交付时,必须读取[宿主研究与报告交付](../stock-analysis/references/host-workflows.md),使用当前宿主实际提供的能力。
9
+
8
10
  ## 无状态分析扩展
9
11
 
10
12
  历史事件窗口验证或公告原文变化分析时,MUST 读取[无状态研究工具](../stock-analysis/references/stateless-analysis.md),使用 analyze_event_study/analyze_disclosure_changes。事件日期、盘前盘后、价格窗口和正文覆盖必须核实。
@@ -63,7 +63,7 @@ gather 工具返回以下字段:
63
63
 
64
64
  ## 输出
65
65
 
66
- 按 [市场复盘 schema](../../schemas/market_review_schema.json) 构建报告对象,再调用 `render_market_report`。缺失的市场数据保持为空并在 `risks` 中说明,不用定性文字补齐。
66
+ 按 [市场复盘 schema](../../schemas/market_review_schema.json) 构建报告对象,再调用 `render_market_report`。缺失的市场数据保持为空并在 `risks` 中说明,不用定性文字补齐。报告文件/连接器交付或分工研究时,必须读取[宿主研究与报告交付](../stock-analysis/references/host-workflows.md);本轮仅剩回复时才传 `final=true`,否则保持默认 false。
67
67
 
68
68
  ## 多市场支持
69
69
 
@@ -0,0 +1,57 @@
1
+ ---
2
+ name: sector-rotation
3
+ description: 行业ETF轮动研究:核对SSRN 4280640论文规则,复核预测分布集中度与历史配权,评估轮动对现有策略的增益。提供确定性计算与配权分项回测,不生成深度模型预测或实盘买卖计划。
4
+ ---
5
+
6
+ # 行业 ETF 轮动研究
7
+
8
+ 用于研究一组行业 ETF 的周度轮动,以及它是否值得补充现有趋势、题材或组合策略。市场由用户指定,允许跨市场;论文的美国样本不构成插件市场限制。
9
+
10
+ 实现状态:**共享Python数值计算与配权分项回测已实现,论文完整训练与信号模型未实现**。MUST 将“原文已披露规则”“尚未披露的实现细节”“插件的跨市场扩展”分别说明。MUST NOT 声称已经严格复现论文或重现其收益。
11
+
12
+ ## 1. 明确要改善什么
13
+
14
+ - 行业配置:比较不同 ETF 的取舍、空仓和资金分配。这是与现有能力最直接的补充。
15
+ - 热点题材:`hot-theme` 判断板块强度与催化,不能用本论文证明其预测能力得到改善。
16
+ - 个股趋势:`bull-trend` 的月度持有与本论文的周内清仓时钟不同。MUST NOT 自动把轮动筛选叠加为个股买入过滤器。
17
+ - 单股回测:`strategy-backtest` 的单标的 YAML 不能代表跨 ETF 共享资金组合。
18
+
19
+ 仅问是否有用时,结合上述区别回答;无独立对照证据时,结论为“有能力补充价值,收益增益未证实”。
20
+
21
+ ## 2. 核对论文与数据
22
+
23
+ 研究具体规则、复现、模型预测或交易时,MUST 先读取 [论文规则、缺项与输入格式](references/strategy_rules.md)。严格复现所需的损失函数或参数缺失时,列出缺项,停止生成模型信号;MUST NOT 自选参数并冠以原策略名称。
24
+
25
+ 用户提供代码即可通过现有 `get_kline` 获取整个池的 daily OHLCV,按研究区间加预热历史;行业归属、ETF身份、币种或复权未知时明确标记。没有代码时才需要确定标的池。MUST 保留整池成员、来源、实际区间及缺失项,不能静默删掉失败成员改变横截面。
26
+
27
+ 跨市场时,分别记录交易所、时区、币种、当地交易日和周末决策截止时间;只使用截止时已发布的数据。共同资金研究还需要历史汇率、换汇成本及结算约束,数据缺失时停止组合收益计算。MUST NOT 将不同币种净值相加、把后收盘市场的数据用于早开盘市场的历史订单,或把本地周线按日期直接拼接后宣称无前视。
28
+
29
+ ## 3. 复核已披露计算
30
+
31
+ 已有模型生成的 MC-dropout 样本或历史成交计数时,调用 `calculate_rotation_metrics(prediction_samples?, trade_counts?)`。Agent负责提供有来源的输入和解释结果,MUST 使用工具计算,不临场手算或重写算子。交易计数可直接复用分项回测返回的 `trade_counts`;缺原模型预测时,MUST NOT 用动量、随机数或语言模型猜测代替。
32
+
33
+ - `confidence`:复核预测值在“中位数 ± 总体标准差”内的经验占比。
34
+ - `allocation`:复核 `1 + wins/buys + streak/(wins+1)` 原始配权分数。
35
+
36
+ 输入格式、样本来源与零历史边界见参考。所有算子以共享 `tools/sector_rotation.py` 为唯一实现,四宿主转发同一JSON。skill里的 `scripts/rotation_metrics.py` 仅保留旧调用兼容入口;没有另一套公式。没有模型训练、账户存储或下单功能。
37
+
38
+ MUST 同时报 `paper_reproduction=partial_calculations_only`。集中度不是盈利概率;原始配权分数不是目标权重、股数或已确认可用资金。两项计算都不能独立决定买入。
39
+
40
+ ## 4. 运行配权分项回测
41
+
42
+ MUST 先读取 [计算与回测接口](references/computation.md),再调用 `backtest_rotation_allocation`。用户给代码时使用 `symbols` 自动获取行情与报价币种;已有核实的行情才传 `datasets`。两种模式互斥,不能要求正常用户手工准备行情文件。
43
+
44
+ 该工具在相同全池周内买卖下比较等权与历史配权,自动计数、费用扣除、净值和回撤;MUST NOT 让Agent自行计算回测收益。按交易所与报价币种分组,每组独立初始资金;报告每组实际区间、跳过周、费用、两组表现及差值。混合代码池不等于跨币种共享资金组合。任一取数失败、复权/价格检查失败或评估期完整交易周缺bar/零成交量则停止,不缩小股票池,不按事后数据完整性选择交易周。预热不足不能用评估期收益补齐初始化。
45
+
46
+ 该分项入口不需要缺失的神经网络,也不能替代它。MUST 同时报 `paper_reproduction=allocation_ablation_only`,并把历史分数归一化和预热约定与原文已披露公式分开。
47
+
48
+ ## 5. 评估相对现有策略的增益
49
+
50
+ 取得完整可执行模型后,按预先固定的区间、标的池、初始资金、成交时点和成本比较:
51
+
52
+ 1. 基准持有、行业 ETF 等权、简单动量轮动、论文模型;现有月度策略仅在标的池和资金口径可比时加入。
53
+ 2. 保持年度训练与周度更新的时间边界,归一化和阈值选择均只看当时已知数据;额外报告论文发表后的独立区间。
54
+ 3. 分别比较净收益、最大回撤、换手、交易次数和资金占用;逐项关闭置信筛选、亏损过滤或动态配权,检验各部件实际贡献。
55
+ 4. 如需改善某个现有策略,另做“原策略 vs 加入该部件”的同条件对照,不能凭独立策略收益推定组合效果。
56
+
57
+ 完成时报告:已实现内容、缺项、对照条件和增益结论。没有完整模型或合格组合回测时,MUST 报“尚未验证”,不能用论文表3、单股回测相加或技术测试通过代替实际增益证据。
@@ -0,0 +1,83 @@
1
+ # 确定性计算与配权分项回测
2
+
3
+ 共享核心为 `tools/sector_rotation.py`。Pi、Hermes、OpenClaw、dsh 暴露相同的两个工具;Agent负责输入来源与结论,计算和资金递推均由Python完成。它们不包含尚未披露的神经网络训练或预测。
4
+
5
+ ## calculate_rotation_metrics
6
+
7
+ `prediction_samples` 与 `trade_counts` 至少提供一项。每组样本2..100000个有限数,全请求最多100000个;最多100组唯一标识。同一组必须属于同一模型、标的和决策时间,来源由调用方核实。
8
+
9
+ ```json
10
+ {
11
+ "prediction_samples": [{"symbol":"XLK","samples":[0.1,0.2,0.1,0.15]}],
12
+ "trade_counts": [{"symbol":"XLK","buys":4,"wins":3,"streak":2}]
13
+ }
14
+ ```
15
+
16
+ 返回逐标的 `confidence` 和/或 `allocation`,其中原始配权分数为2.25;`model_status=not_implemented_missing_specification`、`is_buy_signal=false`、`paper_reproduction=partial_calculations_only`。零历史和零方差边界见 [规则参考](strategy_rules.md)。不验证预测来源的真实性,不把集中度当作盈利概率。
17
+
18
+ ## backtest_rotation_allocation
19
+
20
+ 正常调用直接传股票/ETF代码,无须用户提供行情文件:
21
+
22
+ ```json
23
+ {
24
+ "symbols":["XLK","XLE","512000","512010","1306.T"],
25
+ "start_date":"2021-01-01",
26
+ "end_date":"2026-10-16",
27
+ "history_start_date":"2020-01-01",
28
+ "warmup_weeks":26,
29
+ "capital":1000000,
30
+ "cost_per_side":0.001
31
+ }
32
+ ```
33
+
34
+ | 字段 | 约束 |
35
+ |---|---|
36
+ | `symbols` | 1..20个插件格式代码;与datasets互斥,重复别名拒绝;任一成员取数失败整池停止 |
37
+ | `start_date/end_date` | 必填、有序的YYYY-MM-DD评估区间;截止日不得晚于当前UTC日期,不能使用未收盘bar;完整交易周才成交 |
38
+ | `history_start_date` | 仅代码模式,默认评估前一年;早于start_date,取数长度包括这段预热 |
39
+ | `warmup_weeks` | 评估前完整全池交易周,整数1..104,默认26;不足则停止,不延后起点冒充完整评估 |
40
+ | `capital` | 每个市场/报价币种分组的独立初始资金,默认1000000,0.01..1e12;不是混合池总资金 |
41
+ | `cost_per_side` | 假设的每边比例成本0..0.1,默认0.001;包含预算内买入成本及卖出成本,非交易所费率声明 |
42
+
43
+ A/HK/US/JP/KR/TW均按真实交易所日历取周内首末交易日。币种采用已核实的报价币种,不能依据市场猜测港股ETF一定报价HKD。跨市场或同市场多币种分成独立资金组,MUST NOT 把各组净值相加或称为完成了外汇/结算模拟。
44
+
45
+ 复核已有行情时,用 `datasets` 代替symbols,省略history_start_date。每项包含:
46
+
47
+ ```json
48
+ {
49
+ "symbol":"XLK",
50
+ "currency":"USD",
51
+ "price_basis":"total_return",
52
+ "bars":[
53
+ {"date":"2020-01-02","open":100,"high":102,"low":99,"close":101,"volume":1000}
54
+ ]
55
+ }
56
+ ```
57
+
58
+ 上例只展示单条格式,不能单独运行:每个数据集需2..20000行,并覆盖完整预热及评估交易周;最多20组、总行数不超过100000,JSON输入最大8 MiB。日期递增唯一,OHLC为有限正数且范围一致、volume非负;currency为调用方核实的三字母币种。price_basis限 `qfq/total_return/split_adjusted`,不能把unknown或原始价改标签冒充已复权。额外逐bar `adjusted` 若存在,须与数据集口径一致。
59
+
60
+ ### 固定分项实验
61
+
62
+ 1. 每周全池均买入,没有预测挑选;相同周内首个交易日开盘买入、最后交易日收盘卖出。
63
+ 2. 首次评估前必须有足够完整观察周。历史计数仅使用决策前已结束的周内交易,毛收益>0算赢,持平重置连胜;计数累计,不做未经披露的年度重置。
64
+ 3. 等权组均分预算;历史组对 `1+wins/buys+streak/(wins+1)` 分数归一化。预热、全池买入、归一化是此独立实验的约定,不宣称为作者的完整策略。
65
+ 4. 每笔预算含买入比例成本,按合成复权价格计算小数名义份额;逐日收盘盯市、末日扣卖出成本。费用、最终净值和收盘净值最大回撤由核心计算。
66
+ 5. 评估期完整交易周中任一成员缺bar或零成交量,整组回测停止;不能用周末才知道的数据完整性决定周初是否买入。预热期的不完整周不计观察次数、不删除成员。周跨越评估边界时跳过;首周缺前几天不算完整预热周。
67
+
68
+ 返回 `groups`:market/currency/symbols、实际period、price_basis/data_coverage、evaluated_weeks/skipped_weeks、决策日期和weights、最终trade_counts、equal_weight/historical_weight的净值与成本和指标、年化收益差。指标沿用共享回测函数的四位小数精度。历史计数包括预热,closed_trades仅包含评估期。
69
+
70
+ `paper_reproduction=allocation_ablation_only`。这不是ROC/MC/神经网络挑选后的组合,更不是现有月度策略的直接前后对照。无外汇、结算、整手、价格限制、盘口、税务或成交量容量模型;比例成本与成交时点只是该模拟假设。
71
+
72
+ ### 数据验收
73
+
74
+ 价格序列跳变大于1.5倍(或低于其倒数)、单日high/low大于1.5时,需要独立核查公司行动后再研究;这是一道保守审查阈值,不是认定真实市场不可能发生该涨跌。复权未知、日期/交易时段不符、未收盘或可疑价格会返回安全JSON错误,`market_data_requires_review` 表示整池未生成回测。检查通过也不证明来源无误。
75
+
76
+ ## CLI 与兼容入口
77
+
78
+ ```bash
79
+ python3 tools/sector_rotation.py analyze --input request.json
80
+ python3 tools/sector_rotation.py backtest --input request.json
81
+ ```
82
+
83
+ skill中的 `scripts/rotation_metrics.py confidence predictions.json` 与 `allocation counts.json` 保留兼容,转调同一核心。所有入口stdout为JSON,错误非零退出,不保存账户或提交订单。