OPEN-SOURCE SCRIPT
Momentum Rotor | Basket Breakout & Leadership Rotation

What it does
Momentum Rotor ranks a basket of correlated symbols (default: AAPL, MSFT, NVDA, GOOGL, AMZN, META, TSLA, AVGO — fully user-editable) by N-bar rate-of-change momentum, recalculated every bar via request.security(). It only opens a long position on the chart's own symbol when that symbol is simultaneously the strongest performer in the basket (rank #1 by momentum) and breaking its own N-bar price high. If the held symbol later loses the #1 rank to another basket member, the strategy closes the position — it "rotates out" of a fading leader rather than holding through a reversal.
Core features
How the strategy works
Each bar, the script pulls the N-bar ROC (rate of change) for every symbol in the basket and sorts them from strongest to weakest. Your chart's symbol only becomes eligible to trade when it holds rank #1 and closes above its own N-bar high — combining a relative-strength filter with an absolute breakout trigger, so entries require both "stronger than its peers" and "breaking out on its own chart" to align. Exits are twofold: an ATR stop/target pair from the entry, and a rotation exit that closes the trade the moment (or, with the confirmation filter on, N bars after) another basket member overtakes it in the momentum ranking.
What makes it distinct
Most retail breakout scripts evaluate a single symbol in isolation. Momentum Rotor brings relative-strength rotation — a concept used by institutional sector/factor rotation strategies — into a single-chart script by polling an entire basket with request.security() and gating trade eligibility on relative rank, not just absolute price action. The live leaderboard turns that ranking process into something visible and auditable on the chart itself, rather than a black-box filter.
Tips for use
Limitations
This is an educational strategy template, not a production-ready system or financial advice. It has not been optimized or validated for any specific instrument, timeframe, or market regime. Past performance in this backtest does not indicate future results.
Momentum Rotor ranks a basket of correlated symbols (default: AAPL, MSFT, NVDA, GOOGL, AMZN, META, TSLA, AVGO — fully user-editable) by N-bar rate-of-change momentum, recalculated every bar via request.security(). It only opens a long position on the chart's own symbol when that symbol is simultaneously the strongest performer in the basket (rank #1 by momentum) and breaking its own N-bar price high. If the held symbol later loses the #1 rank to another basket member, the strategy closes the position — it "rotates out" of a fading leader rather than holding through a reversal.
Core features
- Live leaderboard table — ranks the full basket by momentum every bar, with gradient-colored scores and trend glyphs (↑/↓) so you can see who's strengthening or fading at a glance
- Regime/leadership dashboard — shows your symbol's current rank, how many bars the leader has held the top spot, and basket-wide breadth (% of symbols with positive momentum) as a quick risk-on/risk-off gauge
- Risk-based position sizing — position size is calculated from a fixed % of equity risked against an ATR-based stop distance, not a fixed share count
- ATR stop + R-multiple take-profit — stop distance and profit target both scale with volatility instead of using static price offsets
- Optional rotation-confirmation filter — require the leader to lose #1 rank for N consecutive bars before exiting, to reduce whipsaw from brief rank flickers
- Optional regime filter — require price above a long SMA before taking new entries, to avoid trading breakouts in a broader downtrend
- Realistic backtest defaults — includes commission, slippage, and margin settings out of the box rather than assuming frictionless, infinite-leverage fills
How the strategy works
Each bar, the script pulls the N-bar ROC (rate of change) for every symbol in the basket and sorts them from strongest to weakest. Your chart's symbol only becomes eligible to trade when it holds rank #1 and closes above its own N-bar high — combining a relative-strength filter with an absolute breakout trigger, so entries require both "stronger than its peers" and "breaking out on its own chart" to align. Exits are twofold: an ATR stop/target pair from the entry, and a rotation exit that closes the trade the moment (or, with the confirmation filter on, N bars after) another basket member overtakes it in the momentum ranking.
What makes it distinct
Most retail breakout scripts evaluate a single symbol in isolation. Momentum Rotor brings relative-strength rotation — a concept used by institutional sector/factor rotation strategies — into a single-chart script by polling an entire basket with request.security() and gating trade eligibility on relative rank, not just absolute price action. The live leaderboard turns that ranking process into something visible and auditable on the chart itself, rather than a black-box filter.
Tips for use
- Chart the strategy on one of the basket's own symbols (or add your target symbol to the basket inputs) — the "am I #1" check only works when your chart's ticker matches a basket entry
- This instance only manages a position on its own chart's symbol; it does not automatically route orders to whichever symbol becomes the new leader. To rotate capital across the whole basket, run separate instances on each symbol
- Momentum/breakout systems are inherently prone to false breakouts and whipsaws in choppy markets — the optional regime filter and rotation-confirmation delay are there to dampen that; test both on/off for your instrument and timeframe
- Sector ETFs (lower dispersion, steadier trends) and mega-cap tech names (higher dispersion, sharper momentum swings) behave differently in this framework — adjust lenMom/lenBreak accordingly
- Backtest results depend heavily on commission, slippage, and margin assumptions set in Properties — review and adjust these to match your actual broker before drawing conclusions
Limitations
This is an educational strategy template, not a production-ready system or financial advice. It has not been optimized or validated for any specific instrument, timeframe, or market regime. Past performance in this backtest does not indicate future results.
Script open-source
Dans l'esprit TradingView, le créateur de ce script l'a rendu open source afin que les traders puissent examiner et vérifier ses fonctionnalités. Bravo à l'auteur! Bien que vous puissiez l'utiliser gratuitement, n'oubliez pas que la republication du code est soumise à nos Règles.
Clause de non-responsabilité
Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.
Script open-source
Dans l'esprit TradingView, le créateur de ce script l'a rendu open source afin que les traders puissent examiner et vérifier ses fonctionnalités. Bravo à l'auteur! Bien que vous puissiez l'utiliser gratuitement, n'oubliez pas que la republication du code est soumise à nos Règles.
Clause de non-responsabilité
Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.