@v587d/capital-generation 2.31.7 → 2.32.0

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 (75) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +14 -5
  3. package/automation/package.json +1 -1
  4. package/chart-ui/package.json +1 -1
  5. package/config/package.json +1 -1
  6. package/lib/analysis/artifact.d.ts +35 -0
  7. package/lib/analysis/artifact.js +250 -0
  8. package/lib/analysis/registry.d.ts +16 -2
  9. package/lib/analysis/registry.js +133 -23
  10. package/lib/analysis/runtime.d.ts +46 -19
  11. package/lib/analysis/runtime.js +485 -116
  12. package/lib/analysis/schemas.d.ts +46 -0
  13. package/lib/analysis/schemas.js +260 -7
  14. package/lib/analysis/snapshot.d.ts +10 -3
  15. package/lib/analysis/snapshot.js +298 -46
  16. package/lib/analysis/storage.d.ts +29 -1
  17. package/lib/analysis/storage.js +39 -6
  18. package/lib/analysis/tools.js +270 -29
  19. package/lib/analysis/types.d.ts +257 -3
  20. package/lib/analysis/types.js +31 -1
  21. package/lib/analysis/validation.d.ts +37 -1
  22. package/lib/analysis/validation.js +315 -1
  23. package/lib/chart/tool.js +1 -0
  24. package/lib/data-collector/store.d.ts +7 -1
  25. package/lib/data-collector/store.js +14 -0
  26. package/lib/python-runtime/child-env.d.ts +4 -2
  27. package/lib/python-runtime/child-env.js +21 -14
  28. package/lib/python-runtime/manager.d.ts +8 -0
  29. package/lib/python-runtime/manager.js +30 -14
  30. package/lib/sources/tencent-http.js +9 -3
  31. package/lib/web-retriever/local-fetch.d.ts +1 -1
  32. package/lib/web-retriever/local-fetch.js +1 -1
  33. package/lib/web-retriever/wind-client.d.ts +1 -1
  34. package/lib/web-retriever/wind-client.js +1 -1
  35. package/package.json +2 -1
  36. package/plugin-page/README.en.md +1 -1
  37. package/plugin-page/README.zh.md +1 -1
  38. package/preset/capital-generation/agent.patch.yml +1 -1
  39. package/preset/capital-generation/skills/capital-analysis-evidence/SKILL.md +15 -2
  40. package/preset/capital-generation/skills/capital-analysis-synthesis/SKILL.md +54 -9
  41. package/preset/capital-generation/skills/capital-analysis-validation/SKILL.md +41 -7
  42. package/preset/capital-generation/skills/capital-equity-research/SKILL.md +1 -1
  43. package/preset/capital-generation/skills/capital-event-impact/SKILL.md +20 -1
  44. package/preset/capital-generation/skills/capital-orchestration/SKILL.md +1 -1
  45. package/preset/capital-generation/skills/capital-senior-analysis/SKILL.md +38 -8
  46. package/python/analysis/lib/__init__.py +9 -0
  47. package/python/analysis/lib/errors.py +12 -0
  48. package/python/analysis/lib/eventstudy.py +164 -0
  49. package/python/analysis/lib/frames.py +95 -0
  50. package/python/analysis/lib/gaps.py +54 -0
  51. package/python/analysis/lib/jsonio.py +61 -0
  52. package/python/analysis/lib/metrics.py +147 -0
  53. package/python/analysis/lib/pit.py +84 -0
  54. package/python/analysis/lib/prices.py +140 -0
  55. package/python/analysis/lib/timeaxis.py +142 -0
  56. package/python/analysis/lib/version.py +3 -0
  57. package/python/analysis/modules/comps_valuation/module.py +26 -59
  58. package/python/analysis/modules/earnings_bridge/module.py +20 -77
  59. package/python/analysis/modules/event_study/LICENSE +21 -0
  60. package/python/analysis/modules/event_study/module.json +217 -0
  61. package/python/analysis/modules/event_study/module.py +979 -0
  62. package/python/analysis/modules/financial_quality/module.py +25 -88
  63. package/python/analysis/modules/hello/module.py +4 -0
  64. package/python/analysis/modules/kline_quality/module.py +26 -132
  65. package/python/analysis/modules/portfolio_stress/module.py +27 -112
  66. package/python/analysis/modules/returns_risk/module.json +108 -25
  67. package/python/analysis/modules/returns_risk/module.py +68 -227
  68. package/python/analysis/modules/scenario_sensitivity/module.py +11 -44
  69. package/python/analysis/modules/simple_backtest/module.json +244 -36
  70. package/python/analysis/modules/simple_backtest/module.py +1146 -459
  71. package/python/analysis/modules/statement_integrity/module.py +35 -87
  72. package/python/analysis/modules/technical_indicators/module.py +33 -152
  73. package/python/analysis/modules/valuation_sensitivity/module.py +30 -61
  74. package/python/worker.py +5 -0
  75. package/watchlist/package.json +1 -1
