[PYTHON] Difference between Exponential Moving Average (EMA), Double Index Moving Average (DEMA) and Triple Index Moving Average (TEMA)

DEMA and TEMA are technical indicators built into MetaTrader 5 (MT5). Abbreviations for Double Exponential Moving Average and Triple Exponential Moving Average, respectively, which are translated as double exponential moving average and triple exponential moving average.

In this article, we'll look at the differences between EMA, DEMA, and TEMA.

EMA Below is a basic description of the EMA. Comparing IIR filter type moving averages with pandas and scipy

The formula is $y(n)=\alpha x(n)+(1-\alpha)y(n-1)$ If the period $ period $ is used as a parameter like other moving averages, then $ \ alpha = 2 / (period + 1) $.

DEMA

DEMA is an abbreviation for Double EMA, but it is not just about applying EMA twice. Even if you apply EMA twice, the smoothing will only proceed and you will not get very characteristic characteristics.

The above EMA formula

y(n)=EMA[x(n)]

Then, the formula of DEMA is

y(n)=2EMA[x(n)]-EMA[EMA[x(n)]]

Can be written. I also use double EMA, but I also use EMA to calculate. The derivation principle of this formula is described in here.

TEMA

The formula for TEMA is

y(n)=3EMA[x(n)]-3EMA[EMA[x(n)]]+EMA[EMA[EMA[x(n)]]]

Write, and calculate using triple EMA, double EMA, and EMA. The derivation principle of this formula is here.

Differences between EMA, DEMA and TEMA

Differences due to processing examples

Here is an example of applying EMA, DEMA, and TEMA to the Forex chart.

EURJPY.fM30.png

This is what is displayed on the MT5 chart, red is EMA, blue is DMA, and green is TEMA. All periods are also $ period = 14 $.

Looking at this, we can see that DEMA, TEMA and price followability are higher than EMA. In other words, the delay that tends to be a moving average is reduced. However, I feel that the noise is increasing.

Difference due to frequency characteristics

The difference in the above processing example can be explained by the difference in frequency characteristics.

Comparing IIR filter type moving averages with pandas and scipy According to the EMA system function, it can be written as: $H_{EMA}(z)=\frac{\alpha}{1-(1-\alpha)z^{-1}}$

Using this, the system functions of DEMA and TEMA are as follows.

H_{DEMA}=2H_{EMA}(z)-H_{EMA}(z)^2
H_{TEMA}=3H_{EMA}(z)-3H_{EMA}(z)^2+H_{EMA}(z)^3

Substituting $ H_ {EMA} (z) $ into this and dividing it, the result is as follows.

H_{DEMA}(z)=\frac{b_0+b_1z^{-1}}{a_0+a_1z^{-1}+a_2z^{-2}}

However, $ b_0 = \ alpha (2- \ alpha) $, $ b_1 = 2 \ alpha (\ alpha-1) $, $ a_0 = 1 $, $ a_1 = 2 (\ alpha-1) $, $ a_2 = (1- \ alpha) ^ 2 $

H_{TEMA}(z)=\frac{b_0+b_1z^{-1}+b_2z^{-2}}{a_0+a_1z^{-1}+a_2z^{-2}+a_3z^{-3}}

However, $ b_0 = 3 \ alpha (1- \ alpha) + \ alpha ^ 3 $, $ b_1 = 3 \ alpha (\ alpha-2) (1- \ alpha) $, $ b_2 = 3 \ alpha (1- \ alpha) ^ 2 $, $ a_0 = 1 $, $ a_1 = 3 (\ alpha-1) $, $ a_2 = 3 (1- \ alpha) ^ 2 $, $ a_3 = (\ alpha-1) ^ 3 $

With this transformation, you can use the functions freqz and group_delay that calculate the frequency characteristics with the coefficients of the molecular polynomial and denominator polynomial in Python's scipy as arguments.

Amplitude characteristics

Let's find the amplitude characteristics of each EMA with the following code.

%matplotlib inline
import numpy as np
import scipy.signal as sp
import matplotlib.pyplot as plt

period = 14 #period
alpha = 2/(period+1)
#EMA
a = [1, alpha-1]
b = alpha
w, h = sp.freqz(b, a)
#DEMA
a_d = [1, 2*(alpha-1), (1-alpha)**2]
b_d = [alpha*(2-alpha), 2*alpha*(alpha-1)]
w, h_d = sp.freqz(b_d, a_d)
#TEMA
a_t = [1, 3*(alpha-1), 3*(1-alpha)**2, (alpha-1)**3]
b_t = [3*alpha*(1-alpha)+alpha**3, 3*alpha*(alpha-2)*(1-alpha), 3*alpha*(1-alpha)**2]
w, h_t = sp.freqz(b_t, a_t)

plt.xlabel('Frequency(rad/s)')
plt.ylabel('Amplitude')
plt.plot(w, abs(h), 'r', label='EMA')
plt.plot(w, abs(h_d), 'b', label='DEMA')
plt.plot(w, abs(h_t), 'g', label='TEMA')
plt.legend(loc="best")

index.png

Basically, the moving average shows the characteristics of a low-pass filter that passes low frequencies, but compared to EMA, DEMA and TEMA have a slightly wider band to pass through. Due to this, some high frequency signals remain. Therefore, DEMA and TEMA had some noise left compared to EMA.

Group delay characteristics

Next, among the frequency characteristics, let's find the group delay characteristic that differentiates the phase characteristic.

#Group delay characteristics
w, gd = sp.group_delay((b, a))
w, gd_d = sp.group_delay((b_d, a_d))
w, gd_t = sp.group_delay((b_t, a_t))

plt.xlabel('Frequency(rad/s)')
plt.ylabel('Group delay')
plt.plot(w, gd, 'r', label='EMA')
plt.plot(w, gd_d, 'b', label='DEMA')
plt.plot(w, gd_t, 'g', label='TEMA')
plt.legend(loc="best")

index.png

The group delay characteristic represents the delay of the signal at that frequency. Looking at this, EMA has a large delay at low frequencies, while DEMA and TEMA have a small delay. The high followability to input changes can be explained by this group delay characteristic.

Recommended Posts

Difference between Exponential Moving Average (EMA), Double Index Moving Average (DEMA) and Triple Index Moving Average (TEMA)
Comparison of exponential moving average (EMA) code written in Python
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python