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
AntoGH Bonjour peut-on backtester cela ? Et si vous l'avez fait pourriez vous nous donner le code.....
Nicolas désolé c'est un oubli de ma part, tu peux supprimer cette ligne, elle n'est plus utile dans ...
DELBERT Bonjour , Nicolas , merci pour ta réponse , je suis en effet avec PRT 10.3 , et j'ai bidouil...
supertiti https://zupimages.net/viewer.php?id=20/50/2e3v.jpg
volanirina Bonjour Je souhaite l'avoir sur l'indicateur MT4 si possible s'il vous plait. car je l'ai fa...
Nicolas ProRealCode est un site dédié à l'utilisation de la plateforme de trading ProRealTime: https...
Pensera Bravo Nicolas ! Tu es en quelque sorte l’inventeur de Metascore…(oups je viens de lire jusqu...
Nicolas Dans un but d'investissement pure, et non spéculatif.
Enigma08 Hi Juanj. I am new to ProrealTime and I am trying to program the Proscreener. I want the Pro...
Nicolas Please open a topic in ProScreener forum with your query, I'll reply you there. Thank you.
manafull Thanks Nicolas, I post my question, along with the codes I am trying to convert from your in...
Nicolas I replied to your post here: https://www.prorealcode.com/topic/screener-for-rsi-and-linear-r...
Madrosat I Have not see a fully functioning automated strategy on your site?? Really have you???
juanj Hi Madrosat, why would it be on my site? I have developed it for my personal use and also to...
Madrosat ok I understood you are using prorealcode to hack the fish
imonix Have been trying this on demo since Monday before deciding whether to go live with it. Using...
Roberto Blázquez I'm sorry, I've tried it since November 2012 and it's bad results.
larouedegann It doesn't work
ggolfet it seems it's not working on prorealtime 11. I changed MACDSignal to MACDSign in order to av...
juanj Please note that the indicator is meant for PRT 10.3
woktrade Superbe !
Vonasi An updated version with a bug fix can be found here: https://www.prorealcode.com/topic/dis...
snucke hey Vonasi is it possible you can help me get this indicator to show how much a market mo...
Vinks_o_7 Vonasi you're a beast !!! ;-)))
pableitor Amazing indicator , you are pushing PRT to the limit! But looking at the scatter its not c...
Zigo
5 years ago
fifi743 Good morning, there is no mistake about the shorts c11=SMI[14,3,5](close)>-40
Zigo has to be c11=SMI[14,3,5] < -40 (indeed)
xar3 Thank you ;-) Zigo
Sapo Thank you very much. as always your indicators are extremely effective. kind regards Vonasi.
mitsu21 Hi, it looks nice. I'll try it tomorrow thank you for your share kind regards bruno
kj1988 Thank you Vonasi for sharing this indicator. Very useful as a comfirmation to the price act...
ggolfet Thanks Nicolas, I imported the itf file and solved the problem.
Hasardeur Hallo Nicolas, i tried to code the multi period heat map in combination with the Ehler’s ...
Nicolas Please open a new topic with the code in forum.
Marie-Eve Vergoz lorsque je copie les codes, le système me signale des erreurs à la ligne 12 et suivantes
Marie-Eve Vergoz sorry - tout est ok -
AlgoAlex
6 years ago
Marcot18 Alex ti sei superato
AlexF Esagerato!
jobswaps Sube como trazar lineas de tendencias a tantas barras
Frankiesc interesting indicator !! Thanks for sharing!!!
robertogozzi Thank you :)
JJ Tec Hola Las condiciones de entrada / salida vienen en el libro de J. Carter Mastering the trad...
Pixeldd Ich bekomme beim einfügen des Indikators einen Fehler angezeigt. Was bitte muss ich genau än...
HarryPro Hello, Firtly there appears to be a typo on line 6 "lenghkc=20" should this be "lengthkc=...
sir_i Bonjour à tous, Je suis nouveau sur le forum et sur la plateforme, pourriez-vous me dire com...
Nicolas Il faut simplement l'appliquer sur le prix.
sir_i Merci pour la réponse, je ne suis pas familier avec l'application de cette procédure simple,...
Nicolas Il s'agit d'un indicateur compatible avec PRT v11 uniquement. Pour éviter les erreurs de cop...
Sébi Intéressant. Merci pour ce partage Nicolas. L'indicateur ne considère pas les divergences ...
Nicolas ok, pour les divergences chacun voit midi à sa porte je dirai :) Vu qu'il n'y a pas de formu...
mcha merci bien et félicitations pour la réactivité avec laquelle il a été mis dans la bibliothèq...
soulintact Great indicator Nicolas, thanks!

Top