OPEN-SOURCE SCRIPT
Sector Rotation Z-Score Strategy

Sector Rotation Z-Score Strategy
This strategy trades one symbol based on the relative strength between two others. It takes the log ratio of Ticker 1 to Ticker 2, then converts that ratio into a z-score over a rolling lookback (default 63 bars). The z-score is plotted as a histogram: positive means Ticker 1 is outperforming its recent average relationship with Ticker 2, negative means it's lagging.
The idea is straightforward — buy when the pair is rotating back in favor of Ticker 1, exit when it rotates away.
Entry and exit logic
A long is triggered when the z-score crosses above the negative buffer level, and closed when it crosses below the positive buffer level. Both buffers are adjustable in z-score units, so you can enter earlier or later than the zero line rather than being locked to a single crossover point. An optional short side triggers when the z-score crosses back above the extreme rotation level.
The z-score can be used raw or smoothed with a short moving average to cut down on whipsaw crosses.
Filters
ADX gate — entries require ADX above a minimum threshold, so the strategy stays out when the underlying isn't trending. There's also an optional exit when ADX drops below a lower threshold, which closes trades that have gone flat.
PPP (Price % of Pivots) gate — this measures where price sits relative to the most recent pivot high and pivot low, producing a simple bullish or bearish bias. Entries can require that bias to agree with the trade direction. Because the pivot bias and the z-score cross rarely line up on the exact same bar, there's a match window: a cross stays armed for a set number of bars, and the bias is also checked backward over that same window. Exits can optionally close on a bias flip, or require confirmation — meaning the z-score cross is latched and the trade only closes once PPP agrees.
Percent TP/SL — optional fixed take profit and stop loss as a percentage of entry price.
Date range filter — restricts the backtest window and closes any open position on exit from the range.
Symbol handling
Either leg accepts a numeric value instead of a ticker (enter "1" to compare a symbol against a constant). Unresolvable or frozen data feeds fall back to a constant rather than breaking the script, with a configurable detection window.
Example shown as BTC vs RSP to compare crypto relative strength against the equal-weight S&P
Alerts
Entry, exit, take profit, and stop loss all fire formatted alert messages that include the symbol, price, and which leg is strengthening or weakening. Messages are structured for webhook use (Discord and similar). Standard alertcondition entries are also available in the alert dialog.
This strategy trades one symbol based on the relative strength between two others. It takes the log ratio of Ticker 1 to Ticker 2, then converts that ratio into a z-score over a rolling lookback (default 63 bars). The z-score is plotted as a histogram: positive means Ticker 1 is outperforming its recent average relationship with Ticker 2, negative means it's lagging.
The idea is straightforward — buy when the pair is rotating back in favor of Ticker 1, exit when it rotates away.
Entry and exit logic
A long is triggered when the z-score crosses above the negative buffer level, and closed when it crosses below the positive buffer level. Both buffers are adjustable in z-score units, so you can enter earlier or later than the zero line rather than being locked to a single crossover point. An optional short side triggers when the z-score crosses back above the extreme rotation level.
The z-score can be used raw or smoothed with a short moving average to cut down on whipsaw crosses.
Filters
ADX gate — entries require ADX above a minimum threshold, so the strategy stays out when the underlying isn't trending. There's also an optional exit when ADX drops below a lower threshold, which closes trades that have gone flat.
PPP (Price % of Pivots) gate — this measures where price sits relative to the most recent pivot high and pivot low, producing a simple bullish or bearish bias. Entries can require that bias to agree with the trade direction. Because the pivot bias and the z-score cross rarely line up on the exact same bar, there's a match window: a cross stays armed for a set number of bars, and the bias is also checked backward over that same window. Exits can optionally close on a bias flip, or require confirmation — meaning the z-score cross is latched and the trade only closes once PPP agrees.
Percent TP/SL — optional fixed take profit and stop loss as a percentage of entry price.
Date range filter — restricts the backtest window and closes any open position on exit from the range.
Symbol handling
Either leg accepts a numeric value instead of a ticker (enter "1" to compare a symbol against a constant). Unresolvable or frozen data feeds fall back to a constant rather than breaking the script, with a configurable detection window.
Example shown as BTC vs RSP to compare crypto relative strength against the equal-weight S&P
Alerts
Entry, exit, take profit, and stop loss all fire formatted alert messages that include the symbol, price, and which leg is strengthening or weakening. Messages are structured for webhook use (Discord and similar). Standard alertcondition entries are also available in the alert dialog.
Open-source script
In true PulseWire spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by PulseWire. Read more in the Terms of Use.
Open-source script
In true PulseWire spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by PulseWire. Read more in the Terms of Use.
