@weaxs/openclaw-stock-analysis 0.2.1 → 0.2.3

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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -2
  3. package/dist/index.js +84 -20
  4. package/index.ts +92 -23
  5. package/openclaw.plugin.json +3 -0
  6. package/package.json +1 -1
  7. package/scripts/ci-resolve-llm-host.mjs +29 -0
  8. package/scripts/setup-python.mjs +7 -9
  9. package/skills/hot-theme/SKILL.md +3 -2
  10. package/skills/stock-screener/SKILL.md +1 -1
  11. package/skills/strategy-backtest/SKILL.md +9 -5
  12. package/strategies/examples/bottom_volume.yaml +5 -0
  13. package/strategies/examples/box_oscillation.yaml +5 -0
  14. package/strategies/examples/bull_trend.yaml +11 -3
  15. package/strategies/examples/dragon_head.yaml +4 -2
  16. package/strategies/examples/emotion_cycle.yaml +5 -0
  17. package/strategies/examples/ma_crossover.yaml +10 -3
  18. package/strategies/examples/rsi_oversold.yaml +5 -0
  19. package/strategies/examples/shrink_pullback.yaml +7 -1
  20. package/strategies/examples/volume_breakout.yaml +7 -1
  21. package/tools/_subproc.py +49 -0
  22. package/tools/alert_rules.py +19 -9
  23. package/tools/anomaly_detect.py +24 -35
  24. package/tools/backtest.py +180 -100
  25. package/tools/capabilities.py +4 -5
  26. package/tools/diagnostics.py +6 -6
  27. package/tools/gather.py +13 -9
  28. package/tools/import_parser.py +2 -5
  29. package/tools/market_regime.py +27 -35
  30. package/tools/market_review.py +56 -9
  31. package/tools/name_resolver.py +2 -5
  32. package/tools/pattern.py +2 -5
  33. package/tools/position_context.py +2 -5
  34. package/tools/report_renderer.py +10 -6
  35. package/tools/requirements.txt +2 -1
  36. package/tools/risk_screening.py +29 -23
  37. package/tools/screener.py +26 -16
  38. package/tools/search_intel.py +5 -13
  39. package/tools/signal_tracker.py +2 -6
  40. package/tools/stock_data.py +393 -100
  41. package/tools/technical.py +136 -40
  42. package/tools/trading_calendar.py +78 -18
  43. package/tools/volume_analysis.py +2 -5
  44. package/tools/watchlist.py +2 -5
  45. package/tools/watchlist_context.py +2 -5
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Weaxs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Stock Analysis — OpenClaw Plugin
2
2
 
3
- A股 / 港股 / 美股 / 日股 / 韩股 / 台股 行情、技术分析、筛选与回测。OpenClaw Gateway 加载后即可在对话中调用 48 个工具。
3
+ A股 / 港股 / 美股 / 日股 / 韩股 / 台股 行情、技术分析、筛选与回测。OpenClaw Gateway 加载后即可在对话中调用 51 个工具。
4
4
 
5
5
  ## 安装
6
6
 
@@ -17,7 +17,7 @@ npm install
17
17
 
18
18
  ### Python 运行时
19
19
 
20
- 工具实际逻辑在 `tools/*.py`,依赖 `akshare`、`pandas`、`numpy`、`requests`。仓库根 `package.json` 的 postinstall(`scripts/setup-python.mjs`)会自动建 `.venv` 并装好依赖,无需手工操作。前提:本机有 `python3 >= 3.9`。
20
+ 工具实际逻辑在 `tools/*.py`,依赖 `akshare`、`pandas`、`numpy`、`requests`。仓库根 `package.json` 的 postinstall(`scripts/setup-python.mjs`)会自动建 `.venv` 并装好依赖,无需手工操作。前提:本机有 `python3 >= 3.10`。
21
21
 
22
22
  如果 venv 没建起来(比如 Python 缺失),plugin 会回退到系统 `python3`,请自行 `pip install -r tools/requirements.txt`。
23
23
 
package/dist/index.js CHANGED
@@ -4436,19 +4436,9 @@ function __setExecutor(fn) {
4436
4436
  }
4437
4437
  var here = dirname(fileURLToPath(import.meta.url));
4438
4438
  var toolsDir = existsSync(join(here, "tools")) ? join(here, "tools") : join(here, "..", "tools");
4439
- var repoRoot = dirname(toolsDir);
4439
+ var fallbackRoot = dirname(toolsDir);
4440
4440
  var isWin = process.platform === "win32";
4441
4441
  var venvBin = join(".venv", isWin ? "Scripts" : "bin", isWin ? "python.exe" : "python3");
4442
- var venvPython = join(repoRoot, venvBin);
4443
- var parentVenvPython = join(repoRoot, "..", venvBin);
4444
- function pythonBin() {
4445
- if (existsSync(venvPython)) return venvPython;
4446
- if (existsSync(parentVenvPython)) return parentVenvPython;
4447
- return isWin ? "python" : "python3";
4448
- }
4449
- async function runPy(script, args) {
4450
- return executor(pythonBin(), [join(toolsDir, script), ...args]);
4451
- }
4452
4442
  function asText(text) {
4453
4443
  return { content: [{ type: "text", text }] };
4454
4444
  }
