ema¶
Defined in fynance.features.momentums
- ema(X, alpha=0.94, w=None, axis=0, dtype=None)[source]
Compute exponential moving average(s) for each X’ series.
Geometrically decaying weighted average that gives more importance to recent observations. Reacts faster than
smato regime changes; smalleralpha(or smaller equivalent windoww) increases reactivity at the cost of more noise. The recursive formulation makes computation O(T) per series, with no need to store the full window.Either
alpha(smoothing factor in[0, 1]) orw(window size mapped toalpha = 1 - 2 / (1 + w)) can be specified.\[ema^{\apha}_t(X) = \alpha \times ema^{\alpha}_{t-1} + (1-\alpha) \times X_t\]- Parameters:
- Xnp.ndarray[dtype, ndim=1 or 2]
Elements to compute the moving average.
- alphafloat, optional
These coefficient represents the degree of weighting decrease, default is 0.94 corresponding at 20 lags memory.
- wint, optional
Size of the lagged window of the moving average, must be strictly positive. If
w is Nonethe window is ignored and the parameter alpha is used. Default is None.- axis{0, 1}, optional
Axis along wich the computation is done. Default is 0.
- dtypenp.dtype, optional
The type of the output array. If dtype is not given, infer the data type from X input.
- Returns:
- np.ndarray[dtype, ndim=1 or 2]
Exponential moving average of each series.
See also
sma,wma,emstd
Notes
If the lagged window w is specified \(\alpha\) is overwritten by \(\alpha = 1 - \frac{2}{1 + w}\)
Examples
>>> X = np.array([60, 100, 80, 120, 160, 80]) >>> ema(X, w=3, dtype=np.float64) array([ 60., 80., 80., 100., 130., 105.]) >>> ema(X, alpha=0.5, dtype=np.float64) array([ 60., 80., 80., 100., 130., 105.])