# Directional Movement Index

## Technical Analysis Indicator: dx ## Function Prototype

/* Directional Movement Index */
/* Type: indicator */
/* Input arrays: 3    Options: 1    Output arrays: 1 */
/* Inputs: high, low, close */
/* Options: period */
/* Outputs: dx */
int ti_dx_start(TI_REAL const *options);
int ti_dx(int size,
TI_REAL const *const *inputs,
TI_REAL const *options,
TI_REAL *const *outputs);

## Description

This documentation is still a work in progress. It has omissions, and it probably has errors too. If you see any issues, or have any general feedback, please get in touch.

The Directional Movement Index can help determine trend strength.

It takes one option, the period n.

Calculation is as follows:

$$\text{tr}_{t} = maximum(high_{t} - low_{t}, |high_{t} - close_{t-1}|, |low_{t} - close_{t-1}|)$$
$$\text{atr}_{t} = \frac{n-1}{n} \text{atr}_{t-1} + \text{tr}_{t}$$
$$up_{t} = \begin{cases} 0 & high_{t} - high_{t-1} \lt 0 \vee high_{t} - high_{t-1} \lt low_{t-1} - low_{t} \\ high_{t} - high_{t-1} & \text{ else} \end{cases}$$
$$down_{t} = \begin{cases} 0 & low_{t-1} - low_{t} \lt 0 \vee low_{t-1} - low_{t} \lt high_{t} - high_{t-1} \\ low_{t-1} - low_{t} & \text{ else} \end{cases}$$
$$\text{dm_up}_{t} = \frac{n-1}{n} \text{dm_up}_{t-1} + up_{t}$$
$$\text{dm_down}_{t} = \frac{n-1}{n} \text{dm_down}_{t-1} + down_{t}$$
$$\text{di_up}_{t} = \frac{\text{dm_up}_{t}}{\text{atr}_{t}}$$
$$\text{di_down}_{t} = \frac{\text{dm_down}_{t}}{\text{atr}_{t}}$$
$$\text{dm_diff}_{t} = | \text{di_up}_{t} - \text{di_down}_{t} |$$
$$\text{dm_sum}_{t} = \text{di_up}_{t} + \text{di_down}_{t}$$
$$\text{dx}_{t} = 100 \frac{\text{dm_diff}_{t}}{\text{dm_sum}_{t}}$$

## Example Usage

### Calling From C

/* Example usage of Directional Movement Index */
/* Assuming that 'high', 'low', and 'close' are pre-loaded arrays of size 'in_size'. */
TI_REAL *inputs[] = {high, low, close};
TI_REAL options[] = {5}; /* period */
TI_REAL *outputs; /* dx */

/* Determine how large the output size is for our options. */
const int out_size = in_size - ti_dx_start(options);

/* Allocate memory for output. */
outputs = malloc(sizeof(TI_REAL) * out_size); assert(outputs != 0); /* dx */

/* Run the actual calculation. */
const int ret = ti_dx(in_size, inputs, options, outputs);
assert(ret == TI_OKAY);


### Calling From Lua (with Tulip Chart bindings)

-- Example usage of Directional Movement Index
dx = ti.dx(high, low, close, 5)


## Example Calculation

period = 5

datehighlowclosedx
2005-11-0182.1581.2981.59
2005-11-0281.8980.6481.06
2005-11-0383.0381.3182.87
2005-11-0483.3082.6583.00
2005-11-0783.8583.0783.6150.19
2005-11-0883.9083.1183.1551.36
2005-11-0983.3382.4982.8411.09
2005-11-1084.3082.3083.9941.52
2005-11-1184.8484.1584.5552.77
2005-11-1485.0084.1184.3655.91
2005-11-1585.9084.0385.5369.96
2005-11-1686.5885.3986.5476.91
2005-11-1786.9885.7686.8980.26
2005-11-1888.0087.1787.7786.51
2005-11-2187.8787.0187.2975.61

## Other Indicators

Previous indicator: Detrended Price Oscillator

Next indicator: Exponential Decay

Random indicator: Momentum