In technical analysis, a divergence occurs when two values that normally move in the same direction fail to do so. An example is when prices are making higher highs and the Relative Strength Index (RSI) makes a lower high. Other indicators can be used with similar results. When these divergences occur, the prices usually change direction and follow the indicator. As such, a divergence is usually a good predictor of imminent price action.
The MetaStock formula team frequently receives requests for formula to identify divergences. However, this is a visual pattern and a formula must base all signals on mathematical conditions. Therefore, to create a MetaStock formula for a divergence, we must first precisely define what a divergence is.
Referring to the original definition I first presented, a divergence has two parts:
• Price is making a higher high
• indicator is making a lower high
A divergence then requires two comparisons. A "high" in the prices is greater than a previous "high" and a "high" of the chosen indicator is less than a previous "high". The highs referred to are not of a specific bar but peaks in normal up and down movement of the chart. Now we have the dilemma, what is a peak? On this definition rests the entirety of the formula. Consider the chart below:
Points 1 and 2 show places where the prices reached a top. However, are these points one peak or two separate peaks with a low between them? If they are one peak, which line points to the peak? The answer to that questions is personal opinion and will be different for different traders. For example, when a 5 % Zig Zag of the close is used, the peaks and troughs are marked this way:
If a 1% zig zag is used instead, the peaks and troughs change to this:
If the chart is focused just on the March / April region, we can observe a second issue. The chart below added a 1% zig zag of the High (the blue line).
While the peak on 7 March 2014 is the same with both lines, the other peaks are not. While this is just the difference of a few bars, it can affect if a divergence is found.
Once the peaks of the prices are chosen, the peaks of the indicator must be identified. This can be done two ways:
• apply a calculation to the indicator similar to the one used on the prices
• take the value of the indicator at the time of the prices peak/trough
The chart below has added a 14-period RSI with a 1% zig zag.
Notice how much more volatile the RSI zig zag is than the prices. If we were to just look back at the two most recent price peaks and indicator peaks, the points on the chart would not be aligned. In this case, a zig zag of the indicator is not the best choice.
An alternative is to look at the value of the indicator at the time of the price peaks. Consider the chart below:
The peaks of the zig zag of the close are marked with red lines. the peaks of the zig zag of the highs are marked with blue lines. I have drawn short trend lines showing the change in the RSI value between the price peaks.
Both of the sets of peaks show an increase in price from one peak to the next. If we were using the Highs, a formula would say there was a divergence. However the Close actually co-insides with the peaks in the indicator but does not see a divergence as the RSI is also increasing. Which is more accurate?
Perhaps a better solution would be to revisit the zigzag on the RSI. Instead of using the RSI itself, let's smooth it first with a 5 period simple moving average:
Now when a 1% zig zag is applied to the moving average, it corresponds very closely to the zig zag of the highs. When the peaks are compared to the peaks of the highs, both are increasing so no divergence is found:
Would this be a good way to look for divergences? That is a matter of opinion. I like the zig zag of the highs because it finds the visual tops of the prices. By smoothing indicator I could make a zig zag work on the value of the indicator peaks. I prefer that to take the value of the indicator at the time of the price peak. Indicator peaks are seldom at the same time as the price peaks and a divergence is suppose to compare tops of indicators to the tops of the prices.
Here is a formula based on above:
plot:= H;The plot to use for the prices, the indicator to search for divergences against, and the zig zag percentage are all assigned to variables. You can change those as desired. Through experimentation, you may find a better set of values for identifying divergences.
Ind:= Mov( RSI(14), 5, S);
sig:= Peak(1, Ind, zp) < Peak(2, Ind, zp) AND
Peak(1, plot, zp) > Peak(2, plot, zp);
Sig=1 AND Ref(sig=0, -1)