
# PineForge
**An open-source C++17 engine for backtesting and forward execution, with PineScript support through code generation.**
[](https://github.com/pineforge-4pass/pineforge-engine/actions)
[](#validation-scoreboard)
[](#validation-scoreboard)
[](benchmarks/results/speed.md)
[](LICENSE)
[](#)
[](https://cdocs.pineforge.dev)
[](https://pypi.org/project/pineforge-codegen/)
[](https://github.com/pineforge-4pass/pineforge-backtest-mcp)
**[π pineforge.dev](https://www.pineforge.dev) Β· [βοΈ Hosted MCP](https://mcp.pineforge.dev/mcp) Β· [π³ Docker MCP](https://github.com/pineforge-4pass/pineforge-backtest-mcp) Β· [π¦ Transpiler](https://github.com/pineforge-4pass/pineforge-codegen-oss) Β· [π C ABI docs](https://cdocs.pineforge.dev) Β· [π§ͺ Coverage map](docs/coverage.md) Β· [π¬ Benchmarks](benchmarks/)**
---
## Why PineForge
PineForge is a C++17 engine for backtesting and forward execution, with a C ABI for embedding. The engine has two layers:
1. **A generic kernel** β a Pine-agnostic backtest and forward-execution state machine: order matching and fills, sizing, margin and settlement, the bar magnifier, indicator classes, `request.security()`, time and session math. Some TradingView-shaped names survive in its archive, each ruled in [ADR 0001](docs/adr/0001-kernel-adapter-boundary.md) and held there by `scripts/check_kernel_residuals.py`.
2. **A source-adapter parity runtime** (`src/source/` and `src/compat/pine/`, `PineExecutionAdapter` + `PineStrategyHost`) β maps Pine/TradingView execution semantics onto that kernel. This is where TradingView parity lives.
The separate PineForge compiler, [`pineforge-codegen`](https://github.com/pineforge-4pass/pineforge-codegen-oss), translates a PineScript v6 script into a C++ strategy that attaches the engine's Pine execution adapter (its releases 1.0.0 and 1.0.1 do; 0.10.4 predates the adapter); it owns translation, not execution semantics. TradingView comparisons measure this Pine path under the tested configurations. The [order model](docs/pages/fill-model.md) describes the current submodels and the remaining migration work; [Architecture](#architecture-kernel-vs-parity) states the boundary.
- **Proven, not promised.** All 7,989 graded probes β 309 open reference strategies, 680 community-shared scripts and 61 probe scripts the maintainers wrote, on 18 market/timeframe lanes β grade *excellent* or *strong* against TradingView's own trade lists: **7,905 excellent, 84 strong, zero below strong**. The graded probes' TradingView trade lists hold 4,776,328 trades; 17 probes with TradingView-side defects are excluded.
- **Open runtime.** The engine and native live runner are Apache-2.0. The separately distributed [PineForge compiler](https://github.com/pineforge-4pass/pineforge-codegen-oss/blob/main/LICENSE) uses PolyForm Noncommercial terms with additional personal-trading permission; commercial use requires a separate license. Public reference strategies, benchmarks and validation tooling are available in their respective repositories; the community-script test set is not redistributed.
- **Fast.** In-process, no interpreter: median **36Γ faster than PyneCore** on the 200 strategies both engines time, measured at engine `35db01c8` on 2026-09-29 on an AWS c7a.8xlarge with PyneCore timed as a subprocess, its interpreter start-up included (PineForge runs a median 807k bars/s per strategy over its 201 slots with the bar magnifier on; [method](benchmarks/results/speed.md)). Parameter sweeps re-run a loaded `.so` with new inputs β no recompile, no fork.
- **Deterministic to the bit.** Two runs with the same inputs produce identical trade lists. Same on Linux and macOS.
- **Yours to embed.** 114 `PF_API` declarations across two headers β `pineforge.h`'s 71 (62 runtime exports and up to 9 per-strategy functions a generated module defines) and `native_c_api.h`'s 43 native-host declarations β an ABI that is append-only within a major version from 1.0 on. Call it from C, Python, Rust, Go, Node, Julia β or let an AI agent drive it over MCP.
---
## Get a backtest in 60 seconds
### With an AI agent (MCP, Docker only)
```bash
claude mcp add pineforge-backtest \
-- docker run --rm -i -v "$PWD:/work" ghcr.io/pineforge-4pass/pineforge-backtest-mcp:latest
```
For Cursor, which expands `${workspaceFolder}`, or any MCP client (in Claude Desktop, put an absolute path in its place):
```jsonc
{
"mcpServers": {
"pineforge-backtest": {
"command": "docker",
"args": ["run", "--rm", "-i", "-v", "${workspaceFolder}:/work",
"ghcr.io/pineforge-4pass/pineforge-backtest-mcp:latest"]
}
}
}
```
Then ask: *"Fetch BTC/USDT 15m for the last 90 days and backtest this strategy"* β the container transpiles Pine β C++ with the bundled [`pineforge-codegen`](https://github.com/pineforge-4pass/pineforge-codegen-oss), compiles, runs, and hands the agent the trade list. The image is built on the [`pineforge-release`](https://github.com/pineforge-4pass/pineforge-release) image, which ships a released engine and codegen pair; its `engine_info` tool reports the image's version. Your Pine and data stay on your machine; only the Binance tools (`fetch_binance_ohlcv`, `binance_symbols`) call out, to Binance's public API. Mount a directory at `/work`; `-i` is required and `-t` must not be added (a TTY corrupts the JSON-RPC stream).
| Ask | Tool |
|---|---|
| "Fetch BTC/USDT 15m data for the last 30 days" | `fetch_binance_ohlcv` |
| "Backtest this SMA-cross strategy on that data" | `backtest_pine` |
| "Sweep fast 8β21 Γ slow 21β55, rank by net PnL" | `backtest_pine_grid` |
| "What broker overrides are available?" | `list_engine_params` |
Prefer zero install? The hosted server at **[mcp.pineforge.dev/mcp](https://mcp.pineforge.dev/mcp)** (Streamable HTTP, no key) backtests one configuration at a time on crypto OHLCV it fetches itself (seven venues; the free tier serves the last 365 days), metered per IP by a weekly backtest quota. The npm package [`@pineforge/backtest-mcp`](https://www.npmjs.com/package/@pineforge/backtest-mcp) is the local server run from Node 20+ on the host; it runs each transpile and backtest in the `pineforge-release` image through your Docker daemon.
[](https://www.youtube.com/watch?v=lflD47Bum4w)
### From source
```bash
git clone https://github.com/pineforge-4pass/pineforge-engine.git && cd pineforge-engine
python3 scripts/ci_verify.py release --build-dir build --jobs 4
bash tutorial/run.sh # MACD on BTC/USDT, end to end
python3 tutorial/run_stream.py # OHLCV warm-up β realtime trades
```
The [shared local/CI verifier](docs/ci.md) includes source guards, tests and installed-package smoke checks. Its ABI check links callers against both the current library and seven separately built, pinned historical libraries. Preparation uses local Git history and the configured compiler, fetching a pinned commit the clone lacks; see the [ABI fixture guide](tests/fixtures/settlement_cpp_abi/README.md) for repeated checks. The first run needs network (the verifier initialises the public `corpus` submodule itself; configure fetches Eigen when no system copy is found) and a full clone: a depth-1 clone fails the benchmark provenance row ([CI guide](docs/ci.md)). CTest itself stays offline.
Prerequisites: CMake β₯ 3.16 to build (the verifier and `ctest --test-dir` need 3.20), a C++17 compiler whose standard library has floating-point `std::to_chars` (libstdc++ from GCC 11 or later, or libc++ 14 or later; on macOS a deployment target of 13.3 or later; CI builds with GCC on Ubuntu 24.04 and Apple Clang on macOS 26), Eigen 3.3+ (fetched automatically if absent), Python 3 for the tests (`-DPINEFORGE_BUILD_TESTS=OFF -DPINEFORGE_BUILD_TUTORIAL=OFF` for a library-only build). `cmake --install build --prefix /usr/local` installs `lib/libpineforge.a` and `lib/libpineforge_kernel.a`, `include/pineforge/`, and the `find_package(PineForge)` config.
### Prebuilt library
The [v1.0.1 release](https://github.com/pineforge-4pass/pineforge-engine/releases/tag/v1.0.1) attaches the library prebuilt: `pineforge-v1.0.1-linux-x86_64.tar.gz`, `pineforge-v1.0.1-linux-aarch64.tar.gz` and `pineforge-v1.0.1-macos-universal.tar.gz`, each with a `.sha256`. Each unpacks to one directory holding what the install above writes (the two archives, `include/pineforge/` and the `find_package(PineForge)` config) plus `LICENSE`, `NOTICE` and `VERSION`; point `CMAKE_PREFIX_PATH` at it. The package asks for Eigen 3.3+: install it on Linux, while the macOS tarball carries Eigen 3.4.0 ([install guide](docs/pages/install.md)).
### Embedded in your own harness
```c
#include