alphaspec 0.1.0__tar.gz

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 (72) hide show
  1. alphaspec-0.1.0/.github/workflows/ci.yml +17 -0
  2. alphaspec-0.1.0/.github/workflows/pages.yml +39 -0
  3. alphaspec-0.1.0/.github/workflows/release.yml +36 -0
  4. alphaspec-0.1.0/.gitignore +7 -0
  5. alphaspec-0.1.0/CHANGELOG.md +21 -0
  6. alphaspec-0.1.0/LICENSE +202 -0
  7. alphaspec-0.1.0/PKG-INFO +322 -0
  8. alphaspec-0.1.0/README.md +295 -0
  9. alphaspec-0.1.0/README.zh-CN.md +269 -0
  10. alphaspec-0.1.0/catalog/i18n/en.json +1016 -0
  11. alphaspec-0.1.0/catalog/indicators.json +9790 -0
  12. alphaspec-0.1.0/conformance/README.md +10 -0
  13. alphaspec-0.1.0/conformance/cases/as001-unresolved-indicator.json +65 -0
  14. alphaspec-0.1.0/conformance/cases/as002-unresolved-param.json +67 -0
  15. alphaspec-0.1.0/conformance/cases/as003-htf-live-lookahead.json +83 -0
  16. alphaspec-0.1.0/conformance/cases/as004-indicator-cycle.json +83 -0
  17. alphaspec-0.1.0/conformance/cases/as005-unknown-type.json +65 -0
  18. alphaspec-0.1.0/conformance/cases/as006-unknown-param.json +66 -0
  19. alphaspec-0.1.0/conformance/cases/as007-cn-indicator-on-us-market.json +81 -0
  20. alphaspec-0.1.0/conformance/cases/as008-primary-only-timeframe-ignored.json +77 -0
  21. alphaspec-0.1.0/conformance/cases/as009-finer-than-primary.json +66 -0
  22. alphaspec-0.1.0/conformance/cases/as010-tick-timeframe.json +65 -0
  23. alphaspec-0.1.0/conformance/cases/as011-missing-suffix.json +64 -0
  24. alphaspec-0.1.0/conformance/cases/as012-ai-without-search-trials.json +65 -0
  25. alphaspec-0.1.0/conformance/cases/as013-no-exit.json +56 -0
  26. alphaspec-0.1.0/conformance/cases/as014-intraday-on-daily.json +80 -0
  27. alphaspec-0.1.0/conformance/cases/as015-unused-indicator.json +69 -0
  28. alphaspec-0.1.0/conformance/cases/as101-duplicate-symbol.json +42 -0
  29. alphaspec-0.1.0/conformance/cases/as102-all-null.json +38 -0
  30. alphaspec-0.1.0/conformance/cases/as103-asof-future.json +50 -0
  31. alphaspec-0.1.0/conformance/cases/as104-zero-as-missing.json +58 -0
  32. alphaspec-0.1.0/conformance/cases/as105-weights-over-one.json +32 -0
  33. alphaspec-0.1.0/conformance/cases/schema-bare-number-operand.json +63 -0
  34. alphaspec-0.1.0/conformance/cases/schema-no-code-fields.json +67 -0
  35. alphaspec-0.1.0/conformance/cases/schema-noncanonical-timeframe.json +65 -0
  36. alphaspec-0.1.0/conformance/cases/schema-not-uses-rule.json +68 -0
  37. alphaspec-0.1.0/conformance/cases/schema-score-without-direction.json +49 -0
  38. alphaspec-0.1.0/conformance/cases/schema-scores-and-weights.json +56 -0
  39. alphaspec-0.1.0/conformance/cases/schema-universe-mode-conflict.json +63 -0
  40. alphaspec-0.1.0/conformance/cases/score-valid.json +48 -0
  41. alphaspec-0.1.0/conformance/cases/valid-minimal.json +63 -0
  42. alphaspec-0.1.0/docs/MARKETS.md +29 -0
  43. alphaspec-0.1.0/docs/RULES.md +133 -0
  44. alphaspec-0.1.0/docs/RUNTIME_API.md +80 -0
  45. alphaspec-0.1.0/docs/SPEC.md +99 -0
  46. alphaspec-0.1.0/examples/artifacts/01-ema-cross-us-equity.json +20 -0
  47. alphaspec-0.1.0/examples/artifacts/02-rsi-reversion-params.json +25 -0
  48. alphaspec-0.1.0/examples/artifacts/03-mtf-trend-pullback.json +25 -0
  49. alphaspec-0.1.0/examples/artifacts/04-breakout-retest-sequence.json +58 -0
  50. alphaspec-0.1.0/examples/artifacts/05-cn-limit-up-pullback.json +33 -0
  51. alphaspec-0.1.0/examples/scores/01-model-scores-cn-stock.json +17 -0
  52. alphaspec-0.1.0/examples/scores/02-target-weights-us-equity.json +13 -0
  53. alphaspec-0.1.0/pyproject.toml +57 -0
  54. alphaspec-0.1.0/schema/0.1/artifact.schema.json +259 -0
  55. alphaspec-0.1.0/schema/0.1/common.schema.json +96 -0
  56. alphaspec-0.1.0/schema/0.1/evidence.schema.json +152 -0
  57. alphaspec-0.1.0/schema/0.1/gate-result.schema.json +55 -0
  58. alphaspec-0.1.0/schema/0.1/score.schema.json +79 -0
  59. alphaspec-0.1.0/skills/alphaspec/SKILL.md +93 -0
  60. alphaspec-0.1.0/src/alphaspec/__init__.py +8 -0
  61. alphaspec-0.1.0/src/alphaspec/_resources.py +77 -0
  62. alphaspec-0.1.0/src/alphaspec/cli.py +360 -0
  63. alphaspec-0.1.0/src/alphaspec/client.py +146 -0
  64. alphaspec-0.1.0/src/alphaspec/lints.py +266 -0
  65. alphaspec-0.1.0/src/alphaspec/mcp_server.py +231 -0
  66. alphaspec-0.1.0/src/alphaspec/validate.py +108 -0
  67. alphaspec-0.1.0/tests/test_cli_reference.py +78 -0
  68. alphaspec-0.1.0/tests/test_client.py +152 -0
  69. alphaspec-0.1.0/tests/test_conformance.py +23 -0
  70. alphaspec-0.1.0/tests/test_examples.py +29 -0
  71. alphaspec-0.1.0/tests/test_mcp_server.py +91 -0
  72. alphaspec-0.1.0/tools/export_indicator_catalog.py +153 -0
