When working with time series data, like stock market prices, values can often contain a lot of noise, obscuring a real trend. One of the best ways to remove this noise is to run the data through a low pass filter.
Methods like simple moving averages and exponential moving averages are quick to implement and do a relatively good job. However, the disadvantages of these methods is that they only “look back” and do not take into account future values. This results in smoothed data which is out of phase with the original data-set, leading to peaks and troughs occurring later than reality.
A way to get around these issues is to implement a better filter, such as a Fast Fourier Transform or a Savitzky–Golay filter. However, these methods can be fairly complex and heavy to implement.
A simple method I use is shown below. I’m not sure if it’s a recognised technique, but I like to think of it as a one dimensional radial basis function. It looks back and forward around a value’s nearest neighbours, taking a weighted average, which decays exponentially by distance. And, like all good vacuum cleaners, this method cleans right up to the edges, by adding inferred linear slopes to the beginning and ends of the clean data-set.
The graph below shows a very noisy sine wave and its cleaner equivalent.
Here's the code - I hope you find it useful.