Investment Studio > Expressions > Functions > DSP > MAKE_EMA

float array[1][4] make_ema(integer days)

Returns the parameters needed to turn the lopass function into a standard Exponential Moving Average (EMA) of length days, as defined by technical analysts.

In mathematical terms, the EMA employed in the technical analysis of financial markets is a first order IIR (Infinite Impulse Response) filter implementing the difference equation

y[n] = b0 * x[n] + (1 - b0)* y[n - 1]

where x[n] is the input sequence, y[n] is the filter's output and the value of b0 is given by the formula

b0 = 2 / (days + 1)

When talking of a "21 day EMA", a technical analyst means the above IIR with b0 = 2 / (21 + 1) = 0.090909..., or roughly 9%. The "21 day EMA" is therefore equivalently referred to as the "9% EMA".

Examples

To explore the relation between the number of days and the EMA's behaviour, we can exploit the fact that the frequency response of any linear, time-invariant filter is just the Fourier transform of the filter's impulse response (the filter's output when the input sequence is a unit impulse, i.e. a single 1 surrounded by zeros, effectively all the way both to positive and to negative infinity).

This means that we can visualize the difference in frequency response between EMAs of different lengths by passing a unit impulse through each one and computing the Fourier transforms of their output.

With the definitions

_impulse = index(m1(1024), 0, 1)

_5_days = fftp(lopass(make_ema(5), 1, array(_impulse)))

_10_days = fftp(lopass(make_ema(10), 1, array(_impulse)))

_20_days = fftp(lopass(make_ema(20), 1, array(_impulse)))

_50_days = fftp(lopass(make_ema(50), 1, array(_impulse)))

the graph sources

=1024 * index(array(_5_days), x + 1, 1)

=1024 * index(array(_10_days), x + 1, 1)

=1024 * index(array(_20_days), x + 1, 1)

=1024 * index(array(_50_days), x + 1, 1)

(where the "+ 1" is to skip the zeroth, constant "harmonic") yield the following amplitude responses (red for 5 days, green for 10, blue for 20, black for 50):

In each response curve, the highlighted harmonic is the first one with an amplitude factor < 1 / e.

Since the fundamental period is 1024 days, the corresponding periods are 1024 / 177 » 5.8 days for EMA(5), 1024 / 84 » 12.2 for EMA(10), 1024 / 42 » 24.4 for EMA(20) and 1024 / 17 » 60 for EMA(50).

As exemplified above, the conventional definition of the EMA is based on a rule of thumb about its amplitude response: the "X day EMA" has an amplitude response which falls to 1 / e after roughly X days.

With the same definitions as above, the graph sources

=index(array(_5_days), x + 1, 2)

=index(array(_10_days), x + 1, 2)

=index(array(_20_days), x + 1, 2)

=index(array(_50_days), x + 1, 2)

give us the following phase responses:

This graph essentially shows the delay incurred by each frequency component of an input sequence passing through the EMA.

Consider for instance the 20 day EMA (in blue): the largest phase delay, roughly 1.13 radians (18% of the 2 * PI making up a full cycle) occurs around the 70th harmonic, i.e. for cycles 1024 / 70 » 15 days long. We conclude that cycles of this duration suffer a delay of roughly 15 * 0.18 » 2.7 days. Around the 50th harmonic, corresponding to 20 the day cycles of primary interest when using the 20 day EMA, the delay is roughly 1.1 radians, or 20 * 1.1 / (2 * PI) » 3.5 days.

See also ema, lopass, make_hipass, make_lopass, make_notch, make_resonator.