dsh-stock-analysis 0.2.9 → 0.2.10

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,10 +1,10 @@
1
1
  # Stock Analysis — DeepSeek Harness (dsh) Plugin
2
2
 
3
- A股 / 港股 / 美股 / 日股 / 韩股 / 台股 行情、技术分析、筛选与回测。作为 dsh bundle 安装到 profile 后,对话中可调用 70 个工具,并自动注册 21 个工作流/策略方法论 skill。
3
+ A股 / 港股 / 美股 / 日股 / 韩股 / 台股 行情、技术分析、筛选与回测。作为 dsh bundle 安装到 profile 后,对话中可调用 71 个工具,并自动注册 21 个工作流/策略方法论 skill。
4
4
 
5
5
  ## 按需工具发现
6
6
 
7
- 支持作用域工具接口和请求组装钩子的新版 dsh 默认增加一个宿主控制入口 `stock_tool_search`,原有 70 个分析工具的名称、参数和输出不变。原生函数调用模式下,首次请求只暴露这个发现入口;其他插件的工具照常保留。搜索支持中文描述、英文关键词或确切工具名,例如 `财务`、`get_quote`、`run_backtest`;命中工具在下一次模型请求提供完整 schema,再按原名调用。默认最多加载 5 项,`limit` 可设 1 到 20;无匹配返回空列表。
7
+ 支持作用域工具接口和请求组装钩子的新版 dsh 默认增加一个宿主控制入口 `stock_tool_search`;按需发现只控制工具加载,不改变 71 个分析工具的名称、参数和输出。原生函数调用模式下,首次请求只暴露这个发现入口;其他插件的工具照常保留。搜索支持中文描述、英文关键词或确切工具名,例如 `财务`、`get_quote`、`run_backtest`;命中工具在下一次模型请求提供完整 schema,再按原名调用。默认最多加载 5 项,`limit` 可设 1 到 20;无匹配返回空列表。
8
8
 
9
9
  加载集合按 Agent 隔离,搜索和回退均保留宿主已有的工具权限限制。搜索入口被禁止或移除时,下一次请求保留原先已授权的完整股票工具目录。旧版缺少相关接口时保持全量暴露;PTC / both 模式沿用宿主生成的 SDK,不做发现过滤。加载集合在本次运行中保留,恢复会话或重载插件后需重新搜索。
10
10
 
@@ -61,6 +61,8 @@ allowBuilds:
61
61
  - `analyze_pattern` — 12+ 种 K 线形态识别
62
62
  - `calculate_ma` — 独立均线计算
63
63
  - `get_volume_analysis` — 量价分析
64
+ - `analyze_portfolio_risk` — 组合敞口、相关性、止损与确定性压力测试
65
+ - `plan_portfolio_rebalance` — 目标权重、整手和现金约束下的调仓清单
64
66
 
65
67
  ### 市场全景
66
68
  - `get_market_indices` — 主要指数(CN/HK/US/JP/KR/TW)
package/dist/index.js CHANGED
@@ -113,7 +113,9 @@ function registerToolDiscovery(ctx, names) {
113
113
  output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
114
114
  async execute({ query, limit = 5 }, exec) {
115
115
  const needle = query.trim().toLowerCase();
116
- const terms = [...new Set(needle.match(/[a-z0-9_]+|\p{Script=Han}/gu) ?? [])];
116
+ const tokens = needle.match(/[a-z0-9_]+|\p{Script=Han}/gu) ?? [];
117
+ const bigrams = (needle.match(/\p{Script=Han}+/gu) ?? []).flatMap((text) => [...text].slice(0, -1).map((_, index) => text.slice(index, index + 2)));
118
+ const terms = [.../* @__PURE__ */ new Set([...tokens, ...bigrams])];
117
119
  if (!terms.length || !Number.isInteger(limit) || limit < 1 || limit > 20) {
118
120
  return JSON.stringify({ error: "query must contain searchable text and limit must be an integer from 1 to 20" });
119
121
  }
@@ -124,7 +126,7 @@ function registerToolDiscovery(ctx, names) {
124
126
  const matches = candidates.map(({ tool, text }) => {
125
127
  const score = (tool.name === needle ? 1e3 : 0) + (text.includes(needle) ? 10 : 0) + terms.reduce((sum, term, index) => sum + weights[index] * (tool.name.includes(term) ? 3 : text.includes(term) ? 1 : 0), 0);
126
128
  return { tool, score };
127
- }).filter((match) => match.score > 0).sort((a, b) => b.score - a.score || a.tool.name.localeCompare(b.tool.name)).slice(0, limit);
129
+ }).filter((match) => match.score > 0).sort((a, b) => b.score - a.score || a.tool.name.length - b.tool.name.length || a.tool.name.localeCompare(b.tool.name)).slice(0, limit);
128
130
  for (const { tool } of matches) state.loaded.add(tool.name);
129
131
  return JSON.stringify({ loaded: matches.map(({ tool }) => ({ name: tool.name, description: tool.description })) });
130
132
  }