@@ -4457,6 +4447,15 @@ var index_default = definePluginEntry({
4457
4447
  name: "Stock Analysis",
4458
4448
  description: "Stock analysis, screening, and strategy backtesting across A/HK/US/JP/KR/TW markets",
4459
4449
  register(api) {
4450
+ const repoRoot = api.rootDir || fallbackRoot;
4451
+ const pythonBin = () => {
4452
+ const venvPython = join(repoRoot, venvBin);
4453
+ if (existsSync(venvPython)) return venvPython;
4454
+ const parentVenvPython = join(repoRoot, "..", venvBin);
4455
+ if (existsSync(parentVenvPython)) return parentVenvPython;
4456
+ return isWin ? "python" : "python3";
4457
+ };
4458
+ const runPy = (script, args) => executor(pythonBin(), [join(toolsDir, script), ...args]);
4460
4459
  api.registerTool({
4461
4460
  name: "get_kline",
4462
4461
  description: "\u83B7\u53D6\u80A1\u7968K\u7EBF\u6570\u636E\uFF08OHLCV\uFF09\u3002\u652F\u6301A\u80A1\uFF08\u5982600519\uFF09\u3001\u6E2F\u80A1\uFF08\u598200700.HK\uFF09\u3001\u7F8E\u80A1\uFF08\u5982AAPL\uFF09\u3001\u65E5\u80A1\uFF08\u59827203.T\uFF09\u3001\u97E9\u80A1\uFF08\u5982005930.KS\uFF09\u3001\u53F0\u80A1\uFF08\u59822330.TW\uFF09\u53CAA\u80A1ETF\u3002\u9700\u8981\u539F\u59CB\u5386\u53F2\u4EF7\u683C\u81EA\u884C\u8BA1\u7B97\u6216\u753B\u56FE\u65F6\u7528\uFF1B\u53EA\u95EE\u6307\u6807\u7528 get_technical_analysis\uFF0C\u53EA\u95EE\u73B0\u4EF7\u7528 get_quote",
@@ -4486,7 +4485,7 @@ var index_default = definePluginEntry({
4486
4485
  });
4487
4486
  api.registerTool({
4488
4487
  name: "get_quote",
4489
- description: "\u83B7\u53D6\u80A1\u7968\u5B9E\u65F6\u884C\u60C5\u62A5\u4EF7\uFF08\u73B0\u4EF7\u3001\u6DA8\u8DCC\u5E45\u3001\u91CF\u6BD4\u7B49\uFF09\u3002\u652F\u6301A\u80A1\u3001\u6E2F\u80A1\u3001\u7F8E\u80A1\u3001\u65E5\u80A1\u3001\u97E9\u80A1\u3001\u53F0\u80A1\uFF1B\u975E\u4EA4\u6613\u65F6\u6BB5\u8FD4\u56DE\u6700\u8FD1\u4EA4\u6613\u65E5\u6536\u76D8\u4EF7\u5E76\u4EE5 as_of/stale \u6807\u6CE8",
4488
+ description: "\u83B7\u53D6\u80A1\u7968\u5B9E\u65F6\u884C\u60C5\u62A5\u4EF7\uFF08\u73B0\u4EF7\u3001\u6DA8\u8DCC\u5E45\u3001\u91CF\u6BD4\u7B49\uFF09\u3002\u652F\u6301A\u80A1\u3001\u6E2F\u80A1\u3001\u7F8E\u80A1\u3001\u65E5\u80A1\u3001\u97E9\u80A1\u3001\u53F0\u80A1\uFF1B\u975E\u4EA4\u6613\u65F6\u6BB5\u8FD4\u56DE\u6700\u8FD1\u4EA4\u6613\u65E5\u6536\u76D8\u4EF7\u5E76\u4EE5 as_of/stale \u6807\u6CE8\uFF1BETF \u7684 premium_discount_rate \u4E3A\u6B63=\u6EA2\u4EF7\u3001\u8D1F=\u6298\u4EF7",
4490
4489
  parameters: typebox_exports.Object({
4491
4490
  symbol: typebox_exports.String({ description: "\u80A1\u7968\u4EE3\u7801\uFF08A\u80A1\u5982600519\uFF0C\u7F8E\u80A1\u5982AAPL\uFF0C\u6E2F\u80A1\u598200700.HK\uFF09" })
4492
4491
  }),
@@ -4544,16 +4543,21 @@ var index_default = definePluginEntry({
4544
4543
  name: "get_financials",
4545
4544
  description: "\u83B7\u53D6\u80A1\u7968\u8D22\u52A1\u6458\u8981\u3002A\u80A1\u8FD4\u56DE\u6700\u65B0\u62A5\u544A\u671F ROE/\u6BDB\u5229\u7387/\u51C0\u5229\u7387/\u8D1F\u503A\u7387/\u6D41\u52A8\u6BD4\u7387\uFF1B\u5176\u4ED6\u5E02\u573A\u8FD4\u56DE PE/PB/\u5E02\u503C/\u8425\u6536/\u51C0\u5229\u6DA6\u7B49\u3002A\u80A1\u4F30\u503C\u3001\u6210\u957F\u548C\u5206\u7EA2\u7528 get_fundamental_context",
4546
4545
  parameters: typebox_exports.Object({
4547
- symbol: typebox_exports.String({ description: "\u80A1\u7968\u4EE3\u7801\uFF08A\u80A1\u5982600519\uFF0C\u7F8E\u80A1\u5982AAPL\uFF0C\u6E2F\u80A1\u598200700.HK\uFF09" })
4546
+ symbol: typebox_exports.String({ description: "\u80A1\u7968\u4EE3\u7801\uFF08A\u80A1\u5982600519\uFF0C\u7F8E\u80A1\u5982AAPL\uFF0C\u6E2F\u80A1\u598200700.HK\uFF09" }),
4547
+ periods: typebox_exports.Optional(
4548
+ typebox_exports.Number({ description: "\u8FD4\u56DE\u6700\u8FD1N\u4E2A\u62A5\u544A\u671F\u7684\u8D22\u52A1\u8D8B\u52BF\uFF0C\u9ED8\u8BA4 1\uFF08\u4EC5\u6700\u65B0\u4E00\u671F\uFF09" })
4549
+ )
4548
4550
  }),
4549
4551
  async execute(_id, params) {
4550
- const out = await runPy("stock_data.py", ["financials", params.symbol]);
4552
+ const args = ["financials", params.symbol];
4553
+ if (params.periods && params.periods > 1) args.push("--periods", String(params.periods));
4554
+ const out = await runPy("stock_data.py", args);
4551
4555
  return asText(out);
4552
4556
  }
4553
4557
  });
4554
4558
  api.registerTool({
4555
4559
  name: "get_technical_analysis",
4556
- description: "\u83B7\u53D6\u80A1\u7968\u6280\u672F\u9762\u5206\u6790\uFF08MA/MACD/RSI/BOLL/KDJ/\u6210\u4EA4\u91CF\u7B49\u6307\u6807 + 100\u5206\u7EFC\u5408\u8BC4\u5206 + 6\u7EA7\u4E70\u5356\u4FE1\u53F7 + \u8D8B\u52BF/\u504F\u79BB\u5EA6/\u652F\u6491\u538B\u529B\u4F4D\uFF09\u3002\u4E2A\u80A1\u6280\u672F\u9762\u7EFC\u5408\u5224\u65AD\u4E0E\u4E70\u5356\u65F6\u673A\u5206\u6790\u7684\u9996\u9009\uFF1B\u53EA\u8981\u5747\u7EBF\u6570\u503C\u6216\u81EA\u5B9A\u4E49\u5468\u671F\u7528 calculate_ma\uFF0C\u4E13\u95EE\u91CF\u4EF7\u7528 get_volume_analysis\uFF0C\u626B\u5F53\u65E5\u5F02\u52A8\u7528 detect_anomaly",
4560
+ description: "\u83B7\u53D6\u80A1\u7968\u6280\u672F\u9762\u5206\u6790\uFF08MA/MACD/RSI/BOLL/KDJ/\u6210\u4EA4\u91CF\u7B49\u6307\u6807 + 100\u5206\u7EFC\u5408\u8BC4\u5206 + 6\u7EA7\u4E70\u5356\u4FE1\u53F7 + \u8D8B\u52BF/\u504F\u79BB\u5EA6/\u652F\u6491\u538B\u529B\u4F4D\uFF1B\u591A\u5468\u671F\u5171\u632F\u65F6 resonance.direction \u2208 aligned_bullish/aligned_bearish/divergent\uFF09\u3002\u4E2A\u80A1\u6280\u672F\u9762\u7EFC\u5408\u5224\u65AD\u4E0E\u4E70\u5356\u65F6\u673A\u5206\u6790\u7684\u9996\u9009\uFF1B\u53EA\u8981\u5747\u7EBF\u6570\u503C\u6216\u81EA\u5B9A\u4E49\u5468\u671F\u7528 calculate_ma\uFF0C\u4E13\u95EE\u91CF\u4EF7\u7528 get_volume_analysis\uFF0C\u626B\u5F53\u65E5\u5F02\u52A8\u7528 detect_anomaly",
4557
4561
  parameters: typebox_exports.Object({
4558
4562
  symbol: typebox_exports.String({ description: "\u80A1\u7968\u4EE3\u7801\uFF08A\u80A1\u5982600519\uFF0C\u7F8E\u80A1\u5982AAPL\uFF0C\u6E2F\u80A1\u598200700.HK\uFF09" }),
4559
4563
  period: typebox_exports.Optional(
@@ -4564,17 +4568,24 @@ var index_default = definePluginEntry({
4564
4568
  ),
4565
4569
  count: typebox_exports.Optional(
4566
4570
  typebox_exports.Number({ description: "\u7528\u4E8E\u8BA1\u7B97\u6307\u6807\u7684K\u7EBF\u6761\u6570\uFF0C\u9ED8\u8BA4 120" })
4571
+ ),
4572
+ periods: typebox_exports.Optional(
4573
+ typebox_exports.String({
4574
+ description: '\u591A\u5468\u671F\u5171\u632F\u5206\u6790\uFF0C\u9017\u53F7\u5206\u9694\u7684\u5468\u671F\u5217\u8868\uFF0C\u5982 "daily,weekly"\uFF1B\u4E0D\u4F20\u5219\u53EA\u5206\u6790 period \u6307\u5B9A\u7684\u5355\u5468\u671F'
4575
+ })
4567
4576
  )
4568
4577
  }),
4569
4578
  async execute(_id, params) {
4570
- const out = await runPy("technical.py", [
4579
+ const args = [
4571
4580
  "analyze",
4572
4581
  params.symbol,
4573
4582
  "--period",
4574
4583
  params.period ?? "daily",
4575
4584
  "--count",
4576
4585
  String(params.count ?? 120)
4577
- ]);
4586
+ ];
4587
+ if (params.periods) args.push("--periods", params.periods);
4588
+ const out = await runPy("technical.py", args);
4578
4589
  return asText(out);
4579
4590
  }
4580
4591
  });
@@ -4783,6 +4794,42 @@ var index_default = definePluginEntry({
4783
4794
  return asText(out);
4784
4795
  }
4785
4796
  });
4797
+ api.registerTool({
4798
+ name: "get_margin_trading",
4799
+ description: "\u83B7\u53D6A\u80A1\u4E2A\u80A1\u878D\u8D44\u878D\u5238\u660E\u7EC6\uFF08\u878D\u8D44\u4F59\u989D/\u878D\u8D44\u4E70\u5165\u989D/\u878D\u5238\u4F59\u91CF\u7B49\uFF0C\u4E0A\u4EA4\u6240/\u6DF1\u4EA4\u6240\u5B98\u65B9\u6570\u636E\u6309\u4EA4\u6613\u6240\u5206\u6D41\uFF09\uFF0C\u8FD4\u56DE\u6700\u8FD1N\u4E2A\u4EA4\u6613\u65E5\u5E8F\u5217\uFF08\u6700\u65B0\u5728\u524D\uFF09\u3002\u91D1\u989D\u5355\u4F4D\u4E3A\u5143\uFF0Cshort_*_shares \u5355\u4F4D\u4E3A\u80A1\u3002\u4EC5\u4E24\u878D\u6807\u7684\u6709\u6570\u636E\uFF0C\u975E\u4E24\u878D\u6807\u7684\u8FD4\u56DE\u9519\u8BEF",
4800
+ parameters: typebox_exports.Object({
4801
+ symbol: typebox_exports.String({ description: "A\u80A1\u80A1\u7968\u4EE3\u7801\uFF0C\u5982 600519" }),
4802
+ days: typebox_exports.Optional(
4803
+ typebox_exports.Number({ description: "\u8FD4\u56DE\u6700\u8FD1N\u4E2A\u4EA4\u6613\u65E5\uFF0C\u9ED8\u8BA4 10" })
4804
+ )
4805
+ }),
4806
+ async execute(_id, params) {
4807
+ const out = await runPy("stock_data.py", [
4808
+ "margin_trading",
4809
+ params.symbol,
4810
+ "--days",
4811
+ String(params.days ?? 10)
4812
+ ]);
4813
+ return asText(out);
4814
+ }
4815
+ });
4816
+ api.registerTool({
4817
+ name: "get_northbound_flow",
4818
+ description: "\u83B7\u53D6\u5317\u5411\u8D44\u91D1\u5E02\u573A\u7EA7\u51C0\u4E70\u5165\u5E8F\u5217\uFF08\u6700\u65B0\u5728\u524D\uFF0C\u5355\u4F4D\u4EBF\u5143\uFF09\u3002\u6570\u636E\u53E3\u5F84\uFF1A\u4E1C\u65B9\u8D22\u5BCC\u6CAA\u6DF1\u6E2F\u901A\u5386\u53F2\u6570\u636E\uFF1B2024-08-16 \u8D77\u4EA4\u6613\u6240\u505C\u6B62\u62AB\u9732\u65E5\u5EA6\u51C0\u4E70\u989D\uFF0C\u4EC5 2024-08 \u4E4B\u524D\u5386\u53F2\u6570\u636E\u53EF\u67E5\uFF08\u8FD1\u671F\u5C0F days \u7A97\u53E3\u4F1A\u8FD4\u56DE\u505C\u62AB\u9519\u8BEF\uFF0C\u52A0\u5927 days \u53EF\u53D6\u5386\u53F2\uFF09",
4819
+ parameters: typebox_exports.Object({
4820
+ days: typebox_exports.Optional(
4821
+ typebox_exports.Number({ description: "\u8FD4\u56DE\u6700\u8FD1N\u4E2A\u4EA4\u6613\u65E5\uFF0C\u9ED8\u8BA4 10" })
4822
+ )
4823
+ }),
4824
+ async execute(_id, params) {
4825
+ const out = await runPy("stock_data.py", [
4826
+ "northbound_flow",
4827
+ "--days",
4828
+ String(params.days ?? 10)
4829
+ ]);
4830
+ return asText(out);
4831
+ }
4832
+ });
4786
4833
  api.registerTool({
4787
4834
  name: "get_fundamental_context",
4788
4835
  description: "\u83B7\u53D6A\u80A1\u57FA\u672C\u9762\u4E0A\u4E0B\u6587\uFF08\u4F30\u503C PE/PB/\u5E02\u503C + \u8425\u6536/\u51C0\u5229\u589E\u901F + ROE/\u6BDB\u5229\u7387/\u51C0\u5229\u7387 + \u5206\u7EA2\u5386\u53F2\uFF09\u3002\u7528\u4E8E\u8BC4\u4F30\u516C\u53F8\u8D28\u5730\u4E0E\u957F\u671F\u6301\u6709\u4EF7\u503C",
@@ -5274,13 +5321,30 @@ var index_default = definePluginEntry({
5274
5321
  });
5275
5322
  api.registerTool({
5276
5323
  name: "render_market_report",
5277
- 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\u3002\u4EC5\u6E32\u67D3\uFF0C\u4E0D\u4FDD\u5B58\u4E0D\u63A8\u9001",
5324
+ 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",
5278
5325
  parameters: typebox_exports.Object({
5279
- report: typebox_exports.Object({}, { additionalProperties: true, description: "\u7ED3\u6784\u5316\u5E02\u573A\u590D\u76D8" })
5326
+ report: typebox_exports.Object({}, { additionalProperties: true, description: "\u7ED3\u6784\u5316\u5E02\u573A\u590D\u76D8" }),
5327
+ save: typebox_exports.Optional(typebox_exports.Boolean({ description: "\u662F\u5426\u5F52\u6863\u5230\u672C\u5730\u590D\u76D8 JSONL \u5B58\u50A8\uFF0C\u9ED8\u8BA4 false" }))
5280
5328
  }),
5281
5329
  async execute(_id, params) {
5282
5330
  const b64 = Buffer.from(JSON.stringify(params.report), "utf-8").toString("base64");
5283
- const out = await runPy("report_renderer.py", ["market", "--input-b64", b64]);
5331
+ const out = await runPy("report_renderer.py", [
5332
+ "market",
5333
+ "--input-b64",
5334
+ b64,
5335
+ ...params.save ? ["--save"] : []
5336
+ ]);
5337
+ return asText(out);
5338
+ }
5339
+ });
5340
+ api.registerTool({
5341
+ name: "get_review_history",
5342
+ description: "\u590D\u76D8\u5F52\u6863\u5386\u53F2 \u2014 \u8BFB\u53D6\u672C\u5730 JSONL \u5F52\u6863\u7684\u6700\u8FD1 N \u6761\u5E02\u573A\u590D\u76D8\uFF08\u6700\u65B0\u5728\u524D\uFF09\uFF0C\u7528\u4E8E\u7EB5\u5411\u5BF9\u6BD4\uFF08\u6E29\u5EA6/\u59FF\u6001/\u4E3B\u7EBF\u53D8\u5316\uFF09\u3002\u5F52\u6863\u7531 render_market_report \u7684 save=true \u5199\u5165\uFF0C\u7A7A\u5B58\u6863\u8FD4\u56DE\u7A7A\u5217\u8868",
5343
+ parameters: typebox_exports.Object({
5344
+ limit: typebox_exports.Optional(typebox_exports.Number({ description: "\u8FD4\u56DE\u6761\u6570\uFF0C\u9ED8\u8BA4 10" }))
5345
+ }),
5346
+ async execute(_id, params) {
5347
+ const out = await runPy("market_review.py", ["history", "--limit", String(params.limit ?? 10)]);
5284
5348
  return asText(out);
5285
5349
  }
5286
5350
  });
package/index.ts CHANGED
@@ -25,25 +25,10 @@ const here = dirname(fileURLToPath(import.meta.url));
25
25
  const toolsDir = existsSync(join(here, "tools"))
26
26
  ? join(here, "tools")
27
27
  : join(here, "..", "tools");
28
- const repoRoot = dirname(toolsDir);
28
+ const fallbackRoot = dirname(toolsDir);
29
29
  const isWin = process.platform === "win32";
30
30
  // Windows venvs ship Scripts/python.exe (there is no python3); POSIX venvs ship bin/python3.
31
31
  const venvBin = join(".venv", isWin ? "Scripts" : "bin", isWin ? "python.exe" : "python3");
32
- const venvPython = join(repoRoot, venvBin);
33
- // Staged-payload layout (openclaw/tools present in a dev checkout): the venv
34
- // still lives at the repo root one level up.
35
- const parentVenvPython = join(repoRoot, "..", venvBin);
36
-
37
- function pythonBin(): string {
38
- if (existsSync(venvPython)) return venvPython;
39
- if (existsSync(parentVenvPython)) return parentVenvPython;
40
- // On Windows "python3" is usually the Microsoft Store stub — prefer "python".
41
- return isWin ? "python" : "python3";
42
- }
43
-
44
- async function runPy(script: string, args: string[]): Promise<string> {
45
- return executor(pythonBin(), [join(toolsDir, script), ...args]);
46
- }
47
32
 
48
33
  function asText(text: string) {
49
34
  return { content: [{ type: "text" as const, text }] };
@@ -55,6 +40,19 @@ export default definePluginEntry({
55
40
  description:
56
41
  "Stock analysis, screening, and strategy backtesting across A/HK/US/JP/KR/TW markets",
57
42
  register(api) {
43
+ // OpenClaw executes a captured copy of the JS bundle; the venv stays in the installed plugin root.
44
+ const repoRoot = api.rootDir || fallbackRoot;
45
+ const pythonBin = () => {
46
+ const venvPython = join(repoRoot, venvBin);
47
+ if (existsSync(venvPython)) return venvPython;
48
+ // In a staged dev checkout, the venv still lives at the repo root.
49
+ const parentVenvPython = join(repoRoot, "..", venvBin);
50
+ if (existsSync(parentVenvPython)) return parentVenvPython;
51
+ // On Windows "python3" is usually the Microsoft Store stub — prefer "python".
52
+ return isWin ? "python" : "python3";
53
+ };
54
+ const runPy = (script: string, args: string[]) =>
55
+ executor(pythonBin(), [join(toolsDir, script), ...args]);
58
56
  // --- Data Tools ---
59
57
 
60
58
  api.registerTool({
@@ -88,7 +86,7 @@ export default definePluginEntry({
88
86
 
89
87
  api.registerTool({
90
88
  name: "get_quote",
91
- description: "获取股票实时行情报价(现价、涨跌幅、量比等)。支持A股、港股、美股、日股、韩股、台股;非交易时段返回最近交易日收盘价并以 as_of/stale 标注",
89
+ description: "获取股票实时行情报价(现价、涨跌幅、量比等)。支持A股、港股、美股、日股、韩股、台股;非交易时段返回最近交易日收盘价并以 as_of/stale 标注;ETF 的 premium_discount_rate 为正=溢价、负=折价",
92
90
  parameters: Type.Object({
93
91
  symbol: Type.String({ description: "股票代码(A股如600519,美股如AAPL,港股如00700.HK)" }),
94
92
  }),
@@ -151,9 +149,14 @@ export default definePluginEntry({
151
149
  description: "获取股票财务摘要。A股返回最新报告期 ROE/毛利率/净利率/负债率/流动比率;其他市场返回 PE/PB/市值/营收/净利润等。A股估值、成长和分红用 get_fundamental_context",
152
150
  parameters: Type.Object({
153
151
  symbol: Type.String({ description: "股票代码(A股如600519,美股如AAPL,港股如00700.HK)" }),
152
+ periods: Type.Optional(
153
+ Type.Number({ description: "返回最近N个报告期的财务趋势,默认 1(仅最新一期)" })
154
+ ),
154
155
  }),
155
156
  async execute(_id, params) {
156
- const out = await runPy("stock_data.py", ["financials", params.symbol]);
157
+ const args = ["financials", params.symbol];
158
+ if (params.periods && params.periods > 1) args.push("--periods", String(params.periods));
159
+ const out = await runPy("stock_data.py", args);
157
160
  return asText(out);
158
161
  },
159
162
  });
@@ -163,7 +166,7 @@ export default definePluginEntry({
163
166
  api.registerTool({
164
167
  name: "get_technical_analysis",
165
168
  description:
166
- "获取股票技术面分析(MA/MACD/RSI/BOLL/KDJ/成交量等指标 + 100分综合评分 + 6级买卖信号 + 趋势/偏离度/支撑压力位)。个股技术面综合判断与买卖时机分析的首选;只要均线数值或自定义周期用 calculate_ma,专问量价用 get_volume_analysis,扫当日异动用 detect_anomaly",
169
+ "获取股票技术面分析(MA/MACD/RSI/BOLL/KDJ/成交量等指标 + 100分综合评分 + 6级买卖信号 + 趋势/偏离度/支撑压力位;多周期共振时 resonance.direction ∈ aligned_bullish/aligned_bearish/divergent)。个股技术面综合判断与买卖时机分析的首选;只要均线数值或自定义周期用 calculate_ma,专问量价用 get_volume_analysis,扫当日异动用 detect_anomaly",
167
170
  parameters: Type.Object({
168
171
  symbol: Type.String({ description: "股票代码(A股如600519,美股如AAPL,港股如00700.HK)" }),
169
172
  period: Type.Optional(
@@ -175,16 +178,23 @@ export default definePluginEntry({
175
178
  count: Type.Optional(
176
179
  Type.Number({ description: "用于计算指标的K线条数,默认 120" })
177
180
  ),
181
+ periods: Type.Optional(
182
+ Type.String({
183
+ description: '多周期共振分析,逗号分隔的周期列表,如 "daily,weekly";不传则只分析 period 指定的单周期',
184
+ })
185
+ ),
178
186
  }),
179
187
  async execute(_id, params) {
180
- const out = await runPy("technical.py", [
188
+ const args = [
181
189
  "analyze",
182
190
  params.symbol,
183
191
  "--period",
184
192
  params.period ?? "daily",
185
193
  "--count",
186
194
  String(params.count ?? 120),
187
- ]);
195
+ ];
196
+ if (params.periods) args.push("--periods", params.periods);
197
+ const out = await runPy("technical.py", args);
188
198
  return asText(out);
189
199
  },
190
200
  });
@@ -417,6 +427,46 @@ export default definePluginEntry({
417
427
  },
418
428
  });
419
429
 
430
+ api.registerTool({
431
+ name: "get_margin_trading",
432
+ description:
433
+ "获取A股个股融资融券明细(融资余额/融资买入额/融券余量等,上交所/深交所官方数据按交易所分流),返回最近N个交易日序列(最新在前)。金额单位为元,short_*_shares 单位为股。仅两融标的有数据,非两融标的返回错误",
434
+ parameters: Type.Object({
435
+ symbol: Type.String({ description: "A股股票代码,如 600519" }),
436
+ days: Type.Optional(
437
+ Type.Number({ description: "返回最近N个交易日,默认 10" })
438
+ ),
439
+ }),
440
+ async execute(_id, params) {
441
+ const out = await runPy("stock_data.py", [
442
+ "margin_trading",
443
+ params.symbol,
444
+ "--days",
445
+ String(params.days ?? 10),
446
+ ]);
447
+ return asText(out);
448
+ },
449
+ });
450
+
451
+ api.registerTool({
452
+ name: "get_northbound_flow",
453
+ description:
454
+ "获取北向资金市场级净买入序列(最新在前,单位亿元)。数据口径:东方财富沪深港通历史数据;2024-08-16 起交易所停止披露日度净买额,仅 2024-08 之前历史数据可查(近期小 days 窗口会返回停披错误,加大 days 可取历史)",
455
+ parameters: Type.Object({
456
+ days: Type.Optional(
457
+ Type.Number({ description: "返回最近N个交易日,默认 10" })
458
+ ),
459
+ }),
460
+ async execute(_id, params) {
461
+ const out = await runPy("stock_data.py", [
462
+ "northbound_flow",
463
+ "--days",
464
+ String(params.days ?? 10),
465
+ ]);
466
+ return asText(out);
467
+ },
468
+ });
469
+
420
470
  api.registerTool({
421
471
  name: "get_fundamental_context",
422
472
  description:
@@ -972,13 +1022,32 @@ export default definePluginEntry({
972
1022
  api.registerTool({
973
1023
  name: "render_market_report",
974
1024
  description:
975
- "大盘复盘报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 schemas/market_review_schema.json 为准。仅渲染,不保存不推送",
1025
+ "大盘复盘报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 schemas/market_review_schema.json 为准。save=true 时把报告对象追加归档到本地 JSONL(默认不保存不推送),纵向对比用 get_review_history",
976
1026
  parameters: Type.Object({
977
1027
  report: Type.Object({}, { additionalProperties: true, description: "结构化市场复盘" }),
1028
+ save: Type.Optional(Type.Boolean({ description: "是否归档到本地复盘 JSONL 存储,默认 false" })),
978
1029
  }),
979
1030
  async execute(_id, params) {
980
1031
  const b64 = Buffer.from(JSON.stringify(params.report), "utf-8").toString("base64");
981
- const out = await runPy("report_renderer.py", ["market", "--input-b64", b64]);
1032
+ const out = await runPy("report_renderer.py", [
1033
+ "market",
1034
+ "--input-b64",
1035
+ b64,
1036
+ ...(params.save ? ["--save"] : []),
1037
+ ]);
1038
+ return asText(out);
1039
+ },
1040
+ });
1041
+
1042
+ api.registerTool({
1043
+ name: "get_review_history",
1044
+ description:
1045
+ "复盘归档历史 — 读取本地 JSONL 归档的最近 N 条市场复盘(最新在前),用于纵向对比(温度/姿态/主线变化)。归档由 render_market_report 的 save=true 写入,空存档返回空列表",
1046
+ parameters: Type.Object({
1047
+ limit: Type.Optional(Type.Number({ description: "返回条数,默认 10" })),
1048
+ }),
1049
+ async execute(_id, params) {
1050
+ const out = await runPy("market_review.py", ["history", "--limit", String(params.limit ?? 10)]);
982
1051
  return asText(out);
983
1052
  },
984
1053
  });
@@ -21,6 +21,8 @@
21
21
  "get_limit_up_pool",
22
22
  "get_dragon_tiger",
23
23
  "get_hot_stocks",
24
+ "get_margin_trading",
25
+ "get_northbound_flow",
24
26
  "get_fundamental_context",
25
27
  "screen_stocks",
26
28
  "run_backtest",
@@ -51,6 +53,7 @@
51
53
  "record_signal",
52
54
  "evaluate_signals",
53
55
  "get_signal_summary",
56
+ "get_review_history",
54
57
  "parse_stock_list"
55
58
  ]
56
59
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weaxs/openclaw-stock-analysis",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
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",
@@ -0,0 +1,29 @@
1
+ // Some model gateways resolve through Chinese DNS but not the runner's resolver.
2
+ // Keep the original hostname for HTTPS; only supply its missing A record.
3
+ import { execFileSync } from "node:child_process";
4
+ import { lookup } from "node:dns/promises";
5
+ import { isIP } from "node:net";
6
+
7
+ if (process.env.OPENAI_BASE_URL) {
8
+ const host = new URL(process.env.OPENAI_BASE_URL).hostname;
9
+ let resolved = false;
10
+ try {
11
+ await lookup(host, { family: 4 });
12
+ resolved = true;
13
+ } catch { /* ask a second resolver below */ }
14
+
15
+ if (!resolved) {
16
+ // Ask for the China-facing record; the runner's overseas view gets only a CNAME.
17
+ const url = `https://dns.alidns.com/resolve?name=${encodeURIComponent(host)}&type=A&edns_client_subnet=223.5.5.0%2F24`;
18
+ const response = await fetch(url, { signal: AbortSignal.timeout(10_000) });
19
+ if (!response.ok) throw new Error(`AliDNS lookup failed: HTTP ${response.status}`);
20
+ const answer = (await response.json()).Answer?.find((record) => record.type === 1);
21
+ if (isIP(answer?.data) !== 4) throw new Error(`No IPv4 address found for ${host}`);
22
+ execFileSync("sudo", ["tee", "-a", "/etc/hosts"], {
23
+ input: `${answer.data} ${host}\n`,
24
+ stdio: ["pipe", "ignore", "pipe"],
25
+ });
26
+ await fetch(`https://${host}/`, { signal: AbortSignal.timeout(10_000) });
27
+ console.log(`Resolved ${host} through AliDNS; HTTPS is reachable`);
28
+ }
29
+ }
@@ -15,7 +15,7 @@ const venvPython = venvPythonPath(pkgDir);
15
15
  const requirements = join(pkgDir, "tools", "requirements.txt");
16
16
 
17
17
  function run(cmd) {
18
- execSync(cmd, { stdio: "pipe", timeout: 300_000 });
18
+ execSync(cmd, { stdio: "inherit", timeout: 1_800_000 });
19
19
  }
20
20
 
21
21
  function findPython() {
@@ -23,7 +23,7 @@ function findPython() {
23
23
  try {
24
24
  const ver = execSync(`${name} --version`, { stdio: "pipe" }).toString().trim();
25
25
  const match = ver.match(/(\d+)\.(\d+)/);
26
- if (match && (parseInt(match[1]) > 3 || (parseInt(match[1]) === 3 && parseInt(match[2]) >= 9))) {
26
+ if (match && (parseInt(match[1]) > 3 || (parseInt(match[1]) === 3 && parseInt(match[2]) >= 10))) {
27
27
  return name;
28
28
  }
29
29
  } catch {}
@@ -35,23 +35,21 @@ try {
35
35
  const python = findPython();
36
36
  if (!python) {
37
37
  console.warn(
38
- "\n[pi-stock-analysis] python3 >= 3.9 not found.\n" +
39
- "Install Python 3.9+ and re-run: npm rebuild pi-stock-analysis\n"
38
+ "\n[stock-analysis-plugin] python3 >= 3.10 not found.\n" +
39
+ "Install Python 3.10+, then re-run: npm install\n"
40
40
  );
41
- process.exit(0);
41
+ process.exit(1);
42
42
  }
43
43
 
44
44
  if (!existsSync(venvDir)) {
45
45
  run(`${python} -m venv "${venvDir}"`);
46
46
  }
47
47
 
48
- if (existsSync(requirements)) {
49
- run(`"${venvPython}" -m pip install -q -r "${requirements}"`);
50
- }
48
+ run(`"${venvPython}" -m pip install -q -r "${requirements}"`);
51
49
  } catch (err) {
52
50
  console.warn(
53
51
  `\n[pi-stock-analysis] Python setup failed: ${err.message}\n` +
54
52
  "You can manually create the venv and install tools/requirements.txt with your platform's python.\n"
55
53
  );
56
- process.exit(0);
54
+ process.exit(1);
57
55
  }
@@ -10,7 +10,7 @@ description: 热点题材分析:评估板块强度、扩散度、个股关联
10
10
  先运行:
11
11
 
12
12
  ```bash
13
- python3 scripts/gather.py <symbol>
13
+ python3 tools/gather.py fundamental <symbol>
14
14
  ```
15
15
 
16
16
  gather 工具返回:
@@ -21,7 +21,8 @@ gather 工具返回:
21
21
  - `news` — 近期新闻(题材催化来源)
22
22
  - `stock_info` — 公司基本信息(行业、业务)
23
23
  - `sector_rankings` — 板块排名(核心数据源);`get_sector_rankings` 传 `board_type=concept` 时为概念板块排行,热点题材以概念口径为主
24
- - `hot_stocks` — 全市场人气热搜榜(`get_hot_stocks`,东方财富人气榜),辅助判断题材热度
24
+
25
+ 人气热搜榜不在 gather 返回中,需要时单独调用 `get_hot_stocks`(东方财富人气榜)辅助判断题材热度。
25
26
 
26
27
  评估扩散度前,必须获取板块成分股及其当日表现;只有板块排名时,仅可判断当日相对强弱,不判断扩散度或持续性。
27
28
 
@@ -21,7 +21,7 @@ description: 全市场股票筛选:按市场、估值、成长、趋势或自
21
21
  运行数据采集脚本进行 L1 量化筛选:
22
22
 
23
23
  ```bash
24
- python3 scripts/gather.py --market A --top 20
24
+ python3 tools/screener.py screen --market A --top 20
25
25
  ```
26
26
 
27
27
  可选参数:`--market`(A/HK/US)、`--top`(返回数量)、`--config`(自定义 YAML 配置路径)、`--l2`(L2 量化增强开关,默认关闭)。
@@ -39,6 +39,11 @@ exit:
39
39
  stop_loss: -0.05 # 止损比例
40
40
  take_profit: 0.15 # 止盈比例
41
41
 
42
+ simulation:
43
+ slippage: 0.001 # 滑点(买卖双边),默认 0.001
44
+ commission: 0.00025 # 佣金(买卖双边,券商自定;A股常见万2.5),默认 0.0015
45
+ stamp_tax: 0.0005 # 印花税(仅卖出收;A股 0.05%,其他市场按当下实际费率,无则省略),默认 0
46
+
42
47
  position:
43
48
  size: 1.0 # 仓位比例
44
49
  max_positions: 1 # 最大持仓数
@@ -53,14 +58,13 @@ position:
53
58
  运行回测脚本:
54
59
 
55
60
  ```bash
56
- python3 scripts/gather.py --strategy <path.yaml> --symbol <symbol> --start <date> --end <date>
61
+ python3 tools/backtest.py run <path.yaml> <symbol> --start <date> --end <date>
57
62
  ```
58
63
 
59
64
  参数说明:
60
- - `--strategy`:策略 YAML 文件路径
61
- - `--symbol`:股票代码
65
+ - 前两个为位置参数,依次为:策略 YAML 文件路径、股票代码
62
66
  - `--start` / `--end`:回测时间范围(可选,默认近一年)
63
- - 初始资金默认 100 万
67
+ - 初始资金默认 100 万,可用 `--capital` 调整
64
68
 
65
69
  ### 第三步:分析结果
66
70
 
@@ -69,7 +73,7 @@ python3 scripts/gather.py --strategy <path.yaml> --symbol <symbol> --start <date
69
73
  - 从 `metrics` 和 `trades` 计算期望值并与盈亏比一起报告。
70
74
  - 报告最大回撤、连续亏损、交易次数和平均持有期;结果未携带初始风险时,将 1R 分布标为不可计算。
71
75
  - 使用用户指定的互斥日期区间分别运行样本内和样本外回测。
72
- - 只陈述策略配置或结果中可确认的手续费、滑点与成交约束,其余列入限制。
76
+ - 成本假设引用结果中的 `costs` 字段(实际生效的佣金/滑点/印花税费率与实付费用合计),未模拟的费用与成交约束列入限制。
73
77
 
74
78
  未模拟的结算、涨跌停、复权或特定市场费用列入限制,结论限定为该模拟条件下的历史表现。
75
79
 
@@ -42,6 +42,11 @@ exit:
42
42
  stop_loss: "{stop_loss}"
43
43
  take_profit: "{take_profit}"
44
44
 
45
+ simulation:
46
+ slippage: 0.001 # 滑点(买卖双边)
47
+ commission: 0.00025 # 佣金(买卖双边,A股常见万2.5)
48
+ stamp_tax: 0.0005 # 印花税(A股卖出单边;其他市场按当下实际费率,无则删除本行)
49
+
45
50
  position:
46
51
  size: 0.6
47
52
  max_positions: 1
@@ -36,6 +36,11 @@ exit:
36
36
  stop_loss: "{stop_loss}"
37
37
  take_profit: "{take_profit}"
38
38
 
39
+ simulation:
40
+ slippage: 0.001 # 滑点(买卖双边)
41
+ commission: 0.00025 # 佣金(买卖双边,A股常见万2.5)
42
+ stamp_tax: 0.0005 # 印花税(A股卖出单边;其他市场按当下实际费率,无则删除本行)
43
+
39
44
  position:
40
45
  size: 0.8
41
46
  max_positions: 1
@@ -9,11 +9,13 @@ parameters:
9
9
  entry:
10
10
  conditions:
11
11
  - indicator: "ma_diff"
12
- period: 510
12
+ fast_period: 5
13
+ slow_period: 10
13
14
  operator: ">"
14
15
  value: 0
15
16
  - indicator: "ma_diff"
16
- period: 1020
17
+ fast_period: 10
18
+ slow_period: 20
17
19
  operator: ">"
18
20
  value: 0
19
21
  - indicator: "macd_dif"
@@ -27,7 +29,8 @@ entry:
27
29
  exit:
28
30
  conditions:
29
31
  - indicator: "ma_diff"
30
- period: 510
32
+ fast_period: 5
33
+ slow_period: 10
31
34
  operator: "cross_below"
32
35
  value: 0
33
36
  - indicator: "macd_dif"
@@ -37,6 +40,11 @@ exit:
37
40
  stop_loss: "{stop_loss}"
38
41
  take_profit: "{take_profit}"
39
42
 
43
+ simulation:
44
+ slippage: 0.001 # 滑点(买卖双边)
45
+ commission: 0.00025 # 佣金(买卖双边,A股常见万2.5)
46
+ stamp_tax: 0.0005 # 印花税(A股卖出单边;其他市场按当下实际费率,无则删除本行)
47
+
40
48
  position:
41
49
  size: 1.0
42
50
  max_positions: 1