Strategy A/B Test
You are running two Pine strategies head-to-head on the same symbol/timeframe and reporting a data-backed winner.
Step 1: Lock the Environment
The comparison is only valid if both strategies run on the same chart state.
chart_get_state — confirm symbol, timeframe, and chart type
state_snapshot with tag abtest-baseline — so you can roll back cleanly between A and B, and at the end
Example baseline: ES1!, 15m, Candles.
Step 2: Define What "Winner" Means
Ask the user (or use defaults) for the ranking metric. Common options:
- Net Profit — simplest, ignores risk
- Sharpe Ratio — risk-adjusted, preferred
- Max Drawdown — lower is better (use absolute value)
- Profit Factor — gross profit / gross loss
- Composite — rank each metric, sum ranks, lowest wins
Default: report all four, pick winner by Sharpe, break ties by lower max drawdown.
Step 3: Run Strategy A
- Load A's source: paste via
pine_set_source (or pine_open if saved by name)
pine_smart_compile — auto-detect type, compile, surface errors
- If errors: iterate via the
pine-develop loop until clean
strategy_sweep — parameter sweep with cooldown + resume. Pass the param grid (e.g., { ema_len: [20, 50, 100], atr_mult: [1.5, 2.0, 2.5] })
- Collect results —
strategy_sweep returns per-combination {net_profit, sharpe, max_dd, profit_factor, trades}
Example — strategy A (trend-follow):
pine_set_source(<A source>)
pine_smart_compile()
strategy_sweep({ grid: { ema_len: [50, 100, 200], atr_mult: [1.5, 2.0] } })
Pick A's best row by the chosen metric. Record it.
Step 4: Restore, Then Run Strategy B
state_restore with tag abtest-baseline — wipe A off the chart
- Load B's source:
pine_set_source + pine_smart_compile
strategy_sweep over B's param grid
- Pick B's best row
Example — strategy B (mean-reversion):
state_restore({ tag: "abtest-baseline" })
pine_set_source(<B source>)
pine_smart_compile()
strategy_sweep({ grid: { rsi_len: [7, 14, 21], rsi_ob: [70, 75, 80] } })
If strategy_sweep pauses mid-sweep due to cooldown, it will auto-resume — just wait.
Step 5: Compare Side-By-Side
Produce a metrics table:
| Metric |
Strategy A (best) |
Strategy B (best) |
Winner |
| Net Profit |
$8,420 |
$6,110 |
A |
| Sharpe |
1.42 |
1.78 |
B |
| Max DD |
-$2,100 |
-$980 |
B |
| Profit Factor |
1.65 |
1.89 |
B |
| Trades |
84 |
142 |
— |
Pull each row from each strategy's data_get_strategy_results for the best-params run.
Step 6: Declare Winner + Caveats
- Winner by default rule (Sharpe): Strategy B
- Caveats:
- Trade count — if B has far more trades, is it over-trading? Factor commission
- Regime — was the test window mostly trending or chop? A's edge may only show in trends
- Robustness — check variance across the sweep, not just the single best row. Cherry-picked best params can overfit
Step 7: Report + Cleanup
- Show the table + winner call + caveats
capture_screenshot of the winning strategy loaded so the user has a visual
state_restore back to abtest-baseline if the user wants their original chart
- Optionally
state_delete the baseline snapshot once confirmed
Error Notes
PINE_COMPILE_ERROR — fix source, retry compile before sweeping
STRATEGY_SWEEP_COOLDOWN — expected; the tool resumes automatically
CHART_LOADING after restore — wait 1-2s and continue
1---2name: strategy-ab-test-43description: Head-to-head strategy comparison — snapshot state, sweep params on strategy A, restore, sweep strategy B, then compare metrics side-by-side. Use when the user asks "which strategy is better?" or "A/B test these two".4---56# Strategy A/B Test78You are running two Pine strategies head-to-head on the same symbol/timeframe and reporting a data-backed winner.910## Step 1: Lock the Environment1112The comparison is only valid if both strategies run on the same chart state.13141. `chart_get_state` — confirm symbol, timeframe, and chart type152. `state_snapshot` with tag `abtest-baseline` — so you can roll back cleanly between A and B, and at the end1617Example baseline: `ES1!`, `15m`, Candles.1819## Step 2: Define What "Winner" Means2021Ask the user (or use defaults) for the ranking metric. Common options:2223- **Net Profit** — simplest, ignores risk24- **Sharpe Ratio** — risk-adjusted, preferred25- **Max Drawdown** — lower is better (use absolute value)26- **Profit Factor** — gross profit / gross loss27- **Composite** — rank each metric, sum ranks, lowest wins2829Default: report all four, pick winner by **Sharpe**, break ties by **lower max drawdown**.3031## Step 3: Run Strategy A32331. Load A's source: paste via `pine_set_source` (or `pine_open` if saved by name)342. `pine_smart_compile` — auto-detect type, compile, surface errors353. If errors: iterate via the `pine-develop` loop until clean364. `strategy_sweep` — parameter sweep with cooldown + resume. Pass the param grid (e.g., `{ ema_len: [20, 50, 100], atr_mult: [1.5, 2.0, 2.5] }`)375. Collect results — `strategy_sweep` returns per-combination `{net_profit, sharpe, max_dd, profit_factor, trades}`3839Example — strategy A (trend-follow):4041```42pine_set_source(<A source>)43pine_smart_compile()44strategy_sweep({ grid: { ema_len: [50, 100, 200], atr_mult: [1.5, 2.0] } })45```4647Pick A's best row by the chosen metric. Record it.4849## Step 4: Restore, Then Run Strategy B50511. `state_restore` with tag `abtest-baseline` — wipe A off the chart522. Load B's source: `pine_set_source` + `pine_smart_compile`533. `strategy_sweep` over B's param grid544. Pick B's best row5556Example — strategy B (mean-reversion):5758```59state_restore({ tag: "abtest-baseline" })60pine_set_source(<B source>)61pine_smart_compile()62strategy_sweep({ grid: { rsi_len: [7, 14, 21], rsi_ob: [70, 75, 80] } })63```6465If `strategy_sweep` pauses mid-sweep due to cooldown, it will auto-resume — just wait.6667## Step 5: Compare Side-By-Side6869Produce a metrics table:7071| Metric | Strategy A (best) | Strategy B (best) | Winner |72|--------|-------------------|-------------------|--------|73| Net Profit | $8,420 | $6,110 | A |74| Sharpe | 1.42 | 1.78 | **B** |75| Max DD | -$2,100 | -$980 | B |76| Profit Factor | 1.65 | 1.89 | B |77| Trades | 84 | 142 | — |7879Pull each row from each strategy's `data_get_strategy_results` for the best-params run.8081## Step 6: Declare Winner + Caveats8283- **Winner by default rule (Sharpe)**: Strategy B84- **Caveats**:85 - Trade count — if B has far more trades, is it over-trading? Factor commission86 - Regime — was the test window mostly trending or chop? A's edge may only show in trends87 - Robustness — check variance across the sweep, not just the single best row. Cherry-picked best params can overfit8889## Step 7: Report + Cleanup90911. Show the table + winner call + caveats922. `capture_screenshot` of the winning strategy loaded so the user has a visual933. `state_restore` back to `abtest-baseline` if the user wants their original chart944. Optionally `state_delete` the baseline snapshot once confirmed9596## Error Notes9798- `PINE_COMPILE_ERROR` — fix source, retry compile before sweeping99- `STRATEGY_SWEEP_COOLDOWN` — expected; the tool resumes automatically100- `CHART_LOADING` after restore — wait 1-2s and continue