backtester-mcp 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 (55) hide show
  1. backtester_mcp-0.1.0/.github/ISSUE_TEMPLATE/bug_report.yml +54 -0
  2. backtester_mcp-0.1.0/.github/ISSUE_TEMPLATE/config.yml +8 -0
  3. backtester_mcp-0.1.0/.github/ISSUE_TEMPLATE/feature_request.yml +29 -0
  4. backtester_mcp-0.1.0/.github/ISSUE_TEMPLATE/question.yml +20 -0
  5. backtester_mcp-0.1.0/.github/PULL_REQUEST_TEMPLATE.md +12 -0
  6. backtester_mcp-0.1.0/.github/workflows/test.yml +28 -0
  7. backtester_mcp-0.1.0/.gitignore +24 -0
  8. backtester_mcp-0.1.0/CONTRIBUTING.md +62 -0
  9. backtester_mcp-0.1.0/LICENSE +190 -0
  10. backtester_mcp-0.1.0/PKG-INFO +239 -0
  11. backtester_mcp-0.1.0/README.md +215 -0
  12. backtester_mcp-0.1.0/benchmarks/bench_engine.py +44 -0
  13. backtester_mcp-0.1.0/benchmarks/bench_launch.py +51 -0
  14. backtester_mcp-0.1.0/benchmarks/bench_robustness.py +54 -0
  15. backtester_mcp-0.1.0/datasets/btc_hourly.parquet +0 -0
  16. backtester_mcp-0.1.0/datasets/spy_daily.parquet +0 -0
  17. backtester_mcp-0.1.0/datasets/synthetic_kalshi.parquet +0 -0
  18. backtester_mcp-0.1.0/examples/agent_workflow_demo.py +85 -0
  19. backtester_mcp-0.1.0/examples/mcp_example.py +24 -0
  20. backtester_mcp-0.1.0/examples/pbo_demo.py +63 -0
  21. backtester_mcp-0.1.0/examples/quickstart.py +21 -0
  22. backtester_mcp-0.1.0/examples/sample_report.html +76 -0
  23. backtester_mcp-0.1.0/pyproject.toml +29 -0
  24. backtester_mcp-0.1.0/scripts/generate_datasets.py +121 -0
  25. backtester_mcp-0.1.0/src/backtester_mcp/__init__.py +4 -0
  26. backtester_mcp-0.1.0/src/backtester_mcp/__main__.py +8 -0
  27. backtester_mcp-0.1.0/src/backtester_mcp/cli.py +463 -0
  28. backtester_mcp-0.1.0/src/backtester_mcp/data.py +79 -0
  29. backtester_mcp-0.1.0/src/backtester_mcp/engine.py +90 -0
  30. backtester_mcp-0.1.0/src/backtester_mcp/manifest.py +48 -0
  31. backtester_mcp-0.1.0/src/backtester_mcp/mcp_server.py +420 -0
  32. backtester_mcp-0.1.0/src/backtester_mcp/metrics.py +99 -0
  33. backtester_mcp-0.1.0/src/backtester_mcp/optimize.py +76 -0
  34. backtester_mcp-0.1.0/src/backtester_mcp/orderbook.py +127 -0
  35. backtester_mcp-0.1.0/src/backtester_mcp/portfolio.py +71 -0
  36. backtester_mcp-0.1.0/src/backtester_mcp/report.py +249 -0
  37. backtester_mcp-0.1.0/src/backtester_mcp/robustness.py +269 -0
  38. backtester_mcp-0.1.0/src/backtester_mcp/sensitivity.py +74 -0
  39. backtester_mcp-0.1.0/src/backtester_mcp/store.py +295 -0
  40. backtester_mcp-0.1.0/strategies/mean_reversion.py +29 -0
  41. backtester_mcp-0.1.0/strategies/momentum.py +22 -0
  42. backtester_mcp-0.1.0/strategies/prediction_market.py +28 -0
  43. backtester_mcp-0.1.0/tests/conftest.py +73 -0
  44. backtester_mcp-0.1.0/tests/test_cli.py +100 -0
  45. backtester_mcp-0.1.0/tests/test_data.py +76 -0
  46. backtester_mcp-0.1.0/tests/test_engine.py +58 -0
  47. backtester_mcp-0.1.0/tests/test_examples.py +35 -0
  48. backtester_mcp-0.1.0/tests/test_manifest.py +52 -0
  49. backtester_mcp-0.1.0/tests/test_mcp_integration.py +334 -0
  50. backtester_mcp-0.1.0/tests/test_metrics.py +95 -0
  51. backtester_mcp-0.1.0/tests/test_orderbook.py +67 -0
  52. backtester_mcp-0.1.0/tests/test_portfolio.py +51 -0
  53. backtester_mcp-0.1.0/tests/test_robustness.py +79 -0
  54. backtester_mcp-0.1.0/tests/test_sensitivity.py +50 -0
  55. backtester_mcp-0.1.0/tests/test_store.py +105 -0
