OPEN-SOURCE SCRIPT
RSI Median Deviation

RSI Median Deviation – Adaptive Statistical RSI for High-Probability Extremes
The Relative Strength Index (RSI) is a momentum oscillator developed by J. Welles Wilder in 1978 to measure the magnitude of recent price changes and identify potential overbought or oversold conditions. It calculates the ratio of upward to downward price movements over a specified period, scaled to 0-100. However, standard RSI often relies on fixed thresholds like 70/30, which can produce unreliable signals in varying market regimes due to their lack of adaptability to the actual distribution of RSI values.
This indicator was developed because I needed a reliable tool for spotting intermediate high-probability bottoms and tops. Instead of arbitrary horizontal lines, it uses the RSI’s own historical median as a dynamic centerline and measures how far the current RSI deviates from that median over a chosen lookback period. The main signals are triggered only at 2 standard deviation (2σ) extremes — statistically rare events that occur roughly 5 % of the time under a normal distribution. I selected 2σ because it is extreme enough to be meaningful yet frequent enough for practical trading. For oversold signals I further require RSI to be below 42, a filter that significantly improved results in my mean-reversion tests (enter on oversold, exit on the first bar the condition is no longer true).
The combination of percentile median + standard deviation bands is deliberate: the median is far more robust to outliers than a simple average, while the SD bands automatically adjust to the current volatility of the RSI itself, producing adaptive envelopes that work equally well in ranging and trending markets.
Underlying Concepts and Calculations
Base RSI: RSI = 100 − (100 / (1 + RS)), RS = average gain / average loss (default length 10).
Percentile Median: 50th percentile of the last "N" RSI values (default 28 = 4 weeks)
→ dynamic, outlier-resistant centerline.
Standard Deviation Bands: rolling stdev of RSI (default length 27 = = 4 weeks (almost))
→ bands = median ± 1σ / 2σ.
Optional Dynamic MA Envelopes: user-selectable moving average (TEMA, WMA, etc., default WMA length 37) for additional momentum context.

Trend Bias Coloring
Independent of the statistical extremes, the RSI line itself is colored green when above the user-defined Long Threshold (default 60) and red when below the Short Threshold (default 47). This provides an instant bullish/bearish bias overlay similar to classic RSI usage, without interfering with the main 2σ extreme signals.
Extremes are highlighted with background color (green for oversold 2σ + RSI<42, magenta for overbought 2σ) and small diamond markers for ultra-extremes (RSI <25 or >85).

Originality and Development Rationale
The indicator was built and refined through extensive testing on dozens of assets including major cryptocurrencies:
(BTC, ETH, SOL, SUI, BNB, XRP, TRX, DOGE, LINK, PAXG, CVX, HYPE, VIRTUAL and many more),
the Magnificent 7 stocks,, QQQ, SPX, and gold.
Default parameters were chosen to deliver consistent profitability in simple mean-reversion setups while maximizing Sortino ratio and minimizing maximum drawdown across this broad universe — ensuring the settings are robust and not overfitted to any single instrument or timeframe.
How to Use It
Ideal for swing / position trading on the 1h to daily charts (the same defaults work).
Oversold (high-probability long): RSI crosses below lower 2σ band AND RSI < 42
→ green background
→ enter long, exit the first bar the condition disappears.
Overbought (high-probability short): RSI crosses above upper 2σ band
→ magenta background
→ enter short, exit on opposite signal or at median. (Shorts were not tested, it's only an idea)
Use the green/red RSI line coloring for quick trend context and to avoid fighting strong momentum.
Always confirm with price action and manage risk appropriately.
This indicator is not a standalone trading system.
Disclaimer: This is not financial advice. Backtests are based on past results and are not indicative of future performance.
The Relative Strength Index (RSI) is a momentum oscillator developed by J. Welles Wilder in 1978 to measure the magnitude of recent price changes and identify potential overbought or oversold conditions. It calculates the ratio of upward to downward price movements over a specified period, scaled to 0-100. However, standard RSI often relies on fixed thresholds like 70/30, which can produce unreliable signals in varying market regimes due to their lack of adaptability to the actual distribution of RSI values.
This indicator was developed because I needed a reliable tool for spotting intermediate high-probability bottoms and tops. Instead of arbitrary horizontal lines, it uses the RSI’s own historical median as a dynamic centerline and measures how far the current RSI deviates from that median over a chosen lookback period. The main signals are triggered only at 2 standard deviation (2σ) extremes — statistically rare events that occur roughly 5 % of the time under a normal distribution. I selected 2σ because it is extreme enough to be meaningful yet frequent enough for practical trading. For oversold signals I further require RSI to be below 42, a filter that significantly improved results in my mean-reversion tests (enter on oversold, exit on the first bar the condition is no longer true).
The combination of percentile median + standard deviation bands is deliberate: the median is far more robust to outliers than a simple average, while the SD bands automatically adjust to the current volatility of the RSI itself, producing adaptive envelopes that work equally well in ranging and trending markets.
Underlying Concepts and Calculations
Base RSI: RSI = 100 − (100 / (1 + RS)), RS = average gain / average loss (default length 10).
Percentile Median: 50th percentile of the last "N" RSI values (default 28 = 4 weeks)
→ dynamic, outlier-resistant centerline.
Standard Deviation Bands: rolling stdev of RSI (default length 27 = = 4 weeks (almost))
→ bands = median ± 1σ / 2σ.
Optional Dynamic MA Envelopes: user-selectable moving average (TEMA, WMA, etc., default WMA length 37) for additional momentum context.
Trend Bias Coloring
Independent of the statistical extremes, the RSI line itself is colored green when above the user-defined Long Threshold (default 60) and red when below the Short Threshold (default 47). This provides an instant bullish/bearish bias overlay similar to classic RSI usage, without interfering with the main 2σ extreme signals.
Extremes are highlighted with background color (green for oversold 2σ + RSI<42, magenta for overbought 2σ) and small diamond markers for ultra-extremes (RSI <25 or >85).
Originality and Development Rationale
The indicator was built and refined through extensive testing on dozens of assets including major cryptocurrencies:
(BTC, ETH, SOL, SUI, BNB, XRP, TRX, DOGE, LINK, PAXG, CVX, HYPE, VIRTUAL and many more),
the Magnificent 7 stocks,, QQQ, SPX, and gold.
Default parameters were chosen to deliver consistent profitability in simple mean-reversion setups while maximizing Sortino ratio and minimizing maximum drawdown across this broad universe — ensuring the settings are robust and not overfitted to any single instrument or timeframe.
How to Use It
Ideal for swing / position trading on the 1h to daily charts (the same defaults work).
Oversold (high-probability long): RSI crosses below lower 2σ band AND RSI < 42
→ green background
→ enter long, exit the first bar the condition disappears.
Overbought (high-probability short): RSI crosses above upper 2σ band
→ magenta background
→ enter short, exit on opposite signal or at median. (Shorts were not tested, it's only an idea)
Use the green/red RSI line coloring for quick trend context and to avoid fighting strong momentum.
Always confirm with price action and manage risk appropriately.
This indicator is not a standalone trading system.
Disclaimer: This is not financial advice. Backtests are based on past results and are not indicative of future performance.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.