RSI Divergence Detector

RSI Divergence Detector

1. Introduction

The RSI Divergence Detector is a technical indicator that automatically identifies divergences between price and the Relative Strength Index (RSI). Divergences are valuable signals in technical analysis, as they can anticipate potential trend reversals before they occur.

This indicator is especially useful for both trend-following traders and those looking to detect market turning points. Thanks to its visual implementation with color bands and dynamic alerts, it becomes an ideal tool for strategies involving RSI behavior.

2. Indicator Foundation

Divergences between price and an oscillator like RSI are early warning signs of a possible change in trend direction.

Bullish Divergence

Occurs when price prints a lower low, but the RSI shows a higher low. This suggests that despite the price drop, bearish momentum is weakening and an upward reversal could be near.

Bearish Divergence

Occurs when price makes a higher high, while the RSI shows a lower high. This indicates that although price is rising, bullish strength is fading, hinting at a possible retracement or correction.

The indicator automates the detection of these patterns on the chart, eliminating the need for manual comparison and enabling a more systematic, objective signal detection.

3. Code Logic

The code is divided into two main blocks: detection of bullish and bearish divergences, both based on comparing the average price and the RSI.

RSI and Average Price Calculation

RSI is calculated using the average price (open + high + low + close) / 4, which smooths volatility and reduces noise. The RSI period is adjustable (default: 14).

Bullish Divergence Detection

It looks for a recent lower low in price, and simultaneously a higher low in RSI. Key conditions include:

  • srcLL: current price is above its recent lowest point, and that low is lower than an older one.
  • rsiHL: current RSI is higher than its recent low, that low is above an older low, and it’s under the oversold level (30).

When both conditions are met, a green area is drawn under the RSI graph.

Bearish Divergence Detection

The inverse logic applies:

  • srcHH: current price is below its recent highest point, and that high is above an older one.
  • rsiLH: current RSI is lower than its recent high, that high is below an older high, and above the overbought level (70).

A red band is drawn above the RSI to signal potential downward reversal.

Alert System

Both divergence signals use counters to measure the number of bars since the last signal.

The ialert parameter defines the minimum how many bars a divergence remains visually highlighted. if a divergence occurred less than ialert bars ago, a colored band (green or red) is drawn on the RSI. Otherwise, the band is not displayed.

In addition, the RSI line changes color dynamically:

  • Green when there’s a bullish divergence.
  • Red when there’s a bearish divergence.
  • Blue in all other cases.

4. Indicator Settings

The indicator includes customizable parameters that allow it to be adapted to different trading styles and timeframes:

Parameter Description Default Value
rsiprd RSI calculation period 14
ob Overbought threshold 70
os Oversold threshold 30
x Short-range lookback for local highs/lows 5
z Long-range lookback for divergence validation 25
revpct Required price reversal percentage to confirm a low/high 0.01 (1%)
ialert Maximum bars to dsiplay alerts 25

Adjustment Recommendations

  • For lower timeframes (1–15 min), reduce x and z for faster signals.
  • For higher timeframes (4h, daily), increase xz, and ialert to filter stronger signals.
  • Adjust revpct for sensitivity tuning: higher values reduce noise, but may miss early entries.

5. Practical Use

The RSI Divergence Detector is versatile and works across multiple timeframes. Its effectiveness improves when used in trending contexts or around key support/resistance zones.

Recommended Timeframes

  • Intraday (5min–1h): Suitable for scalping or short-term strategies. Use lower x and z.
  • Swing trading (4h–Daily): Better for identifying significant reversals. Default or higher values work well.

Indicator Combinations

This divergence detector can be used alongside:

  • Moving averages: For trend confirmation.
  • Support/resistance levels: Divergence near these areas carries more weight.
  • Volume indicators: Validate divergence strength, especially on longer timeframes.

Limitations

While divergences are powerful, they should not be used in isolation. It is recommended to wait for price action confirmation or additional signals before trading.

6. Indicator Code

The final section contains the full ProRealTime code (ProBuilder language) for implementing the RSI Divergence Detector.

Share this

Risk disclosure:

No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.

ProRealTime ITF files and other attachments : How to import ITF files into ProRealTime platform?

PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials

avatar
Register or

Likes

avatar
Related users ' posts
cdc.andersson Hello, when trying to include this wonderful Dynamic RSI on my trading system i get the erro...
Rafyone Bonjour Nicolas et bonne année lol J'ai une erreur qui apparait concernant drawbarchart une...
Nicolas Je pense que tu n'utilises pas le bon éditeur de code, cette instruction est compatible uniq...
Exalaxe Hey, i just noticed you optimized this strategy. Could i see your new version, please?
Andrea.1981 sorry i add my code but it not enter why i dont know
Andrea.1981 this is code simply stop , and you can see another version for stop / Codice principale...
AntoGH how did you backtest it ?
PastaPesto @AntoGH , I dont know if you are asking me, but my backtest was made in an IS of 4 years bet...
Ridder @PastaPesto Do you still running this and how is the results? Thanks
dau710 Are the Bulls Blue & the Bears Red?
dau710 Many thanks :)
myhlo Super Nicolas !! Cet indicateur offre une nouvelle lecture du Rsi ! Ces bandes up & Dawn...
okiki please how can i acess the indicator i like it and want it please am new here
Nicolas download the itf file above and import it into your prorealtime platform: https://www.prorea...
XXXXVII Excellent indicateur ! Déterminer une tendance est devenu un jeu d'enfants ! Félicitation......
gregoire bonjour nicolas j ai cherché partout sur le net et impossible de trouvé ce que je recherch...
Nicolas Merci de poster une demande dans le forum des indicateurs, ainsi on pourra le coder dans ce ...
bala Hi Nicolas, I have been extensively researching for lot of answers here. I found that you...
Nicolas Don't apply it on price, use the main "indicator and trading system" button found on the top...
Bogdan35 Hello, Is there one for MT4?
Nicolas
7 years ago
Kosuda76 Thanks for your reply! But I do not really understand what the opposite of them the values ​...
AdriTrade Good job Louis!! How could do this for the Williams indicator? Thank you very much.
imorandin Hi!, Nice code. I'm tryng to port it to another language, but I don't understand this part:...
luxrun Thanks, bolsatrilera, for a very good and solid screener!
luxrun sorry... indicator!
Lily Rios Gracias por el indicador, tengo una pregunta necesito programar un screener de valores que e...
Bebbo Grazie del prezioso contributo Nicolas, scusa la mia inesperienza, sono alle prime armi e s...
Nicolas To avoid recalculation, you should use the one from this post.
Bebbo Ok Grazie Nicolas.
bolsatrilera
7 years ago
jissey Bonjour Bolsatrilera, je trouve le cm rsi interessant, merci de l'avoir mis à disposition ! ...
bolsatrilera bonjour jissey, je n'ai aucune information sur ce que tu dis, je suis désolé.
ALE Hi the color is given by a numerical value derived from the oscillator in use. If you comp...
efahmy Thanks mate
Jo-01-R Hello, it is possible to have this indicator but instead of colors rather have numbers rangi...
Nicolas
7 years ago
enricot Scusa nn riesco.
SoloContado If you smooth the signal using a "summation" function, you get a nice "crossing of 0" graph....
ShaunG Greatly appreciated Nicolas!
Etienne Hi, thanks for proving this code. I would like to contribute by adding a computation speed...
Etienne //Compute Super Smoother coefficients once if barindex = 1 then a1 = exp( -1.414 * 3.14159...
Nicolas Thanks a lot!

Top