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.
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: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 here0: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
