OPEN-SOURCE SCRIPT
S&P 500 Breadth Bullstack

Core Idea of the Indicator:
The indicator is not a classic buy/sell trigger, but rather a breadth regime scanner for the S&P 500. In other words, it measures how many stocks in each sector are trading above their 20-day, 50-day, and 200-day moving averages (Simple Moving Average - SMA), compresses that information into a color-coded view, and visualizes whether there is a true “bullstack” in the market.
In practical terms, this means it is especially useful for filtering long positions during strong market phases, scaling into them, and turning defensive earlier when market breadth starts to weaken.
Structure:
The code loads three breadth series for each of the 11 S&P 500 sectors, plus the Nasdaq-100: 20D, 50D, and 200D.
The data is retrieved using request.security() from other symbols or contexts; in Pine, this function is used to access values from other symbols or timeframes within the script.
The script also calculates three averages from the 11 sectors (tot20, tot50, tot200) to create an overall view of S&P 500 market breadth.
Color Logic:
The core idea is: 20D represents short-term momentum, 50D represents medium-term confirmation, and 200D represents the long-term trend.
The bullstackColor() function does not create a simple traffic-light system, but rather a graduated state model: green/turquoise means broad strength, white marks the transition zone, yellow/orange signals early weakness, red to near-black indicates clear breadth weakness, and pink represents a special case of negative divergence.
This divergence occurs when many stocks are still above their 200D line in the long term (b200 > 65), while short-term momentum is already breaking down significantly (b20 < 45) — a typical late-cycle/distribution signal.
Long Strategy:
For longs, the indicator is most powerful when used as a market filter:
The best entries occur when overall breadth rotates up out of capitulation through neutral (white) and then into light green, with 20D breadth picking up first as short-term momentum, 50D breadth then confirming as the medium-term trend, and 200D remaining stable as the long-term trend.
The highest-quality longs occur when not only “Total SPY” turns green, but especially cyclical sectors such as XLY, XLF, XLI, XLK, and ideally the Nasdaq block as well are participating. That usually means the rally is broad, risk-on, and more durable.
Specifically, I would derive three long setups from this:
Early long (first tranche) on the turn from capitulation into the neutral zone, when 20D breadth is clearly rising and 50D breadth is no longer declining.
Confirmation long (second tranche), when 20D > 50D > 50 and several leading sectors turn green at the same time.
Trend add-on (final tranche), when the overall market and cyclical sectors such as XLY, XLF, XLI, and XLK are already green and rotating into turquoise, while pullbacks on the price chart remain shallow (only down to the SMA20 or slightly below).
Position Management:
For position management, the color is almost more important than the entry itself: as long as the picture remains green and 20D only fluctuates slightly, that argues for holding rather than nervous re-trading.
Partial profit-taking or tighter risk management makes sense once strong green fades into pale green or white, because that often marks the transition from expansion to exhaustion.
A clear warning signal against new longs is pink: in that case, the long-term trend is still intact, but short-term momentum is already rolling over — exactly the kind of environment where late longs are often rewarded the least. Caution is warranted here, and the first partial profits should be secured.
Short Strategy:
I would trade shorts much more selectively than longs with this indicator, because breadth indicators in uptrends can often stay “too strong” longer than short setups can tolerate.
The better short opportunities do not occur on the first yellow bar, but when the overall picture shifts from white/yellow into orange/red, while 20D breadth and 50D breadth are weak at the same time and defensive sectors look better than cyclical ones. That points more to genuine market distribution rather than just a simple pullback in individual sectors.
The cleanest short entries come after a failed rebound out of a pink divergence, or when multiple sectors flip synchronously into red/dark red after a warning phase, especially if Nasdaq, Tech, and other cyclical sectors confirm the weakness.
A simple practical rule would therefore be:
One more important point:
The script measures market breadth, not price structure. Therefore, it is best used as a top-down filter together with price triggers in SPY/ES/QQQ or in individual stocks — for example, breadth turning green plus a breakout or trend pullback on the chart, rather than trading breadth in isolation.
The indicator is not a classic buy/sell trigger, but rather a breadth regime scanner for the S&P 500. In other words, it measures how many stocks in each sector are trading above their 20-day, 50-day, and 200-day moving averages (Simple Moving Average - SMA), compresses that information into a color-coded view, and visualizes whether there is a true “bullstack” in the market.
In practical terms, this means it is especially useful for filtering long positions during strong market phases, scaling into them, and turning defensive earlier when market breadth starts to weaken.
Structure:
The code loads three breadth series for each of the 11 S&P 500 sectors, plus the Nasdaq-100: 20D, 50D, and 200D.
The data is retrieved using request.security() from other symbols or contexts; in Pine, this function is used to access values from other symbols or timeframes within the script.
The script also calculates three averages from the 11 sectors (tot20, tot50, tot200) to create an overall view of S&P 500 market breadth.
Color Logic:
The core idea is: 20D represents short-term momentum, 50D represents medium-term confirmation, and 200D represents the long-term trend.
The bullstackColor() function does not create a simple traffic-light system, but rather a graduated state model: green/turquoise means broad strength, white marks the transition zone, yellow/orange signals early weakness, red to near-black indicates clear breadth weakness, and pink represents a special case of negative divergence.
This divergence occurs when many stocks are still above their 200D line in the long term (b200 > 65), while short-term momentum is already breaking down significantly (b20 < 45) — a typical late-cycle/distribution signal.
Long Strategy:
For longs, the indicator is most powerful when used as a market filter:
The best entries occur when overall breadth rotates up out of capitulation through neutral (white) and then into light green, with 20D breadth picking up first as short-term momentum, 50D breadth then confirming as the medium-term trend, and 200D remaining stable as the long-term trend.
The highest-quality longs occur when not only “Total SPY” turns green, but especially cyclical sectors such as XLY, XLF, XLI, XLK, and ideally the Nasdaq block as well are participating. That usually means the rally is broad, risk-on, and more durable.
Specifically, I would derive three long setups from this:
Early long (first tranche) on the turn from capitulation into the neutral zone, when 20D breadth is clearly rising and 50D breadth is no longer declining.
Confirmation long (second tranche), when 20D > 50D > 50 and several leading sectors turn green at the same time.
Trend add-on (final tranche), when the overall market and cyclical sectors such as XLY, XLF, XLI, and XLK are already green and rotating into turquoise, while pullbacks on the price chart remain shallow (only down to the SMA20 or slightly below).
Position Management:
For position management, the color is almost more important than the entry itself: as long as the picture remains green and 20D only fluctuates slightly, that argues for holding rather than nervous re-trading.
Partial profit-taking or tighter risk management makes sense once strong green fades into pale green or white, because that often marks the transition from expansion to exhaustion.
A clear warning signal against new longs is pink: in that case, the long-term trend is still intact, but short-term momentum is already rolling over — exactly the kind of environment where late longs are often rewarded the least. Caution is warranted here, and the first partial profits should be secured.
Short Strategy:
I would trade shorts much more selectively than longs with this indicator, because breadth indicators in uptrends can often stay “too strong” longer than short setups can tolerate.
The better short opportunities do not occur on the first yellow bar, but when the overall picture shifts from white/yellow into orange/red, while 20D breadth and 50D breadth are weak at the same time and defensive sectors look better than cyclical ones. That points more to genuine market distribution rather than just a simple pullback in individual sectors.
The cleanest short entries come after a failed rebound out of a pink divergence, or when multiple sectors flip synchronously into red/dark red after a warning phase, especially if Nasdaq, Tech, and other cyclical sectors confirm the weakness.
A simple practical rule would therefore be:
[]Prefer longs when breadth is expanding, 20D leads first, and 50D/200D then confirm.
[]No fresh longs during pink divergences.- Only take shorts when the weakness is broad, synchronized, and visible across sectors.
One more important point:
The script measures market breadth, not price structure. Therefore, it is best used as a top-down filter together with price triggers in SPY/ES/QQQ or in individual stocks — for example, breadth turning green plus a breakout or trend pullback on the chart, rather than trading breadth in isolation.
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。