Theory
The exponential moving average differs from the simple moving average in that more recent samples are more strongly weighted. This means that the exponential moving average responds more quickly to changes in the signal.
Implementation
The signal
parameter is a one dimensional array. the smoothing
parameter controls how much influence the more recent samples have on the value of the average.
import numpy as npdef exponential_moving_average(signal, points, smoothing=2): """ Calculate the N-point exponential moving average of a signal Inputs: signal: numpy array - A sequence of price points in time points: int - The size of the moving average smoothing: float - The smoothing factor Outputs: ma: numpy array - The moving average at each point in the signal """ weight = smoothing / (points + 1) ema = np.zeros(len(signal)) ema[0] = signal[0] for i in range(1, len(signal)): ema[i] = (signal[i] * weight) + (ema[i - 1] * (1 - weight)) return ema
Results
Consider the following diagram showing the exponential average in blue and the simple average in red. Note that the exponential average leads the simple average, reacting to price changes more quickly.