Indicators/probuilder · probacktest · proorder · proscreener

DivergenceRSI

DivergenceRSI in ProBuilder detects bullish and bearish divergences between price and RSI, returning +1, -1, or 0. Syntax, parameters, examples, and gotchas.

Syntax

probuilder
DivergenceRSI[RSIperiod, LowRSIthreshold, HighRSIthreshold, Bars](price)

Parameters

NameTypeDefaultDescription
RSIperiodinteger5Period used for the underlying RSI calculation.
LowRSIthresholdinteger30Lower RSI level treated as the oversold zone when searching for bullish divergences.
HighRSIthresholdinteger70Upper RSI level treated as the overbought zone when searching for bearish divergences.
Barsinteger20Number of past bars scanned for divergence patterns.
priceprice sourceclosePrice stream fed into the RSI, typically close.

Formula

code
IF price makes a lower low AND RSI[RSIperiod] makes a higher low
   within the last Bars bars (near LowRSIthreshold)  -> +1
IF price makes a higher high AND RSI[RSIperiod] makes a lower high
   within the last Bars bars (near HighRSIthreshold) -> -1
OTHERWISE                                            ->  0

The function compares price extremes with the corresponding RSI extremes inside the lookback window and reports disagreement between the two series.

How it works

A divergence occurs when price and a momentum oscillator move in opposite directions. DivergenceRSI automates the visual pattern most traders draw by hand. On every bar it computes an RSI with period RSIperiod on the chosen price, then inspects the last Bars bars.

A bullish divergence is flagged when price prints a new low while the RSI turns up instead of following, which some traders read as fading selling pressure. A bearish divergence is flagged when price prints a new high while the RSI rolls over, suggesting the advance is losing momentum. The thresholds restrict detection to meaningful extremes, so a bullish signal is anchored near the oversold zone and a bearish signal near the overbought zone.

The output is a three-state series, +1, -1, or 0, which makes it convenient to plot as a histogram or to use directly as a boolean-style condition inside strategies and screeners. Because detection depends on the RSI settings, results only match a chart RSI when both use the same period and price source.

Examples

Example 1, Divergence histogram (Indicator)

probuilder
// Detect divergences with a 5-period RSI, 30/70 thresholds,
// scanning the last 20 bars of closing prices
i = DivergenceRSI[5,30,70,20](close)
return i
style(histogram)

Plots +1 bars when a bullish divergence is found and -1 bars for bearish divergences, rendered as a histogram under the price chart.

Example 2, Screening for fresh bullish divergences (ProScreener)

probuilder
// Flag instruments where a bullish divergence appeared on the last bar
div = DivergenceRSI[14,30,70,25](close)
myRSI = RSI[14](close)
SCREENER[div = 1](myRSI AS "RSI 14")

Returns only instruments that just printed a bullish price/RSI divergence, and displays the current RSI value as a sortable column.

Example 3, Divergence entry with trend filter (ProOrder)

probuilder
// Buy bullish divergences that occur above the long-term average
trend = Average[200](close)
div   = DivergenceRSI[5,30,70,20](close)

IF NOT LongOnMarket AND div = 1 AND close > trend THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

IF LongOnMarket AND div = -1 THEN
  SELL AT MARKET
ENDIF

Enters long when a bullish divergence fires in the direction of the 200-period trend, and exits when a bearish divergence appears.

Interpretation

ValueMeaning
+1Bullish divergence, price made a new low while RSI rose. Sometimes precedes an upward reversal.
-1Bearish divergence, price made a new high while RSI fell. Sometimes precedes a downward reversal.
0No divergence detected in the lookback window.

Divergence is a condition, not a timing signal. Price can keep trending long after a divergence prints, so most systems combine it with a trend filter, a candlestick trigger, or a stop based on recent structure.

Common errors and gotchas

  • Mismatched RSI settings. The divergences reported by DivergenceRSI[5,...] will not line up with a 14-period RSI drawn on the chart. Keep RSIperiod and the price source identical to any visual RSI used for confirmation.
  • Treating the signal as a reversal guarantee. In strong trends, divergences can repeat several times before any reversal occurs. Counter-trend entries on the raw +1/-1 output alone tend to accumulate losses.
  • Lookback window too short. A small Bars value misses divergences that develop slowly across swing points. Values around 20 to 30 bars cover most standard swing structures.
  • Expecting a continuous oscillator. The function returns only +1, -1, or 0. Comparisons such as div > 0.5 work, but arithmetic like smoothing the output rarely produces anything meaningful.
  • DivergenceCCI, equivalent divergence detector built on the CCI oscillator.
  • DivergenceMACD, equivalent divergence detector built on MACD.
  • RSI, the underlying Relative Strength Index oscillator.
  • MACD, moving average convergence divergence oscillator.
  • CCI, commodity channel index oscillator.
  • Stochastic, bounded oscillator often watched for the same divergence patterns.
  • Momentum, raw momentum measure of price change.
  • ROC, rate of change, a percentage momentum measure.