@@ -17,6 +17,11 @@ from __future__ import annotations
17
17
 
18
18
  import math
19
19
 
20
+ from lib import gaps as gapvocab
21
+ from lib.errors import AnalysisInputError
22
+ from lib.frames import require_text as _text
23
+ from lib.jsonio import fmt6 as _fmt6, pct as _pct, require_number, round_metric as _round
24
+
20
25
  MODULE_ID = "valuation_sensitivity"
21
26
  MODULE_VERSION = "1.0.0"
22
27
  OUTPUT_NAME = "dcf_sensitivity"
@@ -48,32 +53,10 @@ LIMITATIONS = [
48
53
  ]
49
54
 
50
55
 
51
- class AnalysisInputError(Exception):
52
- """The input cannot be used at all; the host maps `analysis_code` to the stable protocol code."""
53
-
54
- analysis_code = "analysis_input_invalid"
55
-
56
-
57
- def _round(value):
58
- rounded = round(float(value), VALUE_DECIMALS)
59
- if rounded == 0:
60
- return 0
61
- return int(rounded) if float(rounded).is_integer() else rounded
62
-
63
-
64
56
  def _maybe(value):
65
57
  return None if value is None else _round(value)
66
58
 
67
59
 
68
- def _number(value, label):
69
- if isinstance(value, bool) or not isinstance(value, (int, float)):
70
- raise AnalysisInputError(f"{label} 必须是数字,实际是 {value!r}")
71
- number = float(value)
72
- if number != number or number in (float("inf"), float("-inf")):
73
- raise AnalysisInputError(f"{label} 必须是有限数字,实际是 {value!r}")
74
- return number
75
-
76
-
77
60
  def _finite(value, label):
78
61
  number = float(value)
79
62
  if number != number or number in (float("inf"), float("-inf")):
@@ -81,43 +64,25 @@ def _finite(value, label):
81
64
  return number
82
65
 
83
66
 
84
- def _fmt6(value):
85
- """Six decimals with trailing zeros trimmed: the display shape of every rounded number."""
86
- text = f"{_round(value):.6f}".rstrip("0").rstrip(".")
87
- return text if text not in ("", "-0") else "0"
88
-
89
-
90
- def _pct(value):
91
- return f"{value * 100:.2f}%"
92
-
93
-
94
- def _text(value, label):
95
- if value is None:
96
- return None
97
- if not isinstance(value, str) or not value.strip():
98
- raise AnalysisInputError(f"{label} 必须是非空字符串")
99
- return value.strip()
100
-
101
-
102
67
  def _projections(params):
103
68
  raw = params.get("projections")
104
69
  if not isinstance(raw, list) or not raw:
105
70
  raise AnalysisInputError("projections 必须是非空数组:至少给出 1 期预测期自由现金流(模块不估计 FCF)")
106
71
  if len(raw) > MAX_PROJECTION_YEARS:
107
72
  raise AnalysisInputError(f"projections 最多 {MAX_PROJECTION_YEARS} 期,实际 {len(raw)} 期")
108
- return [_number(item, f"projections[{index}]") for index, item in enumerate(raw)]
73
+ return [require_number(item, f"projections[{index}]") for index, item in enumerate(raw)]
109
74
 
110
75
 
111
76
  def _required_number(params, name, note):
112
77
  if params.get(name) is None:
113
78
  raise AnalysisInputError(f"{name} 缺失:{note}")
114
- return _number(params.get(name), name)
79
+ return require_number(params.get(name), name)
115
80
 
116
81
 
117
82
  def _optional_number(params, name):
118
83
  if params.get(name) is None:
119
84
  return None
120
- return _number(params.get(name), name)
85
+ return require_number(params.get(name), name)
121
86
 
122
87
 
123
88
  def _capm(params):
