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.
量化交易科學園區 - 江加宏Length 14:05Quant Trading Science Park
Episodes
  1. Mon, Nov 28Custom SwingHighBar: Finding Perfect Peaks with Rising Left Side and Falling Right Side
  2. Sat, Dec 31Automatically Detecting an M-Top with XScript: Swing Highs, Neckline and Multiple Filter Conditions
  3. Thu, Mar 16Detecting Volume-Price Divergence with Code: Comparing Price and Volume at Two Swing Highs
  4. Wed, Mar 29Recreating the Day-Trading Three Lines and a Short Alert for Breakouts That Fall Back, Using XS
  5. Tue, Apr 11RSI Bearish Divergence Algorithm and Tolerant Sampling: Syncing Price Highs with Indicator Highs
  6. Thu, Apr 20Measuring Bullish Strength from a Single Candlestick
  7. Fri, Apr 28Manual Entry, Automatic Exit: Using XQ Semi-Automated Trading to Free Up Office Workers
  8. Sun, May 14Automated Trading Without Coding Skills: Split a Strategy into Four Building Blocks of Stock Selection, Entry, Exit and Position Control
  9. Mon, Jun 5Is a High-Volume Long Upper Shadow a Reversal or a Bullish Shakeout? A 3-Year Backtest
  10. Sat, Jun 17Picking Stocks at the Second-Wave Breakout: Big Prior Rally, Low-Volume Consolidation, Then a High-Volume Long Red Candle
  11. Wed, Sep 23Disposition stocks no longer pull back under the new rules: buy those that close strong on day 1 and sell on day 5
  12. Wed, Oct 7A Live Trading Strategy Under the New Disposition Stock Rules: Buy on Day 1, Sell on the Last Day, and Use Stop-Losses
  13. Wed, Oct 21Foreign Capital Rotates Into Defensive Stocks, and a Review of Live Disposition Stock Trades
  14. Wed, Oct 28SDT Shared Variables: Fixed Memory That Saves Resources and Lets Instruments in the Same Strategy Pass Data to Each Other
  15. Wed, Nov 4Disposition Stock Strategy Flips From Bull to Bear: Why It Is Paused When the Market Weakens
  16. Wed, Nov 18Auditing Online Course Strategies with AI: Out-of-Sample Backtesting and Building Your Own XQ Minute-Bar Database
  17. Wed, Nov 25Options Long Straddle Backtest: Trading It Every Day Loses Over the Long Run, With Chances to Profit Only in High-Volatility Periods
▶ Watch the original on YouTube
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

  1. 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 here
    Slide: Two Core Problems of a Divergence System and Defining the High
  2. 1: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 here
    Slide: Writing the RSI Divergence Code: Flow and Conditions
  3. 9: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

This page summarizes the video; it is not an official transcript. Original video: RSI Bearish Divergence Algorithm and Tolerant Sampling: Syncing Price Highs with Indicator Highs (量化交易科學園區 - 江加宏), copyright belongs to the original creator.