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
GraHal Yes sorry, I set up a link to a screen shot on my google drive and then I got locked into th...
gabri Here's the thread https://www.prorealcode.com/topic/multiframe-rsi-of-rsi/
Bernard13 Bonjour Nicolas, Pourriez-vous m'indiquer si cet indicateur fonctionne avec la V11 ? Le di...
nwesterhuijs Thanks, only saw it just now.
juanj For the latest version and discussions see the Ichimoku thread here: https://www.prorealcod...
Louwrens Hi Juanj. Thanks for this. I am tying it as we speak. It does not trade that often, which is...
ALE
8 years ago
CSR strategy DAX 1 D
CSR strategy DAX 1 D
17
Strategies
Jesper I tried it on dax 1D and I did not get any trades. Shifted to 10H and it started working. Wo...
rgrgrgr I have the same problem
avatar
crazytrader Is this working?
Maz
8 years ago
Francesco78 very nice, thanks!
Wilko Interesting! Thanks for sharing!
BjornH Extremely nice, thanks!
finplus Thanks for the job. Which variables do you suggest for timeframe 1 hour? 
Maz Depends massively on your market and the volatility. I suggest using the variable optimizer ...
1Randy This a great momentum filter! I would like to see volume momentum incorporated into the indi...
Nicolas All conditions under parenthesis for the c1 to c4 conditions should be inverted. 
tomus Can you give an example of the overbought codes please? Thanks.
ams123 Frank Merci Nicholas -:)
Djo Not working on V11. The RSI doesn't appear on the chart.
Mika83 Bonjour, J'ai des soucis de lecture de syntaxe avec la variable "drawsegment" sur la versio...
seb234 Salut Mika, j'ai la version V11.1, la formule fonctionne. Mais il est préférable, dixit Nico...
bolsatonimora2 Hi, nice indicator, it´s possible to include a middle line with 50 value? ty!
gabri Hi, EMA26 approximate almost exactly the 50% line
Alain Wilder MA is exactly the 50% line
owes29 Hi is there anyway to develop this for the pro scanner on daily or hourly scans. so it woul...
Nicolas Of course, please add a query in the proscreener forum.
Bruno Carnazzi C'est dommage, cette histoire d'énergie fractale bousille complètement la précision de l'ind...
gregus bonjour a tous quelqu un pourait il recodé en prt il sagit  du dynamique zone ma, je n arri...
Nicolas Merci de faire une requête spécifique sur le forum. 
Nicolas
8 years ago
StepRSI
StepRSI
5
Indicators
Nicolas Thanks for this modification, I still do not have tested it, where did you get this idea to ...
rfsteve Trial and error from study of indicators call it coding mad science was trying to find an in...
Maxime Baudin Nice! Thanks :)
Nicolas
8 years ago
GraHal Forked code I mention above is here ... GraHal wrote: So below is the PRC Stochastic RSI v...
GraHal Try again (quite limited what you can do as Comments in the Library) https://www.prorealco...
AutoFlanders Thanks GraHal, that's what i was looking for
Dimi.A Awesome mate.
mora87 Hi David and Nicola, I'd like to share idea with you guys which is related to David's Idea. ...
Nicolas Please ask for custom coding in forums instead.
jeanphi0034 Hello, I would like to use the QQE indicator which is based on smooth RSI as far as I unders...
ilstefano Bonjour Nicolas, te serait il possible de rajouter la fast ATR manquante dans le code? Merci
Nicolas Désolé je ne comprends pas la question ?
rpreviteri Hi Nicolas, thanks a  loto for sharing your knowliedge, Wasn't the original momentum pinball...
air Thank you!  
elanoa Buonasera sig. Nicolas......sarebbe possibile far in modo che quando l'indicatore raggiunga ...
zilliq The reasons why I think it's time consuming and we loose time to try to do backests and Auto...
filiprb Hello Zilliq, You don't need a system to produce a walk forward test. You can easily create...
Philip Raphael It is incredible! Thanks for sharing, Doctrading!
s00071609 Hi, could you please suggest, what this codes gives, lowest[b](rsi[a]) -- just trying to us...
Nicolas lowest[b](rsi[a]) returns the lowest values of the RSI of "a" periods, over the last "b" per...
s00071609 Hi, what would be the code to get the price for last bullish DTOSC cross over. I am looking ...

Top