@@ -128,7 +93,7 @@ def _capm(params):
128
93
  f"未给 wacc,CAPM 推导缺少 {'、'.join(missing)};"
129
94
  f"要么直接给 wacc,要么把 {' / '.join(CAPM_FIELDS)} 六个参数给全"
130
95
  )
131
- values = {name: _number(params[name], f"CAPM {name}") for name in CAPM_FIELDS}
96
+ values = {name: require_number(params[name], f"CAPM {name}") for name in CAPM_FIELDS}
132
97
  if not 0.0 <= values["debt_weight"] <= 1.0:
133
98
  raise AnalysisInputError(f"CAPM debt_weight 必须在 0..1,实际是 {values['debt_weight']!r}")
134
99
  if not 0.0 <= values["tax_rate"] <= 1.0:
@@ -154,7 +119,7 @@ def _resolve_wacc(params):
154
119
  wacc_raw = params.get("wacc")
155
120
  capm_given = any(params.get(name) is not None for name in CAPM_FIELDS)
156
121
  if wacc_raw is not None:
157
- return _number(wacc_raw, "wacc"), "param", None, capm_given
122
+ return require_number(wacc_raw, "wacc"), "param", None, capm_given
158
123
  wacc, capm = _capm(params)
159
124
  return wacc, "capm", capm, False
160
125
 
@@ -170,7 +135,7 @@ def _axis(params, key, *, floor, inclusive):
170
135
  raise AnalysisInputError(f"{key} 最多 {MAX_GRID_VALUES} 个值,实际 {len(raw)} 个")
171
136
  values = []
172
137
  for index, item in enumerate(raw):
173
- value = _round(_number(item, f"{key}[{index}]"))
138
+ value = _round(require_number(item, f"{key}[{index}]"))
174
139
  if inclusive:
175
140
  if value < floor:
176
141
  raise AnalysisInputError(f"{key} 里的值必须 ≥ {_fmt6(floor)},实际是 {_fmt6(value)}")