@@ -0,0 +1,17 @@
1
+ name: ci
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ python: ["3.10", "3.12"]
9
+ steps:
10
+ - uses: actions/checkout@v4
11
+ - uses: actions/setup-python@v5
12
+ with:
13
+ python-version: ${{ matrix.python }}
14
+ - run: pip install -e '.[dev]'
15
+ - run: pytest -q
16
+ - name: examples validate cleanly with --strict
17
+ run: alphaspec validate --strict examples/artifacts/*.json examples/scores/*.json
@@ -0,0 +1,39 @@
1
+ name: pages
2
+ # Serves the schemas at their $id URLs: https://openalpha-dev.github.io/alphaspec/schema/0.1/<kind>.schema.json
3
+ # One-time setup: repository Settings → Pages → Source: GitHub Actions.
4
+ on:
5
+ push:
6
+ branches: [main]
7
+ paths: ["schema/**", "catalog/**", ".github/workflows/pages.yml"]
8
+ workflow_dispatch:
9
+ permissions:
10
+ contents: read
11
+ pages: write
12
+ id-token: write
13
+ concurrency:
14
+ group: pages
15
+ cancel-in-progress: true
16
+ jobs:
17
+ deploy:
18
+ runs-on: ubuntu-latest
19
+ environment:
20
+ name: github-pages
21
+ url: ${{ steps.deployment.outputs.page_url }}
22
+ steps:
23
+ - uses: actions/checkout@v4
24
+ - name: assemble site
25
+ run: |
26
+ mkdir -p _site
27
+ cp -r schema catalog _site/
28
+ {
29
+ echo '<!doctype html><meta charset="utf-8"><title>AlphaSpec schemas</title>'
30
+ echo '<h1>AlphaSpec</h1><p>Contract schemas and indicator catalog. Source: <a href="https://github.com/openalpha-dev/alphaspec">openalpha-dev/alphaspec</a></p><ul>'
31
+ for f in schema/*/*.json catalog/indicators.json; do echo "<li><a href=\"$f\">$f</a></li>"; done
32
+ echo '</ul>'
33
+ } > _site/index.html
34
+ - uses: actions/configure-pages@v5
35
+ - uses: actions/upload-pages-artifact@v3
36
+ with:
37
+ path: _site
38
+ - id: deployment
39
+ uses: actions/deploy-pages@v4
@@ -0,0 +1,36 @@
1
+ name: release
2
+ # Push a tag vX.Y.Z → test → build → publish to PyPI via Trusted Publishing (no API token stored).
3
+ # One-time setup on pypi.org: add a "pending publisher" for project alphaspec pointing at this
4
+ # repository, workflow release.yml, environment pypi.
5
+ on:
6
+ push:
7
+ tags: ["v*"]
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v4
13
+ - uses: actions/setup-python@v5
14
+ with:
15
+ python-version: "3.12"
16
+ - run: pip install -e '.[dev]' build
17
+ - run: pytest -q
18
+ - name: tag matches package version
19
+ run: test "v$(python -c 'import alphaspec; print(alphaspec.__version__)')" = "${GITHUB_REF_NAME}"
20
+ - run: python -m build
21
+ - uses: actions/upload-artifact@v4
22
+ with:
23
+ name: dist
24
+ path: dist/
25
+ publish:
26
+ needs: build
27
+ runs-on: ubuntu-latest
28
+ environment: pypi
29
+ permissions:
30
+ id-token: write
31
+ steps:
32
+ - uses: actions/download-artifact@v4
33
+ with:
34
+ name: dist
35
+ path: dist/
36
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,7 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.egg-info/
4
+ dist/
5
+ build/
6
+ .pytest_cache/
7
+ .idea/
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 — 2026-10-09
4
+
5
+ - Contract 0.1 drafts: `artifact`, `score`, `gate-result`, `evidence`, shared `common` definitions.
6
+ - Indicator catalog generated from the reference runtime (core / ext.cn / ext.l2 / ext.data profiles).
7
+ - Lint rules AS001–AS015 (artifact) and AS101–AS105 (score), each with a conformance case.
8
+ - Python validator and `alphaspec` CLI.
9
+ - Agent Skill in `skills/alphaspec`.
10
+ - Runtime client (`alphaspec.Client`, standard library only) and CLI commands: `whoami`, `submit`, `evaluate`, `result`, `list`, `promote`.
11
+ - `docs/RUNTIME_API.md`: the agent API a runtime exposes.
12
+ - Indicator catalog now carries meaning: summary, parameter descriptions, unit, pitfalls, in Chinese and English (`catalog/i18n/en.json`, keyed by the Chinese text so a changed description fails the tests until it is re-translated). `catalog --lang en|zh`, MCP `lang`.
13
+ - Reference commands so an installed Agent Skill needs no checkout: `catalog`, `schema`, `examples`, `docs`, `rules --explain`. Examples and docs ship inside the wheel.
14
+ - Agent Skill rewritten to be self-contained (`pip install alphaspec` + the commands above).
15
+ - RUNTIME_API: evaluation is one continuous backtest split into in-sample / out-of-sample segments; daily strategies use 4y + 1y; out-of-sample is judged relative to the market index.
16
+ - gate-result checks carry `metric`, naming what `actual` measures; the CLI prints every check's hint, not only failing ones.
17
+ - Reference runtime: `DONCHIAN_UPPER / MIDDLE / LOWER` now cover the N bars **before** the current one (turtle convention), so `close > DONCHIAN_UPPER` is a reachable breakout. Previously the band included the current bar and that condition could never hold.
18
+ - Reference runtime: Deflated Sharpe pass line 0.95 → 0.90 (documented in RUNTIME_API with the Sharpe it implies per number of trials).
19
+ - MCP server `alphaspec-mcp` (extra `alphaspec[mcp]`, stdio) exposing the offline reference tools and the runtime calls; no order tool.
20
+ - `alphaspec skill [--install DIR]`: the Agent Skill ships in the wheel, so installing it needs no checkout.
21
+ - Schema `$id` URLs: `https://openalpha-dev.github.io/alphaspec/schema/0.1/…`, served by GitHub Pages from this repository.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,322 @@
1
+ Metadata-Version: 2.5
2
+ Name: alphaspec
3
+ Version: 0.1.0
4
+ Summary: The auditable contract between alpha and execution: JSON schemas, a validator and lint rules for declarative trading strategies.
5
+ Project-URL: Homepage, https://github.com/openalpha-dev/alphaspec
6
+ Project-URL: Documentation, https://github.com/openalpha-dev/alphaspec#readme
7
+ Project-URL: Changelog, https://github.com/openalpha-dev/alphaspec/blob/main/CHANGELOG.md
8
+ Project-URL: Schemas, https://openalpha-dev.github.io/alphaspec/schema/0.1/
9
+ Author: OpenAlpha contributors
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: ai-agent,backtest,dsl,json-schema,quant,strategy,trading
13
+ Classifier: Development Status :: 2 - Pre-Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Financial and Insurance Industry
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Topic :: Office/Business :: Financial :: Investment
18
+ Requires-Python: >=3.10
19
+ Requires-Dist: jsonschema>=4.18
20
+ Requires-Dist: referencing>=0.30
21
+ Provides-Extra: dev
22
+ Requires-Dist: mcp>=1.2; extra == 'dev'
23
+ Requires-Dist: pytest>=7; extra == 'dev'
24
+ Provides-Extra: mcp
25
+ Requires-Dist: mcp>=1.2; extra == 'mcp'
26
+ Description-Content-Type: text/markdown
27
+
28
+ # AlphaSpec
29
+
30
+ **A business language for trading strategies that humans, AI agents and execution systems can all read, check and agree on.**
31
+
32
+ [中文说明](https://github.com/openalpha-dev/alphaspec/blob/main/README.zh-CN.md) ·
33
+ [Spec](https://github.com/openalpha-dev/alphaspec/blob/main/docs/SPEC.md) ·
34
+ [Lint rules](https://github.com/openalpha-dev/alphaspec/blob/main/docs/RULES.md) ·
35
+ [Runtime API](https://github.com/openalpha-dev/alphaspec/blob/main/docs/RUNTIME_API.md) ·
36
+ [Reference runtime: EasyQuant](https://www.goeasyquant.com)
37
+
38
+ AlphaSpec is an open specification for handing a trading strategy, or a model's output, to an
39
+ execution system. A strategy is a JSON document: indicators, entry and exit conditions, universe
40
+ and risk settings. There is no code in it, so everything that matters can be checked before
41
+ anything runs, by a validator, by a risk reviewer, or by the AI that wrote it.
42
+
43
+ ```bash
44
+ pip install "alphaspec[mcp]"
45
+ alphaspec validate my_strategy.json
46
+ ```
47
+
48
+ ## Why a strategy DSL, and why now
49
+
50
+ > **The bottleneck of AI-driven research was never the model. It is the missing feedback loop: a
51
+ > model never learns what its signals did in a real market.**
52
+
53
+ **AI made writing strategies cheap. It did not make trusting them cheap.** An agent can produce a
54
+ hundred candidate strategies in an afternoon. What it cannot do on its own is tell which of them
55
+ survive look-ahead bias, multiple testing and real trading rules. The bottleneck has moved from
56
+ *producing* ideas to *verifying* them, and verification needs something both sides can read.
57
+
58
+ Generated code is the wrong medium for that. It is different every time, reveals what it does only
59
+ when executed, needs a sandbox, and a risk desk cannot approve it line by line. A declarative
60
+ document can be:
61
+
62
+ - **validated on the spot** by a schema and semantic lint rules, with no sandbox;
63
+ - **constrained at generation time**, so a model can only emit well-formed documents;
64
+ - **diffed**: "the oversold threshold changed from 30 to 28" is a change a person can review;
65
+ - **audited** field by field by a risk desk;
66
+ - **free of injection surface**: there is nowhere to put code.
67
+
68
+ That makes AlphaSpec a **business language between people and machines**. A person states an idea in
69
+ plain words; an agent turns it into a document built from a shared vocabulary of indicators whose
70
+ meaning, units and pitfalls are written down; the validator says exactly what is wrong and why; the
71
+ runtime executes the same document the person reviewed. Every party is looking at the same thing.
72
+
73
+ AlphaSpec is **not trying to replace Python**. Research in whatever you like: pandas, PyTorch,
74
+ LightGBM, any agent framework. AlphaSpec is the boundary: what crosses into execution must be a
75
+ document that can be checked, compared and audited.
76
+
77
+ ## What it defines
78
+
79
+ Two ways in, written by you or your agent:
80
+
81
+ | Level | Document | For | You send |
82
+ |---|---|---|---|
83
+ | **L1 · Score** | [`score`](https://github.com/openalpha-dev/alphaspec/blob/main/schema/0.1/score.schema.json) | ML teams, factor researchers | `(asOf, market, symbol, score)` or target weights |
84
+ | **L2 · Artifact** | [`artifact`](https://github.com/openalpha-dev/alphaspec/blob/main/schema/0.1/artifact.schema.json) | rule-based strategies, AI agents | a rule tree with parameters, universe and risk overrides |
85
+
86
+ Two ways back, written by the runtime and never by the submitter:
87
+
88
+ | Document | What it answers |
89
+ |---|---|
90
+ | [`gate-result`](https://github.com/openalpha-dev/alphaspec/blob/main/schema/0.1/gate-result.schema.json) | Did it pass, judged on figures the runtime **recomputed itself**, check by check |
91
+ | [`evidence`](https://github.com/openalpha-dev/alphaspec/blob/main/schema/0.1/evidence.schema.json) | What happened in a real market: rejections, slippage, fills, deviation from target, every number with its caliber |
92
+
93
+ The way back is the point. Research tools see prices, news and filings; none of that says whether
94
+ an order was rejected, how much slippage it paid, or how far the portfolio drifted from its target.
95
+ Only an execution system knows, and only if it reports it in a form the research side can read.
96
+ `gate-result` and `evidence` are that form: the feedback loop the model was missing.
97
+
98
+ ```mermaid
99
+ flowchart LR
100
+ R["Your research<br/>Python · ML · AI agent"] -->|AlphaSpec document| V["alphaspec<br/>local validation"]
101
+ V -->|HTTPS · API key| E["Runtime<br/>backtest on windows it chooses"]
102
+ E --> G{"Gate"}
103
+ G -->|passed| P["Paper trading"]
104
+ P -.->|institutions| L["Live execution"]
105
+ G -.->|gate-result| R
106
+ P -.->|evidence| R
107
+ ```
108
+
109
+ A short artifact:
110
+
111
+ ```json
112
+ {
113
+ "contractVersion": "0.1",
114
+ "kind": "artifact",
115
+ "title": "RSI rebound above the 60-day average",
116
+ "market": "cn_stock",
117
+ "timeframe": "1d",
118
+ "dsl": {
119
+ "version": 1,
120
+ "params": { "oversold": 30 },
121
+ "indicators": {
122
+ "rsi": { "type": "RSI", "period": 14 },
123
+ "ma60": { "type": "SMA", "period": 60 }
124
+ },
125
+ "entry": { "type": "AND",
126
+ "left": { "type": "CROSS_UP", "left": { "ind": "rsi" }, "right": { "ref": "oversold" } },
127
+ "right": { "type": "GT", "left": { "ind": "close" }, "right": { "ind": "ma60" } } },
128
+ "exit": { "type": "GTE", "left": { "ind": "rsi" }, "right": { "const": 70 } }
129
+ },
130
+ "universe": { "mode": "SYMBOLS", "symbols": ["600519.SH", "000333.SZ"] },
131
+ "producer": { "kind": "AI_AGENT", "name": "my-agent@0.1", "searchTrials": 3 }
132
+ }
133
+ ```
134
+
135
+ `producer.searchTrials` is how many candidates the search tried, discarded ones included. The more
136
+ you tried, the higher the bar the gate sets, so luck is not mistaken for skill.
137
+
138
+ ## How a submission runs today
139
+
140
+ The reference runtime is **EasyQuant**. A submitted artifact goes through the same path a strategy
141
+ built in its own UI does:
142
+
143
+ 1. **Validate twice.** Locally by `alphaspec`, then again by the runtime, which also compiles the
144
+ rule tree in its own engine.
145
+ 2. **Backtest on windows the runtime chooses.** One continuous backtest, reported as two segments:
146
+ for daily strategies the last 4 years in-sample and the most recent year out-of-sample; for
147
+ intraday strategies 18 months and 6 months. Returns are annualised on trading time; China's T+1,
148
+ price limits and lot sizes apply.
149
+ 3. **Gate, check by check.** In-sample return, drawdown and Sharpe; out-of-sample performance
150
+ **relative to the market index** over the same period; a Deflated Sharpe test that uses
151
+ `searchTrials`; structural look-ahead checks. Each check returns its metric, actual value,
152
+ threshold and a hint, so an agent can act on it instead of guessing.
153
+ 4. **Promote to paper trading.** A passing artifact becomes a paper-trading strategy in the
154
+ submitter's account, created disabled; the person starts it.
155
+ 5. **Go live, for institutions.** On the institutional edition the same strategy runs against
156
+ broker counters through the same risk checks and ledger, and execution evidence flows back.
157
+
158
+ An API key can submit, evaluate, read results and promote. It cannot place orders or touch
159
+ accounts. An agent passes through exactly the same door as a person.
160
+
161
+ ## About EasyQuant
162
+
163
+ [EasyQuant](https://www.goeasyquant.com) is a quantitative strategy delivery platform: research,
164
+ backtesting, paper trading and live execution on one engine, so the number you saw in a backtest is
165
+ computed the same way as the order that reaches the market. It is built around four principles:
166
+ **explainable** (every signal, rejection and fill carries a reason), **reproducible** (one engine for
167
+ backtest, paper and live), **operable** (monitoring, alerting, reconciliation) and **extensible**
168
+ (new markets, data sources and counters plug in behind stable interfaces).
169
+
170
+ - **Markets**: China A-shares, ETFs and convertible bonds, futures and options, Hong Kong and US
171
+ equities, crypto spot.
172
+ - **Execution**: broker and exchange counters such as CTP, XTP, QMT, PTrade, TORA and IBKR; pre-trade
173
+ risk checks on a single order path; a cash and position ledger reconciled against the broker.
174
+ - **Research**: 220+ indicators and chart patterns with written semantics (the same catalog
175
+ AlphaSpec ships), factor research, visual rule canvas, stock pools, parameter scans with
176
+ out-of-sample validation.
177
+ - **Built-in AI research**: describe what you want in plain words. *AI stock screening* turns
178
+ "low valuation, high dividend, no ST" into screening conditions you can adjust, shows the
179
+ candidates and why each was picked, and saves them as a stock pool. *AI strategy drafting* turns a
180
+ trading idea into entry, exit and risk rules, validates them, opens them on the visual canvas with
181
+ buy and sell points previewed, and from there into backtesting and paper trading. Nothing is saved
182
+ or run until you confirm.
183
+ - **Two editions**: an institutional console for funds, brokerage quant desks and studios, including
184
+ live trading; and a [personal edition](https://trade.goeasyquant.com) with AI-assisted research,
185
+ backtesting and paper trading, where any user can create an AlphaSpec API key for free.
186
+
187
+ ### AI writes the same document you would
188
+
189
+ A strategy drafted by EasyQuant's AI, one built by hand on its canvas, and one submitted as AlphaSpec by
190
+ an outside agent are the **same kind of object**: a rule tree from the same indicator vocabulary,
191
+ compiled by the same engine, backtested under the same rules and judged by the same gate. The AI does
192
+ not produce a pile of code that differs every run and can only be proven right by running it. It
193
+ produces a document you can read before it runs, change by hand, compare with the last version, and
194
+ hand back to the AI to improve. That is what makes AI output something a person can take
195
+ responsibility for.
196
+
197
+ ## An open ecosystem
198
+
199
+ AlphaSpec lives under **[openalpha-dev](https://github.com/openalpha-dev)**, a family of open
200
+ components around one idea: alpha should be easy to express, honest to evaluate and safe to execute.
201
+
202
+ - **Any runtime can implement it.** A compliant runtime must pass every case in
203
+ [`conformance/cases`](https://github.com/openalpha-dev/alphaspec/tree/main/conformance/cases).
204
+ EasyQuant is the reference, not the gatekeeper.
205
+ - **Any agent can use it.** The Agent Skill and MCP server work with Claude Code, Cursor, Codex and
206
+ any MCP client. EasyQuant's own AI research assistant uses the same public API and nothing more:
207
+ if it needed a private shortcut, the ecosystem would not be real.
208
+ - **More components to come**, including **AlphaSpace**, a local research desk that brings your own
209
+ model, data and LLM key and submits through the same API, and pieces of the execution stack such as
210
+ risk checks, published as independent modules.
211
+
212
+ ## Getting started
213
+
214
+ ### Command line and Python
215
+
216
+ ```bash
217
+ pip install alphaspec
218
+ alphaspec examples # documents to start from
219
+ alphaspec catalog --search breakout # indicators, with meaning, parameters and pitfalls
220
+ alphaspec validate my_strategy.json # errors and warnings, each with a fix hint
221
+ alphaspec rules --explain AS003 # why a rule exists
222
+ ```
223
+
224
+ Submitting to a runtime (EasyQuant: create the key under *Account → API Keys*):
225
+
226
+ ```bash
227
+ export ALPHASPEC_BASE_URL=https://trade.goeasyquant.com
228
+ export ALPHASPEC_API_KEY=eqk_...
229
+
230
+ alphaspec whoami
231
+ alphaspec submit my_strategy.json --evaluate --promote
232
+ ```
233
+
234
+ ```python
235
+ from alphaspec import Client
236
+
237
+ c = Client() # reads ALPHASPEC_BASE_URL and ALPHASPEC_API_KEY
238
+ aid = c.submit(doc)["artifactId"] # validates locally first
239
+ c.evaluate(aid)
240
+ gate = c.wait(aid)
241
+ if gate["passed"]:
242
+ c.promote(aid, name="my strategy")
243
+ ```
244
+
245
+ ### Agent Skill (agents that can run commands)
246
+
247
+ ```bash
248
+ alphaspec skill --install ~/.claude/skills # or your agent's skills directory
249
+ ```
250
+
251
+ The skill teaches the agent the workflow: start from an example, look indicators up instead of
252
+ guessing, validate until clean, report `searchTrials` honestly, never quote numbers the runtime did
253
+ not return.
254
+
255
+ ### MCP (agents without a shell)
256
+
257
+ ```json
258
+ {
259
+ "mcpServers": {
260
+ "alphaspec": {
261
+ "command": "alphaspec-mcp",
262
+ "env": {
263
+ "ALPHASPEC_BASE_URL": "https://trade.goeasyquant.com",
264
+ "ALPHASPEC_API_KEY": "eqk_..."
265
+ }
266
+ }
267
+ }
268
+ }
269
+ ```
270
+
271
+ The client speaks MCP to a local `alphaspec-mcp` process over stdio; that process calls the runtime
272
+ over HTTPS with the same client as the CLI, so the key only ever goes to the runtime. Tools:
273
+ `validate`, `explain_rule`, `search_indicators`, `get_indicator`, `get_schema`, `list_examples`,
274
+ `get_example`, `get_doc` (offline) and `whoami`, `submit`, `evaluate`, `gate_result`,
275
+ `list_submissions`, `promote` (runtime). There is no order tool.
276
+
277
+ ## Lint rules are where the experience lives
278
+
279
+ Each rule in [docs/RULES.md](https://github.com/openalpha-dev/alphaspec/blob/main/docs/RULES.md)
280
+ exists because a runtime once accepted that mistake, ran it, and produced wrong numbers without an
281
+ error. Some examples:
282
+
283
+ - **AS003**: a daily indicator read from the still-forming daily bar inside an intraday strategy.
284
+ In a backtest that bar already contains the close: look-ahead.
285
+ - **AS001**: an indicator alias with a typo. The rule compiles to nothing and never fires.
286
+ - **AS011**: `600000` instead of `600000.SH`. On venue-qualified markets the bare code is a
287
+ different series.
288
+ - **AS012**: AI-generated output that does not report how many candidates were tried, which leaves
289
+ nothing to correct a multiple-testing-inflated Sharpe ratio with.
290
+
291
+ The indicator catalog carries the same kind of knowledge: `close > DAY_HIGH` can never be true
292
+ because the day's high includes the current bar; the catalog says so and points to
293
+ `DAY_HIGH_BEFORE`.
294
+
295
+ ## Markets
296
+
297
+ Market rules are scoped, not removed. Core indicators work on any market. Indicators that encode a
298
+ market's own rules, such as China's daily price limits, belong to an extension profile and are
299
+ rejected where those rules do not exist. See
300
+ [docs/MARKETS.md](https://github.com/openalpha-dev/alphaspec/blob/main/docs/MARKETS.md).
301
+
302
+ ## Repository layout
303
+
304
+ ```
305
+ schema/0.1/ JSON Schemas: the normative contract
306
+ catalog/ indicator catalog with semantics (Chinese and English)
307
+ conformance/cases/ accept/reject cases every compliant runtime must agree with
308
+ examples/ documents that pass with zero issues
309
+ src/alphaspec/ validator, CLI, runtime client, MCP server
310
+ skills/alphaspec/ Agent Skill
311
+ docs/ semantics, lint rules, markets, runtime API
312
+ ```
313
+
314
+ ## Versioning
315
+
316
+ Every document carries `contractVersion`, so a runtime can refuse what it does not understand.
317
+ Breaking changes bump the contract version. Schemas are published at their `$id` URLs, e.g.
318
+ `https://openalpha-dev.github.io/alphaspec/schema/0.1/artifact.schema.json`.
319
+
320
+ ## License
321
+
322
+ Apache-2.0