ema
Syntax
ema(X, window, warmup=false)
Please see TA-Lib Functions for the parameters and windowing logic.
Arguments
warmup is a Boolean value. The default value is false, indicating that the first (window-1) elements windows return NULL. If set to true, elements in the first (window-1) windows are calculated based on the formula given in the details.
Details
Calculate the Exponential Moving Average (ema) for X in a count-based sliding window of the given length.
The calculation formula is as follows:
warmup=false:
\(EMA_k = \displaystyle{\frac{2}{n+1}}*X_k + \Bigl(1-\displaystyle{\frac{2}{n+1}}\Bigr)*EMA_{k-1}\)
warmup=true:
\(EMA_k=\begin{cases}X_0,\quad\text{k=0}\\\frac{2}{size(X)+1}*X_k+(1-\frac{2}{size(X)+1})*EMA_{k-1}, \quad\text{size(X)<n}\\\frac{2}{n+1}*X_k+(1-\frac{2}{n+1})*EMA_{k-1}, \quad size(X)\geq n\end{cases}\)
where \(EMA(X)_k\) is the k-th exponential moving average, \(n\) is the length of sliding window, \(X_k\) is the k-th element of the vector X.
Examples
$ x=12.1 12.2 12.6 12.8 11.9 11.6 11.2
$ ema(x,3);
[,,12.3,12.55,12.225,11.9125,11.55625]
$ x=matrix(12.1 12.2 12.6 12.8 11.9 11.6 11.2, 14 15 18 19 21 12 10)
$ ema(x,3);
#0 |
#1 |
---|---|
12.30 |
15.666667 |
12.55 |
17.333333 |
12.225 |
19.166667 |
11.9125 |
15.583333 |
11.55625 |
12.791667 |