Divergence Between Two DataSeries (Detect, Plot)

Modified on 2013/05/11 10:38 by Eugene — Categorized as: Community Components

Syntax

SeriesHelper methods:
public static DataSeries PlotPeakDivergence(this WealthScript obj, int proximity, ChartPane cp1, DataSeries ds1, double rev1, ChartPane cp2, DataSeries ds2, double rev2)
public static DataSeries PlotTroughDivergence(this WealthScript obj, int proximity, ChartPane cp1, DataSeries ds1, double rev1, ChartPane cp2, DataSeries ds2, double rev2)

public DataSeries PlotPeakDivergence(int proximity, ChartPane cp1, DataSeries ds1, double rev1, ChartPane cp2, DataSeries ds2, double rev2) public DataSeries PlotTroughDivergence(int proximity, ChartPane cp1, DataSeries ds1, double rev1, ChartPane cp2, DataSeries ds2, double rev2)

Indicators used and returned by the method above:

public PeakDivergence(int proximity, DataSeries ds, double pctRev1, DataSeries ds2, double pctRev2, string description)
public TroughDivergence(int proximity, DataSeries ds, double pctRev1, DataSeries ds2, double pctRev2, string description)

Parameter Description

proximityThe number of bars of separation allowed between the peaks (troughs) of price and the oscillator. Recommend 6.
cp1The ChartPane in which the first DataSeries, ds1, is plotted. Generally this would be the PricePane.
ds1A Price DataSeries, such as High, Low, or Close. This could be any DataSeries, however.
rev1Percentage reversal required to detect a peak (trough) in ds1. For Daily prices, 5 or greater is recommended.
cp2The ChartPane in which the second DataSeries, ds2, is plotted.
ds2An indicator DataSeries (e.g. momentum oscillator like RSI). This could be any DataSeries, however.
rev2Percentage reversal required to detect a peak (trough) in ds2. Generally, use a larger percentage for momentum oscillators, 15 or more.

Description

PlotPeakDivergence (PlotTroughDivergence) return a DataSeries that indicates divergence in the peaks (troughs) between two specified DataSeries - generally between Price and a momentum oscillator like RSI - and shows the divergence by drawing lines above (below) the peaks (troughs).

The DataSeries are created by two public indicator classes called PeakDivergence and TroughDivergence, also contained in the Community.Components module. The values of the indicator DataSeries are zero except for the bars on which divergence is detected. Divergence is indicated by +1 (positive divergence, indicating that the trend is likely to continue) or -1 (negative divergence, indicating that a trend reversal is favorable). If you do not wish to plot the diverging lines above/below the peaks/troughs, you can call the indicators directly instead of using the DivergencePlotter routines.

Note that divergence is detected for the 2 most-recent peaks in the ds1 (price) series. The logic is designed to find peaks/troughs in ds2 (oscillator) that are within proximity bars of the peaks/troughs of ds1. For this reason, it may occur that the diverging lines are drawn through other oscillator peaks/troughs since these are ignored.

Interpretation

The interpretation for positive/negative divergence is based on the article "Trading Divergences" by Sylvain Vervoot in the Feb 2008 issue of TASC magazine.

\ = lower peaks (troughs)
/ = higher peaks (troughs)


Peak Divergence
Price = \ Osc = / >> continue downtrend (+1)
Price = / Osc = \ >> trend reversal (-1)

Trough Divergence
Price = \ Osc = / >> trend reversal (-1)
Price = / Osc = \ >> continue uptrend (+1)


Example

The code example illustrates peak (trough) detection between High (Low) and RSI.

Example using C# extension methods:


/* Show Peak (Trough) divergence between High (Low) and RSI */
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies { public class DivergencePlotterExample : WealthScript { private StrategyParameter period; public DivergencePlotterExample() { period = CreateParameter("RSI Period",14,2,200,20); }

protected override void Execute() { ChartPane rsiPane = CreatePane(40,true,true); ChartPane divPane = CreatePane(40,true,true);

DataSeries rsi = RSI.Series(Close,period.ValueInt); PlotSeriesOscillator(rsiPane,rsi,70,30,Color.FromArgb(63,0,0,255),Color.FromArgb(63,255,0,0),Color.FromArgb(255,0,0,128),LineStyle.Solid,2); DataSeries pd = this.PlotPeakDivergence(6, PricePane, High, 10d, rsiPane, rsi, 15d); PlotSeries(divPane, pd, Color.Blue, LineStyle.Solid, 2); DataSeries td = this.PlotTroughDivergence(6, PricePane, Low, 10d, rsiPane, rsi, 15d); PlotSeries(divPane, td, Color.Red, LineStyle.Solid, 2); } } }

Legacy syntax example:

/* Show Peak (Trough) divergence between High (Low) and RSI */
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Components; /*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/

namespace WealthLab.Strategies { public class DivergencePlotterExample : WealthScript { private StrategyParameter period; public DivergencePlotterExample() { period = CreateParameter("RSI Period",14,2,200,20); }

protected override void Execute() { ChartPane rsiPane = CreatePane(40,true,true); ChartPane divPane = CreatePane(40,true,true);

DataSeries rsi = RSI.Series(Close,period.ValueInt); PlotSeriesOscillator(rsiPane,rsi,70,30,Color.FromArgb(63,0,0,255),Color.FromArgb(63,255,0,0),Color.FromArgb(255,0,0,128),LineStyle.Solid,2); SeriesHelper sh = new SeriesHelper(this); DataSeries pd = sh.PlotPeakDivergence(6, PricePane, High, 10d, rsiPane, rsi, 15d); PlotSeries(divPane, pd, Color.Blue, LineStyle.Solid, 2); DataSeries td = sh.PlotTroughDivergence(6, PricePane, Low, 10d, rsiPane, rsi, 15d); PlotSeries(divPane, td, Color.Red, LineStyle.Solid, 2); } } }