The Moving Average - Using Reflection
One way to mitigate the digital filter lag inherent in a correctly plotted moving average is to 'reflect' the signal. Read how to do it in this article.
Fix the Inescapable Digital Filter Lag!
In our first look at the humble moving average we discussed how to correctly plot this popular trend indicator and how the vast majority of chartists today do not. We discussed it’s true operation, that of a low pass filter, or data smoother and the inherent digital filter lag resulting from the mathematical operation of averaging over a sliding window.
The filter lag is not unique to the moving average however, it is present in every filtering operation, at least in the time domain with a sliding window. The amount of digital filter lag can vary depending on the type of filter but the general rule is that it will always be present to some extent. The result of the digital filter lag are ‘edge effects’ at the start and tail of the data. As traders we are generally interested in the situation at ‘nowtime’, not least for timely entries.
Consider the above signal, a simple composite of 3 components of wavelength 500, 100 and 50 respectively and of diminishing amplitude - with some noise added. A moving average (low pass filter) is set to 200, removing all components above the 200 point wavelength, leaving just the 500 point component. The result is a plot, when correctly lagged, that loses 100 points each side. Now the tail loss is not so much of an issue for traders but the nowtime loss is something that needs to be addressed, ideally.
There are a few appropriate solutions. In this article we will look at signal reflection.
Mirror, Mirror
Reflection is the process of adding a mirrored version of the signal to the end(s) of the real signal and then applying filtering to the signal and reflection together.
The advantage of reflection for our purposes of periodic function extraction is quite compelling. We expect periodic signals to continue with similar frequency and fairly similar amplitude through nowtime and beyond. The accuracy diminishing with higher lag periods. Therefore a moving average applied to a reflected signal is a valuable tool that has minimal drawbacks, especially at negligible lags relative to the overall signal length. The fact we can use the correctly lagged moving average as a band or high pass filter also means a method to help remove the filter lag is of even greater benefit.
Let’s write some pseudo code to visualise a double sided, reflected signal:
signal = [2,3,4,7,5,3,7,8] // The original signal
ref_left = [8,7,3,5,7,4,3] // Reflected signal at the start
ref_right = [7,3,5,7,4,3,2] // Reflected signal at nowtime
reflected_signal = concatenate(ref_left, signal, ref_right)
// Reflected signal result, spaces added for clarity
>> [8,7,3,5,7,4,3, 2,3,4,7,5,3,7,8, 7,3,5,7,4,3,2]
Note that we omit reflecting the start and end points of the original signal to avoid repeating data points. Thus a signal consisting of 8 data points will have two 7 point arrays either side when implementing a fully reflected, double sided approach.
In reality it is only necessary to reflect the original signal at the ‘nowtime’ edge because we aren’t really interested in the tail. In addition, the amount of reflection can be limited to the amount of digital filter lag expected via the application of the moving average. So for a moving average of period 200, reflection of only 100 data points from nowtime is required for the moving average to be extrapolated to nowtime.
However in this example, for the sake of completeness we will reflect each side in it’s entirety.
Apply and Extrapolate
Let’s take another composite signal and demonstrate double side reflection:
When we apply the moving average to this entire signal, which includes the original signal plus the reflection, we are in effect extrapolating the low pass filter to nowtime (and beyond if required). The accuracy of the extrapolation falls the further from the original moving average lag point. As a general rule of thumb, no significance should be given to an extrapolation performed in this manner beyond a distance of nowtime plus the filter lag.
Now let’s target the component of 100 wavelength. We apply a moving average of period 50 to remove the component of wavelength 50 and the high frequency noise. This leaves just the 100 and 500 period components in the signal, which we can extrapolate through nowtime and beyond:
Signal reflection is a powerful and established tool in signal processing to use when trying to battle inherent digital filter lag. It is relatively easy to implement in most trading platforms with a touch of coding and we hope this article inspires a fresh look at the humble moving average once again.
Try it in your strategies today!