#### Home >> (Table of Contents) Studies and Indicators >> Technical Studies Reference >> Divergence Detector

# Technical Studies Reference

- Technical Studies Reference
- Common Study Inputs (Opens a new page)
- Using Studies (Opens a new page)

# Divergence Detector

This study calculates and displays a divergence in slope between the regression line of the Main Price Graph of a chart and that of a Study Subgraph (which corresponds to the **Study Subgraph Reference** Input) on the same chart.

A divergence occurs when there is a significant difference in slope. The conditions for a divergence are discussed below.

Let \(X\) and \(S\) be random variables denoting the **Input Data** and the data from the Study Subgraph, respectively, and let \(X_t\) and \(S_t\) be their respective values at Index \(t\).

Let the **Divergence Length**, **Divergence Threshold in Degrees**, **Opposite Slope Divergence Threshold in Degrees**, and **Value Per Point for Study Reference** be denoted as \(n\), \(\phi_D\), \(\phi_O\), and \(v_1\), respectively.

There are two other parameters that are used in the calculation but are not listed in the Inputs. These are the Tick Size \(s\) and the Value Increment Per Bar in Ticks \(v_2\). \(s\) can be set through **Chart >> Chart Settings >> Main Settings**, and \(v_2\) can be set through **Chart >> Chart Settings >> Chart Drawings**.

We begin by computing the Linear Regression statistics for both the Main Price Graph and the Study Subgraph, from which we take the following slopes.

\(\displaystyle{b_t\left(\frac{X}{s \cdot v_2},n\right)}\)\(\displaystyle{b_t\left(\frac{S}{v_1},n\right)}\) From these slopes, the Main Price Graph and Study Subgraph Angles of Inclination are computed (in degrees) respectively as follows.

\(\theta_t(X, s\cdot v_2, n) = \frac{180^\circ}{\pi}\cdot\tan^{-1}\left(b_t\left(\frac{X}{s \cdot v_2},n\right)\right)\displaystyle\)

\(\theta_t(S, v_1, n) = \frac{180^\circ}{\pi}\cdot\tan^{-1}\left(b_t\left(\frac{S}{v_1},n\right)\right)\displaystyle\)

These Angles are in the interval \((-90^{\circ},90^{\circ})\).

A Divergence exists if any of the following conditions is satisfied.

- \(\left|\theta_t(X, s\cdot v_2, n) - \theta_t(S, v_1, n)\right| \geq \phi_D\)
- \(\theta_t(X, s\cdot v_2, n) > 0\) and \(\theta_t(S, v_1, n) < 0\) and \(\left|\theta_t(X, s\cdot v_2, n) - \theta_t(S, v_1, n)\right| \geq \phi_O\)
- \(\theta_t(X, s\cdot v_2, n) < 0\) and \(\theta_t(S, v_1, n) > 0\) and \(\left|\theta_t(X, s\cdot v_2, n) - \theta_t(S, v_1, n)\right| \geq \phi_O\)

If a Divergence exists at Index \(t\), then the Divergence is either Positive or Negative. Positive and Negative Divergences are denoted as \(PD_t(X,S,n,s,v_1,v_2,\phi_D,\phi_O)\) and \(ND_t(X,S,n,s,v_1,v_2,\phi_D,\phi_O)\), respectively. Since this notation is cumbersome, we will omit the function parameters going forward. These Divergences are computed as follows.

\(\displaystyle{PD_t = \left\{ \begin{matrix} L_t - s & \theta_t(S, v_1, n) > \theta_t(X, s\cdot v_2, n) \\ 0 & \theta_t(S, v_1, n) \leq \theta_t(X, s\cdot v_2, n) \end{matrix}\right .}\)\(\displaystyle{ND_t = \left\{ \begin{matrix} H_t + s & \theta_t(S, v_1, n) \leq \theta_t(X, s\cdot v_2, n) \\ 0 & \theta_t(S, v_1, n) > \theta_t(X, s\cdot v_2, n) \end{matrix}\right .}\)

In the above formulas, \(H_t\) and \(L_t\) denote the High Price and Low Price, respectively, at Index \(t\).

Positive Divergences are indicated with an Up Arrow below a chart bar where they occur. Negative Divergences are indicated with a Down Arrow above the bar where they occur. This behavior as documented here is effective with version 1576 and higher.

The Tool Values Window displays the calculated angles by the **Divergence Detector** study and those angles can be viewed for any chart bar by using the Chart Values Tool. This is very helpful in order to determine the proper **Divergence Threshold in Degrees** Input settings.

The **Subgraph >> Draw Style** setting for the **Positive Divergence** and **Negative Divergence** Subgraphs in the Study Settings window can be changed from the default arrows to any other Draw Style that you require. Refer to Draw Style.

When there is a positive divergence, this generally would indicate that based upon the Study Subgraph being referenced by the Divergence Detector study that the main price graph may change direction from its downward trend if there has been a downward trend. When there is a negative divergence, this generally would indicate that based upon the Study Subgraph being referenced by the Divergence Detector study that the main price graph may change direction from its upward trend if there has been a upward trend.

#### Inputs

- Input Data
**Study Subgraph Reference**: This Input is for specifying the Study Subgraph to which a corresponding Main Price Graph is to be compared for the**Divergence Detector**Study.**Divergence Length**: This Input is for setting the number of chart bars for which linear regression statistics are to be calculated for both the Main Price Graph and the selected Study Subgraph.**Divergence Threshold in Degrees**: This Input is for setting the number of degrees by which the angles of inclination of the regression lines for a Main Price Graph and a Study Subgraph may differ before a divergence is detected. The absolute value of the angle difference must be equal to or exceed**Divergence Threshold in Degrees**for a divergence to exist.**Opposite Slope Divergence Threshold in Degrees**: This Input is for setting the number of degrees by which the angles of inclination of the regression lines for a Main Price Graph and a Study Subgraph may differ before a divergence is detected. This threshold is used only when the lines have angles of inclination that are of opposite signs. The absolute value of the angle difference must be equal to or exceed**Opposite Slope Divergence Threshold in Degrees**for a divergence to exist.**Value Per Point for Study Reference**: Before the regression line is calculated for the study Subgraph referenced through the**Study Subgraph Reference**Input, the values of the study Subgraph are divided by this Input setting. For more information about this**Value Per Point**Input, refer to the explanation in the Drawing a Line with a Specific Angle or Slope section on the Drawing Tools page.

#### Spreadsheet

The spreadsheet below contains the formulas for this study in Spreadsheet format. Save this Spreadsheet to the Data Files Folder.

Open it through **File >> Open Spreadsheet**.

*Last modified Monday, 26th September, 2022.