Sweep strategy parameters
backtest_pine_gridSweep PineScript strategy parameters across input and override grids, compiling once and ranking each combination by net PnL, win rate, drawdown, or trade count.
Instructions
Use when the user wants to optimize, sweep, tune, or compare PineScript parameter values (e.g. 'try fast length 8/12/19', 'find the best commission/qty settings') rather than test a single configuration — for one fixed configuration use backtest_pine. Run a parameter sweep: transpile the Pine source ONCE (locally, in-container), then compile (g++) and backtest that C++ against the OHLCV CSV once per combination in the cartesian product of inputs × overrides grids. Returns a ranked list of {inputs, overrides, summary, elapsed_seconds} entries sorted by sort_by descending, plus the top entry under best. Cap: max_combinations (default 64). Takes the same symbol / market / syminfo as backtest_pine and applies the instrument to every combination (the result's instrument shows it). The lot size is TradingView's own reading for the symbol, from a measured table shipped with this server (Binance's LOT_SIZE.stepSize only for a symbol TradingView does not list; TradingView's usual 0.001 for a listing newer than the table); the tick size and currencies come from Binance's public exchangeInfo (or from the sidecar next to a CSV fetched by fetch_binance_ohlcv). syminfo goes over all of it. Set concurrency > 1 to run backtests in parallel — each docker container has its own startup overhead, so 2-4 is usually plenty.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image | No | Docker image override. Defaults to ghcr.io/pineforge-4pass/pineforge-engine:latest. | |
| inputs | No | Grid of input.*() names → list of values to sweep. Example: {"Fast Length": [8, 12, 19], "Slow Length": [21, 26, 39]} | |
| market | No | 'spot' (default; a warning says when it is assumed) or 'usdt_perp'; the Binance market `symbol` is looked up in. A CSV fetched by fetch_binance_ohlcv for the same symbol keeps the market it was fetched for. | |
| source | Yes | PineScript v6 source. | |
| symbol | No | Binance symbol the CSV holds, e.g. 'BTCUSDT'. The lot size is TradingView's own reading for the symbol, from a measured table shipped with this server (Binance's LOT_SIZE.stepSize only for a symbol TradingView does not list; TradingView's usual 0.001 for a listing newer than the table); the tick size and currencies come from Binance's public exchangeInfo (or from the sidecar next to a CSV fetched by fetch_binance_ohlcv). TradingView's reading differs from Binance's step for most symbols (USDT-M BTCUSDT is 0.000001 on TradingView, 0.001 on Binance), so order quantities are floored as on TradingView; 0.001 is what TradingView reads for 90.6% of Binance spot symbols and 97.5% of USDT-M ones, and a lot size that is Binance's or that usual 0.001 comes with a warning. Without an instrument the engine can book sub-lot margin-call rows TradingView does not. A CSV written by fetch_binance_ohlcv needs neither `symbol` nor `syminfo`: the instrument is recorded next to it (<csv>.instrument.json) and used. If nothing can be resolved the run still goes ahead without a lot grid and says so in `warnings` and applied_runtime.syminfo. | |
| runtime | No | Engine runtime args applied to every combo in the sweep. Same shape as backtest_pine.runtime — input_tf / script_tf / bar_magnifier / magnifier_samples / magnifier_dist. Currently fixed across the grid (not swept); add to the grid axes through future versions if you need to vary them. | |
| sort_by | No | summary.* field to rank by, descending. Default net_pnl. | |
| syminfo | No | The instrument's own values, for a CSV of any other instrument; they win over what `symbol` or the CSV's sidecar gives. Applied to the engine: qty_step (the lot grid) and mincontract, mintick, pointvalue, type, currency, basecurrency. Without a qty_step (or mincontract) the lot grid stays off and the result carries a warning; without a mintick the engine's 0.01 applies. ticker, tickerid, timezone and session are not applied (engine defaults). | |
| overrides | No | Grid of strategy(...) header overrides → list of values, one axis per key. Example: {"default_qty_value": [1, 5], "commission_value": [0.04]}. Call list_engine_params for the full catalog with types and enum values. | |
| concurrency | No | Parallel backtests. Default 1. | |
| report_path | No | Where to write the full sweep JSON IF it is too large to return inline. Oversized sweeps are offloaded here and the tool returns the best + top-ranked combinations + report_path; read the file for all combinations. Defaults to pineforge-grid-<timestamp>.json in the working dir. | |
| fixed_inputs | No | Inputs applied to every combo (overridden by per-combo `inputs` keys). | |
| include_trades | No | Include the per-trade list in each result. Default false (saves tokens). | |
| ohlcv_csv_path | Yes | Path to OHLCV CSV (same format as backtest_pine). | |
| fixed_overrides | No | Overrides applied to every combo (overridden by per-combo `overrides` keys). | |
| max_combinations | No | Hard cap on combinations. Default 64. |