@@ -0,0 +1,54 @@
1
+ name: Bug report
2
+ description: Something broke or behaves unexpectedly
3
+ labels: ["bug"]
4
+ body:
5
+ - type: textarea
6
+ id: what-happened
7
+ attributes:
8
+ label: What happened
9
+ description: What did you run, and what went wrong?
10
+ placeholder: |
11
+ I ran `backtester-mcp backtest -s strategies/momentum.py -d datasets/spy_daily.parquet --robustness`
12
+ and it crashed with...
13
+ validations:
14
+ required: true
15
+ - type: textarea
16
+ id: expected
17
+ attributes:
18
+ label: What you expected
19
+ validations:
20
+ required: true
21
+ - type: textarea
22
+ id: repro
23
+ attributes:
24
+ label: Minimal reproduction
25
+ description: Smallest script or command that reproduces it. Include data shape if relevant.
26
+ render: shell
27
+ validations:
28
+ required: true
29
+ - type: input
30
+ id: version
31
+ attributes:
32
+ label: backtester-mcp version
33
+ description: Output of `pip show backtester-mcp | grep Version`
34
+ validations:
35
+ required: true
36
+ - type: input
37
+ id: python
38
+ attributes:
39
+ label: Python version
40
+ placeholder: "3.11.7"
41
+ validations:
42
+ required: true
43
+ - type: input
44
+ id: os
45
+ attributes:
46
+ label: OS
47
+ placeholder: "macOS 14.4 / Ubuntu 22.04 / Windows 11"
48
+ validations:
49
+ required: true
50
+ - type: textarea
51
+ id: traceback
52
+ attributes:
53
+ label: Traceback or logs
54
+ render: shell
@@ -0,0 +1,8 @@
1
+ blank_issues_enabled: false
2
+ contact_links:
3
+ - name: Show & Tell
4
+ url: https://github.com/bcosm/backtester-mcp/discussions/categories/show-and-tell
5
+ about: Built something with backtester-mcp? Share it.
6
+ - name: General discussion
7
+ url: https://github.com/bcosm/backtester-mcp/discussions/categories/general
8
+ about: Open-ended conversation about strategy validation, PBO, MCP integrations.
@@ -0,0 +1,29 @@
1
+ name: Feature request
2
+ description: Propose a new feature or capability
3
+ labels: ["enhancement"]
4
+ body:
5
+ - type: textarea
6
+ id: problem
7
+ attributes:
8
+ label: What problem are you trying to solve?
9
+ description: Describe the situation, not the solution.
10
+ validations:
11
+ required: true
12
+ - type: textarea
13
+ id: proposal
14
+ attributes:
15
+ label: Proposed feature
16
+ description: What would you like backtester-mcp to do?
17
+ validations:
18
+ required: true
19
+ - type: textarea
20
+ id: alternatives
21
+ attributes:
22
+ label: Alternatives considered
23
+ description: What are you doing today instead, or what other tools handle this?
24
+ - type: textarea
25
+ id: usage
26
+ attributes:
27
+ label: Example usage
28
+ description: Sketch what the API or CLI command would look like.
29
+ render: python
@@ -0,0 +1,20 @@
1
+ name: Question
2
+ description: How do I do X with backtester-mcp?
3
+ labels: ["question"]
4
+ body:
5
+ - type: textarea
6
+ id: question
7
+ attributes:
8
+ label: Your question
9
+ validations:
10
+ required: true
11
+ - type: textarea
12
+ id: tried
13
+ attributes:
14
+ label: What you've already tried
15
+ description: Code snippets, docs you've read, etc.
16
+ - type: input
17
+ id: version
18
+ attributes:
19
+ label: backtester-mcp version
20
+ placeholder: "0.1.0"
@@ -0,0 +1,12 @@
1
+ ## Summary
2
+ What does this change and why?
3
+
4
+ ## Related issue
5
+ Closes #
6
+
7
+ ## Checklist
8
+ - [ ] Tests added or updated for the change
9
+ - [ ] `pytest tests/` passes locally
10
+ - [ ] No `TODO`, `FIXME`, or commented-out code introduced
11
+ - [ ] Public functions have type hints and a docstring
12
+ - [ ] If this changes behavior visible in the README or CLI, the docs are updated
@@ -0,0 +1,28 @@
1
+ name: tests
2
+
3
+ on:
4
+ push:
5
+ branches: [master, main]
6
+ pull_request:
7
+ branches: [master, main]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ python-version: ["3.10", "3.11", "3.12"]
15
+
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+
19
+ - name: Set up Python ${{ matrix.python-version }}
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: ${{ matrix.python-version }}
23
+
24
+ - name: Install dependencies
25
+ run: pip install -e ".[all]"
26
+
27
+ - name: Run tests
28
+ run: pytest tests/ -v --tb=short
@@ -0,0 +1,24 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *$py.class
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ .venv/
8
+ .venv_*/
9
+ .env
10
+ *.egg
11
+ .DS_Store
12
+ *.so
13
+ .idea/
14
+ .vscode/
15
+ *.swp
16
+ *.swo
17
+ *~
18
+ .pytest_cache/
19
+ htmlcov/
20
+ .coverage
21
+ .history/
22
+
23
+ .claude/
24
+ AGENTS.md
@@ -0,0 +1,62 @@
1
+ # Contributing
2
+
3
+ ## Setup
4
+
5
+ ```bash
6
+ git clone https://github.com/bcosm/backtester-mcp.git
7
+ cd backtester-mcp
8
+ python -m venv .venv
9
+ source .venv/bin/activate # or .venv\Scripts\activate on Windows
10
+ pip install -e ".[all]"
11
+ ```
12
+
13
+ ## Running Tests
14
+
15
+ ```bash
16
+ pytest tests/ -v
17
+ ```
18
+
19
+ ## Code Style
20
+
21
+ - snake_case everywhere
22
+ - Comments explain *why*, not *what*
23
+ - Type hints on public functions
24
+ - Lines <= 99 characters
25
+ - No `TODO`, `FIXME`, or commented-out code
26
+
27
+ ## Adding a Strategy
28
+
29
+ Create a file in `strategies/` with:
30
+
31
+ ```python
32
+ import numpy as np
33
+
34
+ DEFAULT_PARAMS = {"lookback": 20}
35
+
36
+ def generate_signals(prices, lookback=20):
37
+ signals = np.zeros(len(prices))
38
+ # your logic here
39
+ return signals
40
+ ```
41
+
42
+ ## Adding a Robustness Method
43
+
44
+ Add the function to `src/backtester_mcp/robustness.py`. It should:
45
+ - Accept a returns array or strategy function
46
+ - Return a dict with clear, named fields
47
+ - Be grounded in published research (cite the paper)
48
+ - Have at least one test in `tests/test_robustness.py`
49
+
50
+ ## Adding an MCP Tool
51
+
52
+ Add the tool in `src/backtester_mcp/mcp_server.py` using `@mcp.tool()`. Include:
53
+ - Clear docstring (this becomes the tool description for agents)
54
+ - Structured JSON response via `_make_response()`
55
+ - `warnings` for any issues detected
56
+ - A test in `tests/test_mcp_integration.py`
57
+
58
+ ## Commits
59
+
60
+ - Atomic: one logical change per commit
61
+ - Imperative mood: "add feature" not "added feature"
62
+ - Each commit should leave tests passing
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by the Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding any notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 backtester-mcp contributors
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
@@ -0,0 +1,239 @@
1
+ Metadata-Version: 2.4
2
+ Name: backtester-mcp
3
+ Version: 0.1.0
4
+ Summary: Local-first backtesting engine with built-in overfitting detection
5
+ Author-email: Baz Cosmopoulos <bcosm@umich.edu>
6
+ License-Expression: Apache-2.0
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.10
9
+ Requires-Dist: duckdb>=0.9
10
+ Requires-Dist: numba>=0.57
11
+ Requires-Dist: numpy>=1.24
12
+ Requires-Dist: optuna>=3.0
13
+ Requires-Dist: pandas>=2.0
14
+ Requires-Dist: pyarrow>=14.0
15
+ Requires-Dist: scipy>=1.10
16
+ Provides-Extra: all
17
+ Requires-Dist: mcp>=1.0; extra == 'all'
18
+ Requires-Dist: pytest>=7.0; extra == 'all'
19
+ Provides-Extra: dev
20
+ Requires-Dist: pytest>=7.0; extra == 'dev'
21
+ Provides-Extra: mcp
22
+ Requires-Dist: mcp>=1.0; extra == 'mcp'
23
+ Description-Content-Type: text/markdown
24
+
25
+ # backtester-mcp
26
+
27
+ Local-first backtesting engine with built-in overfitting detection. Asset-class agnostic. MCP-native.
28
+
29
+ ![License](https://img.shields.io/badge/license-Apache%202.0-blue)
30
+ ![Python](https://img.shields.io/badge/python-3.10%2B-green)
31
+ ![Tests](https://github.com/bcosm/backtester-mcp/actions/workflows/test.yml/badge.svg)
32
+
33
+ ## The Problem
34
+
35
+ QuantConnect requires Docker + C#, supports 9 hardcoded asset classes, and ships zero statistical robustness tools. Solo quants and AI agents need something that's `pip install`, works on any price series (equities, crypto, prediction markets) and tells you if your strategy is overfit before you risk real money.
36
+
37
+ backtester-mcp is a validation layer for AI-generated trading strategies. Vectorized execution on NumPy + Numba, automatic overfitting detection via PBO, walk-forward validation, execution scenario analysis, and a native MCP server so AI agents can validate strategies directly.
38
+
39
+ ## Quick Start
40
+
41
+ ```bash
42
+ pip install backtester-mcp
43
+ ```
44
+
45
+ Runs on synthetic data out of the box, no datasets to download:
46
+
47
+ ```python
48
+ import numpy as np
49
+ from backtester_mcp import backtest
50
+
51
+ # synthetic price series, reproducible
52
+ rng = np.random.default_rng(0)
53
+ prices = np.cumprod(1 + rng.normal(0, 0.01, 2000))
54
+
55
+ # moving average crossover signal
56
+ fast = np.convolve(prices, np.ones(10) / 10, mode="full")[:len(prices)]
57
+ slow = np.convolve(prices, np.ones(50) / 50, mode="full")[:len(prices)]
58
+ signals = np.where(fast > slow, 1.0, -1.0)
59
+ signals[:50] = 0
60
+
61
+ result = backtest(prices, signals)
62
+ print(result.metrics)
63
+ ```
64
+
65
+ To run against a real dataset, clone the repo (`git clone https://github.com/bcosm/backtester-mcp`) and point `load()` at anything in `datasets/` or your own CSV/Parquet file.
66
+
67
+ ## Key Features
68
+
69
+ | Feature | QuantConnect | backtester-mcp |
70
+ |---|---|---|
71
+ | Setup | Docker + .NET | `pip install` |
72
+ | Engine | C# (Python wrapper) | Pure Python + NumPy + Numba |
73
+ | Asset classes | 9 hardcoded | Any price series |
74
+ | Overfitting detection | None | PBO + Bootstrap Sharpe + DSR + Walk-Forward |
75
+ | Execution realism | Hand-tuned per asset | Auto-estimated from data, 3 scenario modes |
76
+ | AI agent interface | Cloud API wrapper | Native MCP server (13 tools) |
77
+ | Run persistence | Cloud-dependent | Local DuckDB registry |
78
+ | Cost | Free tier; $60+/mo | Free & open source |
79
+
80
+ ## Validation Pipeline
81
+
82
+ backtester-mcp runs a full validation pipeline that tells you whether to trust a strategy:
83
+
84
+ 1. **Backtest** with realistic estimated fills
85
+ 2. **Bootstrap Sharpe CI**: is the Sharpe distinguishable from zero?
86
+ 3. **Deflated Sharpe**: does it survive correction for multiple testing?
87
+ 4. **PBO (perturbation)**: are the exact parameters robust, or did you get lucky?
88
+ 5. **Walk-forward validation**: does the strategy hold up out-of-sample?
89
+ 6. **Execution scenarios**: optimistic, base, and conservative cost assumptions
90
+
91
+ ```bash
92
+ backtester-mcp backtest -s strategies/momentum.py -d datasets/spy_daily.parquet \
93
+ --robustness --execution-scenarios --walk-forward
94
+ ```
95
+
96
+ ## CLI Usage
97
+
98
+ ```bash
99
+ # Basic backtest
100
+ backtester-mcp backtest -s strategies/momentum.py -d datasets/spy_daily.parquet
101
+
102
+ # Full validation: robustness + execution scenarios
103
+ backtester-mcp backtest -s strategies/momentum.py -d datasets/spy_daily.parquet \
104
+ --robustness --execution-scenarios
105
+
106
+ # Override strategy parameters
107
+ backtester-mcp backtest -s strategies/momentum.py -d datasets/spy_daily.parquet \
108
+ --set fast_period=20 --set slow_period=100
109
+
110
+ # Estimated fills from market data
111
+ backtester-mcp backtest -s strategies/momentum.py -d datasets/spy_daily.parquet \
112
+ --realistic-fills
113
+
114
+ # Optimize with Bayesian search + PBO check
115
+ backtester-mcp optimize -s strategies/momentum.py -d datasets/spy_daily.parquet \
116
+ -p fast_period:5:50 -p slow_period:20:200
117
+
118
+ # Generate HTML validation report
119
+ backtester-mcp report -s strategies/momentum.py -d datasets/spy_daily.parquet \
120
+ --robustness --execution-scenarios -o report.html
121
+
122
+ # Persist and compare runs
123
+ backtester-mcp backtest -s strategies/momentum.py -d datasets/spy_daily.parquet --save-run
124
+ backtester-mcp list-runs
125
+ backtester-mcp show-run <run-id>
126
+ backtester-mcp compare-runs <run-id-1> <run-id-2>
127
+ ```
128
+
129
+ ## MCP Server
130
+
131
+ backtester-mcp exposes 13 tools via the Model Context Protocol for AI agents:
132
+
133
+ ```json
134
+ {
135
+ "mcpServers": {
136
+ "backtester-mcp": {
137
+ "command": "backtester-mcp",
138
+ "args": ["serve", "--transport", "stdio"]
139
+ }
140
+ }
141
+ }
142
+ ```
143
+
144
+ If your MCP client (e.g. Claude Desktop) can't find the `backtester-mcp` binary because the venv isn't on its PATH, use the Python-module form instead:
145
+
146
+ ```json
147
+ {
148
+ "mcpServers": {
149
+ "backtester-mcp": {
150
+ "command": "/absolute/path/to/.venv/bin/python",
151
+ "args": ["-m", "backtester_mcp", "serve", "--transport", "stdio"]
152
+ }
153
+ }
154
+ }
155
+ ```
156
+
157
+ **Available tools:**
158
+
159
+ | Tool | Purpose |
160
+ |---|---|
161
+ | `backtest_strategy` | Run a backtest (flat, estimated, or conservative fills) |
162
+ | `validate_strategy` | Full validation pipeline with pass/caution verdict |
163
+ | `validate_robustness` | Bootstrap Sharpe CI + DSR + PBO |
164
+ | `optimize_parameters` | Bayesian parameter search with PBO check |
165
+ | `compare_strategies` | Compare multiple strategies side by side |
166
+ | `register_dataset` | Register data from file path, CSV, or base64 |
167
+ | `profile_dataset` | Detailed dataset statistics and quality check |
168
+ | `save_run` | Persist results to local DuckDB store |
169
+ | `list_runs` | List recent runs, filterable by dataset/strategy |
170
+ | `load_run` | Retrieve full results for a run |
171
+ | `compare_runs` | Compare metrics across saved runs |
172
+ | `generate_report` | Create HTML validation report |
173
+ | `strategy_template` | Get a parameterized strategy code template |
174
+
175
+ The `validate_strategy` tool runs the full pipeline in one call and returns a structured verdict:
176
+
177
+ ```json
178
+ {
179
+ "verdict": "caution",
180
+ "reasons": ["bootstrap CI includes zero", "conservative scenario negative return"],
181
+ "metrics": {"sharpe": 0.28, ...},
182
+ "pbo": {"pbo": 0.36, ...},
183
+ "scenarios": {"optimistic": {...}, "base": {...}, "conservative": {...}}
184
+ }
185
+ ```
186
+
187
+ ## Performance
188
+
189
+ Benchmarks on a 2024 thin-and-light laptop CPU (Intel Core Ultra 7 256V, Lunar Lake generation; performance is in the same ballpark as an Apple M3 MacBook Air or Ryzen 7 7840U for single-threaded NumPy/Numba workloads). Python 3.13, numba 0.65, min of 5 runs after JIT warmup:
190
+
191
+ | Bars | Time |
192
+ |---|---|
193
+ | 1,000 | ~0.1 ms |
194
+ | 10,000 | ~0.6 ms |
195
+ | 100,000 | ~8.5 ms |
196
+ | 500,000 | ~56 ms |
197
+
198
+ The first call in a process pays for numba JIT compilation (roughly 1 to 3 seconds depending on the function). Subsequent calls hit numba's on-disk cache and run at the numbers above.
199
+
200
+ ## How PBO Works
201
+
202
+ Probability of Backtest Overfitting (PBO) answers: "if I pick the best strategy from an in-sample optimization, what's the probability it underperforms out-of-sample?"
203
+
204
+ It works by splitting your backtest data into S sub-periods, then forming all combinations of S/2 sub-periods as in-sample and the rest as out-of-sample (combinatorially symmetric cross-validation). For each combination, it checks whether the strategy that ranked best in-sample still performs above median out-of-sample. The PBO score is the fraction of combinations where it doesn't. A score above 0.5 means your strategy is more likely overfit than not.
205
+
206
+ For single strategies, backtester-mcp uses **perturbation PBO**: it generates variants by jittering parameters within +/-20%, runs each variant, then computes PBO over the resulting returns matrix. This answers "would nearby parameters work just as well, or did you get lucky with these exact numbers?"
207
+
208
+ From Lopez de Prado (2018), "The Probability of Backtest Overfitting," *Journal of Computational Finance*.
209
+
210
+ ## Architecture
211
+
212
+ ```
213
+ Data (CSV/Parquet/DuckDB) -> Engine (NumPy + Numba) -> Metrics
214
+ -> Robustness (PBO, Bootstrap, DSR, Walk-Forward)
215
+ -> Sensitivity (Execution Scenarios, Stress Test)
216
+ -> Report (HTML)
217
+ -> Manifest (JSON audit trail)
218
+ -> Store (DuckDB persistence)
219
+ ```
220
+
221
+ Strategies are plain functions: `f(prices, **params) -> signals`. No class hierarchies, no inheritance, no plugin system.
222
+
223
+ ## Stochastic Order Book
224
+
225
+ Most backtesting engines either ignore fill simulation or require hand-tuned models per asset class. backtester-mcp automatically estimates fill characteristics from your price data:
226
+
227
+ - **Spread estimation**: Corwin-Schultz (2012) from OHLC data, Roll (1984) from close prices
228
+ - **Market impact**: square-root model calibrated to observed volatility
229
+ - **Three execution modes**: optimistic (zero cost), base (estimated), conservative (2x spread)
230
+
231
+ This works on any price series without configuration.
232
+
233
+ ## Contributing
234
+
235
+ See [CONTRIBUTING.md](CONTRIBUTING.md). Open an issue before submitting a PR.
236
+
237
+ ## License
238
+
239
+ Apache 2.0