r"""Calculate the Average Directional Index (ADX). Parameters ---------- f : pandas.DataFrame Dataframe with all columns required for calculation. If you are applying ADX through ``vapply``, then these columns are calculated automatically. p : int The
(f, p = 14)
| 78 | # |
| 79 | |
| 80 | def adx(f, p = 14): |
| 81 | r"""Calculate the Average Directional Index (ADX). |
| 82 | |
| 83 | Parameters |
| 84 | ---------- |
| 85 | f : pandas.DataFrame |
| 86 | Dataframe with all columns required for calculation. If you |
| 87 | are applying ADX through ``vapply``, then these columns are |
| 88 | calculated automatically. |
| 89 | p : int |
| 90 | The period over which to calculate the ADX. |
| 91 | |
| 92 | Returns |
| 93 | ------- |
| 94 | new_column : pandas.Series (float) |
| 95 | The array containing the new feature. |
| 96 | |
| 97 | References |
| 98 | ---------- |
| 99 | The Average Directional Movement Index (ADX) was invented by J. Welles |
| 100 | Wilder in 1978 [WIKI_ADX]_. Its value reflects the strength of trend in any |
| 101 | given instrument. |
| 102 | |
| 103 | .. [WIKI_ADX] https://en.wikipedia.org/wiki/Average_directional_movement_index |
| 104 | |
| 105 | """ |
| 106 | c1 = 'diplus' |
| 107 | vexec(f, c1) |
| 108 | c2 = 'diminus' |
| 109 | vexec(f, c2) |
| 110 | # calculations |
| 111 | dip = f[c1] |
| 112 | dim = f[c2] |
| 113 | didiff = abs(dip - dim) |
| 114 | disum = dip + dim |
| 115 | new_column = 100 * didiff.ewm(span=p).mean() / disum |
| 116 | return new_column |
| 117 | |
| 118 | |
| 119 | # |