@@ -273,10 +238,10 @@ def _grid(projections, wacc_axis, growth_axis, net_debt, minority_interest, shar
273
238
  for wacc in wacc_axis:
274
239
  value = None
275
240
  if wacc <= terminal_growth:
276
- gaps.append(
241
+ gaps.append(gapvocab.field_missing(
277
242
  f"敏感性网格格点无定义:wacc={_fmt6(wacc)} ≤ terminal_growth={_fmt6(terminal_growth)},"
278
243
  "Gordon 终值分母 ≤ 0,该格写 null"
279
- )
244
+ ))
280
245
  else:
281
246
  if net_debt is None or shares_outstanding is None:
282
247
  # The bridge-level gap already explains why every cell is null here.
@@ -287,9 +252,9 @@ def _grid(projections, wacc_axis, growth_axis, net_debt, minority_interest, shar
287
252
  equity = _equity_value(model, net_debt, minority_interest)
288
253
  value = _finite(equity / shares_outstanding, "每股模型值")
289
254
  except AnalysisInputError as error:
290
- gaps.append(
255
+ gaps.append(gapvocab.field_missing(
291
256
  f"敏感性网格格点无法计算:wacc={_fmt6(wacc)}、terminal_growth={_fmt6(terminal_growth)}:{error}"
292
- )
257
+ ))
293
258
  cells.append({
294
259
  "wacc": _round(wacc),
295
260
  "terminal_growth": _round(terminal_growth),
@@ -314,28 +279,28 @@ def _sensitivity(wacc_axis, growth_axis, rows, center_wacc, center_growth, gaps)
314
279
  wacc_plus = growth_plus = None
315
280
  wacc_delta = growth_delta = None
316
281
  if center_column is None or center_row is None:
317
- gaps.append(
282
+ gaps.append(gapvocab.field_missing(
318
283
  f"敏感性摘要未计算:网格里没有中心格(wacc={_fmt6(center_wacc)}、"
319
284
  f"terminal_growth={_fmt6(center_growth)})"
320
- )
285
+ ))
321
286
  else:
322
287
  center_value = rows[center_row][center_column]
323
288
  center["value_per_share"] = None if center_value is None else _round(center_value)
324
289
  wacc_column = _fit(wacc_axis, _round(center_wacc + DEFAULT_GRID_STEP))
325
290
  if wacc_column is None:
326
- gaps.append(
291
+ gaps.append(gapvocab.field_missing(
327
292
  f"敏感性摘要未计算:网格没有 wacc+1pp 标准点(wacc={_fmt6(center_wacc + DEFAULT_GRID_STEP)}),"
328
293
  "wacc_1pp_value_per_share 写 null"
329
- )
294
+ ))
330
295
  elif center_value is not None and rows[center_row][wacc_column] is not None:
331
296
  wacc_plus = _round(rows[center_row][wacc_column])
332
297
  wacc_delta = _round(rows[center_row][wacc_column] - center_value)
333
298
  growth_row = _fit(growth_axis, _round(center_growth + DEFAULT_GRID_STEP))
334
299
  if growth_row is None:
335
- gaps.append(
300
+ gaps.append(gapvocab.field_missing(
336
301
  f"敏感性摘要未计算:网格没有 g+1pp 标准点(g={_fmt6(center_growth + DEFAULT_GRID_STEP)}),"
337
302
  "growth_1pp_value_per_share 写 null"
338
- )
303
+ ))
339
304
  elif center_value is not None and rows[growth_row][center_column] is not None:
340
305
  growth_plus = _round(rows[growth_row][center_column])
341
306
  growth_delta = _round(rows[growth_row][center_column] - center_value)
@@ -416,16 +381,20 @@ def analyze(request: dict, context) -> dict:
416
381
  if equity_value is not None and shares_outstanding is not None:
417
382
  value_per_share = _finite(equity_value / shares_outstanding, "每股模型值")
418
383
  if net_debt is None:
419
- gaps.append("net_debt 缺失:只算到企业价值,股权价值与每股模型值未计算(不按 0 处理)")
384
+ gaps.append(gapvocab.field_missing(
385
+ "net_debt 缺失:只算到企业价值,股权价值与每股模型值未计算(不按 0 处理)"
386
+ ))
420
387
  if shares_outstanding is None:
421
- gaps.append("shares_outstanding 缺失:每股模型值与敏感性网格未计算(不猜股本)")
388
+ gaps.append(gapvocab.field_missing(
389
+ "shares_outstanding 缺失:每股模型值与敏感性网格未计算(不猜股本)"
390
+ ))
422
391
  terminal_share = None
423
392
  if enterprise_value > 0:
424
393
  terminal_share = model["terminal_present_value"] / enterprise_value
425
394
  else:
426
- gaps.append(
395
+ gaps.append(gapvocab.field_missing(
427
396
  f"企业价值非正({_fmt6(enterprise_value)}):终值占比无定义,terminal_value_share 写 null"
428
- )
397
+ ))
429
398
 
430
399
  cells, rows, null_cells = _grid(
431
400
  projections, wacc_axis, growth_axis, net_debt, minority_interest, shares_outstanding, mid_year, gaps
@@ -516,7 +485,7 @@ def analyze(request: dict, context) -> dict:
516
485
  summary += "这是给定假设下的模型值,不是目标价、不是预测。"
517
486
 
518
487
  payload = {
519
- "protocol": "capital.analysis/v1",
488
+ "protocol": "capital.analysis/v2",
520
489
  "module_id": MODULE_ID,
521
490
  "module_version": MODULE_VERSION,
522
491
  "analysis_id": request.get("analysis_id"),
package/python/worker.py CHANGED
@@ -221,6 +221,11 @@ def analysis(
221
221
  log_line(message)
222
222
 
223
223
  try:
224
+ # The shared primitives live next to this worker, not next to the module (test fixtures and
225
+ # packaged modules must resolve the same `lib`).
226
+ analysis_root = Path(__file__).resolve().parent / "analysis"
227
+ if (analysis_root / "lib").is_dir() and str(analysis_root) not in sys.path:
228
+ sys.path.insert(0, str(analysis_root))
224
229
  request = json.loads((run_dir / "request.json").read_text(encoding="utf-8"))
225
230
  module_path = (module_dir / entrypoint).resolve()
226
231
  if not inside(module_dir, module_path) or not module_path.is_file():
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@v587d/watchlist",
3
- "version": "2.31.7",
3
+ "version": "2.32.0",
4
4
  "description": "自动模式的用户自选股(宿主级清单 + Fuyao 报价快照 + /capital-watchlist 路由 + 输入框菜单弹窗):由 @v587d/capital-generation 随包携带并作为 host 平面行挂载。",
5
5
  "license": "MIT",
6
6
  "type": "module",