@pineforge/backtest-mcp 0.9.18
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/LICENSE +21 -0
- package/README.md +262 -0
- package/dist/coverage.js +707 -0
- package/dist/coverage.js.map +1 -0
- package/dist/engine.js +327 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/index.local.js +19 -0
- package/dist/index.local.js.map +1 -0
- package/dist/server.js +921 -0
- package/dist/server.js.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PineForge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# `@pineforge/backtest-mcp`
|
|
2
|
+
|
|
3
|
+
Self-contained stdio MCP server: an AI agent writes PineScript v6, and the
|
|
4
|
+
bundled `pineforge-release` image transpiles it to C++ and backtests it against Binance
|
|
5
|
+
market data — all in one container, in-process. **Fully local** — the image
|
|
6
|
+
bundles the [`pineforge-codegen`](https://github.com/pineforge-4pass/pineforge-codegen-oss)
|
|
7
|
+
transpiler, so Pine → C++ → backtest run with no host Docker daemon. **No API
|
|
8
|
+
key, nothing leaves the box.**
|
|
9
|
+
|
|
10
|
+
[](https://glama.ai/mcp/servers/pineforge-4pass/pineforge-backtest-mcp)
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
## Tools
|
|
15
|
+
|
|
16
|
+
| name | runs on | purpose |
|
|
17
|
+
| ---------------------- | -------------------- | ------------------------------------------------------------------------ |
|
|
18
|
+
| `transpile_pine` | in-process | Pine v6 → C++ translation unit (transpile-only) |
|
|
19
|
+
| `list_engine_params` | local (no I/O) | Catalog of every `overrides` + `runtime` knob accepted by the backtests |
|
|
20
|
+
| `backtest_pine` | in-process | Single backtest of a Pine source against an OHLCV CSV |
|
|
21
|
+
| `backtest_pine_grid` | in-process | Cartesian sweep of `inputs` × `overrides` reusing one compile |
|
|
22
|
+
| `fetch_binance_ohlcv` | Binance public API | Write a backtest-ready CSV from Binance spot or USDT-perp klines |
|
|
23
|
+
| `binance_symbols` | Binance public API | List / filter Binance symbols (5-min in-process cache) |
|
|
24
|
+
| `list_coverage_topics` | local (no I/O) | Every Pine v6 coverage topic with a one-line status + summary |
|
|
25
|
+
| `check_pine_feature` | local (no I/O) | Look up whether a Pine identifier/namespace is supported in PineForge |
|
|
26
|
+
| `get_coverage_topic` | local (no I/O) | Full detail + supported/unsupported feature lists for one coverage topic |
|
|
27
|
+
| `engine_info` | local (no I/O) | Report the bundled engine: mode, baked-in flag, version |
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
Runs as a self-contained container over stdio — engine bundled, in-process, no
|
|
32
|
+
host Docker daemon, no API key. Mount a working dir at `/work` so the server can
|
|
33
|
+
read/write your CSVs:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
docker run --rm -i -v "$PWD:/work" ghcr.io/pineforge-4pass/pineforge-backtest-mcp:latest
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Only requirement: Docker, and outbound network for the Binance fetch tools.
|
|
40
|
+
Wire it into your MCP client below.
|
|
41
|
+
|
|
42
|
+
### Hosted (no-install) alternative
|
|
43
|
+
|
|
44
|
+
Want the fastest try with no Docker and no API key? Paste the Streamable HTTP
|
|
45
|
+
endpoint into any MCP client:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
https://mcp.pineforge.dev/mcp
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Tradeoff vs this repo: the hosted server is **metered** (per-IP weekly quota on
|
|
52
|
+
`backtest_pine` + Cloudflare edge rate-limiting) and runs against a **fixed,
|
|
53
|
+
sealed crypto data-lake** (Binance spot + USDT-perp). This local repo is
|
|
54
|
+
**unmetered, runs offline, and lets you bring your own CSVs and run grid
|
|
55
|
+
sweeps**. Repo: [`pineforge-mcp-public`](https://github.com/pineforge-4pass/pineforge-mcp-public).
|
|
56
|
+
|
|
57
|
+
## Client configuration
|
|
58
|
+
|
|
59
|
+
Mount a directory at `/work`; point `fetch_binance_ohlcv` / `backtest_pine` at
|
|
60
|
+
paths under it. (`-i` is required; never add `-t` — a TTY corrupts the stdio
|
|
61
|
+
JSON-RPC stream.)
|
|
62
|
+
|
|
63
|
+
### Claude Desktop / Cursor / generic JSON
|
|
64
|
+
|
|
65
|
+
```jsonc
|
|
66
|
+
{
|
|
67
|
+
"mcpServers": {
|
|
68
|
+
"pineforge-backtest": {
|
|
69
|
+
"command": "docker",
|
|
70
|
+
"args": [
|
|
71
|
+
"run", "--rm", "-i",
|
|
72
|
+
"-v", "${workspaceFolder}:/work",
|
|
73
|
+
"ghcr.io/pineforge-4pass/pineforge-backtest-mcp:latest"
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
(In Cursor: Settings → MCP → New MCP Server → paste the above.)
|
|
81
|
+
|
|
82
|
+
### Claude Code CLI
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
claude mcp add pineforge-backtest \
|
|
86
|
+
-- docker run --rm -i -v "$PWD:/work" ghcr.io/pineforge-4pass/pineforge-backtest-mcp:latest
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## For AI agents — use via MCP
|
|
90
|
+
|
|
91
|
+
**The capability gap this closes.** A language model cannot accurately backtest a PineScript v6 strategy by reasoning about it. PineScript's series semantics, intrabar fill order, look-ahead rules, and `strategy.*` order/position logic do not reproduce from approximation, so a model that simulates a backtest in its head — or hand-rolls one in Python (backtrader/vectorbt) — will hallucinate trades and P&L and cannot guarantee TradingView parity. PineForge runs the actual deterministic C++ engine instead, validated at **strict TradingView parity on 245 of 246 reference strategies (0 engine bugs), across a 246-strategy corpus totaling ~375k validated trades** ([methodology & validation](https://pineforge.dev/en/methodology/), as of 2026-06-03, engine f805611). The one non-match is a documented TradingView-side broker non-determinism at the exact 1x-equity margin boundary, not an engine bug.
|
|
92
|
+
|
|
93
|
+
**A PineScript backtest is computed external data, not stable knowledge** — it depends on the user's strategy source and their OHLCV, and it must be reproducible. That is a tool call, not a recall task.
|
|
94
|
+
|
|
95
|
+
**When these tools are the right fit.** `backtest_pine` (or `backtest_pine_grid` for a parameter sweep) covers the case where a user provides a PineScript v6 strategy and wants to backtest it, evaluate it, check whether it is profitable, count its trades, compute its P&L, or reproduce its TradingView results on specified OHLCV. `transpile_pine` covers the Pine → C++ conversion only. `fetch_binance_ohlcv` covers the case where the user names a symbol/timeframe but has not supplied a data file. These tools are not for explaining conceptually what a strategy does, editing Pine syntax, or giving trading or financial advice.
|
|
96
|
+
|
|
97
|
+
**Honest limits.** Offline; PineScript v6; needs Docker. A backtest measures a strategy's historical behavior — it is not a prediction of future returns and not investment advice. Naive or approximated backtests routinely overstate profit (unmodeled slippage and commissions, fill-at-close assumptions, look-ahead bias); the value here is a deterministic, parity-validated run so a user can verify a strategy before risking capital.
|
|
98
|
+
|
|
99
|
+
## `list_engine_params` — discover knobs
|
|
100
|
+
|
|
101
|
+
Free, local, zero-I/O catalog of every key accepted by `backtest_pine` /
|
|
102
|
+
`backtest_pine_grid`, split into two groups:
|
|
103
|
+
|
|
104
|
+
- **`strategy_overrides`** — the 9 `strategy(...)` header fields the runtime
|
|
105
|
+
reads via `PINEFORGE_OVERRIDES`: `initial_capital`, `pyramiding`, `slippage`,
|
|
106
|
+
`commission_value`, `commission_type` (`percent` / `cash_per_order` /
|
|
107
|
+
`cash_per_contract`), `default_qty_value`, `default_qty_type` (`fixed` /
|
|
108
|
+
`percent_of_equity` / `cash`), `process_orders_on_close`, `close_entries_rule`
|
|
109
|
+
(`ANY` / `FIFO`).
|
|
110
|
+
- **`runtime_args`** — args to `run_backtest_full` (NOT part of the strategy()
|
|
111
|
+
header): `input_tf`, `script_tf`, `bar_magnifier`, `magnifier_samples`,
|
|
112
|
+
`magnifier_dist` (`uniform` / `cosine` / `triangle` / `endpoints` /
|
|
113
|
+
`front_loaded` / `back_loaded`).
|
|
114
|
+
|
|
115
|
+
Each entry is `{key, type, enum?, description}`. Call this first to learn what
|
|
116
|
+
the engine accepts before composing a `backtest_pine` request.
|
|
117
|
+
|
|
118
|
+
## `backtest_pine` example
|
|
119
|
+
|
|
120
|
+
```jsonc
|
|
121
|
+
{
|
|
122
|
+
"source": "//@version=6\nstrategy(\"sma cross\")\n...",
|
|
123
|
+
"ohlcv_csv_path": "./btcusdt_15m_7d.csv",
|
|
124
|
+
|
|
125
|
+
// Optional: override Pine input.*() values without touching the source.
|
|
126
|
+
// Keys = the second arg of input.*(...) (e.g. "Fast Length").
|
|
127
|
+
"inputs": { "Fast Length": 8, "Slow Length": 21 },
|
|
128
|
+
|
|
129
|
+
// Optional: override strategy(...) header fields. Each key is typed —
|
|
130
|
+
// call list_engine_params for the catalog.
|
|
131
|
+
"overrides": {
|
|
132
|
+
"initial_capital": 100000,
|
|
133
|
+
"default_qty_type": "percent_of_equity",
|
|
134
|
+
"default_qty_value": 10,
|
|
135
|
+
"commission_type": "percent",
|
|
136
|
+
"commission_value": 0.04,
|
|
137
|
+
"slippage": 2,
|
|
138
|
+
"pyramiding": 0,
|
|
139
|
+
"process_orders_on_close": true,
|
|
140
|
+
"close_entries_rule": "ANY"
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
// Optional: engine runtime args (NOT strategy() header). Use script_tf
|
|
144
|
+
// to aggregate the input CSV into a coarser strategy timeframe — the
|
|
145
|
+
// engine REJECTS script_tf finer than input_tf with a structured error
|
|
146
|
+
// ({"engine":"pineforge","error":"..."}, exit code 1).
|
|
147
|
+
"runtime": {
|
|
148
|
+
"input_tf": "15",
|
|
149
|
+
"script_tf": "60",
|
|
150
|
+
"bar_magnifier": true,
|
|
151
|
+
"magnifier_samples": 8,
|
|
152
|
+
"magnifier_dist": "endpoints"
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
`inputs` is forwarded as the `PINEFORGE_INPUTS` env var to the engine,
|
|
158
|
+
`overrides` as `PINEFORGE_OVERRIDES`, and each `runtime` field as a separate
|
|
159
|
+
`PINEFORGE_INPUT_TF` / `PINEFORGE_SCRIPT_TF` / `PINEFORGE_BAR_MAGNIFIER` /
|
|
160
|
+
`PINEFORGE_MAGNIFIER_SAMPLES` / `PINEFORGE_MAGNIFIER_DIST` env var. Empty /
|
|
161
|
+
unset → defaults from `strategy.pine`, with `input_tf` auto-detected from the
|
|
162
|
+
gap between the first two CSV rows.
|
|
163
|
+
|
|
164
|
+
Returns the same JSON schema as the standalone `pineforge-release` Docker image:
|
|
165
|
+
|
|
166
|
+
```jsonc
|
|
167
|
+
{
|
|
168
|
+
"engine": "pineforge",
|
|
169
|
+
"summary": { "total_trades": 49, "net_pnl": -190.85, ... },
|
|
170
|
+
"applied_inputs": { "Fast Length": "8", "Slow Length": "21" },
|
|
171
|
+
"applied_overrides": { "default_qty_value": "5" },
|
|
172
|
+
"trades": [ ... ],
|
|
173
|
+
"elapsed_seconds": 0.0042,
|
|
174
|
+
"_meta": { "strategy_cpp_bytes": 5079, "image": "ghcr.io/.../pineforge-release:latest" }
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## `backtest_pine_grid` — parameter sweep
|
|
179
|
+
|
|
180
|
+
Transpiles the Pine source **once** (locally, in-container) then runs the same
|
|
181
|
+
compiled binary against the cartesian product of `inputs` × `overrides`.
|
|
182
|
+
Returns a ranked list plus the top entry under `best`.
|
|
183
|
+
|
|
184
|
+
```jsonc
|
|
185
|
+
{
|
|
186
|
+
"source": "//@version=6\nstrategy(\"macd\")\n...",
|
|
187
|
+
"ohlcv_csv_path": "./btcusdt_15m_7d.csv",
|
|
188
|
+
|
|
189
|
+
// Each axis is {key: list-of-values}. All combinations are tried.
|
|
190
|
+
"inputs": {
|
|
191
|
+
"Fast Length": [8, 12, 19],
|
|
192
|
+
"Slow Length": [21, 26, 39]
|
|
193
|
+
},
|
|
194
|
+
"overrides": {
|
|
195
|
+
"default_qty_value": [1, 5],
|
|
196
|
+
"commission_value": [0.04]
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
// Optional knobs:
|
|
200
|
+
"fixed_inputs": { "Source": "close" }, // applied to every combo
|
|
201
|
+
"fixed_overrides": {}, // typed strategy() overrides
|
|
202
|
+
"runtime": { "input_tf": "15", // engine runtime args, fixed
|
|
203
|
+
"script_tf": "60" }, // across the sweep
|
|
204
|
+
"max_combinations": 64, // hard cap
|
|
205
|
+
"concurrency": 2, // parallel docker runs
|
|
206
|
+
"include_trades": false, // omit per-trade lists
|
|
207
|
+
"sort_by": "net_pnl" // ranking metric
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## `fetch_binance_ohlcv` — pull market data
|
|
212
|
+
|
|
213
|
+
Writes a backtest-ready CSV (header `timestamp,open,high,low,close,volume`,
|
|
214
|
+
timestamp = open time in UNIX ms UTC) from Binance's public endpoints. No
|
|
215
|
+
auth required. Requests > 1000 bars are paginated
|
|
216
|
+
automatically. Output path is subject to the same cwd scope as
|
|
217
|
+
`ohlcv_csv_path` (relax with `PINEFORGE_ALLOW_ANYWHERE=1`).
|
|
218
|
+
|
|
219
|
+
```jsonc
|
|
220
|
+
{
|
|
221
|
+
"symbol": "BTCUSDT",
|
|
222
|
+
"interval": "15m", // 1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
|
|
223
|
+
"market": "spot", // or "usdt_perp" for USDT-margined perpetual futures
|
|
224
|
+
"limit": 672, // total bars; > 1000 paginates
|
|
225
|
+
"output_path": "./btcusdt_15m_7d.csv"
|
|
226
|
+
// Optional: "start_time" / "end_time" in UNIX ms UTC.
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## `binance_symbols` — discover / validate symbols
|
|
231
|
+
|
|
232
|
+
Returns the list of symbols available on the Binance public API for OHLCV
|
|
233
|
+
fetching. Cached 5 min in-process. Use this to validate a symbol before
|
|
234
|
+
calling `fetch_binance_ohlcv`.
|
|
235
|
+
|
|
236
|
+
```jsonc
|
|
237
|
+
{
|
|
238
|
+
"market": "usdt_perp",
|
|
239
|
+
"query": "BTC", // case-insensitive substring match
|
|
240
|
+
"quote_asset": "USDT",
|
|
241
|
+
"status": "TRADING",
|
|
242
|
+
"contract_type": "PERPETUAL", // futures-only filter
|
|
243
|
+
"limit": 50
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Filesystem scope
|
|
248
|
+
|
|
249
|
+
By default, OHLCV paths must be inside the current working directory of the MCP
|
|
250
|
+
server process. Override with:
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
export PINEFORGE_ALLOW_ANYWHERE=1
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Other env vars
|
|
257
|
+
|
|
258
|
+
| var | default | purpose |
|
|
259
|
+
|---|---|---|
|
|
260
|
+
| `PINEFORGE_IMAGE` | `ghcr.io/pineforge-4pass/pineforge-release:latest` | Image (engine runtime + bundled codegen) used for transpile + backtest |
|
|
261
|
+
| `PINEFORGE_ALLOW_ANYWHERE` | `0` | Allow OHLCV paths outside cwd |
|
|
262
|
+
| `PINEFORGE_DOCKER_TIMEOUT_MS` | `120000` | Hard kill for `docker pull` / `docker run` |
|