RSI Bearish Divergence Algorithm and Tolerant Sampling: Syncing Price Highs with Indicator Highs
TL;DR: The key to detecting divergence is to define the price high first, then map the indicator to that high using either a fixed position or tolerant sampling before and after it; once these two problems are solved, RSI, KD, MACD and other divergences can all be coded the same way.
Key ideas
- A divergence system has two problems to solve first: how to define the price high, and whether the indicator truly corresponds to that high; with price and volume as an example, the highest price is not necessarily the point of highest volume.
- There are two simple ways to map the indicator high: take the indicator value at the exact position of the price high, or take the indicator high within 3 or 5 candlesticks before and after the price high, which leaves some flexibility.
- The RSI divergence logic uses SwingHigh and SwingHighBar to find the two most recent reversal highs and their positions; the condition is that the price makes a new high while the RSI at that point is lower than the RSI at the previous high, and the check runs only if both highs are found.
- A reversal point is marked only after the candlesticks on its left and right confirm it, so the green dots and yellow bars appear late; shift left by the same number of bars as the sampling count to find the true reversal high.
- Sampling 1 bar before and after is looser and 2 bars is stricter, each with pros and cons, so adjust to the candlestick pattern; in the example, 1 bar counts as divergence, while with 2 bars a higher RSI is found and it no longer counts as divergence.
Ask AI about this episode
Copy the prompt, paste it into an AI service, and add the question. The AI reads the notes on this site and links to the page and the video timestamps.
Or open directly:ChatGPTClaudeGrokPerplexity
Section summaries
0:05Two Core Problems of a Divergence System and Defining the High
A divergence system must first define the price high and then have the indicator correspond to that high. There are two methods: take the indicator value at the exact position of the price high, or look for the indicator high within three or five candlesticks before and after the high. This works for any indicator.
▶ Watch from here1:55Writing the RSI Divergence Code: Flow and Conditions
First set two parameters, the RSI length (commonly 6 or 12) and the lookback range, then use SwingHigh and SwingHighBar to find the price and position of the two most recent reversal highs. The divergence condition is that the latest high is higher than the previous high, yet the maximum RSI sampled near it (previous bar, current bar, next bar) is lower than the maximum RSI near the previous high; if no second high is found, the function returns -1 and that case must be excluded.
▶ Watch from here9:55Candlestick Chart Test and Adjusting the Bar Count
Applying the RSI divergence to a candlestick chart: green dots mark reversal points and appear only after confirmation, so the actual high is 2 bars to the left; the yellow bars show where divergence occurs. Looking 1 bar before and after is a looser condition, while 2 bars is stricter, and you can adjust to the candlestick pattern.
▶ Watch from here