@@ -177,20 +179,29 @@ function pythonBin() {
177
179
  async function runPy(script, args) {
178
180
  return executor(pythonBin(), [join3(toolsDir, script), ...args]);
179
181
  }
182
+ var textOutput = {
183
+ schema: { type: "string" },
184
+ render: (_args, value) => [{ type: "text", text: value }]
185
+ };
180
186
  function pyTool(opts) {
181
187
  return defineTool2({
182
188
  name: opts.name,
183
189
  description: opts.description,
184
190
  parameters: opts.parameters,
185
- output: {
186
- schema: { type: "string" },
187
- render: (_args, value) => [{ type: "text", text: value }]
188
- },
191
+ output: textOutput,
189
192
  async execute(args) {
190
193
  return runPy(opts.script, opts.argv(args));
191
194
  }
192
195
  });
193
196
  }
197
+ function closedTool(opts) {
198
+ const tool = defineTool2({
199
+ ...opts,
200
+ output: textOutput
201
+ });
202
+ tool.parameters.additionalProperties = false;
203
+ return tool;
204
+ }
194
205
  var skillsDir = join3(here, "..", "skills");
195
206
  function frontmatterValue(raw, key) {
196
207
  const block = raw.match(/^---\r?\n([\s\S]*?)\r?\n---/);
@@ -819,7 +830,7 @@ function apply(ctx, config = {}) {
819
830
  }
820
831
  })
821
832
  );
