OPEN-SOURCE SCRIPT
Strategy Comparator

Strategy Comparator
Important — script type
This is a Pine v6 indicator() script, NOT a strategy() script. TradingView's built-in Strategy Tester does NOT apply to this script. All backtesting metrics (equity curve, return, Sharpe, R-Ratio, win rate, profit factor, max drawdown, Calmar) are computed internally and displayed in this script's own ranking table on the chart, plus on-chart entry markers and alerts.
What it does
A single indicator that runs a live backtest of 18 strategy configurations across a grid of 6 assets × 4 timeframes (up to 1h), ranks them by the chosen metric (default R-Ratio), and surfaces the best (asset, strategy) combination automatically on the chart.
Originality (not a mashup)
This is a self-contained decision framework — not a stack of standard indicators or signals. The three components that make it original:
- Dynamic concurrent backtest engine — the table values are computed live on every bar via request.security across 24 (asset, timeframe) cells, each running all 18 strategy implementations and tracking 6 statistics per strategy. Changing parameters, side, or timeframe re-evaluates the entire grid on the fly. No precomputed values.
- R-Ratio ranking metric — a single number combining return and smoothness via an OLS regression on log-equity vs time in years (R² × slope). Selects strategies that grow linearly rather than via lucky spikes. Available alongside Sharpe, Win Rate, Total Return, Calmar, Profit Factor and Max DD.
- Confidence score (0–100) — a quantitative layer that tests whether the top candidate's edge is meaningful or compatible with noise. Combines: R-Ratio strength (scale-invariant), edge over direction-weighted passive baselines (HODL for long-biased time, REKT for short-biased time), edge over RAND (statistical baseline), win rate excess over 50%, and Kelly fraction = p × (1 − 1/PF). Shown in title as CONF X/100.
The 18 strategy configurations
- 1. HODL — Always long. Buy & hold reference.
- 2. REKT — Always short. Opposite of HODL.
- 3. RAND — Random positions (-1 / 0 / +1). Statistical baseline.
- 4. BBANDS — Bollinger Bands, 3 selectable behaviors (mean reversion, exit at midline, breakout outside).
- 5. KELTNER — Keltner Channel breakout (EMA ± ATR × multiplier).
- 6. SMA — Two-SMA crossover (fast vs slow).
- 7. MACD — MACD line / signal line crossover.
- 8. MOM — Momentum: current price vs price N bars ago.
- 9. BREAKOUT — Donchian Channel: long above N-bar high, short below N-bar low.
- 10. RSI — RSI thresholds, 3 selectable behaviors.
- 11. RWI — Random Walk Index (fast and slow).
- 12. OUTSIDE — Outside-bar pattern with candle-color direction.
- 13. CONSEC — N consecutive up/down bars for direction.
- 14. SUPER — Native Supertrend (ATR-based trailing stop).
- 15. TRATINGS — Aggregate of 14 sub-indicators (SMA/EMA on 6 periods, HMA, VWMA, Ichimoku, RSI, Stoch, CCI, ADX, AO, MOM, MACD, StochRSI, Williams %R, Bulls/Bears Power, Ultimate Oscillator). MA / Oscillator / Both groups selectable.
- 16. PSAR — Parabolic SAR.
- 17. STOCH — Stochastic Slow with overbought/oversold thresholds.
- 18. WICKS — Cumulative upper-vs-lower wick imbalance over a rolling period.
Every strategy honors the Side input (Long Only / Short Only / Both). In Long Only mode, an opposite-direction signal closes the long position to flat (and symmetric for Short Only).
How to use
1. Add it to your chart.
2. Leave the 6 asset slots empty to test only the chart symbol, or fill them to compare different symbols side by side. Duplicate slots are deduplicated automatically.
3. Read the first row of the ranking table — that's the best combination by the chosen metric averaged over the 4 timeframes.
4. On the chart, the top strategy's long (green label below the bar) and short (red label above the bar) entries are plotted with the strategy name as label. A state machine suppresses consecutive same-direction signals; exits to flat reset the state.
5. Read the CONF X/100 in the title row as a quick "should I trust this?" gauge.
6. Tick Show selected strategy in the Strategy Override group to force a specific strategy (overrides the ranking).
7. Create one TradingView alert on the indicator with condition "Any alert() function call" — it covers every entry of the currently tracked strategy. Frequency: Once Per Bar Close (no repaint).
Non-repainting
- Markers: rendered once at chart load for the full history, then incrementally only at confirmed real-time bar closes. No intra-bar flicker.
- Alerts: fire only on alert.freq_once_per_bar_close.
Main inputs
- Strategy Override (checkbox + dropdown) — force a specific strategy as TOP regardless of ranking.
- 6 asset slots (optional) — empty falls back to chart symbol.
- 4 timeframe slots (max 1h).
- History (bars per cell) — backtest depth, 100–5000 step 50, default 1000.
- Ranking metric — R-Ratio (default), Sharpe, Win Rate %, Total Return %, Calmar, Profit Factor, Max DD %.
- Side — Long Only / Short Only / Both.
- Top N (markers) — how many top-ranked strategies to plot on the chart (1–10, default 1).
- Top N (table) — how many rows to display in the ranking table (5–98).
- Show signal markers, Enable alerts, Table position, Table size.
- Per-strategy parameters (period, threshold, multiplier, etc.).
Limits
- Up to 4 timeframes and 6 assets, with 1h as the highest allowed timeframe (Pine request.security cap of 40 calls — 24 used).
- History limited to 5000 bars per cell (Pine max_bars_back cap).
- HODL / REKT / RAND are control strategies (baselines): if they appear at the top of the ranking, the technical strategies are underperforming buy & hold or random.
- All UI labels and tooltips are in English. Translations can be requested in comments if needed.
Important — script type
This is a Pine v6 indicator() script, NOT a strategy() script. TradingView's built-in Strategy Tester does NOT apply to this script. All backtesting metrics (equity curve, return, Sharpe, R-Ratio, win rate, profit factor, max drawdown, Calmar) are computed internally and displayed in this script's own ranking table on the chart, plus on-chart entry markers and alerts.
What it does
A single indicator that runs a live backtest of 18 strategy configurations across a grid of 6 assets × 4 timeframes (up to 1h), ranks them by the chosen metric (default R-Ratio), and surfaces the best (asset, strategy) combination automatically on the chart.
Originality (not a mashup)
This is a self-contained decision framework — not a stack of standard indicators or signals. The three components that make it original:
- Dynamic concurrent backtest engine — the table values are computed live on every bar via request.security across 24 (asset, timeframe) cells, each running all 18 strategy implementations and tracking 6 statistics per strategy. Changing parameters, side, or timeframe re-evaluates the entire grid on the fly. No precomputed values.
- R-Ratio ranking metric — a single number combining return and smoothness via an OLS regression on log-equity vs time in years (R² × slope). Selects strategies that grow linearly rather than via lucky spikes. Available alongside Sharpe, Win Rate, Total Return, Calmar, Profit Factor and Max DD.
- Confidence score (0–100) — a quantitative layer that tests whether the top candidate's edge is meaningful or compatible with noise. Combines: R-Ratio strength (scale-invariant), edge over direction-weighted passive baselines (HODL for long-biased time, REKT for short-biased time), edge over RAND (statistical baseline), win rate excess over 50%, and Kelly fraction = p × (1 − 1/PF). Shown in title as CONF X/100.
The 18 strategy configurations
- 1. HODL — Always long. Buy & hold reference.
- 2. REKT — Always short. Opposite of HODL.
- 3. RAND — Random positions (-1 / 0 / +1). Statistical baseline.
- 4. BBANDS — Bollinger Bands, 3 selectable behaviors (mean reversion, exit at midline, breakout outside).
- 5. KELTNER — Keltner Channel breakout (EMA ± ATR × multiplier).
- 6. SMA — Two-SMA crossover (fast vs slow).
- 7. MACD — MACD line / signal line crossover.
- 8. MOM — Momentum: current price vs price N bars ago.
- 9. BREAKOUT — Donchian Channel: long above N-bar high, short below N-bar low.
- 10. RSI — RSI thresholds, 3 selectable behaviors.
- 11. RWI — Random Walk Index (fast and slow).
- 12. OUTSIDE — Outside-bar pattern with candle-color direction.
- 13. CONSEC — N consecutive up/down bars for direction.
- 14. SUPER — Native Supertrend (ATR-based trailing stop).
- 15. TRATINGS — Aggregate of 14 sub-indicators (SMA/EMA on 6 periods, HMA, VWMA, Ichimoku, RSI, Stoch, CCI, ADX, AO, MOM, MACD, StochRSI, Williams %R, Bulls/Bears Power, Ultimate Oscillator). MA / Oscillator / Both groups selectable.
- 16. PSAR — Parabolic SAR.
- 17. STOCH — Stochastic Slow with overbought/oversold thresholds.
- 18. WICKS — Cumulative upper-vs-lower wick imbalance over a rolling period.
Every strategy honors the Side input (Long Only / Short Only / Both). In Long Only mode, an opposite-direction signal closes the long position to flat (and symmetric for Short Only).
How to use
1. Add it to your chart.
2. Leave the 6 asset slots empty to test only the chart symbol, or fill them to compare different symbols side by side. Duplicate slots are deduplicated automatically.
3. Read the first row of the ranking table — that's the best combination by the chosen metric averaged over the 4 timeframes.
4. On the chart, the top strategy's long (green label below the bar) and short (red label above the bar) entries are plotted with the strategy name as label. A state machine suppresses consecutive same-direction signals; exits to flat reset the state.
5. Read the CONF X/100 in the title row as a quick "should I trust this?" gauge.
6. Tick Show selected strategy in the Strategy Override group to force a specific strategy (overrides the ranking).
7. Create one TradingView alert on the indicator with condition "Any alert() function call" — it covers every entry of the currently tracked strategy. Frequency: Once Per Bar Close (no repaint).
Non-repainting
- Markers: rendered once at chart load for the full history, then incrementally only at confirmed real-time bar closes. No intra-bar flicker.
- Alerts: fire only on alert.freq_once_per_bar_close.
Main inputs
- Strategy Override (checkbox + dropdown) — force a specific strategy as TOP regardless of ranking.
- 6 asset slots (optional) — empty falls back to chart symbol.
- 4 timeframe slots (max 1h).
- History (bars per cell) — backtest depth, 100–5000 step 50, default 1000.
- Ranking metric — R-Ratio (default), Sharpe, Win Rate %, Total Return %, Calmar, Profit Factor, Max DD %.
- Side — Long Only / Short Only / Both.
- Top N (markers) — how many top-ranked strategies to plot on the chart (1–10, default 1).
- Top N (table) — how many rows to display in the ranking table (5–98).
- Show signal markers, Enable alerts, Table position, Table size.
- Per-strategy parameters (period, threshold, multiplier, etc.).
Limits
- Up to 4 timeframes and 6 assets, with 1h as the highest allowed timeframe (Pine request.security cap of 40 calls — 24 used).
- History limited to 5000 bars per cell (Pine max_bars_back cap).
- HODL / REKT / RAND are control strategies (baselines): if they appear at the top of the ranking, the technical strategies are underperforming buy & hold or random.
- All UI labels and tooltips are in English. Translations can be requested in comments if needed.
Skrip sumber terbuka
Dalam semangat TradingView sebenar, pencipta skrip ini telah menjadikannya sumber terbuka, jadi pedagang boleh menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupuan anda boleh menggunakan secara percuma, ingat bahawa penerbitan semula kod ini tertakluk kepada Peraturan Dalaman.
Penafian
Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat TradingView sebenar, pencipta skrip ini telah menjadikannya sumber terbuka, jadi pedagang boleh menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupuan anda boleh menggunakan secara percuma, ingat bahawa penerbitan semula kod ini tertakluk kepada Peraturan Dalaman.
Penafian
Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.