EMA_PERIOD
This function computes an exponential moving average (EMA) where the smoothing factor is derived from a selected period.
The smoothing factor is computed as:
\alpha = \frac{2}{p+1}
where p is the EMA period. The recursive EMA update is:
\text{EMA}_t = \alpha x_t + (1-\alpha)\,\text{EMA}_{t-1}
The first EMA value is initialized with the first numeric observation.
Excel Usage
=EMA_PERIOD(data, period)
data(list[list], required): Time-series observations as a 2D range (data points).period(int, optional, default: 10): EMA period used to derive smoothing factor (periods).
Returns (list[list]): Column vector of exponential moving-average values.
Example 1: EMA with default period
Inputs:
| data | ||||
|---|---|---|---|---|
| 10 | 11 | 13 | 12 | 14 |
Excel formula:
=EMA_PERIOD({10,11,13,12,14})
Expected output:
| Result |
|---|
| 10 |
| 10.1818 |
| 10.6942 |
| 10.9316 |
| 11.4895 |
Example 2: EMA with short period responds quickly
Inputs:
| data | period | |||
|---|---|---|---|---|
| 5 | 8 | 7 | 9 | 2 |
Excel formula:
=EMA_PERIOD({5,8,7,9}, 2)
Expected output:
| Result |
|---|
| 5 |
| 7 |
| 7 |
| 8.33333 |
Example 3: EMA with longer period smooths changes
Inputs:
| data | period | ||||
|---|---|---|---|---|---|
| 20 | 18 | 19 | 21 | 22 | 6 |
Excel formula:
=EMA_PERIOD({20,18,19,21,22}, 6)
Expected output:
| Result |
|---|
| 20 |
| 19.4286 |
| 19.3061 |
| 19.7901 |
| 20.4215 |
Example 4: Scalar input with unit period
Inputs:
| data | period |
|---|---|
| 6 | 1 |
Excel formula:
=EMA_PERIOD(6, 1)
Expected output:
6
Python Code
import numpy as np
def ema_period(data, period=10):
"""
Compute an exponential moving average using a period-derived smoothing constant.
See: https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Time-series observations as a 2D range (data points).
period (int, optional): EMA period used to derive smoothing factor (periods). Default is 10.
Returns:
list[list]: Column vector of exponential moving-average values.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
data = to2d(data)
if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
return "Error: Invalid input - data must be a 2D list"
if period < 1:
return "Error: period must be at least 1"
series = []
for row in data:
for item in row:
try:
series.append(float(item))
except (TypeError, ValueError):
continue
if not series:
return "Error: data must contain at least one numeric value"
alpha = 2.0 / (float(period) + 1.0)
ema = [series[0]]
for value in series[1:]:
ema.append(alpha * value + (1.0 - alpha) * ema[-1])
return [[float(x)] for x in np.asarray(ema, dtype=float).tolist()]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Time-series observations as a 2D range (data points).
EMA period used to derive smoothing factor (periods).