822
- ctx.tools.register(defineTool2({
833
+ ctx.tools.register(closedTool({
823
834
  name: "calculate_rotation_metrics",
824
835
  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",
825
836
  parameters: {
@@ -878,12 +889,11 @@ function apply(ctx, config = {}) {
878
889
  }
879
890
  }
880
891
  },
881
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
882
892
  async execute(params) {
883
893
  return withJsonInput(params, (path) => runPy("sector_rotation.py", ["analyze", "--input", path]));
884
894
  }
885
895
  }));
886
- ctx.tools.register(defineTool2({
896
+ ctx.tools.register(closedTool({
887
897
  name: "backtest_rotation_allocation",
888
898
  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",
889
899
  parameters: {
@@ -1001,12 +1011,11 @@ function apply(ctx, config = {}) {
1001
1011
  "description": "\u6BCF\u8FB9\u6BD4\u4F8B\u6210\u672C0..0.1\uFF0C\u9ED8\u8BA40.001\uFF1B\u4E70\u5165\u9884\u7B97\u542B\u8D39\u7528"
1002
1012
  }
1003
1013
  },
1004
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
1005
1014
  async execute(params) {
1006
1015
  return withJsonInput(params, (path) => runPy("sector_rotation.py", ["backtest", "--input", path]));
1007
1016
  }
1008
1017
  }));
1009
- ctx.tools.register(defineTool2({
1018
+ ctx.tools.register(closedTool({
1010
1019
  name: "analyze_trend_strategy",
1011
1020
  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",
1012
1021
  parameters: {
@@ -1041,7 +1050,6 @@ function apply(ctx, config = {}) {
1041
1050
  "description": "\u53EF\u9009\u56FA\u5B9A\u5BB9\u91CFK\uFF0C\u9ED8\u8BA4ceil(N/3)\uFF0C\u4E0D\u5F97\u5C0F\u4E8E\u771F\u5B9E\u6301\u80A1\u6570\u91CF"
1042
1051
  }
1043
1052
  },
1044
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
1045
1053
  async execute(params) {
1046
1054
  return withJsonInput(params, (path) => runPy("trend_strategy.py", ["fetch", "--input", path]));
1047
1055
  }
@@ -1634,7 +1642,7 @@ function apply(ctx, config = {}) {
1634
1642
  }
1635
1643
  })
1636
1644
  );
1637
- ctx.tools.register(defineTool2({
1645
+ ctx.tools.register(closedTool({
1638
1646
  name: "compare_stocks",
1639
1647
  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",
1640
1648
  parameters: {
@@ -1702,12 +1710,11 @@ function apply(ctx, config = {}) {
1702
1710
  "required": true
1703
1711
  }
1704
1712
  },
1705
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
1706
1713
  async execute(params) {
1707
1714
  return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["peers", "--input", path]));
1708
1715
  }
1709
1716
  }));
1710
- ctx.tools.register(defineTool2({
1717
+ ctx.tools.register(closedTool({
1711
1718
  name: "analyze_disclosure_changes",
1712
1719
  description: "\u65E0\u72B6\u6001\u516C\u544A\u8D22\u62A5\u6BD4\u8F83\uFF1A\u4F20\u65B0\u65E7\u6B63\u6587\u53CA\u6307\u6807\uFF0C\u8FD4\u56DE\u53D8\u66F4\u884C\u53F7\u3001\u6765\u6E90\u4E0E\u8D22\u52A1\u53D8\u5316\uFF1B\u8D1F/\u96F6\u57FA\u6570\u589E\u957F\u7387\u4E3Anull\u3002\u4E0D\u4E0B\u8F7DPDF\u3001\u4E0D\u4EE3\u66FF\u7ECF\u6D4E\u89E3\u8BFB\u3002",
1713
1720
  parameters: {
@@ -1844,14 +1851,13 @@ function apply(ctx, config = {}) {
1844
1851
  "description": "\u53D8\u5316\u4E0A\u96501..100\uFF0C\u9ED8\u8BA430"
1845
1852
  }
1846
1853
  },
1847
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
1848
1854
  async execute(params) {
1849
1855
  return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["disclosure", "--input", path]));
1850
1856
  }
1851
1857
  }));
1852
- ctx.tools.register(defineTool2({
1858
+ ctx.tools.register(closedTool({
1853
1859
  name: "analyze_portfolio_risk",
1854
- description: "\u65E0\u72B6\u6001\u73B0\u8D27\u591A\u5934\u7EC4\u5408\u98CE\u9669\uFF1A\u8F93\u5165\u6301\u4ED3\u3001\u57FA\u51C6\u5E01\u79CD\u73B0\u91D1\u4E0E\u6C47\u7387\uFF0C\u8FD4\u56DE\u96C6\u4E2D\u5EA6\u3001\u5E01\u79CD/\u884C\u4E1A\u655E\u53E3\u3001\u4EF7\u683C\u76C8\u4E8F\u548C\u6B62\u635F\u60C5\u666F\u3002\u7F3A\u6C47\u7387\u62A5\u9519\uFF1B\u4E0D\u4FDD\u5B58\u8D26\u6237\u3002",
1860
+ description: "\u65E0\u72B6\u6001\u73B0\u8D27\u591A\u5934\u7EC4\u5408\u98CE\u9669\u4E0E\u538B\u529B\u6D4B\u8BD5\uFF1A\u8F93\u5165\u6301\u4ED3\u3001\u57FA\u51C6\u5E01\u79CD\u73B0\u91D1\u4E0E\u6C47\u7387\uFF0C\u8FD4\u56DE\u96C6\u4E2D\u5EA6\u3001\u5E01\u79CD/\u884C\u4E1A\u655E\u53E3\u3001\u4EF7\u683C\u76C8\u4E8F\u3001\u6B62\u635F\u60C5\u666F\u53CA\u53EF\u9009\u5E02\u573A/\u884C\u4E1A/\u4E2A\u80A1/\u6C47\u7387\u51B2\u51FB\u635F\u76CA\u3002\u7F3A\u6C47\u7387\u62A5\u9519\uFF1B\u4E0D\u4FDD\u5B58\u8D26\u6237\u3002",
1855
1861
  parameters: {
1856
1862
  "positions": {
1857
1863
  "type": "array",
@@ -1965,14 +1971,231 @@ function apply(ctx, config = {}) {
1965
1971
  "additionalProperties": false
1966
1972
  },
1967
1973
  "description": "\u53EF\u9009\u5386\u53F2\uFF0C\u5171\u540C\u65E5\u671F\u672C\u5E01\u4EF7\u683C\u6536\u76CA\u76F8\u5173\u6027\uFF0C\u4E0D\u8BA1\u6C47\u7387"
1974
+ },
1975
+ "stress_scenarios": {
1976
+ "type": "array",
1977
+ "items": {
1978
+ "type": "object",
1979
+ "properties": {
1980
+ "name": {
1981
+ "type": "string",
1982
+ "description": "\u60C5\u666F\u540D\u79F0",
1983
+ "required": true
1984
+ },
1985
+ "market_shock": {
1986
+ "type": "number",
1987
+ "description": "\u5168\u5E02\u573A\u8D44\u4EA7\u4EF7\u683C\u51B2\u51FB\u6BD4\u4F8B\uFF0C\u5982-0.1"
1988
+ },
1989
+ "sector_shocks": {
1990
+ "type": "array",
1991
+ "items": {
1992
+ "type": "object",
1993
+ "properties": {
1994
+ "sector": {
1995
+ "type": "string",
1996
+ "description": "\u4E0E\u6301\u4ED3\u4E00\u81F4\u7684\u884C\u4E1A\u540D\u79F0",
1997
+ "required": true
1998
+ },
1999
+ "shock": {
2000
+ "type": "number",
2001
+ "description": "\u884C\u4E1A\u4EF7\u683C\u51B2\u51FB\u6BD4\u4F8B",
2002
+ "required": true
2003
+ }
2004
+ },
2005
+ "additionalProperties": false
2006
+ },
2007
+ "description": "\u53EF\u9009\u884C\u4E1A\u51B2\u51FB"
2008
+ },
2009
+ "symbol_shocks": {
2010
+ "type": "array",
2011
+ "items": {
2012
+ "type": "object",
2013
+ "properties": {
2014
+ "symbol": {
2015
+ "type": "string",
2016
+ "description": "\u6301\u4ED3\u6807\u7684\u4EE3\u7801",
2017
+ "required": true
2018
+ },
2019
+ "shock": {
2020
+ "type": "number",
2021
+ "description": "\u6807\u7684\u4EF7\u683C\u51B2\u51FB\u6BD4\u4F8B",
2022
+ "required": true
2023
+ }
2024
+ },
2025
+ "additionalProperties": false
2026
+ },
2027
+ "description": "\u53EF\u9009\u4E2A\u80A1\u51B2\u51FB"
2028
+ },
2029
+ "currency_shocks": {
2030
+ "type": "array",
2031
+ "items": {
2032
+ "type": "object",
2033
+ "properties": {
2034
+ "currency": {
2035
+ "type": "string",
2036
+ "description": "\u6301\u4ED3\u4E2D\u7684\u975E\u57FA\u51C6\u5E01\u79CD\uFF1B\u57FA\u51C6\u5E01\u79CD\u4E0D\u53EF\u76F8\u5BF9\u81EA\u8EAB\u51B2\u51FB",
2037
+ "required": true
2038
+ },
2039
+ "shock": {
2040
+ "type": "number",
2041
+ "description": "\u76F8\u5BF9\u57FA\u51C6\u5E01\u79CD\u7684\u6C47\u7387\u51B2\u51FB\u6BD4\u4F8B",
2042
+ "required": true
2043
+ }
2044
+ },
2045
+ "additionalProperties": false
2046
+ },
2047
+ "description": "\u53EF\u9009\u6C47\u7387\u51B2\u51FB"
2048
+ }
2049
+ },
2050
+ "additionalProperties": false
2051
+ },
2052
+ "description": "\u53EF\u9009\u538B\u529B\u60C5\u666F\uFF1B\u6240\u6709\u51B2\u51FB\u5747\u4E3A\u6BD4\u4F8B\uFF0C\u7531\u5171\u4EAB\u6838\u5FC3\u8BA1\u7B97"
1968
2053
  }
1969
2054
  },
1970
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
1971
2055
  async execute(params) {
1972
2056
  return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["portfolio", "--input", path]));
1973
2057
  }
1974
2058
  }));
1975
- ctx.tools.register(defineTool2({
2059
+ ctx.tools.register(closedTool({
2060
+ name: "plan_portfolio_rebalance",
2061
+ description: "\u65E0\u72B6\u6001\u7EC4\u5408\u8C03\u4ED3\u8BA1\u5212\uFF1A\u6309\u5F53\u524D\u6301\u4ED3\u3001\u76EE\u6807\u6743\u91CD\u3001\u73B0\u91D1\u3001\u6C47\u7387\u3001\u6574\u624B\u3001\u8D39\u7528\u548C\u6362\u624B\u7EA6\u675F\u751F\u6210\u5148\u5356\u540E\u4E70\u6E05\u5355\uFF1B\u4E0D\u4FDD\u5B58\u8D26\u6237\u6216\u63D0\u4EA4\u8BA2\u5355\u3002",
2062
+ parameters: {
2063
+ "base_currency": {
2064
+ "type": "string",
2065
+ "description": "\u57FA\u51C6\u5E01\u79CD",
2066
+ "required": true
2067
+ },
2068
+ "cash": {
2069
+ "type": "number",
2070
+ "description": "\u57FA\u51C6\u5E01\u79CD\u73B0\u91D1\uFF0C\u975E\u8D1F",
2071
+ "required": true
2072
+ },
2073
+ "positions": {
2074
+ "type": "array",
2075
+ "items": {
2076
+ "type": "object",
2077
+ "properties": {
2078
+ "symbol": {
2079
+ "type": "string",
2080
+ "description": "\u552F\u4E00\u4EE3\u7801\uFF0C\u91CD\u590D\u6301\u4ED3\u5148\u6C47\u603B",
2081
+ "required": true
2082
+ },
2083
+ "quantity": {
2084
+ "type": "number",
2085
+ "description": "\u6B63\u6301\u4ED3\u6570\u91CF",
2086
+ "required": true
2087
+ },
2088
+ "price": {
2089
+ "type": "number",
2090
+ "description": "\u6B63\u73B0\u4EF7",
2091
+ "required": true
2092
+ },
2093
+ "currency": {
2094
+ "type": "string",
2095
+ "description": "\u6301\u4ED3\u5E01\u79CD\uFF0C\u5982CNY/USD/HKD",
2096
+ "required": true
2097
+ },
2098
+ "cost": {
2099
+ "type": "number",
2100
+ "description": "\u540C\u5E01\u79CD\u6B63\u6210\u672C\u4EF7\uFF0C\u53EF\u9009\uFF1B\u8C03\u4ED3\u8BA1\u7B97\u4E0D\u4F7F\u7528"
2101
+ },
2102
+ "stop_price": {
2103
+ "type": "number",
2104
+ "description": "\u540C\u5E01\u79CD\u6B63\u6B62\u635F\u4EF7\uFF0C\u53EF\u9009\uFF1B\u8C03\u4ED3\u8BA1\u7B97\u4E0D\u4F7F\u7528"
2105
+ },
2106
+ "sector": {
2107
+ "type": "string",
2108
+ "description": "\u7EDF\u4E00\u884C\u4E1A\u5206\u7C7B\uFF0C\u53EF\u9009"
2109
+ },
2110
+ "as_of": {
2111
+ "type": "string",
2112
+ "description": "\u5B9E\u9645\u4F30\u503C\u65F6\u70B9\uFF0C\u5EFA\u8BAEISO8601"
2113
+ }
2114
+ },
2115
+ "additionalProperties": false
2116
+ },
2117
+ "description": "0..100\u4E2A\u73B0\u8D27\u591A\u5934\u6301\u4ED3\uFF0C\u7A7A\u6570\u7EC4\u8868\u793A\u7EAF\u73B0\u91D1\u5EFA\u4ED3",
2118
+ "required": true
2119
+ },
2120
+ "targets": {
2121
+ "type": "array",
2122
+ "items": {
2123
+ "type": "object",
2124
+ "properties": {
2125
+ "symbol": {
2126
+ "type": "string",
2127
+ "description": "\u76EE\u6807\u6807\u7684\u4EE3\u7801",
2128
+ "required": true
2129
+ },
2130
+ "weight": {
2131
+ "type": "number",
2132
+ "description": "\u76EE\u6807\u6743\u91CD\uFF1B\u5927\u4E8E0\u65F6lot_size\u5FC5\u586B",
2133
+ "required": true
2134
+ },
2135
+ "price": {
2136
+ "type": "number",
2137
+ "description": "\u6B63\u76EE\u6807\u4EF7\u683C\uFF1B\u5F53\u524D\u672A\u6301\u6709\u7684\u65B0\u6807\u7684\u5FC5\u586B"
2138
+ },
2139
+ "currency": {
2140
+ "type": "string",
2141
+ "description": "\u76EE\u6807\u5E01\u79CD\uFF1B\u5F53\u524D\u672A\u6301\u6709\u7684\u65B0\u6807\u7684\u5FC5\u586B"
2142
+ },
2143
+ "sector": {
2144
+ "type": "string",
2145
+ "description": "\u7EDF\u4E00\u884C\u4E1A\u5206\u7C7B\uFF0C\u53EF\u9009"
2146
+ },
2147
+ "as_of": {
2148
+ "type": "string",
2149
+ "description": "\u5B9E\u9645\u4F30\u503C\u65F6\u70B9\uFF0C\u5EFA\u8BAEISO8601"
2150
+ },
2151
+ "lot_size": {
2152
+ "type": "number",
2153
+ "description": "\u6B63\u6574\u6570\u4EA4\u6613\u5355\u4F4D\uFF1Bweight>0\u65F6\u5FC5\u586B"
2154
+ }
2155
+ },
2156
+ "additionalProperties": false
2157
+ },
2158
+ "description": "1..100\u4E2A\u76EE\u6807\uFF1B\u65B0\u6807\u7684price/currency\u5FC5\u586B",
2159
+ "required": true
2160
+ },
2161
+ "fx_rates": {
2162
+ "type": "array",
2163
+ "items": {
2164
+ "type": "object",
2165
+ "properties": {
2166
+ "currency": {
2167
+ "type": "string",
2168
+ "description": "\u552F\u4E00\u975E\u57FA\u51C6\u5E01\u79CD",
2169
+ "required": true
2170
+ },
2171
+ "rate": {
2172
+ "type": "number",
2173
+ "description": "\u4E00\u5355\u4F4D\u8BE5\u5E01\u79CD\u5BF9\u5E94\u591A\u5C11\u57FA\u51C6\u5E01\u79CD\uFF0C\u6B63\u6570",
2174
+ "required": true
2175
+ }
2176
+ },
2177
+ "additionalProperties": false
2178
+ },
2179
+ "description": "\u663E\u5F0F\u73B0\u4EF7\u6C47\u7387\uFF1B\u57FA\u51C6\u5E01\u79CD\u9690\u542B1\uFF0C\u4E0D\u5F97\u91CD\u590D"
2180
+ },
2181
+ "fee_rate": {
2182
+ "type": "number",
2183
+ "description": "\u6BCF\u8FB9\u6BD4\u4F8B\u8D39\u75280..0.1\uFF0C\u9ED8\u8BA40\uFF1B\u4E0D\u542B\u672A\u63D0\u4F9B\u7A0E\u8D39/\u6700\u4F4E\u6536\u8D39"
2184
+ },
2185
+ "slippage_rate": {
2186
+ "type": "number",
2187
+ "description": "\u6BCF\u8FB9\u6ED1\u70B90..0.1\uFF0C\u9ED8\u8BA40"
2188
+ },
2189
+ "max_turnover_fraction": {
2190
+ "type": "number",
2191
+ "description": "\u975E\u8D1F\u6700\u5927\u603B\u6362\u624B\uFF0C\u6309\u4E70\u5356\u53C2\u8003\u91D1\u989D\u5408\u8BA1/\u521D\u59CB\u6743\u76CA\uFF1B\u9ED8\u8BA4\u4E0D\u9650\u5236\uFF0C\u53EF\u5927\u4E8E1"
2192
+ }
2193
+ },
2194
+ async execute(params) {
2195
+ return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["rebalance", "--input", path]));
2196
+ }
2197
+ }));
2198
+ ctx.tools.register(closedTool({
1976
2199
  name: "calculate_position_size",
1977
2200
  description: "\u65E0\u72B6\u6001\u591A\u5934\u4ED3\u4F4D\u9884\u7B97\uFF1A\u540C\u5E01\u79CD\u91D1\u989D\uFF0C\u6309\u73B0\u91D1/\u98CE\u9669\u9884\u7B97/\u6700\u5927\u4ED3\u4F4D/\u4EA4\u6613\u5355\u4F4D\u53D6\u6700\u5C0F\u6570\u91CF\uFF0C\u53CC\u8FB9\u8D39\u7528\u548C\u6ED1\u70B9\u53C2\u4E0E\u6B62\u635F\u60C5\u666F\uFF1B\u4E0D\u6267\u884C\u4EA4\u6613\u3002",
1978
2201
  parameters: {
@@ -2019,12 +2242,11 @@ function apply(ctx, config = {}) {
2019
2242
  "description": "\u6BCF\u8FB9\u6ED1\u70B90..0.1\uFF0C\u9ED8\u8BA40"
2020
2243
  }
2021
2244
  },
2022
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
2023
2245
  async execute(params) {
2024
2246
  return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["position_size", "--input", path]));
2025
2247
  }
2026
2248
  }));
2027
- ctx.tools.register(defineTool2({
2249
+ ctx.tools.register(closedTool({
2028
2250
  name: "analyze_event_study",
2029
2251
  description: "\u65E0\u72B6\u6001\u4E8B\u4EF6\u7814\u7A76\uFF1A\u63D0\u4F9B\u540C\u5E02\u573A\u80A1\u7968\u53CA\u57FA\u51C6\u5B8C\u6574\u4EF7\u683C\u3001\u4E8B\u4EF6\u65E5\u671F\uFF1B\u4E8B\u4EF6\u524D\u4E00\u4EA4\u6613\u65E5\u6536\u76D8\u4E3A\u57FA\u70B9\uFF0C\u8BA1\u7B97\u4EA4\u6613\u65E5\u7A97\u53E3\u6536\u76CA\u5DEE\u3002\u7F3A\u7A97\u53E3/\u57FA\u51C6\u4E3Anull\uFF0C\u6807\u6CE8\u91CD\u53E0\uFF0C\u4E0D\u63A8\u65AD\u56E0\u679C\u3002",
2030
2252
  parameters: {
@@ -2106,12 +2328,11 @@ function apply(ctx, config = {}) {
2106
2328
  "description": "\u540C\u5E02\u573A\u57FA\u51C6\u4EE3\u7801\uFF1B\u4E0Esymbol\u4E00\u8D77\u4F7F\u7528"
2107
2329
  }
2108
2330
  },
2109
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
2110
2331
  async execute(params) {
2111
2332
  return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["event_study", "--input", path]));
2112
2333
  }
2113
2334
  }));
2114
- ctx.tools.register(defineTool2({
2335
+ ctx.tools.register(closedTool({
2115
2336
  name: "validate_backtest",
2116
2337
  description: "\u65E0\u72B6\u6001\u56DE\u6D4B\u7A33\u5065\u6027\uFF1A\u8F93\u5165\u5386\u53F2OHLCV\u548C\u5B8C\u6574\u5019\u9009DSL\uFF1B\u6EDA\u52A8\u8BAD\u7EC3\u9009\u53C2\u6570\u3001\u540E\u7EED\u4E0D\u91CD\u53E0\u7A97\u53E3\u9A8C\u8BC1\uFF0C\u5217\u51FA\u5168\u90E8\u6837\u672C\u5185\u654F\u611F\u6027\u53CA\u8DE8\u6807\u7684\u7ED3\u679C\u3002\u4EC5next_open\uFF0C\u4E0D\u81EA\u52A8\u4F18\u5316\u6216\u4FDD\u5B58\u3002",
2117
2338
  parameters: {
@@ -2227,12 +2448,11 @@ function apply(ctx, config = {}) {
2227
2448
  "description": "1..5\u4E2A\u552F\u4E00\u80A1\u7968\u4EE3\u7801\uFF0C\u914D\u5408start_date/end_date\u76F4\u63A5\u53D6\u6570\uFF1B\u548Cdatasets\u4E92\u65A5"
2228
2449
  }
2229
2450
  },
2230
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
2231
2451
  async execute(params) {
2232
2452
  return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["robustness", "--input", path]));
2233
2453
  }
2234
2454
  }));
2235
- ctx.tools.register(defineTool2({
2455
+ ctx.tools.register(closedTool({
2236
2456
  name: "compare_analysis_snapshots",
2237
2457
  description: "\u65E0\u72B6\u6001\u5FEB\u7167\u6BD4\u8F83\uFF1A\u4F20\u65B0\u65E7JSON\uFF0C\u8FD4\u56DEJSON Pointer\u53CA\u65B0\u589E/\u5220\u9664/\u53D8\u5316/\u5931\u6548/\u6062\u590D\uFF1B\u4E0D\u4FDD\u5B58\u5FEB\u7167\u3002\u6570\u7EC4\u6574\u4F53\u6BD4\u8F83\uFF0C\u8C03\u7528\u524D\u6309symbol\u5BF9\u9F50\u3002",
2238
2458
  parameters: {
@@ -2251,7 +2471,6 @@ function apply(ctx, config = {}) {
2251
2471
  "required": true
2252
2472
  }
2253
2473
  },
2254
- output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
2255
2474
  async execute(params) {
2256
2475
  return withJsonInput(params, (path) => runPy("stateless_analysis.py", ["snapshot", "--input", path]));
2257
2476
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-stock-analysis",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "description": "DeepSeek Harness (dsh) plugin for stock analysis, screening, and strategy backtesting across A/HK/US/JP/KR/TW markets",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env python3
2
+ """Offline baseline: python scripts/profile-offline.py > baseline.json.
3
+
4
+ CLI --help includes interpreter/import/argparse cost, never provider latency.
5
+ Synthetic compute timings exclude imports; this is not a production throughput test.
6
+ """
7
+
8
+ import cProfile
9
+ import json
10
+ import platform
11
+ import pstats
12
+ import statistics
13
+ import subprocess
14
+ import sys
15
+ import tempfile
16
+ import time
17
+ from pathlib import Path
18
+
19
+ ROOT = Path(__file__).resolve().parents[1]
20
+ sys.path.insert(0, str(ROOT / "tools"))
21
+
22
+
23
+ def measure(operation, repeats=5):
24
+ samples = []
25
+ for _ in range(repeats):
26
+ start = time.perf_counter()
27
+ operation()
28
+ samples.append(round((time.perf_counter() - start) * 1000, 3))
29
+ return {"median_ms": statistics.median(samples), "samples_ms": samples}
30
+
31
+
32
+ def main():
33
+ cli = {}
34
+ for path in sorted((ROOT / "tools").glob("*.py")):
35
+ if '__name__ == "__main__"' not in path.read_text():
36
+ continue
37
+
38
+ def help_call(path=path):
39
+ subprocess.run(
40
+ [sys.executable, str(path), "--help"],
41
+ stdout=subprocess.DEVNULL,
42
+ stderr=subprocess.PIPE,
43
+ timeout=15,
44
+ check=True,
45
+ )
46
+
47
+ cli[path.name] = measure(help_call, 3)
48
+
49
+ import _cache
50
+ import numpy as np
51
+ import pandas as pd
52
+ from backtest import load_strategy, simulate
53
+ from stateless_analysis import portfolio
54
+ from technical import analyze_records
55
+
56
+ def bars(count):
57
+ rng = np.random.default_rng(68)
58
+ close = 100 * np.exp(np.cumsum(rng.normal(0.0002, 0.01, count)))
59
+ return pd.DataFrame(
60
+ {
61
+ "date": pd.bdate_range("2000-01-03", periods=count).strftime("%Y-%m-%d"),
62
+ "open": close,
63
+ "close": close,
64
+ "high": close * 1.01,
65
+ "low": close * 0.99,
66
+ "volume": rng.integers(100000, 1000000, count),
67
+ }
68
+ )
69
+
70
+ compute = {}
71
+ strategy = load_strategy(str(ROOT / "strategies/examples/ma_crossover.yaml"))
72
+ for count in (120, 2500):
73
+ frame = bars(count)
74
+ records = frame.to_dict("records")
75
+ assert "error" not in analyze_records("TEST", records)
76
+ compute[f"technical_{count}"] = measure(lambda records=records: analyze_records("TEST", records))
77
+ compute[f"backtest_{count}"] = measure(lambda frame=frame: simulate(frame, strategy, 100000, "TEST"))
78
+ for count in (10, 50, 100):
79
+ histories = []
80
+ for i in range(count):
81
+ records = bars(250).to_dict("records")
82
+ histories.append({"symbol": f"S{i}", "prices": [{"date": r["date"], "close": r["close"]} for r in records]})
83
+ payload = {
84
+ "base_currency": "USD",
85
+ "cash": 0,
86
+ "positions": [{"symbol": f"S{i}", "quantity": 1, "price": 100, "currency": "USD"} for i in range(count)],
87
+ "histories": histories,
88
+ }
89
+ result = portfolio(payload)
90
+ assert len(result["correlations"]) == count * (count - 1) // 2
91
+ compute[f"portfolio_{count}_assets_250_dates"] = measure(lambda payload=payload: portfolio(payload), 3)
92
+ profiler = cProfile.Profile()
93
+ profiler.runcall(simulate, frame, strategy, 100000, "TEST")
94
+ stats = pstats.Stats(profiler)
95
+ hotspots = [
96
+ {"function": f"{Path(file).name}:{line}:{name}", "calls": values[1], "cumulative_s": round(values[3], 4)}
97
+ for (file, line, name), values in sorted(stats.stats.items(), key=lambda item: item[1][3], reverse=True)[:12]
98
+ ]
99
+ original_dir = _cache.CACHE_DIR
100
+ with tempfile.TemporaryDirectory() as directory:
101
+ try:
102
+ _cache.CACHE_DIR = Path(directory)
103
+ payload = [{"symbol": str(i), "value": i} for i in range(10000)]
104
+ _cache.cached("profile", 60, lambda: payload)
105
+
106
+ def unexpected_fetch():
107
+ raise AssertionError("warm cache missed")
108
+
109
+ compute["cache_read_10000_rows"] = measure(lambda: _cache.cached("profile", 60, unexpected_fetch))
110
+ finally:
111
+ _cache.CACHE_DIR = original_dir
112
+ print(
113
+ json.dumps(
114
+ {
115
+ "python": platform.python_version(),
116
+ "platform": platform.platform(),
117
+ "cli": cli,
118
+ "compute": compute,
119
+ "backtest_hotspots": hotspots,
120
+ },
121
+ indent=2,
122
+ )
123
+ )
124
+
125
+
126
+ if __name__ == "__main__":
127
+ main()
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env python3
2
+ """Offline workflow baseline with counted, fixed-latency CLI substitutes.
3
+
4
+ Run with the repo venv (pytest/pandas/numpy installed):
5
+ python scripts/profile-workflows.py [checkout-root]
6
+ This measures orchestration only, not provider throughput. Reuses the test OHLCV fixture.
7
+ """
8
+
9
+ import importlib.util
10
+ import json
11
+ import sys
12
+ import threading
13
+ import time
14
+ from collections import Counter
15
+ from pathlib import Path
16
+
17
+ root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[1]
18
+ sys.path.insert(0, str(root / "tools"))
19
+ import anomaly_detect # noqa: E402
20
+ import gather # noqa: E402
21
+ import risk_screening # noqa: E402
22
+ import technical # noqa: E402
23
+ import watchlist_context # noqa: E402
24
+
25
+ spec = importlib.util.spec_from_file_location("perf_fixtures", root / "tests/conftest.py")
26
+ fixtures = importlib.util.module_from_spec(spec)
27
+ spec.loader.exec_module(fixtures)
28
+ bars = fixtures.make_kline_data.__wrapped__()(120, "volatile")
29
+ for row in bars:
30
+ row["date"] = row["date"].strftime("%Y-%m-%d")
31
+ calls = Counter()
32
+ lock = threading.Lock()
33
+ active = peak = 0
34
+
35
+
36
+ def invoke(script, args, **kwargs):
37
+ global active, peak
38
+ if script == "risk_screening.py" and args[0] == "screen":
39
+ return risk_screening.screen_risk(args[1])
40
+ if script == "technical.py":
41
+ rows = invoke("stock_data.py", ["kline", args[1], "--count", "120"])
42
+ return technical.analyze_records(args[1], rows)
43
+ if script == "anomaly_detect.py":
44
+ return anomaly_detect.detect_anomalies(args[1])
45
+ key = script + ":" + args[0]
46
+ with lock:
47
+ calls[key] += 1
48
+ active += 1
49
+ peak = max(peak, active)
50
+ time.sleep(0.01)
51
+ with lock:
52
+ active -= 1
53
+ if args[0] == "kline":
54
+ n = int(args[args.index("--count") + 1]) if "--count" in args else 120
55
+ return bars[-n:]
56
+ if args[0] == "quote":
57
+ return {"price": 10, "pe": 15, "pb": 2}
58
+ if args[0] == "lockup":
59
+ return {"upcoming_30d": [], "flags": [], "available": True}
60
+ if args[0] == "capital_flow":
61
+ return []
62
+ if script == "market_regime.py":
63
+ return {"market": args[1]}
64
+ return []
65
+
66
+
67
+ gather._run = lambda script, args, **kw: json.dumps(invoke(script, args, **kw))
68
+ risk_screening._run_tool = invoke
69
+ anomaly_detect._run_tool = invoke
70
+ anomaly_detect.fetch_kline = lambda symbol, period, count: invoke(
71
+ "stock_data.py", ["kline", symbol, "--count", str(count)]
72
+ )
73
+ watchlist_context._run_json = invoke
74
+ start = time.perf_counter()
75
+ result = watchlist_context.build_context(["600519", "000001", "000002"], workers=3)
76
+ assert result["summary"]["success"] == 3
77
+ print(
78
+ json.dumps(
79
+ {
80
+ "seconds": time.perf_counter() - start,
81
+ "calls": dict(calls),
82
+ "total_calls": sum(calls.values()),
83
+ "peak_leaf_calls": peak,
84
+ "summary": result["summary"],
85
+ }
86
+ )
87
+ )