Detecting RSI Divergence with Price and Momentum Swings
Summary
The document defines bullish RSI divergence as price making a lower low while RSI makes a higher low, and bearish divergence as price making a higher high while RSI makes a lower high. These patterns indicate that RSI is not confirming the latest price extreme, which may signal strengthening or weakening momentum. The central programming task is to identify comparable swing points in price and RSI, then compare their direction.
Suggested approaches include finding relative highs and lows with a peak-detection method, calculating RSI at those points, and checking pairs of extrema. Another response suggests detecting price higher highs and lower lows first, potentially with a ZigZag indicator, then inspecting RSI. A code example instead plots the difference between fast and slow RSI values and marks its sign. These are implementation suggestions, not validated trading rules: the excerpt provides no backtest or evidence of predictive performance, and the alternative signal definition is not the same as comparing price and RSI swing points.
Key ideas
- Bullish divergence pairs a lower price low with a higher RSI low.
- Bearish divergence pairs a higher price high with a lower RSI high.
- A program can detect swing highs and lows, sample RSI at those points, and compare paired changes.
- A ZigZag indicator is one suggested way to identify price extremes.
- The document supplies coding approaches but no test of their trading performance.
Tags
Full text
# Programmatically detect RSI divergence # Programmatically detect RSI divergence How can I programmatically detect bullish and bearish RSI divergences? A bullish divergence occurs when the underlying security makes a lower low and RSI forms a higher low. RSI does not confirm the lower low and this shows strengthening momentum. A bearish divergence forms when the security records a higher high and RSI forms a lower high. RSI does not confirm the new high and this shows weakening momentum. ## Answer by Casey Jones (score 8) https://quant.stackexchange.com/a/41335 I was searching for answers to the same question and came across your question. After some thought and research, here is the plan I have developed. I will be working in Python. - Calculate relative maxima and minima with SciPy. - Calculate RSI at those points using lib-ta. - For each pair of lows and highs, compare the change in price with the difference in RSI. I'm completely new to technical analysis, so in case I have made any oversights, feedback would be greatly appreciated. I wanted to ask about your programming language and data format, but don't have enough reputation to comment. ## Answer by valentasm (score 3) https://quant.stackexchange.com/a/50973 I want to implement exactly same principle in C# and realized that i should start opposite. Start from finding Higher High or Lower Low and then checking RSI. After finding HH or LL checking RSI is trivial task. To find HH or LL you could use ZigZag indicator. At investopedia you could find how to calculate it in more details. Also you could check Python version of it in quantconnect forum. Also, you could find more versions on internet. ## Answer by alper (score 0) https://quant.stackexchange.com/a/60280 Pine-script that could be applied on tradingview, Shizaru 's reference: ``` // => normal RSI: check double or triple top/bottom on a chart meanwhile RSI is descending/ascending (check the example on chart) // => signal line: when RSI Divergence cross zero line from bottom to top you get a buy signal (the line become green), // vice versa when the RSI Divergence cross zero line in the opposite way you get a sell signal (the line become red) study(title="RSI Divergence", shorttitle="RSI Divergence") src_fast = close, len_fast = input(5, minval=1, title="Length Fast RSI") src_slow = close, len_slow = input(14,minval=1, title="Length Slow RSI") up_fast = rma(max(change(src_fast), 0), len_fast) down_fast = rma(-min(change(src_fast), 0), len_fast) rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast)) up_slow = rma(max(change(src_slow), 0), len_slow) down_slow = rma(-min(change(src_slow), 0), len_slow) rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow)) //plotfast = plot(rsi_fast, color=blue) //plotslow = plot(rsi_slow, color=orange) divergence = rsi_fast - rsi_slow plotdiv = plot(divergence, color = divergence > 0 ? lime:red, linewidth = 2) //band1 = hline(70,color=green) //band0 = hline(30,color=red) band = hline(0) divlong = divergence >=0 divshort = divergence <=0 alertcondition(divlong, title='Div Long', message='Div Long') alertcondition(divshort, title='Div Short', message='Div Short') data1 = divlong data2 = divshort plotshape(data1, style=shape.triangleup,location=location.bottom, color=green , title="DivUp") plotshape(data2, style=shape.triangledown, location=location.top, color=red,title="DivDown") ```
Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.