Detecting Volume-Price Divergence with Code: Comparing Price and Volume at Two Swing Highs

TL;DR: Volume-price divergence can be detected automatically in code: use CSwingHigh to find the two most recent swing highs, and when price is higher but volume is lower, flag it as divergence and mark it; the same structure works with RSI or KD values.

Key ideas

  • The definition of volume-price divergence is simple: the latest high has a higher price than the previous high but lower volume than the previous high, and both conditions must hold at the same time.
  • The program needs 6 variables that record the price, position and corresponding volume of the two highs, and every comparison is built on these values.
  • CSwingHigh is used to find the highs; if it is not available, SwingHigh can replace it, only with more noise. The bar with the highest price is not necessarily the bar with the largest volume, so volume takes the maximum of 3 bars: the bar before the high, the bar after it, and the high itself.
  • When no high is found the function returns -1, and using that directly to fetch volume causes an error, so the position must first be checked to be greater than 0; it also must be confirmed that the divergence just occurred and that the second high really exists, otherwise there is nothing to compare.
  • In XQ the marker can only appear after the fact, because it is placed only after 2 bars on each side of the high confirm it; for RSI and KD divergence, just replace price and volume with the corresponding indicator values and the rest of the logic stays the same.
量化交易科學園區 - 江加宏Length 13:22Quant 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:05Starting the implementation and creating a new indicator

    This section moves into the implementation stage and explains how to write volume-price divergence as code. The first step is to create a new indicator, which serves as the basis for defining variables and conditions later.

    ▶ Watch from here
    Slide: Starting the implementation and creating a new indicator
  2. 0:25Defining the high-point variables and the divergence condition

    First declare 6 variables: the price and position of the two highs, and the volume corresponding to each of the two highs. CSwingHigh looks back 40 bars, requiring 2 lower bars on each side of the high, plus an extra parameter 0.1 that sets the percentage condition between the bar to the left of the high and the high itself; if this function is not available, SwingHigh can replace it, only with more noise. Because the highest price is often not on the bar with the largest volume, volume uses MaxList to take the maximum of 3 bars: the bar before the high, the bar after it, and the high itself; if no high is found it returns -1, so the position must first be checked to be greater than 0. The divergence condition is: the divergence has just occurred, the second high exists, and price is higher but volume is lower, and once satisfied it is marked with Plot. Applied to a stock chart, you can see the spots where price made a new high while volume shrank get marked, and for RSI and KD you only need to substitute the corresponding values.

    ▶ Watch from here

This page summarizes the video; it is not an official transcript. Original video: Detecting Volume-Price Divergence with Code: Comparing Price and Volume at Two Swing Highs (量化交易科學園區 - 江加宏), copyright belongs to the original creator.